blob: 1037d98095f63ea501d0c81a72e28de797315751 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070024import android.animation.ValueAnimator;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
30import android.graphics.PointF;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
35import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070036import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080038import android.view.InputDevice;
39import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070046import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070047import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohen7d30a372013-07-01 17:03:59 -070049import android.view.animation.AnimationUtils;
50import android.view.animation.DecelerateInterpolator;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Adam Cohen7d30a372013-07-01 17:03:59 -070052import android.view.animation.LinearInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chungc58497e2013-09-03 17:48:37 -070056interface Page {
57 public int getPageChildCount();
58 public View getChildOnPageAt(int i);
59 public void removeAllViewsOnPage();
60 public void removeViewOnPageAt(int i);
61 public int indexOfChildOnPage(View v);
62}
63
Winson Chung321e9ee2010-08-09 13:37:56 -070064/**
65 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070066 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070067 */
Michael Jurka8b805b12012-04-18 14:23:14 -070068public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070069 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070070 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070071 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070072
Winson Chung86f77532010-08-24 11:08:22 -070073 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070074 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070075
Adam Cohen7d30a372013-07-01 17:03:59 -070076 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070077 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Adam Cohenb5ba0972011-09-07 18:02:31 -070080 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070081 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080082
Adam Cohenb64cb5a2011-02-15 13:53:42 -080083 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080084 // The page is moved more than halfway, automatically move to the next page on touch up.
85 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080086
Adam Cohen265b9a62011-12-07 14:37:18 -080087 // The following constants need to be scaled based on density. The scaled versions will be
88 // assigned to the corresponding member variables below.
89 private static final int FLING_THRESHOLD_VELOCITY = 500;
90 private static final int MIN_SNAP_VELOCITY = 1500;
91 private static final int MIN_FLING_VELOCITY = 250;
92
Adam Cohen7d30a372013-07-01 17:03:59 -070093 // We are disabling touch interaction of the widget region for factory ROM.
94 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070095 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070096 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070097
Adam Cohen21cd0022013-10-09 18:57:02 -070098 public static final int INVALID_RESTORE_PAGE = -1001;
99
Adam Cohenf358a4b2013-07-23 16:47:31 -0700100 private boolean mFreeScroll = false;
101 private int mFreeScrollMinScrollX = -1;
102 private int mFreeScrollMaxScrollX = -1;
103
Winson Chung8aad6102012-05-11 16:27:49 -0700104 static final int AUTOMATIC_PAGE_SPACING = -1;
105
Adam Cohen265b9a62011-12-07 14:37:18 -0800106 protected int mFlingThresholdVelocity;
107 protected int mMinFlingVelocity;
108 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700109
Adam Cohenb5ba0972011-09-07 18:02:31 -0700110 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700111 protected float mSmoothingTime;
112 protected float mTouchX;
113
114 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700115 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700116
117 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700118 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700119 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700120
Michael Jurka0142d492010-08-25 17:46:15 -0700121 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800122 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800123 protected LauncherScroller mScroller;
124 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700126 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Adam Cohen7d30a372013-07-01 17:03:59 -0700128 private float mParentDownMotionX;
129 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 private float mDownMotionY;
132 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700133 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800135 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800137 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700138 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700139
Adam Cohendbdff6b2013-09-18 19:09:15 -0700140 private boolean mCancelTap;
141
Adam Cohenedb40762013-07-18 16:45:45 -0700142 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka0142d492010-08-25 17:46:15 -0700144 protected final static int TOUCH_STATE_REST = 0;
145 protected final static int TOUCH_STATE_SCROLLING = 1;
146 protected final static int TOUCH_STATE_PREV_PAGE = 2;
147 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700148 protected final static int TOUCH_STATE_REORDERING = 4;
149
Adam Cohene45440e2010-10-14 18:33:38 -0700150 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700153 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800154
Winson Chung321e9ee2010-08-09 13:37:56 -0700155
Michael Jurka0142d492010-08-25 17:46:15 -0700156 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Michael Jurka7426c422010-11-11 15:23:47 -0800158 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 private int mPagingTouchSlop;
160 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700161 protected int mPageLayoutPaddingTop;
162 protected int mPageLayoutPaddingBottom;
163 protected int mPageLayoutPaddingLeft;
164 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700165 protected int mPageLayoutWidthGap;
166 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700167 protected int mCellCountX = 0;
168 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700169 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800170 protected boolean mAllowOverScroll = true;
171 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800172 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700173 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700174
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800176 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
177 // the screens from continuing to translate beyond the normal bounds.
178 protected int mOverScrollX;
179
Michael Jurka5f1c5092010-09-03 14:15:02 -0700180 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700181
Michael Jurka5f1c5092010-09-03 14:15:02 -0700182 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Winson Chung86f77532010-08-24 11:08:22 -0700184 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700185
Michael Jurkae326f182011-11-21 14:05:46 -0800186 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700187
Michael Jurka0142d492010-08-25 17:46:15 -0700188 // If true, syncPages and syncPageItems will be called to refresh pages
189 protected boolean mContentIsRefreshable = true;
190
191 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700192 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700193
194 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
195 // to switch to a new page
196 protected boolean mUsePagingTouchSlop = true;
197
Michael Jurka8b805b12012-04-18 14:23:14 -0700198 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700199 // (SmoothPagedView does this)
200 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700201 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700202
Patrick Dubroy1262e362010-10-06 15:49:50 -0700203 protected boolean mIsPageMoving = false;
204
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 // All syncs and layout passes are deferred until data is ready.
206 protected boolean mIsDataReady = false;
207
Adam Cohen7d30a372013-07-01 17:03:59 -0700208 protected boolean mAllowLongPress = true;
209
Winson Chungd2be3812013-07-16 11:11:32 -0700210 // Page Indicator
211 private int mPageIndicatorViewId;
212 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700213 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700214
Adam Cohen7d30a372013-07-01 17:03:59 -0700215 // The viewport whether the pages are to be contained (the actual view may be larger than the
216 // viewport)
217 private Rect mViewport = new Rect();
218
219 // Reordering
220 // We use the min scale to determine how much to expand the actually PagedView measured
221 // dimensions such that when we are zoomed out, the view is not clipped
222 private int REORDERING_DROP_REPOSITION_DURATION = 200;
223 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
224 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700225 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700226 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700227 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700228 protected View mDragView;
229 protected AnimatorSet mZoomInOutAnim;
230 private Runnable mSidePageHoverRunnable;
231 private int mSidePageHoverIndex = -1;
232 // This variable's scope is only for the duration of startReordering() and endReordering()
233 private boolean mReorderingStarted = false;
234 // This variable's scope is for the duration of startReordering() and after the zoomIn()
235 // animation after endReordering()
236 private boolean mIsReordering;
237 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
238 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
239 private int mPostReorderingPreZoomInRemainingAnimationCount;
240 private Runnable mPostReorderingPreZoomInRunnable;
241
Adam Cohen7d30a372013-07-01 17:03:59 -0700242 // Convenience/caching
243 private Matrix mTmpInvMatrix = new Matrix();
244 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700245 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700246 private Rect mTmpRect = new Rect();
247 private Rect mAltTmpRect = new Rect();
248
249 // Fling to delete
250 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
251 private float FLING_TO_DELETE_FRICTION = 0.035f;
252 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
253 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
254 protected int mFlingToDeleteThresholdVelocity = -1400;
255 // Drag to delete
256 private boolean mDeferringForDelete = false;
257 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
258 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
259
260 // Drop to delete
261 private View mDeleteDropTarget;
262
Adam Cohen7d30a372013-07-01 17:03:59 -0700263 // Bouncer
264 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700265
John Spurlock77e1f472013-09-11 10:09:51 -0400266 protected final Rect mInsets = new Rect();
267
Winson Chung86f77532010-08-24 11:08:22 -0700268 public interface PageSwitchListener {
269 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 }
271
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 public PagedView(Context context) {
273 this(context, null);
274 }
275
276 public PagedView(Context context, AttributeSet attrs) {
277 this(context, attrs, 0);
278 }
279
280 public PagedView(Context context, AttributeSet attrs, int defStyle) {
281 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700282
Adam Cohen9c4949e2010-10-05 12:27:22 -0700283 TypedArray a = context.obtainStyledAttributes(attrs,
284 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700285
Adam Cohen9c4949e2010-10-05 12:27:22 -0700286 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800287 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700288 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800289 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700290 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800291 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700292 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800293 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700294 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700295 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700296 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700297 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700298 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700299 a.recycle();
300
Winson Chung321e9ee2010-08-09 13:37:56 -0700301 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700302 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
304
305 /**
306 * Initializes various states for this workspace.
307 */
Michael Jurka0142d492010-08-25 17:46:15 -0700308 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700309 mDirtyPageContent = new ArrayList<Boolean>();
310 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800311 mScroller = new LauncherScroller(getContext());
312 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700313 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700314 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700315
316 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700317 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
319 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700320 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800321
Adam Cohen7d30a372013-07-01 17:03:59 -0700322 // Scale the fling-to-delete threshold by the density
323 mFlingToDeleteThresholdVelocity =
324 (int) (mFlingToDeleteThresholdVelocity * mDensity);
325
Adam Cohen265b9a62011-12-07 14:37:18 -0800326 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
327 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
328 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 }
331
Adam Cohenf9618852013-11-08 06:45:03 -0800332 protected void setDefaultInterpolator(Interpolator interpolator) {
333 mDefaultInterpolator = interpolator;
334 mScroller.setInterpolator(mDefaultInterpolator);
335 }
336
Winson Chungd2be3812013-07-16 11:11:32 -0700337 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700338 super.onAttachedToWindow();
339
Winson Chungd2be3812013-07-16 11:11:32 -0700340 // Hook up the page indicator
341 ViewGroup parent = (ViewGroup) getParent();
342 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
343 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700344 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700345
Winson Chung7819a562013-09-19 15:55:45 -0700346 ArrayList<PageIndicator.PageMarkerResources> markers =
347 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700348 for (int i = 0; i < getChildCount(); ++i) {
349 markers.add(getPageIndicatorMarker(i));
350 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700351
Winson Chungc58497e2013-09-03 17:48:37 -0700352 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700353
354 OnClickListener listener = getPageIndicatorClickListener();
355 if (listener != null) {
356 mPageIndicator.setOnClickListener(listener);
357 }
Adam Cohen53805212013-10-01 10:39:23 -0700358 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700359 }
360 }
361
Adam Cohen53805212013-10-01 10:39:23 -0700362 protected String getPageIndicatorDescription() {
363 return getCurrentPageDescription();
364 }
365
366 protected OnClickListener getPageIndicatorClickListener() {
367 return null;
368 }
369
Winson Chungd2be3812013-07-16 11:11:32 -0700370 protected void onDetachedFromWindow() {
371 // Unhook the page indicator
372 mPageIndicator = null;
373 }
374
Adam Cohen7d30a372013-07-01 17:03:59 -0700375 void setDeleteDropTarget(View v) {
376 mDeleteDropTarget = v;
377 }
378
379 // Convenience methods to map points from self to parent and vice versa
380 float[] mapPointFromViewToParent(View v, float x, float y) {
381 mTmpPoint[0] = x;
382 mTmpPoint[1] = y;
383 v.getMatrix().mapPoints(mTmpPoint);
384 mTmpPoint[0] += v.getLeft();
385 mTmpPoint[1] += v.getTop();
386 return mTmpPoint;
387 }
388 float[] mapPointFromParentToView(View v, float x, float y) {
389 mTmpPoint[0] = x - v.getLeft();
390 mTmpPoint[1] = y - v.getTop();
391 v.getMatrix().invert(mTmpInvMatrix);
392 mTmpInvMatrix.mapPoints(mTmpPoint);
393 return mTmpPoint;
394 }
395
396 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700397 if (mDragView != null) {
398 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
399 (mDragViewBaselineLeft - mDragView.getLeft());
400 float y = mLastMotionY - mDownMotionY;
401 mDragView.setTranslationX(x);
402 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700403
Adam Cohenf358a4b2013-07-23 16:47:31 -0700404 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
405 + x + ", " + y);
406 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700407 }
408
409 public void setMinScale(float f) {
410 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700411 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700412 requestLayout();
413 }
414
415 @Override
416 public void setScaleX(float scaleX) {
417 super.setScaleX(scaleX);
418 if (isReordering(true)) {
419 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
420 mLastMotionX = p[0];
421 mLastMotionY = p[1];
422 updateDragViewTranslationDuringDrag();
423 }
424 }
425
426 // Convenience methods to get the actual width/height of the PagedView (since it is measured
427 // to be larger to account for the minimum possible scale)
428 int getViewportWidth() {
429 return mViewport.width();
430 }
431 int getViewportHeight() {
432 return mViewport.height();
433 }
434
435 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
436 // PagedView both horizontally and vertically
437 int getViewportOffsetX() {
438 return (getMeasuredWidth() - getViewportWidth()) / 2;
439 }
440
441 int getViewportOffsetY() {
442 return (getMeasuredHeight() - getViewportHeight()) / 2;
443 }
444
Adam Cohenedb40762013-07-18 16:45:45 -0700445 PageIndicator getPageIndicator() {
446 return mPageIndicator;
447 }
Winson Chung7819a562013-09-19 15:55:45 -0700448 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
449 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700450 }
Adam Cohenedb40762013-07-18 16:45:45 -0700451
Adam Cohen674531f2013-12-13 15:07:14 -0800452 /**
453 * Add a page change listener which will be called when a page is _finished_ listening.
454 *
455 */
Winson Chung86f77532010-08-24 11:08:22 -0700456 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
457 mPageSwitchListener = pageSwitchListener;
458 if (mPageSwitchListener != null) {
459 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 }
461 }
462
463 /**
Winson Chung52aee602013-01-30 12:01:02 -0800464 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
465 */
466 public boolean isLayoutRtl() {
467 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
468 }
469
470 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700471 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
472 * out pages.
473 */
474 protected void setDataIsReady() {
475 mIsDataReady = true;
476 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700477
Winson Chungf0ea4d32011-06-06 14:27:16 -0700478 protected boolean isDataReady() {
479 return mIsDataReady;
480 }
481
482 /**
Winson Chung86f77532010-08-24 11:08:22 -0700483 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 *
Winson Chung86f77532010-08-24 11:08:22 -0700485 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 */
Winson Chung86f77532010-08-24 11:08:22 -0700487 int getCurrentPage() {
488 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700490
Winson Chung360e63f2012-04-27 13:48:05 -0700491 int getNextPage() {
492 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
493 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700494
Winson Chung86f77532010-08-24 11:08:22 -0700495 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 return getChildCount();
497 }
498
Winson Chung86f77532010-08-24 11:08:22 -0700499 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 return getChildAt(index);
501 }
502
Adam Cohenae4f1552011-10-20 00:15:42 -0700503 protected int indexToPage(int index) {
504 return index;
505 }
506
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800508 * Updates the scroll of the current page immediately to its final scroll position. We use this
509 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
510 * the previous tab page.
511 */
512 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 // If the current page is invalid, just reset the scroll position to zero
514 int newX = 0;
515 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700516 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700517 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800518 scrollTo(newX, 0);
519 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700520 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800521 }
522
523 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700524 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700525 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
526 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700527 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700528 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700529 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800530 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700531 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700532 }
533
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700534 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700535 mScroller.abortAnimation();
536 // We need to clean up the next page here to avoid computeScrollHelper from
537 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700538 if (resetNextPage) {
539 mNextPage = INVALID_PAGE;
540 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700541 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700542
543 private void forceFinishScroller() {
544 mScroller.forceFinished(true);
545 // We need to clean up the next page here to avoid computeScrollHelper from
546 // updating current page on the pass.
547 mNextPage = INVALID_PAGE;
548 }
549
Adam Cohen6f127a62014-06-12 14:54:41 -0700550 private int validateNewPage(int newPage) {
551 int validatedPage = newPage;
552 // When in free scroll mode, we need to clamp to the free scroll page range.
553 if (mFreeScroll) {
554 getFreeScrollPageRange(mTempVisiblePagesRange);
555 validatedPage = Math.max(mTempVisiblePagesRange[0],
556 Math.min(newPage, mTempVisiblePagesRange[1]));
557 }
558 // Ensure that it is clamped by the actual set of children in all cases
559 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
560 return validatedPage;
561 }
562
Chet Haasebc2f0822012-10-26 17:59:53 -0700563 /**
Winson Chung86f77532010-08-24 11:08:22 -0700564 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 */
Winson Chung86f77532010-08-24 11:08:22 -0700566 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800567 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700568 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800569 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800570 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
571 // the default
572 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800573 return;
574 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700575 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700576 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700577 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700578 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800579 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 }
581
Winson Chung8c87cd82013-07-23 16:20:10 -0700582 /**
583 * The restore page will be set in place of the current page at the next (likely first)
584 * layout.
585 */
586 void setRestorePage(int restorePage) {
587 mRestorePage = restorePage;
588 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800589 int getRestorePage() {
590 return mRestorePage;
591 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700592
Adam Cohen674531f2013-12-13 15:07:14 -0800593 /**
594 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
595 * has settled.
596 */
Michael Jurka0142d492010-08-25 17:46:15 -0700597 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700598 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800599 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 }
Winson Chungd2be3812013-07-16 11:11:32 -0700601
Adam Cohen674531f2013-12-13 15:07:14 -0800602 updatePageIndicator();
603 }
604
605 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700606 // Update the page indicator (when we aren't reordering)
607 if (mPageIndicator != null && !isReordering(false)) {
608 mPageIndicator.setActiveMarker(getNextPage());
609 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800611 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700612 if (!mIsPageMoving) {
613 mIsPageMoving = true;
614 onPageBeginMoving();
615 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700616 }
617
Michael Jurkace7e05f2011-02-01 22:02:35 -0800618 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700619 if (mIsPageMoving) {
620 mIsPageMoving = false;
621 onPageEndMoving();
622 }
Michael Jurka0142d492010-08-25 17:46:15 -0700623 }
624
Adam Cohen26976d92011-03-22 15:33:33 -0700625 protected boolean isPageMoving() {
626 return mIsPageMoving;
627 }
628
Michael Jurka0142d492010-08-25 17:46:15 -0700629 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700630 protected void onPageBeginMoving() {
631 }
632
633 // a method that subclasses can override to add behavior
634 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700635 }
636
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 /**
Winson Chung86f77532010-08-24 11:08:22 -0700638 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 *
640 * @param l The listener used to respond to long clicks.
641 */
642 @Override
643 public void setOnLongClickListener(OnLongClickListener l) {
644 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700645 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700647 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 }
Adam Cohen1697b792013-09-17 19:08:21 -0700649 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 }
651
652 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800653 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700654 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800655 }
656
657 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700658 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700659 // In free scroll mode, we clamp the scrollX
660 if (mFreeScroll) {
661 x = Math.min(x, mFreeScrollMaxScrollX);
662 x = Math.max(x, mFreeScrollMinScrollX);
663 }
664
Adam Cohen0ffac432013-07-10 11:19:26 -0700665 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800666 mUnboundedScrollX = x;
667
Adam Cohen0ffac432013-07-10 11:19:26 -0700668 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
669 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
670 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800671 super.scrollTo(0, y);
672 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700673 if (isRtl) {
674 overScroll(x - mMaxScrollX);
675 } else {
676 overScroll(x);
677 }
Adam Cohen68d73932010-11-15 10:50:58 -0800678 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700679 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800680 super.scrollTo(mMaxScrollX, y);
681 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700682 if (isRtl) {
683 overScroll(x);
684 } else {
685 overScroll(x - mMaxScrollX);
686 }
Adam Cohen68d73932010-11-15 10:50:58 -0800687 }
688 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800689 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800690 super.scrollTo(x, y);
691 }
692
Michael Jurka0142d492010-08-25 17:46:15 -0700693 mTouchX = x;
694 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700695
696 // Update the last motion events when scrolling
697 if (isReordering(true)) {
698 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
699 mLastMotionX = p[0];
700 mLastMotionY = p[1];
701 updateDragViewTranslationDuringDrag();
702 }
Michael Jurka0142d492010-08-25 17:46:15 -0700703 }
704
Adam Cohen53805212013-10-01 10:39:23 -0700705 private void sendScrollAccessibilityEvent() {
706 AccessibilityManager am =
707 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
708 if (am.isEnabled()) {
709 AccessibilityEvent ev =
710 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700711 ev.setItemCount(getChildCount());
712 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700713 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700714
Alan Viverette254139a2013-10-08 13:13:46 -0700715 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700716 if (getNextPage() >= mCurrentPage) {
717 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
718 } else {
719 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
720 }
721
722 ev.setAction(action);
723 sendAccessibilityEventUnchecked(ev);
724 }
725 }
726
Michael Jurka0142d492010-08-25 17:46:15 -0700727 // we moved this functionality to a helper function so SmoothPagedView can reuse it
728 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700730 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (getScrollX() != mScroller.getCurrX()
732 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700733 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700734 float scaleX = mFreeScroll ? getScaleX() : 1f;
735 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
736 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700737 }
Michael Jurka0142d492010-08-25 17:46:15 -0700738 invalidate();
739 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700740 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700741 sendScrollAccessibilityEvent();
742
Adam Cohen6f127a62014-06-12 14:54:41 -0700743 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700744 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700745 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700746
Winson Chung9c0565f2013-07-19 13:49:06 -0700747 // Load the associated pages if necessary
748 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
749 loadAssociatedPages(mCurrentPage);
750 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
751 }
752
Adam Cohen73aa9752010-11-24 16:26:58 -0800753 // We don't want to trigger a page end moving unless the page has settled
754 // and the user has stopped scrolling
755 if (mTouchState == TOUCH_STATE_REST) {
756 pageEndMoving();
757 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700758
Adam Cohen7d30a372013-07-01 17:03:59 -0700759 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700760 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700761 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700762 if (am.isEnabled()) {
763 // Notify the user when the page changes
764 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700765 }
Michael Jurka0142d492010-08-25 17:46:15 -0700766 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 }
Michael Jurka0142d492010-08-25 17:46:15 -0700768 return false;
769 }
770
771 @Override
772 public void computeScroll() {
773 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 }
775
Adam Cohen7d30a372013-07-01 17:03:59 -0700776 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
777 return mTopAlignPageWhenShrinkingForBouncer;
778 }
779
Adam Cohen96d30a12013-07-16 18:13:21 -0700780 public static class LayoutParams extends ViewGroup.LayoutParams {
781 public boolean isFullScreenPage = false;
782
783 /**
784 * {@inheritDoc}
785 */
786 public LayoutParams(int width, int height) {
787 super(width, height);
788 }
789
790 public LayoutParams(ViewGroup.LayoutParams source) {
791 super(source);
792 }
793 }
794
795 protected LayoutParams generateDefaultLayoutParams() {
796 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
797 }
798
Adam Cohenedb40762013-07-18 16:45:45 -0700799 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700800 LayoutParams lp = generateDefaultLayoutParams();
801 lp.isFullScreenPage = true;
802 super.addView(page, 0, lp);
803 }
804
Adam Cohen410f3cd2013-09-22 12:09:32 -0700805 public int getNormalChildHeight() {
806 return mNormalChildHeight;
807 }
808
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 @Override
810 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700812 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
813 return;
814 }
815
Adam Cohen7d30a372013-07-01 17:03:59 -0700816 // We measure the dimensions of the PagedView to be larger than the pages so that when we
817 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700818 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
819 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
820 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700821 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800822 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700823 // viewport, we can be at most one and a half screens offset once we scale down
824 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700825 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
826 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700827
Winson Chungc82d2622013-11-06 13:23:29 -0800828 int parentWidthSize = (int) (2f * maxSize);
829 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700830 int scaledWidthSize, scaledHeightSize;
831 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700832 scaledWidthSize = (int) (parentWidthSize / mMinScale);
833 scaledHeightSize = (int) (parentHeightSize / mMinScale);
834 } else {
835 scaledWidthSize = widthSize;
836 scaledHeightSize = heightSize;
837 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700838 mViewport.set(0, 0, widthSize, heightSize);
839
840 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
841 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
842 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844
Winson Chung8aad6102012-05-11 16:27:49 -0700845 // Return early if we aren't given a proper dimension
846 if (widthSize <= 0 || heightSize <= 0) {
847 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
848 return;
849 }
850
Adam Lesinski6b879f02010-11-04 16:15:23 -0700851 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
852 * of the All apps view on XLarge displays to not take up more space then it needs. Width
853 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
854 * each page to have the same width.
855 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700856 final int verticalPadding = getPaddingTop() + getPaddingBottom();
857 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700858
859 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700860 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700861 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700862 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
863 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
864 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
865 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 final int childCount = getChildCount();
867 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700868 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700869 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100870 if (child.getVisibility() != GONE) {
871 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700872
Vladimir Marko2824b072013-10-04 16:42:17 +0100873 int childWidthMode;
874 int childHeightMode;
875 int childWidth;
876 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700877
Vladimir Marko2824b072013-10-04 16:42:17 +0100878 if (!lp.isFullScreenPage) {
879 if (lp.width == LayoutParams.WRAP_CONTENT) {
880 childWidthMode = MeasureSpec.AT_MOST;
881 } else {
882 childWidthMode = MeasureSpec.EXACTLY;
883 }
884
885 if (lp.height == LayoutParams.WRAP_CONTENT) {
886 childHeightMode = MeasureSpec.AT_MOST;
887 } else {
888 childHeightMode = MeasureSpec.EXACTLY;
889 }
890
Adam Cohen3b185e22013-10-29 14:45:58 -0700891 childWidth = getViewportWidth() - horizontalPadding
892 - mInsets.left - mInsets.right;
893 childHeight = getViewportHeight() - verticalPadding
894 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100895 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700896 } else {
897 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700898 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100899
Adam Cohen3b185e22013-10-29 14:45:58 -0700900 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
901 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700902 }
903
Vladimir Marko2824b072013-10-04 16:42:17 +0100904 final int childWidthMeasureSpec =
905 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
906 final int childHeightMeasureSpec =
907 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
908 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700909 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700910 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700911 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800912 }
913
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700915 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700916 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700917 return;
918 }
919
Winson Chung785d2eb2011-04-14 16:08:02 -0700920 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700922
Adam Cohen7d30a372013-07-01 17:03:59 -0700923 int offsetX = getViewportOffsetX();
924 int offsetY = getViewportOffsetY();
925
926 // Update the viewport offsets
927 mViewport.offset(offsetX, offsetY);
928
Adam Cohen0ffac432013-07-10 11:19:26 -0700929 final boolean isRtl = isLayoutRtl();
930
931 final int startIndex = isRtl ? childCount - 1 : 0;
932 final int endIndex = isRtl ? -1 : childCount;
933 final int delta = isRtl ? -1 : 1;
934
Adam Cohen7d30a372013-07-01 17:03:59 -0700935 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700936
Adam Cohen3b185e22013-10-29 14:45:58 -0700937 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000938 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700939
940 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700941 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
942 mPageScrolls = new int[getChildCount()];
943 }
944
Adam Cohen0ffac432013-07-10 11:19:26 -0700945 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700946 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700948 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100949 int childTop;
950 if (lp.isFullScreenPage) {
951 childTop = offsetY;
952 } else {
953 childTop = offsetY + getPaddingTop() + mInsets.top;
954 if (mCenterPagesVertically) {
955 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
956 }
957 }
958
Adam Cohen96d30a12013-07-16 18:13:21 -0700959 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700960 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800961
Winson Chung785d2eb2011-04-14 16:08:02 -0700962 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700963 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800964 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700965
Adam Cohen3b185e22013-10-29 14:45:58 -0700966 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
967 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000968
969 int pageGap = mPageSpacing;
970 int next = i + delta;
971 if (next != endIndex) {
972 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
973 } else {
974 nextLp = null;
975 }
976
977 // Prevent full screen pages from showing in the viewport
978 // when they are not the current page.
979 if (lp.isFullScreenPage) {
980 pageGap = getPaddingLeft();
981 } else if (nextLp != null && nextLp.isFullScreenPage) {
982 pageGap = getPaddingRight();
983 }
984
985 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 }
987 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700988
989 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
990 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800991 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700992 setHorizontalScrollBarEnabled(true);
993 mFirstLayout = false;
994 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700995
996 if (childCount > 0) {
997 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700998 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700999 } else {
1000 mMaxScrollX = 0;
1001 }
1002
1003 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
1004 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001005 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001006 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001007 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001008 } else {
1009 setCurrentPage(getNextPage());
1010 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001011 }
1012 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001013
1014 if (isReordering(true)) {
1015 updateDragViewTranslationDuringDrag();
1016 }
Winson Chunge3193b92010-09-10 11:44:42 -07001017 }
1018
Adam Cohen3b185e22013-10-29 14:45:58 -07001019 public void setPageSpacing(int pageSpacing) {
1020 mPageSpacing = pageSpacing;
1021 requestLayout();
1022 }
1023
Adam Cohenf34bab52010-09-30 14:11:56 -07001024 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001025 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1026
1027 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001028 for (int i = 0; i < getChildCount(); i++) {
1029 View child = getChildAt(i);
1030 if (child != null) {
1031 float scrollProgress = getScrollProgress(screenCenter, child, i);
1032 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001033 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001034 }
1035 }
1036 invalidate();
1037 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001038 }
1039
Winson Chungc58497e2013-09-03 17:48:37 -07001040 protected void enablePagedViewAnimations() {
1041 mAllowPagedViewAnimations = true;
1042
1043 }
1044 protected void disablePagedViewAnimations() {
1045 mAllowPagedViewAnimations = false;
1046 }
1047
Winson Chunge3193b92010-09-10 11:44:42 -07001048 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001049 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001050 // Update the page indicator, we don't update the page indicator as we
1051 // add/remove pages
1052 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001053 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001054 mPageIndicator.addMarker(pageIndex,
1055 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001056 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001057 }
1058
Adam Cohen2591f6a2011-10-25 14:36:40 -07001059 // This ensures that when children are added, they get the correct transforms / alphas
1060 // in accordance with any scroll effects.
1061 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001062 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001063 invalidate();
1064 }
1065
Michael Jurka8b805b12012-04-18 14:23:14 -07001066 @Override
1067 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001068 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001069 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001070 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001071 }
1072
Winson Chungd2be3812013-07-16 11:11:32 -07001073 private void removeMarkerForView(int index) {
1074 // Update the page indicator, we don't update the page indicator as we
1075 // add/remove pages
1076 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001077 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001078 }
1079 }
1080
1081 @Override
1082 public void removeView(View v) {
1083 // XXX: We should find a better way to hook into this before the view
1084 // gets removed form its parent...
1085 removeMarkerForView(indexOfChild(v));
1086 super.removeView(v);
1087 }
1088 @Override
1089 public void removeViewInLayout(View v) {
1090 // XXX: We should find a better way to hook into this before the view
1091 // gets removed form its parent...
1092 removeMarkerForView(indexOfChild(v));
1093 super.removeViewInLayout(v);
1094 }
1095 @Override
1096 public void removeViewAt(int index) {
1097 // XXX: We should find a better way to hook into this before the view
1098 // gets removed form its parent...
1099 removeViewAt(index);
1100 super.removeViewAt(index);
1101 }
1102 @Override
1103 public void removeAllViewsInLayout() {
1104 // Update the page indicator, we don't update the page indicator as we
1105 // add/remove pages
1106 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001107 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001108 }
1109
1110 super.removeAllViewsInLayout();
1111 }
1112
Adam Cohen73894962011-10-31 13:17:17 -07001113 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001114 if (index < 0 || index > getChildCount() - 1) return 0;
1115
Adam Cohenf698c6e2013-07-17 18:44:25 -07001116 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001117
Adam Cohenf698c6e2013-07-17 18:44:25 -07001118 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001119 }
1120
Adam Cohen6f127a62014-06-12 14:54:41 -07001121 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001122 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001123 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001124 }
1125
Michael Jurkadde558b2011-11-09 22:09:06 -08001126 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001127 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001128 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001129
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001130 range[0] = -1;
1131 range[1] = -1;
1132
Michael Jurkac4fb9172010-09-02 17:19:20 -07001133 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001134 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001135 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001136
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001137 int count = getChildCount();
1138 for (int i = 0; i < count; i++) {
1139 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001140
Winson Chungc9ca2982013-07-19 12:07:38 -07001141 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001142 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001143 if (mTmpIntPoint[0] > viewportWidth) {
1144 if (range[0] == -1) {
1145 continue;
1146 } else {
1147 break;
1148 }
1149 }
1150
Adam Cohen6a678da2013-09-24 10:58:01 -07001151 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001152 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1153 if (mTmpIntPoint[0] < 0) {
1154 if (range[0] == -1) {
1155 continue;
1156 } else {
1157 break;
1158 }
1159 }
1160 curScreen = i;
1161 if (range[0] < 0) {
1162 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001163 }
1164 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001165
1166 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001167 } else {
1168 range[0] = -1;
1169 range[1] = -1;
1170 }
1171 }
1172
Michael Jurka920d7f42012-05-14 16:29:55 -07001173 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001174 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001175 }
1176
Michael Jurkadde558b2011-11-09 22:09:06 -08001177 @Override
1178 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001179 // Find out which screens are visible; as an optimization we only call draw on them
1180 final int pageCount = getChildCount();
1181 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001182 int halfScreenSize = getViewportWidth() / 2;
1183 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1184 // Otherwise it is equal to the scaled overscroll position.
1185 int screenCenter = mOverScrollX + halfScreenSize;
1186
1187 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1188 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1189 // set it for the next frame
1190 mForceScreenScrolled = false;
1191 screenScrolled(screenCenter);
1192 mLastScreenCenter = screenCenter;
1193 }
1194
Michael Jurkadde558b2011-11-09 22:09:06 -08001195 getVisiblePages(mTempVisiblePagesRange);
1196 final int leftScreen = mTempVisiblePagesRange[0];
1197 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001198 if (leftScreen != -1 && rightScreen != -1) {
1199 final long drawingTime = getDrawingTime();
1200 // Clip to the bounds
1201 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001202 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1203 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001204
Adam Cohen7d30a372013-07-01 17:03:59 -07001205 // Draw all the children, leaving the drag view for last
1206 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001207 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001208 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001209 if (mForceDrawAllChildrenNextFrame ||
1210 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001211 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001212 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001213 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001214 // Draw the drag view on top (if there is one)
1215 if (mDragView != null) {
1216 drawChild(canvas, mDragView, drawingTime);
1217 }
1218
Michael Jurka5e368ff2012-05-14 23:13:15 -07001219 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001220 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001221 }
Michael Jurka0142d492010-08-25 17:46:15 -07001222 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 }
1224
1225 @Override
1226 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001227 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (page != mCurrentPage || !mScroller.isFinished()) {
1229 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 return true;
1231 }
1232 return false;
1233 }
1234
1235 @Override
1236 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001237 int focusablePage;
1238 if (mNextPage != INVALID_PAGE) {
1239 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001241 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 }
Winson Chung86f77532010-08-24 11:08:22 -07001243 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001245 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 }
1247 return false;
1248 }
1249
1250 @Override
1251 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001252 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001254 if (getCurrentPage() > 0) {
1255 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 return true;
1257 }
1258 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001259 if (getCurrentPage() < getPageCount() - 1) {
1260 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 return true;
1262 }
1263 }
1264 return super.dispatchUnhandledMove(focused, direction);
1265 }
1266
1267 @Override
1268 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001269 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001270 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001271 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001274 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001275 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 }
1277 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001278 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001279 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 }
1281 }
1282 }
1283
1284 /**
1285 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001286 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001287 *
Winson Chung86f77532010-08-24 11:08:22 -07001288 * This happens when live folders requery, and if they're off page, they
1289 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 */
1291 @Override
1292 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001293 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 View v = focused;
1295 while (true) {
1296 if (v == current) {
1297 super.focusableViewAvailable(focused);
1298 return;
1299 }
1300 if (v == this) {
1301 return;
1302 }
1303 ViewParent parent = v.getParent();
1304 if (parent instanceof View) {
1305 v = (View)v.getParent();
1306 } else {
1307 return;
1308 }
1309 }
1310 }
1311
1312 /**
1313 * {@inheritDoc}
1314 */
1315 @Override
1316 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1317 if (disallowIntercept) {
1318 // We need to make sure to cancel our long press if
1319 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001320 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001321 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 }
1323 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1324 }
1325
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001326 /**
1327 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1328 */
1329 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001330 if (isLayoutRtl()) {
1331 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001332 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001333 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001334 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001335 }
1336
1337 /**
1338 * Return true if a tap at (x, y) should trigger a flip to the next page.
1339 */
1340 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001341 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001342 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001343 }
1344 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001345 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001346 }
Winson Chung52aee602013-01-30 12:01:02 -08001347
Adam Cohen7d30a372013-07-01 17:03:59 -07001348 /** Returns whether x and y originated within the buffered viewport */
1349 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1350 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1351 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1352 return mTmpRect.contains(x, y);
1353 }
1354
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 @Override
1356 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001357 if (DISABLE_TOUCH_INTERACTION) {
1358 return false;
1359 }
1360
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 /*
1362 * This method JUST determines whether we want to intercept the motion.
1363 * If we return true, onTouchEvent will be called and we do the actual
1364 * scrolling there.
1365 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001366 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001367
Winson Chung45e1d6e2010-11-09 17:19:49 -08001368 // Skip touch handling if there are no pages to swipe
1369 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1370
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 /*
1372 * Shortcut the most recurring case: the user is in the dragging
1373 * state and he is moving his finger. We want to intercept this
1374 * motion.
1375 */
1376 final int action = ev.getAction();
1377 if ((action == MotionEvent.ACTION_MOVE) &&
1378 (mTouchState == TOUCH_STATE_SCROLLING)) {
1379 return true;
1380 }
1381
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 switch (action & MotionEvent.ACTION_MASK) {
1383 case MotionEvent.ACTION_MOVE: {
1384 /*
1385 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1386 * whether the user has moved far enough from his original down touch.
1387 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001388 if (mActivePointerId != INVALID_POINTER) {
1389 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001390 }
1391 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1392 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1393 // i.e. fall through to the next case (don't break)
1394 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1395 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001396 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 }
1398
1399 case MotionEvent.ACTION_DOWN: {
1400 final float x = ev.getX();
1401 final float y = ev.getY();
1402 // Remember location of down touch
1403 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001404 mDownMotionY = y;
1405 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001406 mLastMotionX = x;
1407 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001408 float[] p = mapPointFromViewToParent(this, x, y);
1409 mParentDownMotionX = p[0];
1410 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001411 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001412 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 /*
1416 * If being flinged and user touches the screen, initiate drag;
1417 * otherwise don't. mScroller.isFinished should be false when
1418 * being flinged.
1419 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001420 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001421 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001422
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001423 if (finishedScrolling) {
1424 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001425 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001426 setCurrentPage(getNextPage());
1427 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001428 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001429 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001430 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1431 mTouchState = TOUCH_STATE_SCROLLING;
1432 } else {
1433 mTouchState = TOUCH_STATE_REST;
1434 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001435 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001436
Winson Chung86f77532010-08-24 11:08:22 -07001437 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001439 if (!DISABLE_TOUCH_SIDE_PAGES) {
1440 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1441 if (getChildCount() > 0) {
1442 if (hitsPreviousPage(x, y)) {
1443 mTouchState = TOUCH_STATE_PREV_PAGE;
1444 } else if (hitsNextPage(x, y)) {
1445 mTouchState = TOUCH_STATE_NEXT_PAGE;
1446 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001447 }
1448 }
1449 }
1450 break;
1451 }
1452
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001454 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001455 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 break;
1457
1458 case MotionEvent.ACTION_POINTER_UP:
1459 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001460 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 break;
1462 }
1463
1464 /*
1465 * The only time we want to intercept motion events is if we are in the
1466 * drag mode.
1467 */
1468 return mTouchState != TOUCH_STATE_REST;
1469 }
1470
Adam Cohenf8d28232011-02-01 21:47:00 -08001471 protected void determineScrollingStart(MotionEvent ev) {
1472 determineScrollingStart(ev, 1.0f);
1473 }
1474
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 /*
1476 * Determines if we should change the touch state to start scrolling after the
1477 * user moves their touch point too far.
1478 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001479 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001480 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001482 if (pointerIndex == -1) return;
1483
1484 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 final float x = ev.getX(pointerIndex);
1486 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001487 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1488
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 final int xDiff = (int) Math.abs(x - mLastMotionX);
1490 final int yDiff = (int) Math.abs(y - mLastMotionY);
1491
Adam Cohenf8d28232011-02-01 21:47:00 -08001492 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 boolean xPaged = xDiff > mPagingTouchSlop;
1494 boolean xMoved = xDiff > touchSlop;
1495 boolean yMoved = yDiff > touchSlop;
1496
Adam Cohenf8d28232011-02-01 21:47:00 -08001497 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001498 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 // Scroll if the user moved far enough along the X axis
1500 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001501 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001503 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001504 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001505 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1506 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001508 }
1509 }
1510
Adam Cohen7d30a372013-07-01 17:03:59 -07001511 protected float getMaxScrollProgress() {
1512 return 1.0f;
1513 }
1514
Adam Cohenf8d28232011-02-01 21:47:00 -08001515 protected void cancelCurrentPageLongPress() {
1516 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001517 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001518 // Try canceling the long press. It could also have been scheduled
1519 // by a distant descendant, so use the mAllowLongPress flag to block
1520 // everything
1521 final View currentPage = getPageAt(mCurrentPage);
1522 if (currentPage != null) {
1523 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 }
1525 }
1526 }
1527
Adam Cohen7d30a372013-07-01 17:03:59 -07001528 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1529 final int halfScreenSize = getViewportWidth() / 2;
1530
1531 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1532 screenCenter = Math.max(halfScreenSize, screenCenter);
1533
1534 return getScrollProgress(screenCenter, v, page);
1535 }
1536
Adam Cohenb5ba0972011-09-07 18:02:31 -07001537 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001538 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001539
Adam Cohenedb40762013-07-18 16:45:45 -07001540 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001541 int count = getChildCount();
1542
1543 final int totalDistance;
1544
1545 int adjacentPage = page + 1;
1546 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1547 adjacentPage = page - 1;
1548 }
1549
1550 if (adjacentPage < 0 || adjacentPage > count - 1) {
1551 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1552 } else {
1553 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1554 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001555
1556 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001557 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1558 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001559 return scrollProgress;
1560 }
1561
Adam Cohenedb40762013-07-18 16:45:45 -07001562 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001563 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001564 return 0;
1565 } else {
1566 return mPageScrolls[index];
1567 }
1568 }
1569
Adam Cohen564a2e72013-10-09 14:47:32 -07001570 // While layout transitions are occurring, a child's position may stray from its baseline
1571 // position. This method returns the magnitude of this stray at any given time.
1572 public int getLayoutTransitionOffsetForPage(int index) {
1573 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1574 return 0;
1575 } else {
1576 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001577
1578 int scrollOffset = 0;
1579 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1580 if (!lp.isFullScreenPage) {
1581 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1582 }
1583
Adam Cohen564a2e72013-10-09 14:47:32 -07001584 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1585 return (int) (child.getX() - baselineX);
1586 }
1587 }
1588
Adam Cohene0f66b52010-11-23 15:06:07 -08001589 // This curve determines how the effect of scrolling over the limits of the page dimishes
1590 // as the user pulls further and further from the bounds
1591 private float overScrollInfluenceCurve(float f) {
1592 f -= 1.0f;
1593 return f * f * f + 1.0f;
1594 }
1595
Adam Cohenb5ba0972011-09-07 18:02:31 -07001596 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001597 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001598
1599 // We want to reach the max over scroll effect when the user has
1600 // over scrolled half the size of the screen
1601 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1602
1603 if (f == 0) return;
1604
1605 // Clamp this factor, f, to -1 < f < 1
1606 if (Math.abs(f) >= 1) {
1607 f /= Math.abs(f);
1608 }
1609
1610 int overScrollAmount = (int) Math.round(f * screenSize);
1611 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001612 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001613 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001614 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001615 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001616 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001617 }
1618 invalidate();
1619 }
1620
1621 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001622 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001623
1624 float f = (amount / screenSize);
1625
1626 if (f == 0) return;
1627 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1628
Adam Cohen7bfc9792011-01-28 13:52:37 -08001629 // Clamp this factor, f, to -1 < f < 1
1630 if (Math.abs(f) >= 1) {
1631 f /= Math.abs(f);
1632 }
1633
Adam Cohene0f66b52010-11-23 15:06:07 -08001634 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001635 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001636 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001637 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001638 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001639 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001640 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001641 }
1642 invalidate();
1643 }
1644
Adam Cohenb5ba0972011-09-07 18:02:31 -07001645 protected void overScroll(float amount) {
1646 dampedOverScroll(amount);
1647 }
1648
Michael Jurkac5b262c2011-01-12 20:24:50 -08001649 protected float maxOverScroll() {
1650 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001651 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001652 float f = 1.0f;
1653 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1654 return OVERSCROLL_DAMP_FACTOR * f;
1655 }
1656
Adam Cohenf358a4b2013-07-23 16:47:31 -07001657 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001658 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001659 }
1660
Adam Cohenf9618852013-11-08 06:45:03 -08001661 protected void disableFreeScroll() {
1662 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001663 }
1664
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001665 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001666 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001667 if (isLayoutRtl()) {
1668 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1669 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1670 } else {
1671 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1672 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1673 }
1674 }
1675
Adam Cohenf9618852013-11-08 06:45:03 -08001676 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001677 mFreeScroll = freeScroll;
1678
Adam Cohenf9618852013-11-08 06:45:03 -08001679 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001680 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001681 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001682 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1683 setCurrentPage(mTempVisiblePagesRange[0]);
1684 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1685 setCurrentPage(mTempVisiblePagesRange[1]);
1686 }
1687 }
1688
1689 setEnableOverscroll(!freeScroll);
1690 }
1691
1692 private void setEnableOverscroll(boolean enable) {
1693 mAllowOverScroll = enable;
1694 }
1695
1696 int getNearestHoverOverPageIndex() {
1697 if (mDragView != null) {
1698 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1699 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001700 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001701 int minDistance = Integer.MAX_VALUE;
1702 int minIndex = indexOfChild(mDragView);
1703 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1704 View page = getPageAt(i);
1705 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1706 int d = Math.abs(dragX - pageX);
1707 if (d < minDistance) {
1708 minIndex = i;
1709 minDistance = d;
1710 }
1711 }
1712 return minIndex;
1713 }
1714 return -1;
1715 }
1716
Winson Chung321e9ee2010-08-09 13:37:56 -07001717 @Override
1718 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001719 if (DISABLE_TOUCH_INTERACTION) {
1720 return false;
1721 }
1722
Adam Cohen1697b792013-09-17 19:08:21 -07001723 super.onTouchEvent(ev);
1724
Winson Chung45e1d6e2010-11-09 17:19:49 -08001725 // Skip touch handling if there are no pages to swipe
1726 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1727
Michael Jurkab8f06722010-10-10 15:58:46 -07001728 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001729
1730 final int action = ev.getAction();
1731
1732 switch (action & MotionEvent.ACTION_MASK) {
1733 case MotionEvent.ACTION_DOWN:
1734 /*
1735 * If being flinged and user touches, stop the fling. isFinished
1736 * will be false if being flinged.
1737 */
1738 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001739 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001740 }
1741
1742 // Remember where the motion event started
1743 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001744 mDownMotionY = mLastMotionY = ev.getY();
1745 mDownScrollX = getScrollX();
1746 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1747 mParentDownMotionX = p[0];
1748 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001749 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001750 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001751 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001752
Michael Jurka0142d492010-08-25 17:46:15 -07001753 if (mTouchState == TOUCH_STATE_SCROLLING) {
1754 pageBeginMoving();
1755 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001756 break;
1757
1758 case MotionEvent.ACTION_MOVE:
1759 if (mTouchState == TOUCH_STATE_SCROLLING) {
1760 // Scroll to follow the motion event
1761 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001762
1763 if (pointerIndex == -1) return true;
1764
Winson Chung321e9ee2010-08-09 13:37:56 -07001765 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001766 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001767
Adam Cohenaefd4e12011-02-14 16:39:38 -08001768 mTotalMotionX += Math.abs(deltaX);
1769
Winson Chungc0844aa2011-02-02 15:25:58 -08001770 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1771 // keep the remainder because we are actually testing if we've moved from the last
1772 // scrolled position (which is discrete).
1773 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001774 mTouchX += deltaX;
1775 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1776 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001777 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001778 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001779 } else {
1780 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001782 mLastMotionX = x;
1783 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001784 } else {
1785 awakenScrollBars();
1786 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001787 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1788 // Update the last motion position
1789 mLastMotionX = ev.getX();
1790 mLastMotionY = ev.getY();
1791
1792 // Update the parent down so that our zoom animations take this new movement into
1793 // account
1794 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1795 mParentDownMotionX = pt[0];
1796 mParentDownMotionY = pt[1];
1797 updateDragViewTranslationDuringDrag();
1798
1799 // Find the closest page to the touch point
1800 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001801
1802 // Change the drag view if we are hovering over the drop target
1803 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1804 (int) mParentDownMotionX, (int) mParentDownMotionY);
1805 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1806
Adam Cohen7d30a372013-07-01 17:03:59 -07001807 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1808 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1809 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1810 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1811
Adam Cohenf358a4b2013-07-23 16:47:31 -07001812 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1813 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1814 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001815 mTempVisiblePagesRange[0] = 0;
1816 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001817 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001818 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1819 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1820 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1821 mSidePageHoverIndex = pageUnderPointIndex;
1822 mSidePageHoverRunnable = new Runnable() {
1823 @Override
1824 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001825 // Setup the scroll to the correct page before we swap the views
1826 snapToPage(pageUnderPointIndex);
1827
1828 // For each of the pages between the paged view and the drag view,
1829 // animate them from the previous position to the new position in
1830 // the layout (as a result of the drag view moving in the layout)
1831 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1832 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1833 dragViewIndex + 1 : pageUnderPointIndex;
1834 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1835 dragViewIndex - 1 : pageUnderPointIndex;
1836 for (int i = lowerIndex; i <= upperIndex; ++i) {
1837 View v = getChildAt(i);
1838 // dragViewIndex < pageUnderPointIndex, so after we remove the
1839 // drag view all subsequent views to pageUnderPointIndex will
1840 // shift down.
1841 int oldX = getViewportOffsetX() + getChildOffset(i);
1842 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1843
1844 // Animate the view translation from its old position to its new
1845 // position
1846 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1847 if (anim != null) {
1848 anim.cancel();
1849 }
1850
1851 v.setTranslationX(oldX - newX);
1852 anim = new AnimatorSet();
1853 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1854 anim.playTogether(
1855 ObjectAnimator.ofFloat(v, "translationX", 0f));
1856 anim.start();
1857 v.setTag(anim);
1858 }
1859
1860 removeView(mDragView);
1861 onRemoveView(mDragView, false);
1862 addView(mDragView, pageUnderPointIndex);
1863 onAddView(mDragView, pageUnderPointIndex);
1864 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001865 if (mPageIndicator != null) {
1866 mPageIndicator.setActiveMarker(getNextPage());
1867 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001868 }
1869 };
1870 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1871 }
1872 } else {
1873 removeCallbacks(mSidePageHoverRunnable);
1874 mSidePageHoverIndex = -1;
1875 }
Adam Cohen564976a2010-10-13 18:52:07 -07001876 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001877 determineScrollingStart(ev);
1878 }
1879 break;
1880
1881 case MotionEvent.ACTION_UP:
1882 if (mTouchState == TOUCH_STATE_SCROLLING) {
1883 final int activePointerId = mActivePointerId;
1884 final int pointerIndex = ev.findPointerIndex(activePointerId);
1885 final float x = ev.getX(pointerIndex);
1886 final VelocityTracker velocityTracker = mVelocityTracker;
1887 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1888 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001889 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001890 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001891 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1892 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001893
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001894 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1895
Adam Cohen00481b32011-11-18 12:03:48 -08001896 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001897 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001898
Adam Cohenf358a4b2013-07-23 16:47:31 -07001899 if (!mFreeScroll) {
1900 // In the case that the page is moved far to one direction and then is flung
1901 // in the opposite direction, we use a threshold to determine whether we should
1902 // just return to the starting page, or if we should skip one further.
1903 boolean returnToOriginalPage = false;
1904 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1905 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1906 returnToOriginalPage = true;
1907 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001908
Adam Cohenf358a4b2013-07-23 16:47:31 -07001909 int finalPage;
1910 // We give flings precedence over large moves, which is why we short-circuit our
1911 // test for a large move if a fling has been registered. That is, a large
1912 // move to the left and fling to the right will register as a fling to the right.
1913 final boolean isRtl = isLayoutRtl();
1914 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1915 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1916 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1917 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1918 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1919 snapToPageWithVelocity(finalPage, velocityX);
1920 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1921 (isFling && isVelocityXLeft)) &&
1922 mCurrentPage < getChildCount() - 1) {
1923 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1924 snapToPageWithVelocity(finalPage, velocityX);
1925 } else {
1926 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001927 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001928 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001929 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001930 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001931 }
1932
1933 float scaleX = getScaleX();
1934 int vX = (int) (-velocityX * scaleX);
1935 int initialScrollX = (int) (getScrollX() * scaleX);
1936
Adam Cohenf9618852013-11-08 06:45:03 -08001937 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001938 mScroller.fling(initialScrollX,
1939 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1940 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 }
Adam Cohencae7f572013-11-04 14:42:52 -08001942 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1943 // at this point we have not moved beyond the touch slop
1944 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1945 // we can just page
1946 int nextPage = Math.max(0, mCurrentPage - 1);
1947 if (nextPage != mCurrentPage) {
1948 snapToPage(nextPage);
1949 } else {
1950 snapToDestination();
1951 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001952 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 // at this point we have not moved beyond the touch slop
1954 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1955 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001956 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1957 if (nextPage != mCurrentPage) {
1958 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001959 } else {
1960 snapToDestination();
1961 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001962 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1963 // Update the last motion position
1964 mLastMotionX = ev.getX();
1965 mLastMotionY = ev.getY();
1966
1967 // Update the parent down so that our zoom animations take this new movement into
1968 // account
1969 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1970 mParentDownMotionX = pt[0];
1971 mParentDownMotionY = pt[1];
1972 updateDragViewTranslationDuringDrag();
1973 boolean handledFling = false;
1974 if (!DISABLE_FLING_TO_DELETE) {
1975 // Check the velocity and see if we are flinging-to-delete
1976 PointF flingToDeleteVector = isFlingingToDelete();
1977 if (flingToDeleteVector != null) {
1978 onFlingToDelete(flingToDeleteVector);
1979 handledFling = true;
1980 }
1981 }
1982 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1983 (int) mParentDownMotionY)) {
1984 onDropToDelete();
1985 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001986 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001987 if (!mCancelTap) {
1988 onUnhandledTap(ev);
1989 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001990 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001991
1992 // Remove the callback to wait for the side page hover timeout
1993 removeCallbacks(mSidePageHoverRunnable);
1994 // End any intermediate reordering states
1995 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001996 break;
1997
1998 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001999 if (mTouchState == TOUCH_STATE_SCROLLING) {
2000 snapToDestination();
2001 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002002 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 break;
2004
2005 case MotionEvent.ACTION_POINTER_UP:
2006 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002007 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002008 break;
2009 }
2010
2011 return true;
2012 }
2013
Adam Cohen7d30a372013-07-01 17:03:59 -07002014 public void onFlingToDelete(View v) {}
2015 public void onRemoveView(View v, boolean deletePermanently) {}
2016 public void onRemoveViewAnimationCompleted() {}
2017 public void onAddView(View v, int index) {}
2018
2019 private void resetTouchState() {
2020 releaseVelocityTracker();
2021 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002022 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002023 mTouchState = TOUCH_STATE_REST;
2024 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002025 }
2026
Adam Cohen1697b792013-09-17 19:08:21 -07002027 protected void onUnhandledTap(MotionEvent ev) {
2028 ((Launcher) getContext()).onClick(this);
2029 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002030
Winson Chung185d7162011-02-28 13:47:29 -08002031 @Override
2032 public boolean onGenericMotionEvent(MotionEvent event) {
2033 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2034 switch (event.getAction()) {
2035 case MotionEvent.ACTION_SCROLL: {
2036 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2037 final float vscroll;
2038 final float hscroll;
2039 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2040 vscroll = 0;
2041 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2042 } else {
2043 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2044 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2045 }
2046 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002047 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2048 : (hscroll > 0 || vscroll > 0);
2049 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002050 scrollRight();
2051 } else {
2052 scrollLeft();
2053 }
2054 return true;
2055 }
2056 }
2057 }
2058 }
2059 return super.onGenericMotionEvent(event);
2060 }
2061
Michael Jurkab8f06722010-10-10 15:58:46 -07002062 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2063 if (mVelocityTracker == null) {
2064 mVelocityTracker = VelocityTracker.obtain();
2065 }
2066 mVelocityTracker.addMovement(ev);
2067 }
2068
2069 private void releaseVelocityTracker() {
2070 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002071 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002072 mVelocityTracker.recycle();
2073 mVelocityTracker = null;
2074 }
2075 }
2076
Winson Chung321e9ee2010-08-09 13:37:56 -07002077 private void onSecondaryPointerUp(MotionEvent ev) {
2078 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2079 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2080 final int pointerId = ev.getPointerId(pointerIndex);
2081 if (pointerId == mActivePointerId) {
2082 // This was our active pointer going up. Choose a new
2083 // active pointer and adjust accordingly.
2084 // TODO: Make this decision more intelligent.
2085 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2086 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2087 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002088 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002089 mActivePointerId = ev.getPointerId(newPointerIndex);
2090 if (mVelocityTracker != null) {
2091 mVelocityTracker.clear();
2092 }
2093 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002094 }
2095
Winson Chung321e9ee2010-08-09 13:37:56 -07002096 @Override
2097 public void requestChildFocus(View child, View focused) {
2098 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002099 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002100 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002101 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002102 }
2103 }
2104
Winson Chung1908d072011-02-24 18:09:44 -08002105 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002106 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002107 }
2108
Adam Cohen7d30a372013-07-01 17:03:59 -07002109 int getPageNearestToPoint(float x) {
2110 int index = 0;
2111 for (int i = 0; i < getChildCount(); ++i) {
2112 if (x < getChildAt(i).getRight() - getScrollX()) {
2113 return index;
2114 } else {
2115 index++;
2116 }
2117 }
2118 return Math.min(index, getChildCount() - 1);
2119 }
2120
Adam Cohend19d3ca2010-09-15 14:43:42 -07002121 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002122 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002123 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002124 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002125 final int childCount = getChildCount();
2126 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002127 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002128 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002129 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002130 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002131 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2132 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2133 minDistanceFromScreenCenter = distanceFromScreenCenter;
2134 minDistanceFromScreenCenterIndex = i;
2135 }
2136 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002137 return minDistanceFromScreenCenterIndex;
2138 }
2139
2140 protected void snapToDestination() {
2141 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002142 }
2143
Adam Cohene0f66b52010-11-23 15:06:07 -08002144 private static class ScrollInterpolator implements Interpolator {
2145 public ScrollInterpolator() {
2146 }
2147
2148 public float getInterpolation(float t) {
2149 t -= 1.0f;
2150 return t*t*t*t*t + 1;
2151 }
2152 }
2153
2154 // We want the duration of the page snap animation to be influenced by the distance that
2155 // the screen has to travel, however, we don't want this duration to be effected in a
2156 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2157 // of travel has on the overall snap duration.
2158 float distanceInfluenceForSnapDuration(float f) {
2159 f -= 0.5f; // center the values about 0.
2160 f *= 0.3f * Math.PI / 2.0f;
2161 return (float) Math.sin(f);
2162 }
2163
Michael Jurka0142d492010-08-25 17:46:15 -07002164 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002165 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002166 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002167
Adam Cohenedb40762013-07-18 16:45:45 -07002168 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002169 int delta = newX - mUnboundedScrollX;
2170 int duration = 0;
2171
Adam Cohen265b9a62011-12-07 14:37:18 -08002172 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002173 // If the velocity is low enough, then treat this more as an automatic page advance
2174 // as opposed to an apparent physical response to flinging
2175 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2176 return;
2177 }
2178
2179 // Here we compute a "distance" that will be used in the computation of the overall
2180 // snap duration. This is a function of the actual distance that needs to be traveled;
2181 // we keep this value close to half screen size in order to reduce the variance in snap
2182 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002183 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002184 float distance = halfScreenSize + halfScreenSize *
2185 distanceInfluenceForSnapDuration(distanceRatio);
2186
2187 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002188 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002189
2190 // we want the page's snap velocity to approximately match the velocity at which the
2191 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002192 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2193 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002194
2195 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002196 }
2197
2198 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002199 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002200 }
2201
Adam Cohen7d30a372013-07-01 17:03:59 -07002202 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002203 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002204 }
2205
Michael Jurka0142d492010-08-25 17:46:15 -07002206 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002207 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002208 }
2209
Adam Cohenf9618852013-11-08 06:45:03 -08002210 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2211 snapToPage(whichPage, duration, false, interpolator);
2212 }
2213
2214 protected void snapToPage(int whichPage, int duration, boolean immediate,
2215 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002216 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002217
Adam Cohenedb40762013-07-18 16:45:45 -07002218 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002219 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002220 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002221 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002222 }
2223
2224 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002225 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002226 }
Michael Jurka0142d492010-08-25 17:46:15 -07002227
Adam Cohenf9618852013-11-08 06:45:03 -08002228 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2229 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002230 whichPage = validateNewPage(whichPage);
2231
Adam Cohen7d30a372013-07-01 17:03:59 -07002232 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002233 View focusedChild = getFocusedChild();
2234 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002235 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002236 focusedChild.clearFocus();
2237 }
2238
Adam Cohen53805212013-10-01 10:39:23 -07002239 sendScrollAccessibilityEvent();
2240
Michael Jurka0142d492010-08-25 17:46:15 -07002241 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002242 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002243 if (immediate) {
2244 duration = 0;
2245 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002246 duration = Math.abs(delta);
2247 }
2248
Adam Cohenf358a4b2013-07-23 16:47:31 -07002249 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002250 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002251 }
Adam Cohenf9618852013-11-08 06:45:03 -08002252
2253 if (interpolator != null) {
2254 mScroller.setInterpolator(interpolator);
2255 } else {
2256 mScroller.setInterpolator(mDefaultInterpolator);
2257 }
2258
Adam Cohen68d73932010-11-15 10:50:58 -08002259 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002260
Adam Cohen674531f2013-12-13 15:07:14 -08002261 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002262
2263 // Trigger a compute() to finish switching pages if necessary
2264 if (immediate) {
2265 computeScroll();
2266 }
2267
Winson Chung9c0565f2013-07-19 13:49:06 -07002268 // Defer loading associated pages until the scroll settles
2269 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2270
Adam Cohen7d30a372013-07-01 17:03:59 -07002271 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002272 invalidate();
2273 }
2274
Winson Chung321e9ee2010-08-09 13:37:56 -07002275 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002276 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002277 }
2278
2279 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002280 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002281 }
2282
Winson Chung86f77532010-08-24 11:08:22 -07002283 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002284 int result = -1;
2285 if (v != null) {
2286 ViewParent vp = v.getParent();
2287 int count = getChildCount();
2288 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002289 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002290 return i;
2291 }
2292 }
2293 }
2294 return result;
2295 }
2296
2297 /**
2298 * @return True is long presses are still allowed for the current touch
2299 */
2300 public boolean allowLongPress() {
2301 return mAllowLongPress;
2302 }
2303
Adam Cohendbdff6b2013-09-18 19:09:15 -07002304 @Override
2305 public boolean performLongClick() {
2306 mCancelTap = true;
2307 return super.performLongClick();
2308 }
2309
Michael Jurka0142d492010-08-25 17:46:15 -07002310 /**
2311 * Set true to allow long-press events to be triggered, usually checked by
2312 * {@link Launcher} to accept or block dpad-initiated long-presses.
2313 */
2314 public void setAllowLongPress(boolean allowLongPress) {
2315 mAllowLongPress = allowLongPress;
2316 }
2317
Winson Chung321e9ee2010-08-09 13:37:56 -07002318 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002319 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002320
2321 SavedState(Parcelable superState) {
2322 super(superState);
2323 }
2324
2325 private SavedState(Parcel in) {
2326 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002327 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002328 }
2329
2330 @Override
2331 public void writeToParcel(Parcel out, int flags) {
2332 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002333 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002334 }
2335
2336 public static final Parcelable.Creator<SavedState> CREATOR =
2337 new Parcelable.Creator<SavedState>() {
2338 public SavedState createFromParcel(Parcel in) {
2339 return new SavedState(in);
2340 }
2341
2342 public SavedState[] newArray(int size) {
2343 return new SavedState[size];
2344 }
2345 };
2346 }
2347
Winson Chungf314b0e2011-08-16 11:54:27 -07002348 protected void loadAssociatedPages(int page) {
2349 loadAssociatedPages(page, false);
2350 }
2351 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002352 if (mContentIsRefreshable) {
2353 final int count = getChildCount();
2354 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002355 int lowerPageBound = getAssociatedLowerPageBound(page);
2356 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002357 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2358 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002359 // First, clear any pages that should no longer be loaded
2360 for (int i = 0; i < count; ++i) {
2361 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002362 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002363 if (layout.getPageChildCount() > 0) {
2364 layout.removeAllViewsOnPage();
2365 }
2366 mDirtyPageContent.set(i, true);
2367 }
2368 }
2369 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002370 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002371 if ((i != page) && immediateAndOnly) {
2372 continue;
2373 }
Michael Jurka0142d492010-08-25 17:46:15 -07002374 if (lowerPageBound <= i && i <= upperPageBound) {
2375 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002376 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002377 mDirtyPageContent.set(i, false);
2378 }
Winson Chung86f77532010-08-24 11:08:22 -07002379 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002380 }
2381 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002382 }
2383 }
2384
Winson Chunge3193b92010-09-10 11:44:42 -07002385 protected int getAssociatedLowerPageBound(int page) {
2386 return Math.max(0, page - 1);
2387 }
2388 protected int getAssociatedUpperPageBound(int page) {
2389 final int count = getChildCount();
2390 return Math.min(page + 1, count - 1);
2391 }
2392
Winson Chung86f77532010-08-24 11:08:22 -07002393 /**
2394 * This method is called ONLY to synchronize the number of pages that the paged view has.
2395 * To actually fill the pages with information, implement syncPageItems() below. It is
2396 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2397 * and therefore, individual page items do not need to be updated in this method.
2398 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002399 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002400
2401 /**
2402 * This method is called to synchronize the items that are on a particular page. If views on
2403 * the page can be reused, then they should be updated within this method.
2404 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002405 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002406
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002407 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002408 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002409 }
2410 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002411 invalidatePageData(currentPage, false);
2412 }
2413 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002414 if (!mIsDataReady) {
2415 return;
2416 }
2417
Michael Jurka0142d492010-08-25 17:46:15 -07002418 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002419 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002420 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002421
Michael Jurka0142d492010-08-25 17:46:15 -07002422 // Update all the pages
2423 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002424
Winson Chung5a808352011-06-27 19:08:49 -07002425 // We must force a measure after we've loaded the pages to update the content width and
2426 // to determine the full scroll width
2427 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2428 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2429
2430 // Set a new page as the current page if necessary
2431 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002432 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002433 }
2434
Michael Jurka0142d492010-08-25 17:46:15 -07002435 // Mark each of the pages as dirty
2436 final int count = getChildCount();
2437 mDirtyPageContent.clear();
2438 for (int i = 0; i < count; ++i) {
2439 mDirtyPageContent.add(true);
2440 }
2441
2442 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002443 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002444 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002445 }
Adam Cohen06559042013-09-29 18:30:56 -07002446 if (isPageMoving()) {
2447 // If the page is moving, then snap it to the final position to ensure we don't get
2448 // stuck between pages
2449 snapToDestination();
2450 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002451 }
Winson Chung007c6982011-06-14 13:27:53 -07002452
Adam Cohen7d30a372013-07-01 17:03:59 -07002453 // Animate the drag view back to the original position
2454 void animateDragViewToOriginalPosition() {
2455 if (mDragView != null) {
2456 AnimatorSet anim = new AnimatorSet();
2457 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2458 anim.playTogether(
2459 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002460 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2461 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2462 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002463 anim.addListener(new AnimatorListenerAdapter() {
2464 @Override
2465 public void onAnimationEnd(Animator animation) {
2466 onPostReorderingAnimationCompleted();
2467 }
2468 });
2469 anim.start();
2470 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002471 }
2472
Adam Cohen7d30a372013-07-01 17:03:59 -07002473 protected void onStartReordering() {
2474 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2475 mTouchState = TOUCH_STATE_REORDERING;
2476 mIsReordering = true;
2477
Adam Cohen7d30a372013-07-01 17:03:59 -07002478 // We must invalidate to trigger a redraw to update the layers such that the drag view
2479 // is always drawn on top
2480 invalidate();
2481 }
2482
2483 private void onPostReorderingAnimationCompleted() {
2484 // Trigger the callback when reordering has settled
2485 --mPostReorderingPreZoomInRemainingAnimationCount;
2486 if (mPostReorderingPreZoomInRunnable != null &&
2487 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2488 mPostReorderingPreZoomInRunnable.run();
2489 mPostReorderingPreZoomInRunnable = null;
2490 }
2491 }
2492
2493 protected void onEndReordering() {
2494 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002495 }
2496
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002498 int dragViewIndex = indexOfChild(v);
2499
Adam Cohen327acfe2014-06-06 11:52:52 -07002500 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002501
Adam Cohen7d30a372013-07-01 17:03:59 -07002502 mTempVisiblePagesRange[0] = 0;
2503 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002504 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002505 mReorderingStarted = true;
2506
2507 // Check if we are within the reordering range
2508 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002509 dragViewIndex <= mTempVisiblePagesRange[1]) {
2510 // Find the drag view under the pointer
2511 mDragView = getChildAt(dragViewIndex);
2512 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2513 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002514 snapToPage(getPageNearestToCenterOfScreen());
2515 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002516 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002517 return true;
2518 }
2519 return false;
2520 }
2521
2522 boolean isReordering(boolean testTouchState) {
2523 boolean state = mIsReordering;
2524 if (testTouchState) {
2525 state &= (mTouchState == TOUCH_STATE_REORDERING);
2526 }
2527 return state;
2528 }
2529 void endReordering() {
2530 // For simplicity, we call endReordering sometimes even if reordering was never started.
2531 // In that case, we don't want to do anything.
2532 if (!mReorderingStarted) return;
2533 mReorderingStarted = false;
2534
2535 // If we haven't flung-to-delete the current child, then we just animate the drag view
2536 // back into position
2537 final Runnable onCompleteRunnable = new Runnable() {
2538 @Override
2539 public void run() {
2540 onEndReordering();
2541 }
2542 };
2543 if (!mDeferringForDelete) {
2544 mPostReorderingPreZoomInRunnable = new Runnable() {
2545 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002546 onCompleteRunnable.run();
2547 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002548 };
2549 };
2550
2551 mPostReorderingPreZoomInRemainingAnimationCount =
2552 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2553 // Snap to the current page
2554 snapToPage(indexOfChild(mDragView), 0);
2555 // Animate the drag view back to the front position
2556 animateDragViewToOriginalPosition();
2557 } else {
2558 // Handled in post-delete-animation-callbacks
2559 }
2560 }
2561
Adam Cohen7d30a372013-07-01 17:03:59 -07002562 /*
2563 * Flinging to delete - IN PROGRESS
2564 */
2565 private PointF isFlingingToDelete() {
2566 ViewConfiguration config = ViewConfiguration.get(getContext());
2567 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2568
2569 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2570 // Do a quick dot product test to ensure that we are flinging upwards
2571 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2572 mVelocityTracker.getYVelocity());
2573 PointF upVec = new PointF(0f, -1f);
2574 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2575 (vel.length() * upVec.length()));
2576 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2577 return vel;
2578 }
2579 }
2580 return null;
2581 }
2582
2583 /**
2584 * Creates an animation from the current drag view along its current velocity vector.
2585 * For this animation, the alpha runs for a fixed duration and we update the position
2586 * progressively.
2587 */
2588 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2589 private View mDragView;
2590 private PointF mVelocity;
2591 private Rect mFrom;
2592 private long mPrevTime;
2593 private float mFriction;
2594
2595 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2596
2597 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2598 long startTime, float friction) {
2599 mDragView = dragView;
2600 mVelocity = vel;
2601 mFrom = from;
2602 mPrevTime = startTime;
2603 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2604 }
2605
2606 @Override
2607 public void onAnimationUpdate(ValueAnimator animation) {
2608 float t = ((Float) animation.getAnimatedValue()).floatValue();
2609 long curTime = AnimationUtils.currentAnimationTimeMillis();
2610
2611 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2612 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2613
2614 mDragView.setTranslationX(mFrom.left);
2615 mDragView.setTranslationY(mFrom.top);
2616 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2617
2618 mVelocity.x *= mFriction;
2619 mVelocity.y *= mFriction;
2620 mPrevTime = curTime;
2621 }
2622 };
2623
2624 private static final int ANIM_TAG_KEY = 100;
2625
2626 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2627 return new Runnable() {
2628 @Override
2629 public void run() {
2630 int dragViewIndex = indexOfChild(dragView);
2631
2632 // For each of the pages around the drag view, animate them from the previous
2633 // position to the new position in the layout (as a result of the drag view moving
2634 // in the layout)
2635 // NOTE: We can make an assumption here because we have side-bound pages that we
2636 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002637 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002638 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2639 boolean slideFromLeft = (isLastWidgetPage ||
2640 dragViewIndex > mTempVisiblePagesRange[0]);
2641
2642 // Setup the scroll to the correct page before we swap the views
2643 if (slideFromLeft) {
2644 snapToPageImmediately(dragViewIndex - 1);
2645 }
2646
2647 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2648 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2649 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2650 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2651 ArrayList<Animator> animations = new ArrayList<Animator>();
2652 for (int i = lowerIndex; i <= upperIndex; ++i) {
2653 View v = getChildAt(i);
2654 // dragViewIndex < pageUnderPointIndex, so after we remove the
2655 // drag view all subsequent views to pageUnderPointIndex will
2656 // shift down.
2657 int oldX = 0;
2658 int newX = 0;
2659 if (slideFromLeft) {
2660 if (i == 0) {
2661 // Simulate the page being offscreen with the page spacing
2662 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002663 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002664 } else {
2665 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2666 }
2667 newX = getViewportOffsetX() + getChildOffset(i);
2668 } else {
2669 oldX = getChildOffset(i) - getChildOffset(i - 1);
2670 newX = 0;
2671 }
2672
2673 // Animate the view translation from its old position to its new
2674 // position
2675 AnimatorSet anim = (AnimatorSet) v.getTag();
2676 if (anim != null) {
2677 anim.cancel();
2678 }
2679
2680 // Note: Hacky, but we want to skip any optimizations to not draw completely
2681 // hidden views
2682 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2683 v.setTranslationX(oldX - newX);
2684 anim = new AnimatorSet();
2685 anim.playTogether(
2686 ObjectAnimator.ofFloat(v, "translationX", 0f),
2687 ObjectAnimator.ofFloat(v, "alpha", 1f));
2688 animations.add(anim);
2689 v.setTag(ANIM_TAG_KEY, anim);
2690 }
2691
2692 AnimatorSet slideAnimations = new AnimatorSet();
2693 slideAnimations.playTogether(animations);
2694 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2695 slideAnimations.addListener(new AnimatorListenerAdapter() {
2696 @Override
2697 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002698 mDeferringForDelete = false;
2699 onEndReordering();
2700 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002701 }
2702 });
2703 slideAnimations.start();
2704
2705 removeView(dragView);
2706 onRemoveView(dragView, true);
2707 }
2708 };
2709 }
2710
2711 public void onFlingToDelete(PointF vel) {
2712 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2713
2714 // NOTE: Because it takes time for the first frame of animation to actually be
2715 // called and we expect the animation to be a continuation of the fling, we have
2716 // to account for the time that has elapsed since the fling finished. And since
2717 // we don't have a startDelay, we will always get call to update when we call
2718 // start() (which we want to ignore).
2719 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2720 private int mCount = -1;
2721 private long mStartTime;
2722 private float mOffset;
2723 /* Anonymous inner class ctor */ {
2724 mStartTime = startTime;
2725 }
2726
2727 @Override
2728 public float getInterpolation(float t) {
2729 if (mCount < 0) {
2730 mCount++;
2731 } else if (mCount == 0) {
2732 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2733 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2734 mCount++;
2735 }
2736 return Math.min(1f, mOffset + t);
2737 }
2738 };
2739
2740 final Rect from = new Rect();
2741 final View dragView = mDragView;
2742 from.left = (int) dragView.getTranslationX();
2743 from.top = (int) dragView.getTranslationY();
2744 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2745 from, startTime, FLING_TO_DELETE_FRICTION);
2746
2747 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2748
2749 // Create and start the animation
2750 ValueAnimator mDropAnim = new ValueAnimator();
2751 mDropAnim.setInterpolator(tInterpolator);
2752 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2753 mDropAnim.setFloatValues(0f, 1f);
2754 mDropAnim.addUpdateListener(updateCb);
2755 mDropAnim.addListener(new AnimatorListenerAdapter() {
2756 public void onAnimationEnd(Animator animation) {
2757 onAnimationEndRunnable.run();
2758 }
2759 });
2760 mDropAnim.start();
2761 mDeferringForDelete = true;
2762 }
2763
2764 /* Drag to delete */
2765 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2766 if (mDeleteDropTarget != null) {
2767 mAltTmpRect.set(0, 0, 0, 0);
2768 View parent = (View) mDeleteDropTarget.getParent();
2769 if (parent != null) {
2770 parent.getGlobalVisibleRect(mAltTmpRect);
2771 }
2772 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2773 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2774 return mTmpRect.contains(x, y);
2775 }
2776 return false;
2777 }
2778
2779 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2780
2781 private void onDropToDelete() {
2782 final View dragView = mDragView;
2783
2784 final float toScale = 0f;
2785 final float toAlpha = 0f;
2786
2787 // Create and start the complex animation
2788 ArrayList<Animator> animations = new ArrayList<Animator>();
2789 AnimatorSet motionAnim = new AnimatorSet();
2790 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2791 motionAnim.playTogether(
2792 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2793 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2794 animations.add(motionAnim);
2795
2796 AnimatorSet alphaAnim = new AnimatorSet();
2797 alphaAnim.setInterpolator(new LinearInterpolator());
2798 alphaAnim.playTogether(
2799 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2800 animations.add(alphaAnim);
2801
2802 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2803
2804 AnimatorSet anim = new AnimatorSet();
2805 anim.playTogether(animations);
2806 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2807 anim.addListener(new AnimatorListenerAdapter() {
2808 public void onAnimationEnd(Animator animation) {
2809 onAnimationEndRunnable.run();
2810 }
2811 });
2812 anim.start();
2813
2814 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002815 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002816
2817 /* Accessibility */
2818 @Override
2819 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2820 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002821 info.setScrollable(getPageCount() > 1);
2822 if (getCurrentPage() < getPageCount() - 1) {
2823 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2824 }
2825 if (getCurrentPage() > 0) {
2826 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2827 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002828 }
2829
2830 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002831 public void sendAccessibilityEvent(int eventType) {
2832 // Don't let the view send real scroll events.
2833 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2834 super.sendAccessibilityEvent(eventType);
2835 }
2836 }
2837
2838 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002839 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2840 super.onInitializeAccessibilityEvent(event);
2841 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002842 }
2843
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002844 @Override
2845 public boolean performAccessibilityAction(int action, Bundle arguments) {
2846 if (super.performAccessibilityAction(action, arguments)) {
2847 return true;
2848 }
2849 switch (action) {
2850 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2851 if (getCurrentPage() < getPageCount() - 1) {
2852 scrollRight();
2853 return true;
2854 }
2855 } break;
2856 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2857 if (getCurrentPage() > 0) {
2858 scrollLeft();
2859 return true;
2860 }
2861 } break;
2862 }
2863 return false;
2864 }
2865
Adam Cohen0ffac432013-07-10 11:19:26 -07002866 protected String getCurrentPageDescription() {
2867 return String.format(getContext().getString(R.string.default_scroll_format),
2868 getNextPage() + 1, getChildCount());
2869 }
2870
Winson Chungd11265e2011-08-30 13:37:23 -07002871 @Override
2872 public boolean onHoverEvent(android.view.MotionEvent event) {
2873 return true;
2874 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002875}