blob: c8544980999047db1b4e73b49f871c1d60d7411d [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;
Adam Cohen53805212013-10-01 10:39:23 -070035import android.support.v4.view.accessibility.AccessibilityEventCompat;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
45import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohen7d30a372013-07-01 17:03:59 -070050import android.view.animation.AnimationUtils;
51import android.view.animation.DecelerateInterpolator;
Adam Cohene0f66b52010-11-23 15:06:07 -080052import android.view.animation.Interpolator;
Adam Cohen7d30a372013-07-01 17:03:59 -070053import android.view.animation.LinearInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070054import android.widget.Scroller;
55
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chungc58497e2013-09-03 17:48:37 -070058interface Page {
59 public int getPageChildCount();
60 public View getChildOnPageAt(int i);
61 public void removeAllViewsOnPage();
62 public void removeViewOnPageAt(int i);
63 public int indexOfChildOnPage(View v);
64}
65
Winson Chung321e9ee2010-08-09 13:37:56 -070066/**
67 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070068 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070069 */
Michael Jurka8b805b12012-04-18 14:23:14 -070070public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070071 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070072 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070073 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070074
Winson Chung86f77532010-08-24 11:08:22 -070075 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070076 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070077
Adam Cohen7d30a372013-07-01 17:03:59 -070078 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070079 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Adam Cohenb5ba0972011-09-07 18:02:31 -070082 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070083 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080084
Adam Cohenb64cb5a2011-02-15 13:53:42 -080085 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080086 // The page is moved more than halfway, automatically move to the next page on touch up.
87 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080088
Adam Cohen265b9a62011-12-07 14:37:18 -080089 // The following constants need to be scaled based on density. The scaled versions will be
90 // assigned to the corresponding member variables below.
91 private static final int FLING_THRESHOLD_VELOCITY = 500;
92 private static final int MIN_SNAP_VELOCITY = 1500;
93 private static final int MIN_FLING_VELOCITY = 250;
94
Adam Cohen7d30a372013-07-01 17:03:59 -070095 // We are disabling touch interaction of the widget region for factory ROM.
96 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070097 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070098 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070099
Adam Cohen21cd0022013-10-09 18:57:02 -0700100 public static final int INVALID_RESTORE_PAGE = -1001;
101
Adam Cohenf358a4b2013-07-23 16:47:31 -0700102 private boolean mFreeScroll = false;
103 private int mFreeScrollMinScrollX = -1;
104 private int mFreeScrollMaxScrollX = -1;
105
Winson Chung8aad6102012-05-11 16:27:49 -0700106 static final int AUTOMATIC_PAGE_SPACING = -1;
107
Adam Cohen265b9a62011-12-07 14:37:18 -0800108 protected int mFlingThresholdVelocity;
109 protected int mMinFlingVelocity;
110 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700111
Adam Cohenb5ba0972011-09-07 18:02:31 -0700112 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected float mSmoothingTime;
114 protected float mTouchX;
115
116 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700117 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700118
119 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700120 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700121 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700122
Michael Jurka0142d492010-08-25 17:46:15 -0700123 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800124 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800125 protected LauncherScroller mScroller;
126 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700128 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Adam Cohen7d30a372013-07-01 17:03:59 -0700130 private float mParentDownMotionX;
131 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700133 private float mDownMotionY;
134 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700135 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800137 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800138 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800139 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700140 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700141
Adam Cohendbdff6b2013-09-18 19:09:15 -0700142 private boolean mCancelTap;
143
Adam Cohenedb40762013-07-18 16:45:45 -0700144 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 protected final static int TOUCH_STATE_REST = 0;
147 protected final static int TOUCH_STATE_SCROLLING = 1;
148 protected final static int TOUCH_STATE_PREV_PAGE = 2;
149 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700150 protected final static int TOUCH_STATE_REORDERING = 4;
151
Adam Cohene45440e2010-10-14 18:33:38 -0700152 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700155 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800156 private boolean mScrollAbortedFromIntercept = false;
157
Winson Chung321e9ee2010-08-09 13:37:56 -0700158
Michael Jurka0142d492010-08-25 17:46:15 -0700159 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700160
Michael Jurka7426c422010-11-11 15:23:47 -0800161 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 private int mPagingTouchSlop;
163 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700164 protected int mPageLayoutPaddingTop;
165 protected int mPageLayoutPaddingBottom;
166 protected int mPageLayoutPaddingLeft;
167 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700168 protected int mPageLayoutWidthGap;
169 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700170 protected int mCellCountX = 0;
171 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700172 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800173 protected boolean mAllowOverScroll = true;
174 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800175 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700176 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700177
Michael Jurka8b805b12012-04-18 14:23:14 -0700178 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800179 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
180 // the screens from continuing to translate beyond the normal bounds.
181 protected int mOverScrollX;
182
Michael Jurka5f1c5092010-09-03 14:15:02 -0700183 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Michael Jurka5f1c5092010-09-03 14:15:02 -0700185 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Winson Chung86f77532010-08-24 11:08:22 -0700187 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700188
Michael Jurkae326f182011-11-21 14:05:46 -0800189 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700190
Michael Jurka0142d492010-08-25 17:46:15 -0700191 // If true, syncPages and syncPageItems will be called to refresh pages
192 protected boolean mContentIsRefreshable = true;
193
194 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700195 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700196
197 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
198 // to switch to a new page
199 protected boolean mUsePagingTouchSlop = true;
200
Michael Jurka8b805b12012-04-18 14:23:14 -0700201 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700202 // (SmoothPagedView does this)
203 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700204 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700205
Patrick Dubroy1262e362010-10-06 15:49:50 -0700206 protected boolean mIsPageMoving = false;
207
Winson Chungf0ea4d32011-06-06 14:27:16 -0700208 // All syncs and layout passes are deferred until data is ready.
209 protected boolean mIsDataReady = false;
210
Adam Cohen7d30a372013-07-01 17:03:59 -0700211 protected boolean mAllowLongPress = true;
212
Winson Chungd2be3812013-07-16 11:11:32 -0700213 // Page Indicator
214 private int mPageIndicatorViewId;
215 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700216 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700217
Adam Cohen7d30a372013-07-01 17:03:59 -0700218 // The viewport whether the pages are to be contained (the actual view may be larger than the
219 // viewport)
220 private Rect mViewport = new Rect();
221
222 // Reordering
223 // We use the min scale to determine how much to expand the actually PagedView measured
224 // dimensions such that when we are zoomed out, the view is not clipped
225 private int REORDERING_DROP_REPOSITION_DURATION = 200;
226 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
227 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700228 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700229 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700230 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700231 protected View mDragView;
232 protected AnimatorSet mZoomInOutAnim;
233 private Runnable mSidePageHoverRunnable;
234 private int mSidePageHoverIndex = -1;
235 // This variable's scope is only for the duration of startReordering() and endReordering()
236 private boolean mReorderingStarted = false;
237 // This variable's scope is for the duration of startReordering() and after the zoomIn()
238 // animation after endReordering()
239 private boolean mIsReordering;
240 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
241 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
242 private int mPostReorderingPreZoomInRemainingAnimationCount;
243 private Runnable mPostReorderingPreZoomInRunnable;
244
Adam Cohen7d30a372013-07-01 17:03:59 -0700245 // Convenience/caching
246 private Matrix mTmpInvMatrix = new Matrix();
247 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700248 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700249 private Rect mTmpRect = new Rect();
250 private Rect mAltTmpRect = new Rect();
251
252 // Fling to delete
253 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
254 private float FLING_TO_DELETE_FRICTION = 0.035f;
255 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
256 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
257 protected int mFlingToDeleteThresholdVelocity = -1400;
258 // Drag to delete
259 private boolean mDeferringForDelete = false;
260 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
261 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
262
263 // Drop to delete
264 private View mDeleteDropTarget;
265
Adam Cohen7d30a372013-07-01 17:03:59 -0700266 // Bouncer
267 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700268
John Spurlock77e1f472013-09-11 10:09:51 -0400269 protected final Rect mInsets = new Rect();
270
Winson Chung86f77532010-08-24 11:08:22 -0700271 public interface PageSwitchListener {
272 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 }
274
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 public PagedView(Context context) {
276 this(context, null);
277 }
278
279 public PagedView(Context context, AttributeSet attrs) {
280 this(context, attrs, 0);
281 }
282
283 public PagedView(Context context, AttributeSet attrs, int defStyle) {
284 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700285
Adam Cohen9c4949e2010-10-05 12:27:22 -0700286 TypedArray a = context.obtainStyledAttributes(attrs,
287 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700288
Adam Cohen9c4949e2010-10-05 12:27:22 -0700289 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800290 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700291 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800292 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700293 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800294 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700295 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800296 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700297 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700298 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700299 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700300 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700301 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700302 a.recycle();
303
Winson Chung321e9ee2010-08-09 13:37:56 -0700304 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700305 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 }
307
308 /**
309 * Initializes various states for this workspace.
310 */
Michael Jurka0142d492010-08-25 17:46:15 -0700311 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700312 mDirtyPageContent = new ArrayList<Boolean>();
313 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800314 mScroller = new LauncherScroller(getContext());
315 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700316 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700317 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700318
319 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700320 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
322 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700323 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800324
Adam Cohen7d30a372013-07-01 17:03:59 -0700325 // Scale the fling-to-delete threshold by the density
326 mFlingToDeleteThresholdVelocity =
327 (int) (mFlingToDeleteThresholdVelocity * mDensity);
328
Adam Cohen265b9a62011-12-07 14:37:18 -0800329 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
330 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
331 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700332 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 }
334
Adam Cohenf9618852013-11-08 06:45:03 -0800335 protected void setDefaultInterpolator(Interpolator interpolator) {
336 mDefaultInterpolator = interpolator;
337 mScroller.setInterpolator(mDefaultInterpolator);
338 }
339
Winson Chungd2be3812013-07-16 11:11:32 -0700340 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700341 super.onAttachedToWindow();
342
Winson Chungd2be3812013-07-16 11:11:32 -0700343 // Hook up the page indicator
344 ViewGroup parent = (ViewGroup) getParent();
345 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
346 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700347 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700348
Winson Chung7819a562013-09-19 15:55:45 -0700349 ArrayList<PageIndicator.PageMarkerResources> markers =
350 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700351 for (int i = 0; i < getChildCount(); ++i) {
352 markers.add(getPageIndicatorMarker(i));
353 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700354
Winson Chungc58497e2013-09-03 17:48:37 -0700355 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700356
357 OnClickListener listener = getPageIndicatorClickListener();
358 if (listener != null) {
359 mPageIndicator.setOnClickListener(listener);
360 }
Adam Cohen53805212013-10-01 10:39:23 -0700361 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700362 }
363 }
364
Adam Cohen53805212013-10-01 10:39:23 -0700365 protected String getPageIndicatorDescription() {
366 return getCurrentPageDescription();
367 }
368
369 protected OnClickListener getPageIndicatorClickListener() {
370 return null;
371 }
372
Winson Chungd2be3812013-07-16 11:11:32 -0700373 protected void onDetachedFromWindow() {
374 // Unhook the page indicator
375 mPageIndicator = null;
376 }
377
Adam Cohen7d30a372013-07-01 17:03:59 -0700378 void setDeleteDropTarget(View v) {
379 mDeleteDropTarget = v;
380 }
381
382 // Convenience methods to map points from self to parent and vice versa
383 float[] mapPointFromViewToParent(View v, float x, float y) {
384 mTmpPoint[0] = x;
385 mTmpPoint[1] = y;
386 v.getMatrix().mapPoints(mTmpPoint);
387 mTmpPoint[0] += v.getLeft();
388 mTmpPoint[1] += v.getTop();
389 return mTmpPoint;
390 }
391 float[] mapPointFromParentToView(View v, float x, float y) {
392 mTmpPoint[0] = x - v.getLeft();
393 mTmpPoint[1] = y - v.getTop();
394 v.getMatrix().invert(mTmpInvMatrix);
395 mTmpInvMatrix.mapPoints(mTmpPoint);
396 return mTmpPoint;
397 }
398
399 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700400 if (mDragView != null) {
401 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
402 (mDragViewBaselineLeft - mDragView.getLeft());
403 float y = mLastMotionY - mDownMotionY;
404 mDragView.setTranslationX(x);
405 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700406
Adam Cohenf358a4b2013-07-23 16:47:31 -0700407 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
408 + x + ", " + y);
409 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700410 }
411
412 public void setMinScale(float f) {
413 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700414 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700415 requestLayout();
416 }
417
418 @Override
419 public void setScaleX(float scaleX) {
420 super.setScaleX(scaleX);
421 if (isReordering(true)) {
422 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
423 mLastMotionX = p[0];
424 mLastMotionY = p[1];
425 updateDragViewTranslationDuringDrag();
426 }
427 }
428
429 // Convenience methods to get the actual width/height of the PagedView (since it is measured
430 // to be larger to account for the minimum possible scale)
431 int getViewportWidth() {
432 return mViewport.width();
433 }
434 int getViewportHeight() {
435 return mViewport.height();
436 }
437
438 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
439 // PagedView both horizontally and vertically
440 int getViewportOffsetX() {
441 return (getMeasuredWidth() - getViewportWidth()) / 2;
442 }
443
444 int getViewportOffsetY() {
445 return (getMeasuredHeight() - getViewportHeight()) / 2;
446 }
447
Adam Cohenedb40762013-07-18 16:45:45 -0700448 PageIndicator getPageIndicator() {
449 return mPageIndicator;
450 }
Winson Chung7819a562013-09-19 15:55:45 -0700451 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
452 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700453 }
Adam Cohenedb40762013-07-18 16:45:45 -0700454
Winson Chung86f77532010-08-24 11:08:22 -0700455 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
456 mPageSwitchListener = pageSwitchListener;
457 if (mPageSwitchListener != null) {
458 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 }
460 }
461
462 /**
Winson Chung52aee602013-01-30 12:01:02 -0800463 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
464 */
465 public boolean isLayoutRtl() {
466 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
467 }
468
469 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
471 * out pages.
472 */
473 protected void setDataIsReady() {
474 mIsDataReady = true;
475 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700476
Winson Chungf0ea4d32011-06-06 14:27:16 -0700477 protected boolean isDataReady() {
478 return mIsDataReady;
479 }
480
481 /**
Winson Chung86f77532010-08-24 11:08:22 -0700482 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 *
Winson Chung86f77532010-08-24 11:08:22 -0700484 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700485 */
Winson Chung86f77532010-08-24 11:08:22 -0700486 int getCurrentPage() {
487 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700489
Winson Chung360e63f2012-04-27 13:48:05 -0700490 int getNextPage() {
491 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
492 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700493
Winson Chung86f77532010-08-24 11:08:22 -0700494 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 return getChildCount();
496 }
497
Winson Chung86f77532010-08-24 11:08:22 -0700498 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 return getChildAt(index);
500 }
501
Adam Cohenae4f1552011-10-20 00:15:42 -0700502 protected int indexToPage(int index) {
503 return index;
504 }
505
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800507 * Updates the scroll of the current page immediately to its final scroll position. We use this
508 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
509 * the previous tab page.
510 */
511 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700512 // If the current page is invalid, just reset the scroll position to zero
513 int newX = 0;
514 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700515 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700516 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800517 scrollTo(newX, 0);
518 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700519 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800520 }
521
522 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700523 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700524 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
525 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700526 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700527 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700528 mCurrentPage = getNextPage();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700529 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700530 }
531
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700532 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700533 mScroller.abortAnimation();
534 // We need to clean up the next page here to avoid computeScrollHelper from
535 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700536 if (resetNextPage) {
537 mNextPage = INVALID_PAGE;
538 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700539 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700540
541 private void forceFinishScroller() {
542 mScroller.forceFinished(true);
543 // We need to clean up the next page here to avoid computeScrollHelper from
544 // updating current page on the pass.
545 mNextPage = INVALID_PAGE;
546 }
547
Chet Haasebc2f0822012-10-26 17:59:53 -0700548 /**
Winson Chung86f77532010-08-24 11:08:22 -0700549 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 */
Winson Chung86f77532010-08-24 11:08:22 -0700551 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800552 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700553 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800554 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
556 // the default
557 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800558 return;
559 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700560 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700561 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chung181c3dc2013-07-17 15:36:20 -0700562 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700563 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800564 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 }
566
Winson Chung8c87cd82013-07-23 16:20:10 -0700567 /**
568 * The restore page will be set in place of the current page at the next (likely first)
569 * layout.
570 */
571 void setRestorePage(int restorePage) {
572 mRestorePage = restorePage;
573 }
574
Michael Jurka0142d492010-08-25 17:46:15 -0700575 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700576 if (mPageSwitchListener != null) {
577 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 }
Winson Chungd2be3812013-07-16 11:11:32 -0700579
580 // Update the page indicator (when we aren't reordering)
581 if (mPageIndicator != null && !isReordering(false)) {
582 mPageIndicator.setActiveMarker(getNextPage());
583 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800585 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700586 if (!mIsPageMoving) {
587 mIsPageMoving = true;
588 onPageBeginMoving();
589 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700590 }
591
Michael Jurkace7e05f2011-02-01 22:02:35 -0800592 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700593 if (mIsPageMoving) {
594 mIsPageMoving = false;
595 onPageEndMoving();
596 }
Michael Jurka0142d492010-08-25 17:46:15 -0700597 }
598
Adam Cohen26976d92011-03-22 15:33:33 -0700599 protected boolean isPageMoving() {
600 return mIsPageMoving;
601 }
602
Michael Jurka0142d492010-08-25 17:46:15 -0700603 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700604 protected void onPageBeginMoving() {
605 }
606
607 // a method that subclasses can override to add behavior
608 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700609 }
610
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 /**
Winson Chung86f77532010-08-24 11:08:22 -0700612 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 *
614 * @param l The listener used to respond to long clicks.
615 */
616 @Override
617 public void setOnLongClickListener(OnLongClickListener l) {
618 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700619 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700621 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700622 }
Adam Cohen1697b792013-09-17 19:08:21 -0700623 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 }
625
626 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800627 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700628 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800629 }
630
631 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700632 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700633 // In free scroll mode, we clamp the scrollX
634 if (mFreeScroll) {
635 x = Math.min(x, mFreeScrollMaxScrollX);
636 x = Math.max(x, mFreeScrollMinScrollX);
637 }
638
Adam Cohen0ffac432013-07-10 11:19:26 -0700639 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800640 mUnboundedScrollX = x;
641
Adam Cohen0ffac432013-07-10 11:19:26 -0700642 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
643 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
644 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800645 super.scrollTo(0, y);
646 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700647 if (isRtl) {
648 overScroll(x - mMaxScrollX);
649 } else {
650 overScroll(x);
651 }
Adam Cohen68d73932010-11-15 10:50:58 -0800652 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700653 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800654 super.scrollTo(mMaxScrollX, y);
655 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700656 if (isRtl) {
657 overScroll(x);
658 } else {
659 overScroll(x - mMaxScrollX);
660 }
Adam Cohen68d73932010-11-15 10:50:58 -0800661 }
662 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800663 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800664 super.scrollTo(x, y);
665 }
666
Michael Jurka0142d492010-08-25 17:46:15 -0700667 mTouchX = x;
668 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700669
670 // Update the last motion events when scrolling
671 if (isReordering(true)) {
672 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
673 mLastMotionX = p[0];
674 mLastMotionY = p[1];
675 updateDragViewTranslationDuringDrag();
676 }
Michael Jurka0142d492010-08-25 17:46:15 -0700677 }
678
Adam Cohen53805212013-10-01 10:39:23 -0700679 private void sendScrollAccessibilityEvent() {
680 AccessibilityManager am =
681 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
682 if (am.isEnabled()) {
683 AccessibilityEvent ev =
684 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700685 ev.setItemCount(getChildCount());
686 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700687 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700688
Alan Viverette254139a2013-10-08 13:13:46 -0700689 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700690 if (getNextPage() >= mCurrentPage) {
691 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
692 } else {
693 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
694 }
695
696 ev.setAction(action);
697 sendAccessibilityEventUnchecked(ev);
698 }
699 }
700
Michael Jurka0142d492010-08-25 17:46:15 -0700701 // we moved this functionality to a helper function so SmoothPagedView can reuse it
702 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700704 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 if (getScrollX() != mScroller.getCurrX()
706 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700707 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700708 float scaleX = mFreeScroll ? getScaleX() : 1f;
709 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
710 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700711 }
Michael Jurka0142d492010-08-25 17:46:15 -0700712 invalidate();
713 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700714 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700715 sendScrollAccessibilityEvent();
716
Winson Chung86f77532010-08-24 11:08:22 -0700717 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700718 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700719 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700720
Winson Chung9c0565f2013-07-19 13:49:06 -0700721 // Load the associated pages if necessary
722 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
723 loadAssociatedPages(mCurrentPage);
724 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
725 }
726
Adam Cohen73aa9752010-11-24 16:26:58 -0800727 // We don't want to trigger a page end moving unless the page has settled
728 // and the user has stopped scrolling
729 if (mTouchState == TOUCH_STATE_REST) {
730 pageEndMoving();
731 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700732
Adam Cohen7d30a372013-07-01 17:03:59 -0700733 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700734 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700735 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700736 if (am.isEnabled()) {
737 // Notify the user when the page changes
738 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700739 }
Michael Jurka0142d492010-08-25 17:46:15 -0700740 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 }
Michael Jurka0142d492010-08-25 17:46:15 -0700742 return false;
743 }
744
745 @Override
746 public void computeScroll() {
747 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 }
749
Adam Cohen7d30a372013-07-01 17:03:59 -0700750 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
751 return mTopAlignPageWhenShrinkingForBouncer;
752 }
753
Adam Cohen96d30a12013-07-16 18:13:21 -0700754 public static class LayoutParams extends ViewGroup.LayoutParams {
755 public boolean isFullScreenPage = false;
756
757 /**
758 * {@inheritDoc}
759 */
760 public LayoutParams(int width, int height) {
761 super(width, height);
762 }
763
764 public LayoutParams(ViewGroup.LayoutParams source) {
765 super(source);
766 }
767 }
768
769 protected LayoutParams generateDefaultLayoutParams() {
770 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
771 }
772
Adam Cohenedb40762013-07-18 16:45:45 -0700773 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700774 LayoutParams lp = generateDefaultLayoutParams();
775 lp.isFullScreenPage = true;
776 super.addView(page, 0, lp);
777 }
778
Adam Cohen410f3cd2013-09-22 12:09:32 -0700779 public int getNormalChildHeight() {
780 return mNormalChildHeight;
781 }
782
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 @Override
784 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700786 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
787 return;
788 }
789
Adam Cohen7d30a372013-07-01 17:03:59 -0700790 // We measure the dimensions of the PagedView to be larger than the pages so that when we
791 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700792 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
793 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
794 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700795 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800796 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700797 // viewport, we can be at most one and a half screens offset once we scale down
798 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700799 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
800 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700801
Winson Chungc82d2622013-11-06 13:23:29 -0800802 int parentWidthSize = (int) (2f * maxSize);
803 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700804 int scaledWidthSize, scaledHeightSize;
805 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700806 scaledWidthSize = (int) (parentWidthSize / mMinScale);
807 scaledHeightSize = (int) (parentHeightSize / mMinScale);
808 } else {
809 scaledWidthSize = widthSize;
810 scaledHeightSize = heightSize;
811 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700812 mViewport.set(0, 0, widthSize, heightSize);
813
814 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
815 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
816 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 }
818
Winson Chung8aad6102012-05-11 16:27:49 -0700819 // Return early if we aren't given a proper dimension
820 if (widthSize <= 0 || heightSize <= 0) {
821 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
822 return;
823 }
824
Adam Lesinski6b879f02010-11-04 16:15:23 -0700825 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
826 * of the All apps view on XLarge displays to not take up more space then it needs. Width
827 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
828 * each page to have the same width.
829 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700830 final int verticalPadding = getPaddingTop() + getPaddingBottom();
831 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700832
833 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700834 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700836 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
837 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
838 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
839 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 final int childCount = getChildCount();
841 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700842 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700843 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100844 if (child.getVisibility() != GONE) {
845 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700846
Vladimir Marko2824b072013-10-04 16:42:17 +0100847 int childWidthMode;
848 int childHeightMode;
849 int childWidth;
850 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700851
Vladimir Marko2824b072013-10-04 16:42:17 +0100852 if (!lp.isFullScreenPage) {
853 if (lp.width == LayoutParams.WRAP_CONTENT) {
854 childWidthMode = MeasureSpec.AT_MOST;
855 } else {
856 childWidthMode = MeasureSpec.EXACTLY;
857 }
858
859 if (lp.height == LayoutParams.WRAP_CONTENT) {
860 childHeightMode = MeasureSpec.AT_MOST;
861 } else {
862 childHeightMode = MeasureSpec.EXACTLY;
863 }
864
Adam Cohen3b185e22013-10-29 14:45:58 -0700865 childWidth = getViewportWidth() - horizontalPadding
866 - mInsets.left - mInsets.right;
867 childHeight = getViewportHeight() - verticalPadding
868 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100869 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700870 } else {
871 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700872 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100873
Adam Cohen3b185e22013-10-29 14:45:58 -0700874 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
875 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700876 }
877
Vladimir Marko2824b072013-10-04 16:42:17 +0100878 final int childWidthMeasureSpec =
879 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
880 final int childHeightMeasureSpec =
881 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
882 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700883 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700884 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700885 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800886 }
887
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700889 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700891 return;
892 }
893
Winson Chung785d2eb2011-04-14 16:08:02 -0700894 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700896
Adam Cohen7d30a372013-07-01 17:03:59 -0700897 int offsetX = getViewportOffsetX();
898 int offsetY = getViewportOffsetY();
899
900 // Update the viewport offsets
901 mViewport.offset(offsetX, offsetY);
902
Adam Cohen0ffac432013-07-10 11:19:26 -0700903 final boolean isRtl = isLayoutRtl();
904
905 final int startIndex = isRtl ? childCount - 1 : 0;
906 final int endIndex = isRtl ? -1 : childCount;
907 final int delta = isRtl ? -1 : 1;
908
Adam Cohen7d30a372013-07-01 17:03:59 -0700909 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700910
Adam Cohen3b185e22013-10-29 14:45:58 -0700911 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000912 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700913
914 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700915 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
916 mPageScrolls = new int[getChildCount()];
917 }
918
Adam Cohen0ffac432013-07-10 11:19:26 -0700919 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700920 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700922 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100923 int childTop;
924 if (lp.isFullScreenPage) {
925 childTop = offsetY;
926 } else {
927 childTop = offsetY + getPaddingTop() + mInsets.top;
928 if (mCenterPagesVertically) {
929 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
930 }
931 }
932
Adam Cohen96d30a12013-07-16 18:13:21 -0700933 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700934 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800935
Winson Chung785d2eb2011-04-14 16:08:02 -0700936 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700937 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800938 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700939
Adam Cohen3b185e22013-10-29 14:45:58 -0700940 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
941 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000942
943 int pageGap = mPageSpacing;
944 int next = i + delta;
945 if (next != endIndex) {
946 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
947 } else {
948 nextLp = null;
949 }
950
951 // Prevent full screen pages from showing in the viewport
952 // when they are not the current page.
953 if (lp.isFullScreenPage) {
954 pageGap = getPaddingLeft();
955 } else if (nextLp != null && nextLp.isFullScreenPage) {
956 pageGap = getPaddingRight();
957 }
958
959 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 }
961 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700962
963 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
964 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800965 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700966 setHorizontalScrollBarEnabled(true);
967 mFirstLayout = false;
968 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700969
970 if (childCount > 0) {
971 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700972 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700973 } else {
974 mMaxScrollX = 0;
975 }
976
977 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
978 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700979 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700980 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700981 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700982 } else {
983 setCurrentPage(getNextPage());
984 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700985 }
986 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -0700987
988 if (isReordering(true)) {
989 updateDragViewTranslationDuringDrag();
990 }
Winson Chunge3193b92010-09-10 11:44:42 -0700991 }
992
Adam Cohen3b185e22013-10-29 14:45:58 -0700993 public void setPageSpacing(int pageSpacing) {
994 mPageSpacing = pageSpacing;
995 requestLayout();
996 }
997
Adam Cohenf34bab52010-09-30 14:11:56 -0700998 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700999 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1000
1001 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001002 for (int i = 0; i < getChildCount(); i++) {
1003 View child = getChildAt(i);
1004 if (child != null) {
1005 float scrollProgress = getScrollProgress(screenCenter, child, i);
1006 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001007 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001008 }
1009 }
1010 invalidate();
1011 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001012 }
1013
Winson Chungc58497e2013-09-03 17:48:37 -07001014 protected void enablePagedViewAnimations() {
1015 mAllowPagedViewAnimations = true;
1016
1017 }
1018 protected void disablePagedViewAnimations() {
1019 mAllowPagedViewAnimations = false;
1020 }
1021
Winson Chunge3193b92010-09-10 11:44:42 -07001022 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001023 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001024 // Update the page indicator, we don't update the page indicator as we
1025 // add/remove pages
1026 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001027 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001028 mPageIndicator.addMarker(pageIndex,
1029 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001030 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001031 }
1032
Adam Cohen2591f6a2011-10-25 14:36:40 -07001033 // This ensures that when children are added, they get the correct transforms / alphas
1034 // in accordance with any scroll effects.
1035 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001036 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001037 invalidate();
1038 }
1039
Michael Jurka8b805b12012-04-18 14:23:14 -07001040 @Override
1041 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001042 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001043 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001044 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001045 }
1046
Winson Chungd2be3812013-07-16 11:11:32 -07001047 private void removeMarkerForView(int index) {
1048 // Update the page indicator, we don't update the page indicator as we
1049 // add/remove pages
1050 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001051 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001052 }
1053 }
1054
1055 @Override
1056 public void removeView(View v) {
1057 // XXX: We should find a better way to hook into this before the view
1058 // gets removed form its parent...
1059 removeMarkerForView(indexOfChild(v));
1060 super.removeView(v);
1061 }
1062 @Override
1063 public void removeViewInLayout(View v) {
1064 // XXX: We should find a better way to hook into this before the view
1065 // gets removed form its parent...
1066 removeMarkerForView(indexOfChild(v));
1067 super.removeViewInLayout(v);
1068 }
1069 @Override
1070 public void removeViewAt(int index) {
1071 // XXX: We should find a better way to hook into this before the view
1072 // gets removed form its parent...
1073 removeViewAt(index);
1074 super.removeViewAt(index);
1075 }
1076 @Override
1077 public void removeAllViewsInLayout() {
1078 // Update the page indicator, we don't update the page indicator as we
1079 // add/remove pages
1080 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001081 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001082 }
1083
1084 super.removeAllViewsInLayout();
1085 }
1086
Adam Cohen73894962011-10-31 13:17:17 -07001087 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001088 if (index < 0 || index > getChildCount() - 1) return 0;
1089
Adam Cohenf698c6e2013-07-17 18:44:25 -07001090 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001091
Adam Cohenf698c6e2013-07-17 18:44:25 -07001092 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001093 }
1094
Adam Cohenf358a4b2013-07-23 16:47:31 -07001095 protected void getOverviewModePages(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001096 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001097 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001098 }
1099
Michael Jurkadde558b2011-11-09 22:09:06 -08001100 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001101 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001102 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001103
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001104 range[0] = -1;
1105 range[1] = -1;
1106
Michael Jurkac4fb9172010-09-02 17:19:20 -07001107 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001108 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001109 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001110
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001111 int count = getChildCount();
1112 for (int i = 0; i < count; i++) {
1113 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001114
Winson Chungc9ca2982013-07-19 12:07:38 -07001115 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001116 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001117 if (mTmpIntPoint[0] > viewportWidth) {
1118 if (range[0] == -1) {
1119 continue;
1120 } else {
1121 break;
1122 }
1123 }
1124
Adam Cohen6a678da2013-09-24 10:58:01 -07001125 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001126 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1127 if (mTmpIntPoint[0] < 0) {
1128 if (range[0] == -1) {
1129 continue;
1130 } else {
1131 break;
1132 }
1133 }
1134 curScreen = i;
1135 if (range[0] < 0) {
1136 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001137 }
1138 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001139
1140 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001141 } else {
1142 range[0] = -1;
1143 range[1] = -1;
1144 }
1145 }
1146
Michael Jurka920d7f42012-05-14 16:29:55 -07001147 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001148 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001149 }
1150
Michael Jurkadde558b2011-11-09 22:09:06 -08001151 @Override
1152 protected void dispatchDraw(Canvas canvas) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001153 int halfScreenSize = getViewportWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -07001154 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1155 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -08001156 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -08001157
1158 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -07001159 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1160 // set it for the next frame
1161 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -08001162 screenScrolled(screenCenter);
1163 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -08001164 }
1165
1166 // Find out which screens are visible; as an optimization we only call draw on them
1167 final int pageCount = getChildCount();
1168 if (pageCount > 0) {
1169 getVisiblePages(mTempVisiblePagesRange);
1170 final int leftScreen = mTempVisiblePagesRange[0];
1171 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001172 if (leftScreen != -1 && rightScreen != -1) {
1173 final long drawingTime = getDrawingTime();
1174 // Clip to the bounds
1175 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001176 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1177 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001178
Adam Cohen7d30a372013-07-01 17:03:59 -07001179 // Draw all the children, leaving the drag view for last
1180 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001181 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001182 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001183 if (mForceDrawAllChildrenNextFrame ||
1184 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001185 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001186 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001187 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001188 // Draw the drag view on top (if there is one)
1189 if (mDragView != null) {
1190 drawChild(canvas, mDragView, drawingTime);
1191 }
1192
Michael Jurka5e368ff2012-05-14 23:13:15 -07001193 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001194 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001195 }
Michael Jurka0142d492010-08-25 17:46:15 -07001196 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
1198
1199 @Override
1200 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001201 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001202 if (page != mCurrentPage || !mScroller.isFinished()) {
1203 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 return true;
1205 }
1206 return false;
1207 }
1208
1209 @Override
1210 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001211 int focusablePage;
1212 if (mNextPage != INVALID_PAGE) {
1213 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001215 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 }
Winson Chung86f77532010-08-24 11:08:22 -07001217 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001219 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221 return false;
1222 }
1223
1224 @Override
1225 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001226 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (getCurrentPage() > 0) {
1229 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 return true;
1231 }
1232 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001233 if (getCurrentPage() < getPageCount() - 1) {
1234 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 return true;
1236 }
1237 }
1238 return super.dispatchUnhandledMove(focused, direction);
1239 }
1240
1241 @Override
1242 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001243 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001244 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001245 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 }
1247 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001248 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001249 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 }
1251 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001252 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001253 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001254 }
1255 }
1256 }
1257
1258 /**
1259 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001260 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 *
Winson Chung86f77532010-08-24 11:08:22 -07001262 * This happens when live folders requery, and if they're off page, they
1263 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 */
1265 @Override
1266 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001267 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 View v = focused;
1269 while (true) {
1270 if (v == current) {
1271 super.focusableViewAvailable(focused);
1272 return;
1273 }
1274 if (v == this) {
1275 return;
1276 }
1277 ViewParent parent = v.getParent();
1278 if (parent instanceof View) {
1279 v = (View)v.getParent();
1280 } else {
1281 return;
1282 }
1283 }
1284 }
1285
1286 /**
1287 * {@inheritDoc}
1288 */
1289 @Override
1290 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1291 if (disallowIntercept) {
1292 // We need to make sure to cancel our long press if
1293 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001294 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001295 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 }
1297 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1298 }
1299
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001300 /**
1301 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1302 */
1303 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001304 if (isLayoutRtl()) {
1305 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001306 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001307 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001308 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001309 }
1310
1311 /**
1312 * Return true if a tap at (x, y) should trigger a flip to the next page.
1313 */
1314 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001315 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001316 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001317 }
1318 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001319 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001320 }
Winson Chung52aee602013-01-30 12:01:02 -08001321
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 /** Returns whether x and y originated within the buffered viewport */
1323 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1324 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1325 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1326 return mTmpRect.contains(x, y);
1327 }
1328
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 @Override
1330 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001331 if (DISABLE_TOUCH_INTERACTION) {
1332 return false;
1333 }
1334
Winson Chung321e9ee2010-08-09 13:37:56 -07001335 /*
1336 * This method JUST determines whether we want to intercept the motion.
1337 * If we return true, onTouchEvent will be called and we do the actual
1338 * scrolling there.
1339 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001340 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001341
Winson Chung45e1d6e2010-11-09 17:19:49 -08001342 // Skip touch handling if there are no pages to swipe
1343 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1344
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 /*
1346 * Shortcut the most recurring case: the user is in the dragging
1347 * state and he is moving his finger. We want to intercept this
1348 * motion.
1349 */
1350 final int action = ev.getAction();
1351 if ((action == MotionEvent.ACTION_MOVE) &&
1352 (mTouchState == TOUCH_STATE_SCROLLING)) {
1353 return true;
1354 }
1355
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 switch (action & MotionEvent.ACTION_MASK) {
1357 case MotionEvent.ACTION_MOVE: {
1358 /*
1359 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1360 * whether the user has moved far enough from his original down touch.
1361 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001362 if (mActivePointerId != INVALID_POINTER) {
1363 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001364 }
1365 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1366 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1367 // i.e. fall through to the next case (don't break)
1368 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1369 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001370 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 }
1372
1373 case MotionEvent.ACTION_DOWN: {
1374 final float x = ev.getX();
1375 final float y = ev.getY();
1376 // Remember location of down touch
1377 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001378 mDownMotionY = y;
1379 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 mLastMotionX = x;
1381 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001382 float[] p = mapPointFromViewToParent(this, x, y);
1383 mParentDownMotionX = p[0];
1384 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001385 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001386 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001388
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 /*
1390 * If being flinged and user touches the screen, initiate drag;
1391 * otherwise don't. mScroller.isFinished should be false when
1392 * being flinged.
1393 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001394 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001395 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
Adam Cohen2da0a052013-11-08 06:28:17 -08001396
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001397 if (finishedScrolling) {
1398 mTouchState = TOUCH_STATE_REST;
Adam Cohen2da0a052013-11-08 06:28:17 -08001399 if (!mScroller.isFinished()) {
1400 mScrollAbortedFromIntercept = true;
1401 abortScrollerAnimation(false);
1402 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001403 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001404 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1405 mTouchState = TOUCH_STATE_SCROLLING;
1406 } else {
1407 mTouchState = TOUCH_STATE_REST;
1408 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001409 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001410
Winson Chung86f77532010-08-24 11:08:22 -07001411 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001413 if (!DISABLE_TOUCH_SIDE_PAGES) {
1414 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1415 if (getChildCount() > 0) {
1416 if (hitsPreviousPage(x, y)) {
1417 mTouchState = TOUCH_STATE_PREV_PAGE;
1418 } else if (hitsNextPage(x, y)) {
1419 mTouchState = TOUCH_STATE_NEXT_PAGE;
1420 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 }
1422 }
1423 }
1424 break;
1425 }
1426
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001428 case MotionEvent.ACTION_CANCEL:
Adam Cohencae7f572013-11-04 14:42:52 -08001429 if (mScrollAbortedFromIntercept) {
1430 snapToDestination();
1431 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001432 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 break;
1434
1435 case MotionEvent.ACTION_POINTER_UP:
1436 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001437 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 break;
1439 }
1440
1441 /*
1442 * The only time we want to intercept motion events is if we are in the
1443 * drag mode.
1444 */
1445 return mTouchState != TOUCH_STATE_REST;
1446 }
1447
Adam Cohenf8d28232011-02-01 21:47:00 -08001448 protected void determineScrollingStart(MotionEvent ev) {
1449 determineScrollingStart(ev, 1.0f);
1450 }
1451
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 /*
1453 * Determines if we should change the touch state to start scrolling after the
1454 * user moves their touch point too far.
1455 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001456 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001457 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001459 if (pointerIndex == -1) return;
1460
1461 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 final float x = ev.getX(pointerIndex);
1463 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001464 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1465
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 final int xDiff = (int) Math.abs(x - mLastMotionX);
1467 final int yDiff = (int) Math.abs(y - mLastMotionY);
1468
Adam Cohenf8d28232011-02-01 21:47:00 -08001469 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 boolean xPaged = xDiff > mPagingTouchSlop;
1471 boolean xMoved = xDiff > touchSlop;
1472 boolean yMoved = yDiff > touchSlop;
1473
Adam Cohenf8d28232011-02-01 21:47:00 -08001474 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001475 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 // Scroll if the user moved far enough along the X axis
1477 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001478 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001479 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001480 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001481 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001482 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1483 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001485 }
1486 }
1487
Adam Cohen7d30a372013-07-01 17:03:59 -07001488 protected float getMaxScrollProgress() {
1489 return 1.0f;
1490 }
1491
Adam Cohenf8d28232011-02-01 21:47:00 -08001492 protected void cancelCurrentPageLongPress() {
1493 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001494 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001495 // Try canceling the long press. It could also have been scheduled
1496 // by a distant descendant, so use the mAllowLongPress flag to block
1497 // everything
1498 final View currentPage = getPageAt(mCurrentPage);
1499 if (currentPage != null) {
1500 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001501 }
1502 }
1503 }
1504
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1506 final int halfScreenSize = getViewportWidth() / 2;
1507
1508 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1509 screenCenter = Math.max(halfScreenSize, screenCenter);
1510
1511 return getScrollProgress(screenCenter, v, page);
1512 }
1513
Adam Cohenb5ba0972011-09-07 18:02:31 -07001514 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001515 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001516
Adam Cohenedb40762013-07-18 16:45:45 -07001517 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001518 int count = getChildCount();
1519
1520 final int totalDistance;
1521
1522 int adjacentPage = page + 1;
1523 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1524 adjacentPage = page - 1;
1525 }
1526
1527 if (adjacentPage < 0 || adjacentPage > count - 1) {
1528 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1529 } else {
1530 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1531 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001532
1533 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001534 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1535 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001536 return scrollProgress;
1537 }
1538
Adam Cohenedb40762013-07-18 16:45:45 -07001539 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001540 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001541 return 0;
1542 } else {
1543 return mPageScrolls[index];
1544 }
1545 }
1546
Adam Cohen564a2e72013-10-09 14:47:32 -07001547 // While layout transitions are occurring, a child's position may stray from its baseline
1548 // position. This method returns the magnitude of this stray at any given time.
1549 public int getLayoutTransitionOffsetForPage(int index) {
1550 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1551 return 0;
1552 } else {
1553 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001554
1555 int scrollOffset = 0;
1556 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1557 if (!lp.isFullScreenPage) {
1558 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1559 }
1560
Adam Cohen564a2e72013-10-09 14:47:32 -07001561 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1562 return (int) (child.getX() - baselineX);
1563 }
1564 }
1565
Adam Cohene0f66b52010-11-23 15:06:07 -08001566 // This curve determines how the effect of scrolling over the limits of the page dimishes
1567 // as the user pulls further and further from the bounds
1568 private float overScrollInfluenceCurve(float f) {
1569 f -= 1.0f;
1570 return f * f * f + 1.0f;
1571 }
1572
Adam Cohenb5ba0972011-09-07 18:02:31 -07001573 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001574 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001575
1576 // We want to reach the max over scroll effect when the user has
1577 // over scrolled half the size of the screen
1578 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1579
1580 if (f == 0) return;
1581
1582 // Clamp this factor, f, to -1 < f < 1
1583 if (Math.abs(f) >= 1) {
1584 f /= Math.abs(f);
1585 }
1586
1587 int overScrollAmount = (int) Math.round(f * screenSize);
1588 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001589 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001590 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001591 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001592 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001593 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001594 }
1595 invalidate();
1596 }
1597
1598 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001599 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001600
1601 float f = (amount / screenSize);
1602
1603 if (f == 0) return;
1604 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1605
Adam Cohen7bfc9792011-01-28 13:52:37 -08001606 // Clamp this factor, f, to -1 < f < 1
1607 if (Math.abs(f) >= 1) {
1608 f /= Math.abs(f);
1609 }
1610
Adam Cohene0f66b52010-11-23 15:06:07 -08001611 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001612 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001613 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001614 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001615 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001616 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001617 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001618 }
1619 invalidate();
1620 }
1621
Adam Cohenb5ba0972011-09-07 18:02:31 -07001622 protected void overScroll(float amount) {
1623 dampedOverScroll(amount);
1624 }
1625
Michael Jurkac5b262c2011-01-12 20:24:50 -08001626 protected float maxOverScroll() {
1627 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001628 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001629 float f = 1.0f;
1630 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1631 return OVERSCROLL_DAMP_FACTOR * f;
1632 }
1633
Adam Cohenf358a4b2013-07-23 16:47:31 -07001634 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001635 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001636 }
1637
Adam Cohenf9618852013-11-08 06:45:03 -08001638 protected void disableFreeScroll() {
1639 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001640 }
1641
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001642 void updateFreescrollBounds() {
1643 getOverviewModePages(mTempVisiblePagesRange);
1644 if (isLayoutRtl()) {
1645 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1646 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1647 } else {
1648 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1649 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1650 }
1651 }
1652
Adam Cohenf9618852013-11-08 06:45:03 -08001653 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001654 mFreeScroll = freeScroll;
1655
Adam Cohenf9618852013-11-08 06:45:03 -08001656 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001657 updateFreescrollBounds();
1658 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001659 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1660 setCurrentPage(mTempVisiblePagesRange[0]);
1661 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1662 setCurrentPage(mTempVisiblePagesRange[1]);
1663 }
1664 }
1665
1666 setEnableOverscroll(!freeScroll);
1667 }
1668
1669 private void setEnableOverscroll(boolean enable) {
1670 mAllowOverScroll = enable;
1671 }
1672
1673 int getNearestHoverOverPageIndex() {
1674 if (mDragView != null) {
1675 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1676 + mDragView.getTranslationX());
1677 getOverviewModePages(mTempVisiblePagesRange);
1678 int minDistance = Integer.MAX_VALUE;
1679 int minIndex = indexOfChild(mDragView);
1680 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1681 View page = getPageAt(i);
1682 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1683 int d = Math.abs(dragX - pageX);
1684 if (d < minDistance) {
1685 minIndex = i;
1686 minDistance = d;
1687 }
1688 }
1689 return minIndex;
1690 }
1691 return -1;
1692 }
1693
Winson Chung321e9ee2010-08-09 13:37:56 -07001694 @Override
1695 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001696 if (DISABLE_TOUCH_INTERACTION) {
1697 return false;
1698 }
1699
Adam Cohen1697b792013-09-17 19:08:21 -07001700 super.onTouchEvent(ev);
1701
Winson Chung45e1d6e2010-11-09 17:19:49 -08001702 // Skip touch handling if there are no pages to swipe
1703 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1704
Michael Jurkab8f06722010-10-10 15:58:46 -07001705 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001706
1707 final int action = ev.getAction();
1708
1709 switch (action & MotionEvent.ACTION_MASK) {
1710 case MotionEvent.ACTION_DOWN:
1711 /*
1712 * If being flinged and user touches, stop the fling. isFinished
1713 * will be false if being flinged.
1714 */
1715 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001716 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001717 }
1718
1719 // Remember where the motion event started
1720 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001721 mDownMotionY = mLastMotionY = ev.getY();
1722 mDownScrollX = getScrollX();
1723 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1724 mParentDownMotionX = p[0];
1725 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001726 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001727 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001728 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001729
Michael Jurka0142d492010-08-25 17:46:15 -07001730 if (mTouchState == TOUCH_STATE_SCROLLING) {
1731 pageBeginMoving();
1732 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001733 break;
1734
1735 case MotionEvent.ACTION_MOVE:
1736 if (mTouchState == TOUCH_STATE_SCROLLING) {
1737 // Scroll to follow the motion event
1738 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001739
1740 if (pointerIndex == -1) return true;
1741
Winson Chung321e9ee2010-08-09 13:37:56 -07001742 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001743 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001744
Adam Cohenaefd4e12011-02-14 16:39:38 -08001745 mTotalMotionX += Math.abs(deltaX);
1746
Winson Chungc0844aa2011-02-02 15:25:58 -08001747 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1748 // keep the remainder because we are actually testing if we've moved from the last
1749 // scrolled position (which is discrete).
1750 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001751 mTouchX += deltaX;
1752 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1753 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001754 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001755 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001756 } else {
1757 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001758 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001759 mLastMotionX = x;
1760 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001761 } else {
1762 awakenScrollBars();
1763 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001764 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1765 // Update the last motion position
1766 mLastMotionX = ev.getX();
1767 mLastMotionY = ev.getY();
1768
1769 // Update the parent down so that our zoom animations take this new movement into
1770 // account
1771 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1772 mParentDownMotionX = pt[0];
1773 mParentDownMotionY = pt[1];
1774 updateDragViewTranslationDuringDrag();
1775
1776 // Find the closest page to the touch point
1777 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001778
1779 // Change the drag view if we are hovering over the drop target
1780 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1781 (int) mParentDownMotionX, (int) mParentDownMotionY);
1782 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1783
Adam Cohen7d30a372013-07-01 17:03:59 -07001784 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1785 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1786 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1787 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1788
Adam Cohenf358a4b2013-07-23 16:47:31 -07001789 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1790 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1791 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001792 mTempVisiblePagesRange[0] = 0;
1793 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001794 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001795 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1796 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1797 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1798 mSidePageHoverIndex = pageUnderPointIndex;
1799 mSidePageHoverRunnable = new Runnable() {
1800 @Override
1801 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001802 // Setup the scroll to the correct page before we swap the views
1803 snapToPage(pageUnderPointIndex);
1804
1805 // For each of the pages between the paged view and the drag view,
1806 // animate them from the previous position to the new position in
1807 // the layout (as a result of the drag view moving in the layout)
1808 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1809 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1810 dragViewIndex + 1 : pageUnderPointIndex;
1811 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1812 dragViewIndex - 1 : pageUnderPointIndex;
1813 for (int i = lowerIndex; i <= upperIndex; ++i) {
1814 View v = getChildAt(i);
1815 // dragViewIndex < pageUnderPointIndex, so after we remove the
1816 // drag view all subsequent views to pageUnderPointIndex will
1817 // shift down.
1818 int oldX = getViewportOffsetX() + getChildOffset(i);
1819 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1820
1821 // Animate the view translation from its old position to its new
1822 // position
1823 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1824 if (anim != null) {
1825 anim.cancel();
1826 }
1827
1828 v.setTranslationX(oldX - newX);
1829 anim = new AnimatorSet();
1830 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1831 anim.playTogether(
1832 ObjectAnimator.ofFloat(v, "translationX", 0f));
1833 anim.start();
1834 v.setTag(anim);
1835 }
1836
1837 removeView(mDragView);
1838 onRemoveView(mDragView, false);
1839 addView(mDragView, pageUnderPointIndex);
1840 onAddView(mDragView, pageUnderPointIndex);
1841 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001842 if (mPageIndicator != null) {
1843 mPageIndicator.setActiveMarker(getNextPage());
1844 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001845 }
1846 };
1847 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1848 }
1849 } else {
1850 removeCallbacks(mSidePageHoverRunnable);
1851 mSidePageHoverIndex = -1;
1852 }
Adam Cohen564976a2010-10-13 18:52:07 -07001853 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001854 determineScrollingStart(ev);
1855 }
1856 break;
1857
1858 case MotionEvent.ACTION_UP:
1859 if (mTouchState == TOUCH_STATE_SCROLLING) {
1860 final int activePointerId = mActivePointerId;
1861 final int pointerIndex = ev.findPointerIndex(activePointerId);
1862 final float x = ev.getX(pointerIndex);
1863 final VelocityTracker velocityTracker = mVelocityTracker;
1864 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1865 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001866 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001867 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001868 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1869 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001870
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001871 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1872
Adam Cohen00481b32011-11-18 12:03:48 -08001873 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001874 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001875
Adam Cohenf358a4b2013-07-23 16:47:31 -07001876 if (!mFreeScroll) {
1877 // In the case that the page is moved far to one direction and then is flung
1878 // in the opposite direction, we use a threshold to determine whether we should
1879 // just return to the starting page, or if we should skip one further.
1880 boolean returnToOriginalPage = false;
1881 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1882 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1883 returnToOriginalPage = true;
1884 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001885
Adam Cohenf358a4b2013-07-23 16:47:31 -07001886 int finalPage;
1887 // We give flings precedence over large moves, which is why we short-circuit our
1888 // test for a large move if a fling has been registered. That is, a large
1889 // move to the left and fling to the right will register as a fling to the right.
1890 final boolean isRtl = isLayoutRtl();
1891 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1892 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1893 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1894 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1895 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1896 snapToPageWithVelocity(finalPage, velocityX);
1897 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1898 (isFling && isVelocityXLeft)) &&
1899 mCurrentPage < getChildCount() - 1) {
1900 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1901 snapToPageWithVelocity(finalPage, velocityX);
1902 } else {
1903 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001904 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001905 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001906 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001907 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001908 }
1909
1910 float scaleX = getScaleX();
1911 int vX = (int) (-velocityX * scaleX);
1912 int initialScrollX = (int) (getScrollX() * scaleX);
1913
Adam Cohenf9618852013-11-08 06:45:03 -08001914 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001915 mScroller.fling(initialScrollX,
1916 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1917 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001918 }
Adam Cohencae7f572013-11-04 14:42:52 -08001919 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1920 // at this point we have not moved beyond the touch slop
1921 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1922 // we can just page
1923 int nextPage = Math.max(0, mCurrentPage - 1);
1924 if (nextPage != mCurrentPage) {
1925 snapToPage(nextPage);
1926 } else {
1927 snapToDestination();
1928 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001929 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001930 // 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
Winson Chung86f77532010-08-24 11:08:22 -07001933 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1934 if (nextPage != mCurrentPage) {
1935 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001936 } else {
1937 snapToDestination();
1938 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001939 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1940 // Update the last motion position
1941 mLastMotionX = ev.getX();
1942 mLastMotionY = ev.getY();
1943
1944 // Update the parent down so that our zoom animations take this new movement into
1945 // account
1946 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1947 mParentDownMotionX = pt[0];
1948 mParentDownMotionY = pt[1];
1949 updateDragViewTranslationDuringDrag();
1950 boolean handledFling = false;
1951 if (!DISABLE_FLING_TO_DELETE) {
1952 // Check the velocity and see if we are flinging-to-delete
1953 PointF flingToDeleteVector = isFlingingToDelete();
1954 if (flingToDeleteVector != null) {
1955 onFlingToDelete(flingToDeleteVector);
1956 handledFling = true;
1957 }
1958 }
1959 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1960 (int) mParentDownMotionY)) {
1961 onDropToDelete();
1962 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001963 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001964 if (!mCancelTap) {
1965 onUnhandledTap(ev);
1966 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001967 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001968
1969 // Remove the callback to wait for the side page hover timeout
1970 removeCallbacks(mSidePageHoverRunnable);
1971 // End any intermediate reordering states
1972 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001973 break;
1974
1975 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001976 if (mTouchState == TOUCH_STATE_SCROLLING) {
1977 snapToDestination();
1978 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001979 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001980 break;
1981
1982 case MotionEvent.ACTION_POINTER_UP:
1983 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001984 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001985 break;
1986 }
1987
1988 return true;
1989 }
1990
Adam Cohen7d30a372013-07-01 17:03:59 -07001991 public void onFlingToDelete(View v) {}
1992 public void onRemoveView(View v, boolean deletePermanently) {}
1993 public void onRemoveViewAnimationCompleted() {}
1994 public void onAddView(View v, int index) {}
1995
1996 private void resetTouchState() {
1997 releaseVelocityTracker();
1998 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001999 mCancelTap = false;
Adam Cohencae7f572013-11-04 14:42:52 -08002000 mScrollAbortedFromIntercept = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002001 mTouchState = TOUCH_STATE_REST;
2002 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002003 }
2004
Adam Cohen1697b792013-09-17 19:08:21 -07002005 protected void onUnhandledTap(MotionEvent ev) {
2006 ((Launcher) getContext()).onClick(this);
2007 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002008
Winson Chung185d7162011-02-28 13:47:29 -08002009 @Override
2010 public boolean onGenericMotionEvent(MotionEvent event) {
2011 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2012 switch (event.getAction()) {
2013 case MotionEvent.ACTION_SCROLL: {
2014 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2015 final float vscroll;
2016 final float hscroll;
2017 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2018 vscroll = 0;
2019 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2020 } else {
2021 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2022 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2023 }
2024 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002025 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2026 : (hscroll > 0 || vscroll > 0);
2027 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002028 scrollRight();
2029 } else {
2030 scrollLeft();
2031 }
2032 return true;
2033 }
2034 }
2035 }
2036 }
2037 return super.onGenericMotionEvent(event);
2038 }
2039
Michael Jurkab8f06722010-10-10 15:58:46 -07002040 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2041 if (mVelocityTracker == null) {
2042 mVelocityTracker = VelocityTracker.obtain();
2043 }
2044 mVelocityTracker.addMovement(ev);
2045 }
2046
2047 private void releaseVelocityTracker() {
2048 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002049 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002050 mVelocityTracker.recycle();
2051 mVelocityTracker = null;
2052 }
2053 }
2054
Winson Chung321e9ee2010-08-09 13:37:56 -07002055 private void onSecondaryPointerUp(MotionEvent ev) {
2056 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2057 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2058 final int pointerId = ev.getPointerId(pointerIndex);
2059 if (pointerId == mActivePointerId) {
2060 // This was our active pointer going up. Choose a new
2061 // active pointer and adjust accordingly.
2062 // TODO: Make this decision more intelligent.
2063 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2064 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2065 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002066 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002067 mActivePointerId = ev.getPointerId(newPointerIndex);
2068 if (mVelocityTracker != null) {
2069 mVelocityTracker.clear();
2070 }
2071 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002072 }
2073
Winson Chung321e9ee2010-08-09 13:37:56 -07002074 @Override
2075 public void requestChildFocus(View child, View focused) {
2076 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002077 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002078 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002079 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002080 }
2081 }
2082
Winson Chung1908d072011-02-24 18:09:44 -08002083 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002084 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002085 }
2086
Adam Cohen7d30a372013-07-01 17:03:59 -07002087 int getPageNearestToPoint(float x) {
2088 int index = 0;
2089 for (int i = 0; i < getChildCount(); ++i) {
2090 if (x < getChildAt(i).getRight() - getScrollX()) {
2091 return index;
2092 } else {
2093 index++;
2094 }
2095 }
2096 return Math.min(index, getChildCount() - 1);
2097 }
2098
Adam Cohend19d3ca2010-09-15 14:43:42 -07002099 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002100 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002101 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002102 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002103 final int childCount = getChildCount();
2104 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002105 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002106 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002107 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002108 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002109 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2110 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2111 minDistanceFromScreenCenter = distanceFromScreenCenter;
2112 minDistanceFromScreenCenterIndex = i;
2113 }
2114 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002115 return minDistanceFromScreenCenterIndex;
2116 }
2117
2118 protected void snapToDestination() {
2119 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 }
2121
Adam Cohene0f66b52010-11-23 15:06:07 -08002122 private static class ScrollInterpolator implements Interpolator {
2123 public ScrollInterpolator() {
2124 }
2125
2126 public float getInterpolation(float t) {
2127 t -= 1.0f;
2128 return t*t*t*t*t + 1;
2129 }
2130 }
2131
2132 // We want the duration of the page snap animation to be influenced by the distance that
2133 // the screen has to travel, however, we don't want this duration to be effected in a
2134 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2135 // of travel has on the overall snap duration.
2136 float distanceInfluenceForSnapDuration(float f) {
2137 f -= 0.5f; // center the values about 0.
2138 f *= 0.3f * Math.PI / 2.0f;
2139 return (float) Math.sin(f);
2140 }
2141
Michael Jurka0142d492010-08-25 17:46:15 -07002142 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002143 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
Adam Cohen7d30a372013-07-01 17:03:59 -07002144 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002145
Adam Cohenedb40762013-07-18 16:45:45 -07002146 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002147 int delta = newX - mUnboundedScrollX;
2148 int duration = 0;
2149
Adam Cohen265b9a62011-12-07 14:37:18 -08002150 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002151 // If the velocity is low enough, then treat this more as an automatic page advance
2152 // as opposed to an apparent physical response to flinging
2153 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2154 return;
2155 }
2156
2157 // Here we compute a "distance" that will be used in the computation of the overall
2158 // snap duration. This is a function of the actual distance that needs to be traveled;
2159 // we keep this value close to half screen size in order to reduce the variance in snap
2160 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002161 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002162 float distance = halfScreenSize + halfScreenSize *
2163 distanceInfluenceForSnapDuration(distanceRatio);
2164
2165 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002166 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002167
2168 // we want the page's snap velocity to approximately match the velocity at which the
2169 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002170 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2171 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002172
2173 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002174 }
2175
2176 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002177 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002178 }
2179
Adam Cohen7d30a372013-07-01 17:03:59 -07002180 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002181 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002182 }
2183
Michael Jurka0142d492010-08-25 17:46:15 -07002184 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002185 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002186 }
2187
Adam Cohenf9618852013-11-08 06:45:03 -08002188 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2189 snapToPage(whichPage, duration, false, interpolator);
2190 }
2191
2192 protected void snapToPage(int whichPage, int duration, boolean immediate,
2193 TimeInterpolator interpolator) {
Winson Chung86f77532010-08-24 11:08:22 -07002194 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07002195
Adam Cohenedb40762013-07-18 16:45:45 -07002196 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002197 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002198 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002199 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002200 }
2201
2202 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002203 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002204 }
Michael Jurka0142d492010-08-25 17:46:15 -07002205
Adam Cohenf9618852013-11-08 06:45:03 -08002206 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2207 TimeInterpolator interpolator) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002208 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002209 View focusedChild = getFocusedChild();
2210 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002211 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002212 focusedChild.clearFocus();
2213 }
2214
Adam Cohen53805212013-10-01 10:39:23 -07002215 sendScrollAccessibilityEvent();
2216
Michael Jurka0142d492010-08-25 17:46:15 -07002217 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002218 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002219 if (immediate) {
2220 duration = 0;
2221 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002222 duration = Math.abs(delta);
2223 }
2224
Adam Cohenf358a4b2013-07-23 16:47:31 -07002225 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002226 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002227 }
Adam Cohenf9618852013-11-08 06:45:03 -08002228
2229 if (interpolator != null) {
2230 mScroller.setInterpolator(interpolator);
2231 } else {
2232 mScroller.setInterpolator(mDefaultInterpolator);
2233 }
2234
Adam Cohen68d73932010-11-15 10:50:58 -08002235 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002236
Michael Jurka0142d492010-08-25 17:46:15 -07002237 notifyPageSwitchListener();
Adam Cohen7d30a372013-07-01 17:03:59 -07002238
2239 // Trigger a compute() to finish switching pages if necessary
2240 if (immediate) {
2241 computeScroll();
2242 }
2243
Winson Chung9c0565f2013-07-19 13:49:06 -07002244 // Defer loading associated pages until the scroll settles
2245 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2246
Adam Cohen7d30a372013-07-01 17:03:59 -07002247 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002248 invalidate();
2249 }
2250
Winson Chung321e9ee2010-08-09 13:37:56 -07002251 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002252 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002253 }
2254
2255 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002256 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002257 }
2258
Winson Chung86f77532010-08-24 11:08:22 -07002259 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002260 int result = -1;
2261 if (v != null) {
2262 ViewParent vp = v.getParent();
2263 int count = getChildCount();
2264 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002265 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002266 return i;
2267 }
2268 }
2269 }
2270 return result;
2271 }
2272
2273 /**
2274 * @return True is long presses are still allowed for the current touch
2275 */
2276 public boolean allowLongPress() {
2277 return mAllowLongPress;
2278 }
2279
Adam Cohendbdff6b2013-09-18 19:09:15 -07002280 @Override
2281 public boolean performLongClick() {
2282 mCancelTap = true;
2283 return super.performLongClick();
2284 }
2285
Michael Jurka0142d492010-08-25 17:46:15 -07002286 /**
2287 * Set true to allow long-press events to be triggered, usually checked by
2288 * {@link Launcher} to accept or block dpad-initiated long-presses.
2289 */
2290 public void setAllowLongPress(boolean allowLongPress) {
2291 mAllowLongPress = allowLongPress;
2292 }
2293
Winson Chung321e9ee2010-08-09 13:37:56 -07002294 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002295 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002296
2297 SavedState(Parcelable superState) {
2298 super(superState);
2299 }
2300
2301 private SavedState(Parcel in) {
2302 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002303 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002304 }
2305
2306 @Override
2307 public void writeToParcel(Parcel out, int flags) {
2308 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002309 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002310 }
2311
2312 public static final Parcelable.Creator<SavedState> CREATOR =
2313 new Parcelable.Creator<SavedState>() {
2314 public SavedState createFromParcel(Parcel in) {
2315 return new SavedState(in);
2316 }
2317
2318 public SavedState[] newArray(int size) {
2319 return new SavedState[size];
2320 }
2321 };
2322 }
2323
Winson Chungf314b0e2011-08-16 11:54:27 -07002324 protected void loadAssociatedPages(int page) {
2325 loadAssociatedPages(page, false);
2326 }
2327 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002328 if (mContentIsRefreshable) {
2329 final int count = getChildCount();
2330 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002331 int lowerPageBound = getAssociatedLowerPageBound(page);
2332 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002333 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2334 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002335 // First, clear any pages that should no longer be loaded
2336 for (int i = 0; i < count; ++i) {
2337 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002338 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002339 if (layout.getPageChildCount() > 0) {
2340 layout.removeAllViewsOnPage();
2341 }
2342 mDirtyPageContent.set(i, true);
2343 }
2344 }
2345 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002346 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002347 if ((i != page) && immediateAndOnly) {
2348 continue;
2349 }
Michael Jurka0142d492010-08-25 17:46:15 -07002350 if (lowerPageBound <= i && i <= upperPageBound) {
2351 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002352 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002353 mDirtyPageContent.set(i, false);
2354 }
Winson Chung86f77532010-08-24 11:08:22 -07002355 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002356 }
2357 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002358 }
2359 }
2360
Winson Chunge3193b92010-09-10 11:44:42 -07002361 protected int getAssociatedLowerPageBound(int page) {
2362 return Math.max(0, page - 1);
2363 }
2364 protected int getAssociatedUpperPageBound(int page) {
2365 final int count = getChildCount();
2366 return Math.min(page + 1, count - 1);
2367 }
2368
Winson Chung86f77532010-08-24 11:08:22 -07002369 /**
2370 * This method is called ONLY to synchronize the number of pages that the paged view has.
2371 * To actually fill the pages with information, implement syncPageItems() below. It is
2372 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2373 * and therefore, individual page items do not need to be updated in this method.
2374 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002375 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002376
2377 /**
2378 * This method is called to synchronize the items that are on a particular page. If views on
2379 * the page can be reused, then they should be updated within this method.
2380 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002381 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002382
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002383 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002384 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002385 }
2386 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002387 invalidatePageData(currentPage, false);
2388 }
2389 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002390 if (!mIsDataReady) {
2391 return;
2392 }
2393
Michael Jurka0142d492010-08-25 17:46:15 -07002394 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002395 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002396 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002397
Michael Jurka0142d492010-08-25 17:46:15 -07002398 // Update all the pages
2399 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002400
Winson Chung5a808352011-06-27 19:08:49 -07002401 // We must force a measure after we've loaded the pages to update the content width and
2402 // to determine the full scroll width
2403 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2404 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2405
2406 // Set a new page as the current page if necessary
2407 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002408 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002409 }
2410
Michael Jurka0142d492010-08-25 17:46:15 -07002411 // Mark each of the pages as dirty
2412 final int count = getChildCount();
2413 mDirtyPageContent.clear();
2414 for (int i = 0; i < count; ++i) {
2415 mDirtyPageContent.add(true);
2416 }
2417
2418 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002419 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002420 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002421 }
Adam Cohen06559042013-09-29 18:30:56 -07002422 if (isPageMoving()) {
2423 // If the page is moving, then snap it to the final position to ensure we don't get
2424 // stuck between pages
2425 snapToDestination();
2426 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002427 }
Winson Chung007c6982011-06-14 13:27:53 -07002428
Adam Cohen7d30a372013-07-01 17:03:59 -07002429 // Animate the drag view back to the original position
2430 void animateDragViewToOriginalPosition() {
2431 if (mDragView != null) {
2432 AnimatorSet anim = new AnimatorSet();
2433 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2434 anim.playTogether(
2435 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002436 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2437 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2438 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002439 anim.addListener(new AnimatorListenerAdapter() {
2440 @Override
2441 public void onAnimationEnd(Animator animation) {
2442 onPostReorderingAnimationCompleted();
2443 }
2444 });
2445 anim.start();
2446 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002447 }
2448
Adam Cohen7d30a372013-07-01 17:03:59 -07002449 protected void onStartReordering() {
2450 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2451 mTouchState = TOUCH_STATE_REORDERING;
2452 mIsReordering = true;
2453
Adam Cohen7d30a372013-07-01 17:03:59 -07002454 // We must invalidate to trigger a redraw to update the layers such that the drag view
2455 // is always drawn on top
2456 invalidate();
2457 }
2458
2459 private void onPostReorderingAnimationCompleted() {
2460 // Trigger the callback when reordering has settled
2461 --mPostReorderingPreZoomInRemainingAnimationCount;
2462 if (mPostReorderingPreZoomInRunnable != null &&
2463 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2464 mPostReorderingPreZoomInRunnable.run();
2465 mPostReorderingPreZoomInRunnable = null;
2466 }
2467 }
2468
2469 protected void onEndReordering() {
2470 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002471 }
2472
Adam Cohenf358a4b2013-07-23 16:47:31 -07002473 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002474 int dragViewIndex = indexOfChild(v);
2475
2476 if (mTouchState != TOUCH_STATE_REST) return false;
2477
Adam Cohen7d30a372013-07-01 17:03:59 -07002478 mTempVisiblePagesRange[0] = 0;
2479 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002480 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002481 mReorderingStarted = true;
2482
2483 // Check if we are within the reordering range
2484 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002485 dragViewIndex <= mTempVisiblePagesRange[1]) {
2486 // Find the drag view under the pointer
2487 mDragView = getChildAt(dragViewIndex);
2488 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2489 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002490 snapToPage(getPageNearestToCenterOfScreen());
2491 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002492 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002493 return true;
2494 }
2495 return false;
2496 }
2497
2498 boolean isReordering(boolean testTouchState) {
2499 boolean state = mIsReordering;
2500 if (testTouchState) {
2501 state &= (mTouchState == TOUCH_STATE_REORDERING);
2502 }
2503 return state;
2504 }
2505 void endReordering() {
2506 // For simplicity, we call endReordering sometimes even if reordering was never started.
2507 // In that case, we don't want to do anything.
2508 if (!mReorderingStarted) return;
2509 mReorderingStarted = false;
2510
2511 // If we haven't flung-to-delete the current child, then we just animate the drag view
2512 // back into position
2513 final Runnable onCompleteRunnable = new Runnable() {
2514 @Override
2515 public void run() {
2516 onEndReordering();
2517 }
2518 };
2519 if (!mDeferringForDelete) {
2520 mPostReorderingPreZoomInRunnable = new Runnable() {
2521 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002522 onCompleteRunnable.run();
2523 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002524 };
2525 };
2526
2527 mPostReorderingPreZoomInRemainingAnimationCount =
2528 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2529 // Snap to the current page
2530 snapToPage(indexOfChild(mDragView), 0);
2531 // Animate the drag view back to the front position
2532 animateDragViewToOriginalPosition();
2533 } else {
2534 // Handled in post-delete-animation-callbacks
2535 }
2536 }
2537
Adam Cohen7d30a372013-07-01 17:03:59 -07002538 /*
2539 * Flinging to delete - IN PROGRESS
2540 */
2541 private PointF isFlingingToDelete() {
2542 ViewConfiguration config = ViewConfiguration.get(getContext());
2543 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2544
2545 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2546 // Do a quick dot product test to ensure that we are flinging upwards
2547 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2548 mVelocityTracker.getYVelocity());
2549 PointF upVec = new PointF(0f, -1f);
2550 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2551 (vel.length() * upVec.length()));
2552 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2553 return vel;
2554 }
2555 }
2556 return null;
2557 }
2558
2559 /**
2560 * Creates an animation from the current drag view along its current velocity vector.
2561 * For this animation, the alpha runs for a fixed duration and we update the position
2562 * progressively.
2563 */
2564 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2565 private View mDragView;
2566 private PointF mVelocity;
2567 private Rect mFrom;
2568 private long mPrevTime;
2569 private float mFriction;
2570
2571 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2572
2573 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2574 long startTime, float friction) {
2575 mDragView = dragView;
2576 mVelocity = vel;
2577 mFrom = from;
2578 mPrevTime = startTime;
2579 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2580 }
2581
2582 @Override
2583 public void onAnimationUpdate(ValueAnimator animation) {
2584 float t = ((Float) animation.getAnimatedValue()).floatValue();
2585 long curTime = AnimationUtils.currentAnimationTimeMillis();
2586
2587 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2588 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2589
2590 mDragView.setTranslationX(mFrom.left);
2591 mDragView.setTranslationY(mFrom.top);
2592 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2593
2594 mVelocity.x *= mFriction;
2595 mVelocity.y *= mFriction;
2596 mPrevTime = curTime;
2597 }
2598 };
2599
2600 private static final int ANIM_TAG_KEY = 100;
2601
2602 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2603 return new Runnable() {
2604 @Override
2605 public void run() {
2606 int dragViewIndex = indexOfChild(dragView);
2607
2608 // For each of the pages around the drag view, animate them from the previous
2609 // position to the new position in the layout (as a result of the drag view moving
2610 // in the layout)
2611 // NOTE: We can make an assumption here because we have side-bound pages that we
2612 // will always have pages to animate in from the left
Adam Cohenf358a4b2013-07-23 16:47:31 -07002613 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002614 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2615 boolean slideFromLeft = (isLastWidgetPage ||
2616 dragViewIndex > mTempVisiblePagesRange[0]);
2617
2618 // Setup the scroll to the correct page before we swap the views
2619 if (slideFromLeft) {
2620 snapToPageImmediately(dragViewIndex - 1);
2621 }
2622
2623 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2624 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2625 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2626 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2627 ArrayList<Animator> animations = new ArrayList<Animator>();
2628 for (int i = lowerIndex; i <= upperIndex; ++i) {
2629 View v = getChildAt(i);
2630 // dragViewIndex < pageUnderPointIndex, so after we remove the
2631 // drag view all subsequent views to pageUnderPointIndex will
2632 // shift down.
2633 int oldX = 0;
2634 int newX = 0;
2635 if (slideFromLeft) {
2636 if (i == 0) {
2637 // Simulate the page being offscreen with the page spacing
2638 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002639 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002640 } else {
2641 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2642 }
2643 newX = getViewportOffsetX() + getChildOffset(i);
2644 } else {
2645 oldX = getChildOffset(i) - getChildOffset(i - 1);
2646 newX = 0;
2647 }
2648
2649 // Animate the view translation from its old position to its new
2650 // position
2651 AnimatorSet anim = (AnimatorSet) v.getTag();
2652 if (anim != null) {
2653 anim.cancel();
2654 }
2655
2656 // Note: Hacky, but we want to skip any optimizations to not draw completely
2657 // hidden views
2658 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2659 v.setTranslationX(oldX - newX);
2660 anim = new AnimatorSet();
2661 anim.playTogether(
2662 ObjectAnimator.ofFloat(v, "translationX", 0f),
2663 ObjectAnimator.ofFloat(v, "alpha", 1f));
2664 animations.add(anim);
2665 v.setTag(ANIM_TAG_KEY, anim);
2666 }
2667
2668 AnimatorSet slideAnimations = new AnimatorSet();
2669 slideAnimations.playTogether(animations);
2670 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2671 slideAnimations.addListener(new AnimatorListenerAdapter() {
2672 @Override
2673 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002674 mDeferringForDelete = false;
2675 onEndReordering();
2676 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002677 }
2678 });
2679 slideAnimations.start();
2680
2681 removeView(dragView);
2682 onRemoveView(dragView, true);
2683 }
2684 };
2685 }
2686
2687 public void onFlingToDelete(PointF vel) {
2688 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2689
2690 // NOTE: Because it takes time for the first frame of animation to actually be
2691 // called and we expect the animation to be a continuation of the fling, we have
2692 // to account for the time that has elapsed since the fling finished. And since
2693 // we don't have a startDelay, we will always get call to update when we call
2694 // start() (which we want to ignore).
2695 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2696 private int mCount = -1;
2697 private long mStartTime;
2698 private float mOffset;
2699 /* Anonymous inner class ctor */ {
2700 mStartTime = startTime;
2701 }
2702
2703 @Override
2704 public float getInterpolation(float t) {
2705 if (mCount < 0) {
2706 mCount++;
2707 } else if (mCount == 0) {
2708 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2709 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2710 mCount++;
2711 }
2712 return Math.min(1f, mOffset + t);
2713 }
2714 };
2715
2716 final Rect from = new Rect();
2717 final View dragView = mDragView;
2718 from.left = (int) dragView.getTranslationX();
2719 from.top = (int) dragView.getTranslationY();
2720 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2721 from, startTime, FLING_TO_DELETE_FRICTION);
2722
2723 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2724
2725 // Create and start the animation
2726 ValueAnimator mDropAnim = new ValueAnimator();
2727 mDropAnim.setInterpolator(tInterpolator);
2728 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2729 mDropAnim.setFloatValues(0f, 1f);
2730 mDropAnim.addUpdateListener(updateCb);
2731 mDropAnim.addListener(new AnimatorListenerAdapter() {
2732 public void onAnimationEnd(Animator animation) {
2733 onAnimationEndRunnable.run();
2734 }
2735 });
2736 mDropAnim.start();
2737 mDeferringForDelete = true;
2738 }
2739
2740 /* Drag to delete */
2741 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2742 if (mDeleteDropTarget != null) {
2743 mAltTmpRect.set(0, 0, 0, 0);
2744 View parent = (View) mDeleteDropTarget.getParent();
2745 if (parent != null) {
2746 parent.getGlobalVisibleRect(mAltTmpRect);
2747 }
2748 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2749 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2750 return mTmpRect.contains(x, y);
2751 }
2752 return false;
2753 }
2754
2755 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2756
2757 private void onDropToDelete() {
2758 final View dragView = mDragView;
2759
2760 final float toScale = 0f;
2761 final float toAlpha = 0f;
2762
2763 // Create and start the complex animation
2764 ArrayList<Animator> animations = new ArrayList<Animator>();
2765 AnimatorSet motionAnim = new AnimatorSet();
2766 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2767 motionAnim.playTogether(
2768 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2769 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2770 animations.add(motionAnim);
2771
2772 AnimatorSet alphaAnim = new AnimatorSet();
2773 alphaAnim.setInterpolator(new LinearInterpolator());
2774 alphaAnim.playTogether(
2775 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2776 animations.add(alphaAnim);
2777
2778 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2779
2780 AnimatorSet anim = new AnimatorSet();
2781 anim.playTogether(animations);
2782 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2783 anim.addListener(new AnimatorListenerAdapter() {
2784 public void onAnimationEnd(Animator animation) {
2785 onAnimationEndRunnable.run();
2786 }
2787 });
2788 anim.start();
2789
2790 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002791 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002792
2793 /* Accessibility */
2794 @Override
2795 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2796 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002797 info.setScrollable(getPageCount() > 1);
2798 if (getCurrentPage() < getPageCount() - 1) {
2799 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2800 }
2801 if (getCurrentPage() > 0) {
2802 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2803 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002804 }
2805
2806 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002807 public void sendAccessibilityEvent(int eventType) {
2808 // Don't let the view send real scroll events.
2809 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2810 super.sendAccessibilityEvent(eventType);
2811 }
2812 }
2813
2814 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002815 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2816 super.onInitializeAccessibilityEvent(event);
2817 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002818 }
2819
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002820 @Override
2821 public boolean performAccessibilityAction(int action, Bundle arguments) {
2822 if (super.performAccessibilityAction(action, arguments)) {
2823 return true;
2824 }
2825 switch (action) {
2826 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2827 if (getCurrentPage() < getPageCount() - 1) {
2828 scrollRight();
2829 return true;
2830 }
2831 } break;
2832 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2833 if (getCurrentPage() > 0) {
2834 scrollLeft();
2835 return true;
2836 }
2837 } break;
2838 }
2839 return false;
2840 }
2841
Adam Cohen0ffac432013-07-10 11:19:26 -07002842 protected String getCurrentPageDescription() {
2843 return String.format(getContext().getString(R.string.default_scroll_format),
2844 getNextPage() + 1, getChildCount());
2845 }
2846
Winson Chungd11265e2011-08-30 13:37:23 -07002847 @Override
2848 public boolean onHoverEvent(android.view.MotionEvent event) {
2849 return true;
2850 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002851}