blob: 4e34628da711bb69e5e9be4ae4469d9caf97b075 [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();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700342 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700343 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700344 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700345 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700346
Winson Chung7819a562013-09-19 15:55:45 -0700347 ArrayList<PageIndicator.PageMarkerResources> markers =
348 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700349 for (int i = 0; i < getChildCount(); ++i) {
350 markers.add(getPageIndicatorMarker(i));
351 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700352
Winson Chungc58497e2013-09-03 17:48:37 -0700353 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700354
355 OnClickListener listener = getPageIndicatorClickListener();
356 if (listener != null) {
357 mPageIndicator.setOnClickListener(listener);
358 }
Adam Cohen53805212013-10-01 10:39:23 -0700359 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700360 }
361 }
362
Adam Cohen53805212013-10-01 10:39:23 -0700363 protected String getPageIndicatorDescription() {
364 return getCurrentPageDescription();
365 }
366
367 protected OnClickListener getPageIndicatorClickListener() {
368 return null;
369 }
370
Winson Chungd2be3812013-07-16 11:11:32 -0700371 protected void onDetachedFromWindow() {
372 // Unhook the page indicator
373 mPageIndicator = null;
374 }
375
Adam Cohen7d30a372013-07-01 17:03:59 -0700376 void setDeleteDropTarget(View v) {
377 mDeleteDropTarget = v;
378 }
379
380 // Convenience methods to map points from self to parent and vice versa
381 float[] mapPointFromViewToParent(View v, float x, float y) {
382 mTmpPoint[0] = x;
383 mTmpPoint[1] = y;
384 v.getMatrix().mapPoints(mTmpPoint);
385 mTmpPoint[0] += v.getLeft();
386 mTmpPoint[1] += v.getTop();
387 return mTmpPoint;
388 }
389 float[] mapPointFromParentToView(View v, float x, float y) {
390 mTmpPoint[0] = x - v.getLeft();
391 mTmpPoint[1] = y - v.getTop();
392 v.getMatrix().invert(mTmpInvMatrix);
393 mTmpInvMatrix.mapPoints(mTmpPoint);
394 return mTmpPoint;
395 }
396
397 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700398 if (mDragView != null) {
399 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
400 (mDragViewBaselineLeft - mDragView.getLeft());
401 float y = mLastMotionY - mDownMotionY;
402 mDragView.setTranslationX(x);
403 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700404
Adam Cohenf358a4b2013-07-23 16:47:31 -0700405 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
406 + x + ", " + y);
407 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700408 }
409
410 public void setMinScale(float f) {
411 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700412 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700413 requestLayout();
414 }
415
416 @Override
417 public void setScaleX(float scaleX) {
418 super.setScaleX(scaleX);
419 if (isReordering(true)) {
420 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
421 mLastMotionX = p[0];
422 mLastMotionY = p[1];
423 updateDragViewTranslationDuringDrag();
424 }
425 }
426
427 // Convenience methods to get the actual width/height of the PagedView (since it is measured
428 // to be larger to account for the minimum possible scale)
429 int getViewportWidth() {
430 return mViewport.width();
431 }
432 int getViewportHeight() {
433 return mViewport.height();
434 }
435
436 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
437 // PagedView both horizontally and vertically
438 int getViewportOffsetX() {
439 return (getMeasuredWidth() - getViewportWidth()) / 2;
440 }
441
442 int getViewportOffsetY() {
443 return (getMeasuredHeight() - getViewportHeight()) / 2;
444 }
445
Adam Cohenedb40762013-07-18 16:45:45 -0700446 PageIndicator getPageIndicator() {
447 return mPageIndicator;
448 }
Winson Chung7819a562013-09-19 15:55:45 -0700449 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
450 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700451 }
Adam Cohenedb40762013-07-18 16:45:45 -0700452
Adam Cohen674531f2013-12-13 15:07:14 -0800453 /**
454 * Add a page change listener which will be called when a page is _finished_ listening.
455 *
456 */
Winson Chung86f77532010-08-24 11:08:22 -0700457 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
458 mPageSwitchListener = pageSwitchListener;
459 if (mPageSwitchListener != null) {
460 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 }
462 }
463
464 /**
Winson Chung52aee602013-01-30 12:01:02 -0800465 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
466 */
467 public boolean isLayoutRtl() {
468 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
469 }
470
471 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
473 * out pages.
474 */
475 protected void setDataIsReady() {
476 mIsDataReady = true;
477 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700478
Winson Chungf0ea4d32011-06-06 14:27:16 -0700479 protected boolean isDataReady() {
480 return mIsDataReady;
481 }
482
483 /**
Winson Chung86f77532010-08-24 11:08:22 -0700484 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700485 *
Winson Chung86f77532010-08-24 11:08:22 -0700486 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 */
Winson Chung86f77532010-08-24 11:08:22 -0700488 int getCurrentPage() {
489 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700491
Winson Chung360e63f2012-04-27 13:48:05 -0700492 int getNextPage() {
493 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
494 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700495
Winson Chung86f77532010-08-24 11:08:22 -0700496 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 return getChildCount();
498 }
499
Winson Chung86f77532010-08-24 11:08:22 -0700500 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 return getChildAt(index);
502 }
503
Adam Cohenae4f1552011-10-20 00:15:42 -0700504 protected int indexToPage(int index) {
505 return index;
506 }
507
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800509 * Updates the scroll of the current page immediately to its final scroll position. We use this
510 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
511 * the previous tab page.
512 */
513 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700514 // If the current page is invalid, just reset the scroll position to zero
515 int newX = 0;
516 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700517 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700518 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800519 scrollTo(newX, 0);
520 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700521 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800522 }
523
524 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700525 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700526 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
527 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700528 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700529 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700530 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800531 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700532 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700533 }
534
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700535 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700536 mScroller.abortAnimation();
537 // We need to clean up the next page here to avoid computeScrollHelper from
538 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700539 if (resetNextPage) {
540 mNextPage = INVALID_PAGE;
541 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700542 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700543
544 private void forceFinishScroller() {
545 mScroller.forceFinished(true);
546 // We need to clean up the next page here to avoid computeScrollHelper from
547 // updating current page on the pass.
548 mNextPage = INVALID_PAGE;
549 }
550
Adam Cohen6f127a62014-06-12 14:54:41 -0700551 private int validateNewPage(int newPage) {
552 int validatedPage = newPage;
553 // When in free scroll mode, we need to clamp to the free scroll page range.
554 if (mFreeScroll) {
555 getFreeScrollPageRange(mTempVisiblePagesRange);
556 validatedPage = Math.max(mTempVisiblePagesRange[0],
557 Math.min(newPage, mTempVisiblePagesRange[1]));
558 }
559 // Ensure that it is clamped by the actual set of children in all cases
560 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
561 return validatedPage;
562 }
563
Chet Haasebc2f0822012-10-26 17:59:53 -0700564 /**
Winson Chung86f77532010-08-24 11:08:22 -0700565 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 */
Winson Chung86f77532010-08-24 11:08:22 -0700567 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800568 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700569 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800570 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800571 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
572 // the default
573 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800574 return;
575 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700576 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700577 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700578 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700579 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800580 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700581 }
582
Winson Chung8c87cd82013-07-23 16:20:10 -0700583 /**
584 * The restore page will be set in place of the current page at the next (likely first)
585 * layout.
586 */
587 void setRestorePage(int restorePage) {
588 mRestorePage = restorePage;
589 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800590 int getRestorePage() {
591 return mRestorePage;
592 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700593
Adam Cohen674531f2013-12-13 15:07:14 -0800594 /**
595 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
596 * has settled.
597 */
Michael Jurka0142d492010-08-25 17:46:15 -0700598 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700599 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800600 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 }
Winson Chungd2be3812013-07-16 11:11:32 -0700602
Adam Cohen674531f2013-12-13 15:07:14 -0800603 updatePageIndicator();
604 }
605
606 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700607 // Update the page indicator (when we aren't reordering)
608 if (mPageIndicator != null && !isReordering(false)) {
609 mPageIndicator.setActiveMarker(getNextPage());
610 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800612 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700613 if (!mIsPageMoving) {
614 mIsPageMoving = true;
615 onPageBeginMoving();
616 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700617 }
618
Michael Jurkace7e05f2011-02-01 22:02:35 -0800619 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700620 if (mIsPageMoving) {
621 mIsPageMoving = false;
622 onPageEndMoving();
623 }
Michael Jurka0142d492010-08-25 17:46:15 -0700624 }
625
Adam Cohen26976d92011-03-22 15:33:33 -0700626 protected boolean isPageMoving() {
627 return mIsPageMoving;
628 }
629
Michael Jurka0142d492010-08-25 17:46:15 -0700630 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700631 protected void onPageBeginMoving() {
632 }
633
634 // a method that subclasses can override to add behavior
635 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700636 }
637
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 /**
Winson Chung86f77532010-08-24 11:08:22 -0700639 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 *
641 * @param l The listener used to respond to long clicks.
642 */
643 @Override
644 public void setOnLongClickListener(OnLongClickListener l) {
645 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700646 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700648 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
Adam Cohen1697b792013-09-17 19:08:21 -0700650 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 }
652
653 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800654 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700655 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800656 }
657
658 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700659 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700660 // In free scroll mode, we clamp the scrollX
661 if (mFreeScroll) {
662 x = Math.min(x, mFreeScrollMaxScrollX);
663 x = Math.max(x, mFreeScrollMinScrollX);
664 }
665
Adam Cohen0ffac432013-07-10 11:19:26 -0700666 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800667 mUnboundedScrollX = x;
668
Adam Cohen0ffac432013-07-10 11:19:26 -0700669 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
670 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
671 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800672 super.scrollTo(0, y);
673 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700674 if (isRtl) {
675 overScroll(x - mMaxScrollX);
676 } else {
677 overScroll(x);
678 }
Adam Cohen68d73932010-11-15 10:50:58 -0800679 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700680 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800681 super.scrollTo(mMaxScrollX, y);
682 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700683 if (isRtl) {
684 overScroll(x);
685 } else {
686 overScroll(x - mMaxScrollX);
687 }
Adam Cohen68d73932010-11-15 10:50:58 -0800688 }
689 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800690 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800691 super.scrollTo(x, y);
692 }
693
Michael Jurka0142d492010-08-25 17:46:15 -0700694 mTouchX = x;
695 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700696
697 // Update the last motion events when scrolling
698 if (isReordering(true)) {
699 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
700 mLastMotionX = p[0];
701 mLastMotionY = p[1];
702 updateDragViewTranslationDuringDrag();
703 }
Michael Jurka0142d492010-08-25 17:46:15 -0700704 }
705
Adam Cohen53805212013-10-01 10:39:23 -0700706 private void sendScrollAccessibilityEvent() {
707 AccessibilityManager am =
708 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
709 if (am.isEnabled()) {
710 AccessibilityEvent ev =
711 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700712 ev.setItemCount(getChildCount());
713 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700714 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700715
Alan Viverette254139a2013-10-08 13:13:46 -0700716 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700717 if (getNextPage() >= mCurrentPage) {
718 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
719 } else {
720 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
721 }
722
723 ev.setAction(action);
724 sendAccessibilityEventUnchecked(ev);
725 }
726 }
727
Michael Jurka0142d492010-08-25 17:46:15 -0700728 // we moved this functionality to a helper function so SmoothPagedView can reuse it
729 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700731 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 if (getScrollX() != mScroller.getCurrX()
733 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700734 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700735 float scaleX = mFreeScroll ? getScaleX() : 1f;
736 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
737 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700738 }
Michael Jurka0142d492010-08-25 17:46:15 -0700739 invalidate();
740 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700741 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700742 sendScrollAccessibilityEvent();
743
Adam Cohen6f127a62014-06-12 14:54:41 -0700744 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700745 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700746 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700747
Winson Chung9c0565f2013-07-19 13:49:06 -0700748 // Load the associated pages if necessary
749 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
750 loadAssociatedPages(mCurrentPage);
751 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
752 }
753
Adam Cohen73aa9752010-11-24 16:26:58 -0800754 // We don't want to trigger a page end moving unless the page has settled
755 // and the user has stopped scrolling
756 if (mTouchState == TOUCH_STATE_REST) {
757 pageEndMoving();
758 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700759
Adam Cohen7d30a372013-07-01 17:03:59 -0700760 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700761 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700762 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700763 if (am.isEnabled()) {
764 // Notify the user when the page changes
765 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700766 }
Michael Jurka0142d492010-08-25 17:46:15 -0700767 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
Michael Jurka0142d492010-08-25 17:46:15 -0700769 return false;
770 }
771
772 @Override
773 public void computeScroll() {
774 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 }
776
Adam Cohen7d30a372013-07-01 17:03:59 -0700777 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
778 return mTopAlignPageWhenShrinkingForBouncer;
779 }
780
Adam Cohen96d30a12013-07-16 18:13:21 -0700781 public static class LayoutParams extends ViewGroup.LayoutParams {
782 public boolean isFullScreenPage = false;
783
784 /**
785 * {@inheritDoc}
786 */
787 public LayoutParams(int width, int height) {
788 super(width, height);
789 }
790
791 public LayoutParams(ViewGroup.LayoutParams source) {
792 super(source);
793 }
794 }
795
796 protected LayoutParams generateDefaultLayoutParams() {
797 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
798 }
799
Adam Cohenedb40762013-07-18 16:45:45 -0700800 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700801 LayoutParams lp = generateDefaultLayoutParams();
802 lp.isFullScreenPage = true;
803 super.addView(page, 0, lp);
804 }
805
Adam Cohen410f3cd2013-09-22 12:09:32 -0700806 public int getNormalChildHeight() {
807 return mNormalChildHeight;
808 }
809
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 @Override
811 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700813 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
814 return;
815 }
816
Adam Cohen7d30a372013-07-01 17:03:59 -0700817 // We measure the dimensions of the PagedView to be larger than the pages so that when we
818 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700819 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
820 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
821 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700822 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800823 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700824 // viewport, we can be at most one and a half screens offset once we scale down
825 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700826 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
827 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700828
Winson Chungc82d2622013-11-06 13:23:29 -0800829 int parentWidthSize = (int) (2f * maxSize);
830 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700831 int scaledWidthSize, scaledHeightSize;
832 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700833 scaledWidthSize = (int) (parentWidthSize / mMinScale);
834 scaledHeightSize = (int) (parentHeightSize / mMinScale);
835 } else {
836 scaledWidthSize = widthSize;
837 scaledHeightSize = heightSize;
838 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700839 mViewport.set(0, 0, widthSize, heightSize);
840
841 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
842 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
843 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 }
845
Winson Chung8aad6102012-05-11 16:27:49 -0700846 // Return early if we aren't given a proper dimension
847 if (widthSize <= 0 || heightSize <= 0) {
848 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
849 return;
850 }
851
Adam Lesinski6b879f02010-11-04 16:15:23 -0700852 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
853 * of the All apps view on XLarge displays to not take up more space then it needs. Width
854 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
855 * each page to have the same width.
856 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700857 final int verticalPadding = getPaddingTop() + getPaddingBottom();
858 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700859
860 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700861 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700862 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700863 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
864 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
865 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
866 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 final int childCount = getChildCount();
868 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700869 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700870 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100871 if (child.getVisibility() != GONE) {
872 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700873
Vladimir Marko2824b072013-10-04 16:42:17 +0100874 int childWidthMode;
875 int childHeightMode;
876 int childWidth;
877 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700878
Vladimir Marko2824b072013-10-04 16:42:17 +0100879 if (!lp.isFullScreenPage) {
880 if (lp.width == LayoutParams.WRAP_CONTENT) {
881 childWidthMode = MeasureSpec.AT_MOST;
882 } else {
883 childWidthMode = MeasureSpec.EXACTLY;
884 }
885
886 if (lp.height == LayoutParams.WRAP_CONTENT) {
887 childHeightMode = MeasureSpec.AT_MOST;
888 } else {
889 childHeightMode = MeasureSpec.EXACTLY;
890 }
891
Adam Cohen3b185e22013-10-29 14:45:58 -0700892 childWidth = getViewportWidth() - horizontalPadding
893 - mInsets.left - mInsets.right;
894 childHeight = getViewportHeight() - verticalPadding
895 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100896 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700897 } else {
898 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700899 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100900
Adam Cohen3b185e22013-10-29 14:45:58 -0700901 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
902 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700903 }
904
Vladimir Marko2824b072013-10-04 16:42:17 +0100905 final int childWidthMeasureSpec =
906 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
907 final int childHeightMeasureSpec =
908 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
909 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700910 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700911 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700912 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800913 }
914
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700916 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700917 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700918 return;
919 }
920
Winson Chung785d2eb2011-04-14 16:08:02 -0700921 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700923
Adam Cohen7d30a372013-07-01 17:03:59 -0700924 int offsetX = getViewportOffsetX();
925 int offsetY = getViewportOffsetY();
926
927 // Update the viewport offsets
928 mViewport.offset(offsetX, offsetY);
929
Adam Cohen0ffac432013-07-10 11:19:26 -0700930 final boolean isRtl = isLayoutRtl();
931
932 final int startIndex = isRtl ? childCount - 1 : 0;
933 final int endIndex = isRtl ? -1 : childCount;
934 final int delta = isRtl ? -1 : 1;
935
Adam Cohen7d30a372013-07-01 17:03:59 -0700936 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700937
Adam Cohen3b185e22013-10-29 14:45:58 -0700938 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000939 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700940
941 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700942 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
943 mPageScrolls = new int[getChildCount()];
944 }
945
Adam Cohen0ffac432013-07-10 11:19:26 -0700946 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700947 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700949 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100950 int childTop;
951 if (lp.isFullScreenPage) {
952 childTop = offsetY;
953 } else {
954 childTop = offsetY + getPaddingTop() + mInsets.top;
955 if (mCenterPagesVertically) {
956 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
957 }
958 }
959
Adam Cohen96d30a12013-07-16 18:13:21 -0700960 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700961 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800962
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700964 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800965 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700966
Adam Cohen3b185e22013-10-29 14:45:58 -0700967 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
968 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000969
970 int pageGap = mPageSpacing;
971 int next = i + delta;
972 if (next != endIndex) {
973 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
974 } else {
975 nextLp = null;
976 }
977
978 // Prevent full screen pages from showing in the viewport
979 // when they are not the current page.
980 if (lp.isFullScreenPage) {
981 pageGap = getPaddingLeft();
982 } else if (nextLp != null && nextLp.isFullScreenPage) {
983 pageGap = getPaddingRight();
984 }
985
986 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 }
988 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700989
990 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
991 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800992 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700993 setHorizontalScrollBarEnabled(true);
994 mFirstLayout = false;
995 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700996
997 if (childCount > 0) {
998 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700999 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -07001000 } else {
1001 mMaxScrollX = 0;
1002 }
1003
1004 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
1005 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001006 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001007 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001008 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001009 } else {
1010 setCurrentPage(getNextPage());
1011 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001012 }
1013 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001014
1015 if (isReordering(true)) {
1016 updateDragViewTranslationDuringDrag();
1017 }
Winson Chunge3193b92010-09-10 11:44:42 -07001018 }
1019
Adam Cohen3b185e22013-10-29 14:45:58 -07001020 public void setPageSpacing(int pageSpacing) {
1021 mPageSpacing = pageSpacing;
1022 requestLayout();
1023 }
1024
Adam Cohenf34bab52010-09-30 14:11:56 -07001025 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001026 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1027
1028 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001029 for (int i = 0; i < getChildCount(); i++) {
1030 View child = getChildAt(i);
1031 if (child != null) {
1032 float scrollProgress = getScrollProgress(screenCenter, child, i);
1033 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001034 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001035 }
1036 }
1037 invalidate();
1038 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001039 }
1040
Winson Chungc58497e2013-09-03 17:48:37 -07001041 protected void enablePagedViewAnimations() {
1042 mAllowPagedViewAnimations = true;
1043
1044 }
1045 protected void disablePagedViewAnimations() {
1046 mAllowPagedViewAnimations = false;
1047 }
1048
Winson Chunge3193b92010-09-10 11:44:42 -07001049 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001050 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001051 // Update the page indicator, we don't update the page indicator as we
1052 // add/remove pages
1053 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001054 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001055 mPageIndicator.addMarker(pageIndex,
1056 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001057 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001058 }
1059
Adam Cohen2591f6a2011-10-25 14:36:40 -07001060 // This ensures that when children are added, they get the correct transforms / alphas
1061 // in accordance with any scroll effects.
1062 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001063 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001064 invalidate();
1065 }
1066
Michael Jurka8b805b12012-04-18 14:23:14 -07001067 @Override
1068 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001069 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001070 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001071 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001072 }
1073
Winson Chungd2be3812013-07-16 11:11:32 -07001074 private void removeMarkerForView(int index) {
1075 // Update the page indicator, we don't update the page indicator as we
1076 // add/remove pages
1077 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001078 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001079 }
1080 }
1081
1082 @Override
1083 public void removeView(View v) {
1084 // XXX: We should find a better way to hook into this before the view
1085 // gets removed form its parent...
1086 removeMarkerForView(indexOfChild(v));
1087 super.removeView(v);
1088 }
1089 @Override
1090 public void removeViewInLayout(View v) {
1091 // XXX: We should find a better way to hook into this before the view
1092 // gets removed form its parent...
1093 removeMarkerForView(indexOfChild(v));
1094 super.removeViewInLayout(v);
1095 }
1096 @Override
1097 public void removeViewAt(int index) {
1098 // XXX: We should find a better way to hook into this before the view
1099 // gets removed form its parent...
1100 removeViewAt(index);
1101 super.removeViewAt(index);
1102 }
1103 @Override
1104 public void removeAllViewsInLayout() {
1105 // Update the page indicator, we don't update the page indicator as we
1106 // add/remove pages
1107 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001108 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001109 }
1110
1111 super.removeAllViewsInLayout();
1112 }
1113
Adam Cohen73894962011-10-31 13:17:17 -07001114 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001115 if (index < 0 || index > getChildCount() - 1) return 0;
1116
Adam Cohenf698c6e2013-07-17 18:44:25 -07001117 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001118
Adam Cohenf698c6e2013-07-17 18:44:25 -07001119 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001120 }
1121
Adam Cohen6f127a62014-06-12 14:54:41 -07001122 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001123 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001124 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001125 }
1126
Michael Jurkadde558b2011-11-09 22:09:06 -08001127 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001128 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001129 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001130
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001131 range[0] = -1;
1132 range[1] = -1;
1133
Michael Jurkac4fb9172010-09-02 17:19:20 -07001134 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001135 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001136 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001137
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001138 int count = getChildCount();
1139 for (int i = 0; i < count; i++) {
1140 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001141
Winson Chungc9ca2982013-07-19 12:07:38 -07001142 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001143 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001144 if (mTmpIntPoint[0] > viewportWidth) {
1145 if (range[0] == -1) {
1146 continue;
1147 } else {
1148 break;
1149 }
1150 }
1151
Adam Cohen6a678da2013-09-24 10:58:01 -07001152 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001153 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1154 if (mTmpIntPoint[0] < 0) {
1155 if (range[0] == -1) {
1156 continue;
1157 } else {
1158 break;
1159 }
1160 }
1161 curScreen = i;
1162 if (range[0] < 0) {
1163 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001164 }
1165 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001166
1167 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001168 } else {
1169 range[0] = -1;
1170 range[1] = -1;
1171 }
1172 }
1173
Michael Jurka920d7f42012-05-14 16:29:55 -07001174 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001175 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001176 }
1177
Michael Jurkadde558b2011-11-09 22:09:06 -08001178 @Override
1179 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001180 // Find out which screens are visible; as an optimization we only call draw on them
1181 final int pageCount = getChildCount();
1182 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001183 int halfScreenSize = getViewportWidth() / 2;
1184 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1185 // Otherwise it is equal to the scaled overscroll position.
1186 int screenCenter = mOverScrollX + halfScreenSize;
1187
1188 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1189 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1190 // set it for the next frame
1191 mForceScreenScrolled = false;
1192 screenScrolled(screenCenter);
1193 mLastScreenCenter = screenCenter;
1194 }
1195
Michael Jurkadde558b2011-11-09 22:09:06 -08001196 getVisiblePages(mTempVisiblePagesRange);
1197 final int leftScreen = mTempVisiblePagesRange[0];
1198 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001199 if (leftScreen != -1 && rightScreen != -1) {
1200 final long drawingTime = getDrawingTime();
1201 // Clip to the bounds
1202 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001203 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1204 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001205
Adam Cohen7d30a372013-07-01 17:03:59 -07001206 // Draw all the children, leaving the drag view for last
1207 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001208 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001209 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001210 if (mForceDrawAllChildrenNextFrame ||
1211 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001212 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001213 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001214 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001215 // Draw the drag view on top (if there is one)
1216 if (mDragView != null) {
1217 drawChild(canvas, mDragView, drawingTime);
1218 }
1219
Michael Jurka5e368ff2012-05-14 23:13:15 -07001220 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001221 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001222 }
Michael Jurka0142d492010-08-25 17:46:15 -07001223 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 }
1225
1226 @Override
1227 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001228 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001229 if (page != mCurrentPage || !mScroller.isFinished()) {
1230 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 return true;
1232 }
1233 return false;
1234 }
1235
1236 @Override
1237 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001238 int focusablePage;
1239 if (mNextPage != INVALID_PAGE) {
1240 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001242 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 }
Winson Chung86f77532010-08-24 11:08:22 -07001244 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001246 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 }
1248 return false;
1249 }
1250
1251 @Override
1252 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001253 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001254 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001255 if (getCurrentPage() > 0) {
1256 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 return true;
1258 }
1259 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001260 if (getCurrentPage() < getPageCount() - 1) {
1261 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 return true;
1263 }
1264 }
1265 return super.dispatchUnhandledMove(focused, direction);
1266 }
1267
1268 @Override
1269 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001270 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001271 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001272 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 }
1274 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001275 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001276 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 }
1278 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001279 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001280 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 }
1282 }
1283 }
1284
1285 /**
1286 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001287 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 *
Winson Chung86f77532010-08-24 11:08:22 -07001289 * This happens when live folders requery, and if they're off page, they
1290 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 */
1292 @Override
1293 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001294 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 View v = focused;
1296 while (true) {
1297 if (v == current) {
1298 super.focusableViewAvailable(focused);
1299 return;
1300 }
1301 if (v == this) {
1302 return;
1303 }
1304 ViewParent parent = v.getParent();
1305 if (parent instanceof View) {
1306 v = (View)v.getParent();
1307 } else {
1308 return;
1309 }
1310 }
1311 }
1312
1313 /**
1314 * {@inheritDoc}
1315 */
1316 @Override
1317 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1318 if (disallowIntercept) {
1319 // We need to make sure to cancel our long press if
1320 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001321 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001322 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001323 }
1324 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1325 }
1326
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001327 /**
1328 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1329 */
1330 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001331 if (isLayoutRtl()) {
1332 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001333 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001334 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001335 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001336 }
1337
1338 /**
1339 * Return true if a tap at (x, y) should trigger a flip to the next page.
1340 */
1341 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001342 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001343 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001344 }
1345 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001346 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001347 }
Winson Chung52aee602013-01-30 12:01:02 -08001348
Adam Cohen7d30a372013-07-01 17:03:59 -07001349 /** Returns whether x and y originated within the buffered viewport */
1350 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1351 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1352 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1353 return mTmpRect.contains(x, y);
1354 }
1355
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 @Override
1357 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001358 if (DISABLE_TOUCH_INTERACTION) {
1359 return false;
1360 }
1361
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 /*
1363 * This method JUST determines whether we want to intercept the motion.
1364 * If we return true, onTouchEvent will be called and we do the actual
1365 * scrolling there.
1366 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001367 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001368
Winson Chung45e1d6e2010-11-09 17:19:49 -08001369 // Skip touch handling if there are no pages to swipe
1370 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1371
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 /*
1373 * Shortcut the most recurring case: the user is in the dragging
1374 * state and he is moving his finger. We want to intercept this
1375 * motion.
1376 */
1377 final int action = ev.getAction();
1378 if ((action == MotionEvent.ACTION_MOVE) &&
1379 (mTouchState == TOUCH_STATE_SCROLLING)) {
1380 return true;
1381 }
1382
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 switch (action & MotionEvent.ACTION_MASK) {
1384 case MotionEvent.ACTION_MOVE: {
1385 /*
1386 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1387 * whether the user has moved far enough from his original down touch.
1388 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001389 if (mActivePointerId != INVALID_POINTER) {
1390 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001391 }
1392 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1393 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1394 // i.e. fall through to the next case (don't break)
1395 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1396 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001397 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 }
1399
1400 case MotionEvent.ACTION_DOWN: {
1401 final float x = ev.getX();
1402 final float y = ev.getY();
1403 // Remember location of down touch
1404 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001405 mDownMotionY = y;
1406 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 mLastMotionX = x;
1408 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001409 float[] p = mapPointFromViewToParent(this, x, y);
1410 mParentDownMotionX = p[0];
1411 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001412 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001413 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001415
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 /*
1417 * If being flinged and user touches the screen, initiate drag;
1418 * otherwise don't. mScroller.isFinished should be false when
1419 * being flinged.
1420 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001421 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001422 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001423
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001424 if (finishedScrolling) {
1425 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001426 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001427 setCurrentPage(getNextPage());
1428 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001429 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001430 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001431 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1432 mTouchState = TOUCH_STATE_SCROLLING;
1433 } else {
1434 mTouchState = TOUCH_STATE_REST;
1435 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001436 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001437
Winson Chung86f77532010-08-24 11:08:22 -07001438 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001440 if (!DISABLE_TOUCH_SIDE_PAGES) {
1441 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1442 if (getChildCount() > 0) {
1443 if (hitsPreviousPage(x, y)) {
1444 mTouchState = TOUCH_STATE_PREV_PAGE;
1445 } else if (hitsNextPage(x, y)) {
1446 mTouchState = TOUCH_STATE_NEXT_PAGE;
1447 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 }
1449 }
1450 }
1451 break;
1452 }
1453
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001455 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001456 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 break;
1458
1459 case MotionEvent.ACTION_POINTER_UP:
1460 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001461 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 break;
1463 }
1464
1465 /*
1466 * The only time we want to intercept motion events is if we are in the
1467 * drag mode.
1468 */
1469 return mTouchState != TOUCH_STATE_REST;
1470 }
1471
Adam Cohenf8d28232011-02-01 21:47:00 -08001472 protected void determineScrollingStart(MotionEvent ev) {
1473 determineScrollingStart(ev, 1.0f);
1474 }
1475
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 /*
1477 * Determines if we should change the touch state to start scrolling after the
1478 * user moves their touch point too far.
1479 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001480 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001481 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001483 if (pointerIndex == -1) return;
1484
1485 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 final float x = ev.getX(pointerIndex);
1487 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001488 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1489
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 final int xDiff = (int) Math.abs(x - mLastMotionX);
1491 final int yDiff = (int) Math.abs(y - mLastMotionY);
1492
Adam Cohenf8d28232011-02-01 21:47:00 -08001493 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 boolean xPaged = xDiff > mPagingTouchSlop;
1495 boolean xMoved = xDiff > touchSlop;
1496 boolean yMoved = yDiff > touchSlop;
1497
Adam Cohenf8d28232011-02-01 21:47:00 -08001498 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001499 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 // Scroll if the user moved far enough along the X axis
1501 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001502 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001504 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001506 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1507 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001509 }
1510 }
1511
Adam Cohen7d30a372013-07-01 17:03:59 -07001512 protected float getMaxScrollProgress() {
1513 return 1.0f;
1514 }
1515
Adam Cohenf8d28232011-02-01 21:47:00 -08001516 protected void cancelCurrentPageLongPress() {
1517 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001518 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001519 // Try canceling the long press. It could also have been scheduled
1520 // by a distant descendant, so use the mAllowLongPress flag to block
1521 // everything
1522 final View currentPage = getPageAt(mCurrentPage);
1523 if (currentPage != null) {
1524 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 }
1526 }
1527 }
1528
Adam Cohen7d30a372013-07-01 17:03:59 -07001529 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1530 final int halfScreenSize = getViewportWidth() / 2;
1531
1532 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1533 screenCenter = Math.max(halfScreenSize, screenCenter);
1534
1535 return getScrollProgress(screenCenter, v, page);
1536 }
1537
Adam Cohenb5ba0972011-09-07 18:02:31 -07001538 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001539 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001540
Adam Cohenedb40762013-07-18 16:45:45 -07001541 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001542 int count = getChildCount();
1543
1544 final int totalDistance;
1545
1546 int adjacentPage = page + 1;
1547 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1548 adjacentPage = page - 1;
1549 }
1550
1551 if (adjacentPage < 0 || adjacentPage > count - 1) {
1552 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1553 } else {
1554 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1555 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001556
1557 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001558 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1559 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001560 return scrollProgress;
1561 }
1562
Adam Cohenedb40762013-07-18 16:45:45 -07001563 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001564 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001565 return 0;
1566 } else {
1567 return mPageScrolls[index];
1568 }
1569 }
1570
Adam Cohen564a2e72013-10-09 14:47:32 -07001571 // While layout transitions are occurring, a child's position may stray from its baseline
1572 // position. This method returns the magnitude of this stray at any given time.
1573 public int getLayoutTransitionOffsetForPage(int index) {
1574 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1575 return 0;
1576 } else {
1577 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001578
1579 int scrollOffset = 0;
1580 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1581 if (!lp.isFullScreenPage) {
1582 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1583 }
1584
Adam Cohen564a2e72013-10-09 14:47:32 -07001585 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1586 return (int) (child.getX() - baselineX);
1587 }
1588 }
1589
Adam Cohene0f66b52010-11-23 15:06:07 -08001590 // This curve determines how the effect of scrolling over the limits of the page dimishes
1591 // as the user pulls further and further from the bounds
1592 private float overScrollInfluenceCurve(float f) {
1593 f -= 1.0f;
1594 return f * f * f + 1.0f;
1595 }
1596
Adam Cohenb5ba0972011-09-07 18:02:31 -07001597 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001598 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001599
1600 // We want to reach the max over scroll effect when the user has
1601 // over scrolled half the size of the screen
1602 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1603
1604 if (f == 0) return;
1605
1606 // Clamp this factor, f, to -1 < f < 1
1607 if (Math.abs(f) >= 1) {
1608 f /= Math.abs(f);
1609 }
1610
1611 int overScrollAmount = (int) Math.round(f * screenSize);
1612 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001613 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001614 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001615 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001616 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001617 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001618 }
1619 invalidate();
1620 }
1621
1622 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001623 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001624
1625 float f = (amount / screenSize);
1626
1627 if (f == 0) return;
1628 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1629
Adam Cohen7bfc9792011-01-28 13:52:37 -08001630 // Clamp this factor, f, to -1 < f < 1
1631 if (Math.abs(f) >= 1) {
1632 f /= Math.abs(f);
1633 }
1634
Adam Cohene0f66b52010-11-23 15:06:07 -08001635 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001636 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001637 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001638 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001639 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001640 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001641 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001642 }
1643 invalidate();
1644 }
1645
Adam Cohenb5ba0972011-09-07 18:02:31 -07001646 protected void overScroll(float amount) {
1647 dampedOverScroll(amount);
1648 }
1649
Michael Jurkac5b262c2011-01-12 20:24:50 -08001650 protected float maxOverScroll() {
1651 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001652 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001653 float f = 1.0f;
1654 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1655 return OVERSCROLL_DAMP_FACTOR * f;
1656 }
1657
Adam Cohenf358a4b2013-07-23 16:47:31 -07001658 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001659 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001660 }
1661
Adam Cohenf9618852013-11-08 06:45:03 -08001662 protected void disableFreeScroll() {
1663 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001664 }
1665
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001666 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001667 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001668 if (isLayoutRtl()) {
1669 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1670 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1671 } else {
1672 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1673 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1674 }
1675 }
1676
Adam Cohenf9618852013-11-08 06:45:03 -08001677 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001678 mFreeScroll = freeScroll;
1679
Adam Cohenf9618852013-11-08 06:45:03 -08001680 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001681 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001682 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001683 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1684 setCurrentPage(mTempVisiblePagesRange[0]);
1685 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1686 setCurrentPage(mTempVisiblePagesRange[1]);
1687 }
1688 }
1689
1690 setEnableOverscroll(!freeScroll);
1691 }
1692
1693 private void setEnableOverscroll(boolean enable) {
1694 mAllowOverScroll = enable;
1695 }
1696
1697 int getNearestHoverOverPageIndex() {
1698 if (mDragView != null) {
1699 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1700 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001701 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001702 int minDistance = Integer.MAX_VALUE;
1703 int minIndex = indexOfChild(mDragView);
1704 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1705 View page = getPageAt(i);
1706 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1707 int d = Math.abs(dragX - pageX);
1708 if (d < minDistance) {
1709 minIndex = i;
1710 minDistance = d;
1711 }
1712 }
1713 return minIndex;
1714 }
1715 return -1;
1716 }
1717
Winson Chung321e9ee2010-08-09 13:37:56 -07001718 @Override
1719 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001720 if (DISABLE_TOUCH_INTERACTION) {
1721 return false;
1722 }
1723
Adam Cohen1697b792013-09-17 19:08:21 -07001724 super.onTouchEvent(ev);
1725
Winson Chung45e1d6e2010-11-09 17:19:49 -08001726 // Skip touch handling if there are no pages to swipe
1727 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1728
Michael Jurkab8f06722010-10-10 15:58:46 -07001729 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001730
1731 final int action = ev.getAction();
1732
1733 switch (action & MotionEvent.ACTION_MASK) {
1734 case MotionEvent.ACTION_DOWN:
1735 /*
1736 * If being flinged and user touches, stop the fling. isFinished
1737 * will be false if being flinged.
1738 */
1739 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001740 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001741 }
1742
1743 // Remember where the motion event started
1744 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001745 mDownMotionY = mLastMotionY = ev.getY();
1746 mDownScrollX = getScrollX();
1747 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1748 mParentDownMotionX = p[0];
1749 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001750 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001751 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001752 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001753
Michael Jurka0142d492010-08-25 17:46:15 -07001754 if (mTouchState == TOUCH_STATE_SCROLLING) {
1755 pageBeginMoving();
1756 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001757 break;
1758
1759 case MotionEvent.ACTION_MOVE:
1760 if (mTouchState == TOUCH_STATE_SCROLLING) {
1761 // Scroll to follow the motion event
1762 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001763
1764 if (pointerIndex == -1) return true;
1765
Winson Chung321e9ee2010-08-09 13:37:56 -07001766 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001767 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001768
Adam Cohenaefd4e12011-02-14 16:39:38 -08001769 mTotalMotionX += Math.abs(deltaX);
1770
Winson Chungc0844aa2011-02-02 15:25:58 -08001771 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1772 // keep the remainder because we are actually testing if we've moved from the last
1773 // scrolled position (which is discrete).
1774 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001775 mTouchX += deltaX;
1776 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1777 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001778 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001779 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001780 } else {
1781 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001782 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001783 mLastMotionX = x;
1784 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001785 } else {
1786 awakenScrollBars();
1787 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001788 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1789 // Update the last motion position
1790 mLastMotionX = ev.getX();
1791 mLastMotionY = ev.getY();
1792
1793 // Update the parent down so that our zoom animations take this new movement into
1794 // account
1795 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1796 mParentDownMotionX = pt[0];
1797 mParentDownMotionY = pt[1];
1798 updateDragViewTranslationDuringDrag();
1799
1800 // Find the closest page to the touch point
1801 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001802
1803 // Change the drag view if we are hovering over the drop target
1804 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1805 (int) mParentDownMotionX, (int) mParentDownMotionY);
1806 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1807
Adam Cohen7d30a372013-07-01 17:03:59 -07001808 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1809 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1810 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1811 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1812
Adam Cohenf358a4b2013-07-23 16:47:31 -07001813 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1814 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1815 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001816 mTempVisiblePagesRange[0] = 0;
1817 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001818 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001819 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1820 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1821 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1822 mSidePageHoverIndex = pageUnderPointIndex;
1823 mSidePageHoverRunnable = new Runnable() {
1824 @Override
1825 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001826 // Setup the scroll to the correct page before we swap the views
1827 snapToPage(pageUnderPointIndex);
1828
1829 // For each of the pages between the paged view and the drag view,
1830 // animate them from the previous position to the new position in
1831 // the layout (as a result of the drag view moving in the layout)
1832 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1833 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1834 dragViewIndex + 1 : pageUnderPointIndex;
1835 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1836 dragViewIndex - 1 : pageUnderPointIndex;
1837 for (int i = lowerIndex; i <= upperIndex; ++i) {
1838 View v = getChildAt(i);
1839 // dragViewIndex < pageUnderPointIndex, so after we remove the
1840 // drag view all subsequent views to pageUnderPointIndex will
1841 // shift down.
1842 int oldX = getViewportOffsetX() + getChildOffset(i);
1843 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1844
1845 // Animate the view translation from its old position to its new
1846 // position
1847 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1848 if (anim != null) {
1849 anim.cancel();
1850 }
1851
1852 v.setTranslationX(oldX - newX);
1853 anim = new AnimatorSet();
1854 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1855 anim.playTogether(
1856 ObjectAnimator.ofFloat(v, "translationX", 0f));
1857 anim.start();
1858 v.setTag(anim);
1859 }
1860
1861 removeView(mDragView);
1862 onRemoveView(mDragView, false);
1863 addView(mDragView, pageUnderPointIndex);
1864 onAddView(mDragView, pageUnderPointIndex);
1865 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001866 if (mPageIndicator != null) {
1867 mPageIndicator.setActiveMarker(getNextPage());
1868 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001869 }
1870 };
1871 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1872 }
1873 } else {
1874 removeCallbacks(mSidePageHoverRunnable);
1875 mSidePageHoverIndex = -1;
1876 }
Adam Cohen564976a2010-10-13 18:52:07 -07001877 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001878 determineScrollingStart(ev);
1879 }
1880 break;
1881
1882 case MotionEvent.ACTION_UP:
1883 if (mTouchState == TOUCH_STATE_SCROLLING) {
1884 final int activePointerId = mActivePointerId;
1885 final int pointerIndex = ev.findPointerIndex(activePointerId);
1886 final float x = ev.getX(pointerIndex);
1887 final VelocityTracker velocityTracker = mVelocityTracker;
1888 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1889 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001890 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001891 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001892 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1893 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001894
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001895 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1896
Adam Cohen00481b32011-11-18 12:03:48 -08001897 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001898 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001899
Adam Cohenf358a4b2013-07-23 16:47:31 -07001900 if (!mFreeScroll) {
1901 // In the case that the page is moved far to one direction and then is flung
1902 // in the opposite direction, we use a threshold to determine whether we should
1903 // just return to the starting page, or if we should skip one further.
1904 boolean returnToOriginalPage = false;
1905 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1906 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1907 returnToOriginalPage = true;
1908 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001909
Adam Cohenf358a4b2013-07-23 16:47:31 -07001910 int finalPage;
1911 // We give flings precedence over large moves, which is why we short-circuit our
1912 // test for a large move if a fling has been registered. That is, a large
1913 // move to the left and fling to the right will register as a fling to the right.
1914 final boolean isRtl = isLayoutRtl();
1915 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1916 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1917 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1918 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1919 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1920 snapToPageWithVelocity(finalPage, velocityX);
1921 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1922 (isFling && isVelocityXLeft)) &&
1923 mCurrentPage < getChildCount() - 1) {
1924 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1925 snapToPageWithVelocity(finalPage, velocityX);
1926 } else {
1927 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001928 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001929 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001930 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001931 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001932 }
1933
1934 float scaleX = getScaleX();
1935 int vX = (int) (-velocityX * scaleX);
1936 int initialScrollX = (int) (getScrollX() * scaleX);
1937
Adam Cohenf9618852013-11-08 06:45:03 -08001938 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001939 mScroller.fling(initialScrollX,
1940 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1941 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 }
Adam Cohencae7f572013-11-04 14:42:52 -08001943 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1944 // at this point we have not moved beyond the touch slop
1945 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1946 // we can just page
1947 int nextPage = Math.max(0, mCurrentPage - 1);
1948 if (nextPage != mCurrentPage) {
1949 snapToPage(nextPage);
1950 } else {
1951 snapToDestination();
1952 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001953 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001954 // at this point we have not moved beyond the touch slop
1955 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1956 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001957 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1958 if (nextPage != mCurrentPage) {
1959 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001960 } else {
1961 snapToDestination();
1962 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001963 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1964 // Update the last motion position
1965 mLastMotionX = ev.getX();
1966 mLastMotionY = ev.getY();
1967
1968 // Update the parent down so that our zoom animations take this new movement into
1969 // account
1970 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1971 mParentDownMotionX = pt[0];
1972 mParentDownMotionY = pt[1];
1973 updateDragViewTranslationDuringDrag();
1974 boolean handledFling = false;
1975 if (!DISABLE_FLING_TO_DELETE) {
1976 // Check the velocity and see if we are flinging-to-delete
1977 PointF flingToDeleteVector = isFlingingToDelete();
1978 if (flingToDeleteVector != null) {
1979 onFlingToDelete(flingToDeleteVector);
1980 handledFling = true;
1981 }
1982 }
1983 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1984 (int) mParentDownMotionY)) {
1985 onDropToDelete();
1986 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001987 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001988 if (!mCancelTap) {
1989 onUnhandledTap(ev);
1990 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001991 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001992
1993 // Remove the callback to wait for the side page hover timeout
1994 removeCallbacks(mSidePageHoverRunnable);
1995 // End any intermediate reordering states
1996 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001997 break;
1998
1999 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002000 if (mTouchState == TOUCH_STATE_SCROLLING) {
2001 snapToDestination();
2002 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002003 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002004 break;
2005
2006 case MotionEvent.ACTION_POINTER_UP:
2007 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002008 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002009 break;
2010 }
2011
2012 return true;
2013 }
2014
Adam Cohen7d30a372013-07-01 17:03:59 -07002015 public void onFlingToDelete(View v) {}
2016 public void onRemoveView(View v, boolean deletePermanently) {}
2017 public void onRemoveViewAnimationCompleted() {}
2018 public void onAddView(View v, int index) {}
2019
2020 private void resetTouchState() {
2021 releaseVelocityTracker();
2022 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002023 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002024 mTouchState = TOUCH_STATE_REST;
2025 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002026 }
2027
Adam Cohen1697b792013-09-17 19:08:21 -07002028 protected void onUnhandledTap(MotionEvent ev) {
2029 ((Launcher) getContext()).onClick(this);
2030 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002031
Winson Chung185d7162011-02-28 13:47:29 -08002032 @Override
2033 public boolean onGenericMotionEvent(MotionEvent event) {
2034 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2035 switch (event.getAction()) {
2036 case MotionEvent.ACTION_SCROLL: {
2037 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2038 final float vscroll;
2039 final float hscroll;
2040 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2041 vscroll = 0;
2042 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2043 } else {
2044 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2045 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2046 }
2047 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002048 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2049 : (hscroll > 0 || vscroll > 0);
2050 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002051 scrollRight();
2052 } else {
2053 scrollLeft();
2054 }
2055 return true;
2056 }
2057 }
2058 }
2059 }
2060 return super.onGenericMotionEvent(event);
2061 }
2062
Michael Jurkab8f06722010-10-10 15:58:46 -07002063 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2064 if (mVelocityTracker == null) {
2065 mVelocityTracker = VelocityTracker.obtain();
2066 }
2067 mVelocityTracker.addMovement(ev);
2068 }
2069
2070 private void releaseVelocityTracker() {
2071 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002072 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002073 mVelocityTracker.recycle();
2074 mVelocityTracker = null;
2075 }
2076 }
2077
Winson Chung321e9ee2010-08-09 13:37:56 -07002078 private void onSecondaryPointerUp(MotionEvent ev) {
2079 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2080 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2081 final int pointerId = ev.getPointerId(pointerIndex);
2082 if (pointerId == mActivePointerId) {
2083 // This was our active pointer going up. Choose a new
2084 // active pointer and adjust accordingly.
2085 // TODO: Make this decision more intelligent.
2086 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2087 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2088 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002089 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002090 mActivePointerId = ev.getPointerId(newPointerIndex);
2091 if (mVelocityTracker != null) {
2092 mVelocityTracker.clear();
2093 }
2094 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002095 }
2096
Winson Chung321e9ee2010-08-09 13:37:56 -07002097 @Override
2098 public void requestChildFocus(View child, View focused) {
2099 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002100 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002101 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002102 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002103 }
2104 }
2105
Winson Chung1908d072011-02-24 18:09:44 -08002106 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002107 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002108 }
2109
Adam Cohen7d30a372013-07-01 17:03:59 -07002110 int getPageNearestToPoint(float x) {
2111 int index = 0;
2112 for (int i = 0; i < getChildCount(); ++i) {
2113 if (x < getChildAt(i).getRight() - getScrollX()) {
2114 return index;
2115 } else {
2116 index++;
2117 }
2118 }
2119 return Math.min(index, getChildCount() - 1);
2120 }
2121
Adam Cohend19d3ca2010-09-15 14:43:42 -07002122 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002123 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002124 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002125 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002126 final int childCount = getChildCount();
2127 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002128 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002129 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002130 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002131 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002132 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2133 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2134 minDistanceFromScreenCenter = distanceFromScreenCenter;
2135 minDistanceFromScreenCenterIndex = i;
2136 }
2137 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002138 return minDistanceFromScreenCenterIndex;
2139 }
2140
2141 protected void snapToDestination() {
2142 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002143 }
2144
Adam Cohene0f66b52010-11-23 15:06:07 -08002145 private static class ScrollInterpolator implements Interpolator {
2146 public ScrollInterpolator() {
2147 }
2148
2149 public float getInterpolation(float t) {
2150 t -= 1.0f;
2151 return t*t*t*t*t + 1;
2152 }
2153 }
2154
2155 // We want the duration of the page snap animation to be influenced by the distance that
2156 // the screen has to travel, however, we don't want this duration to be effected in a
2157 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2158 // of travel has on the overall snap duration.
2159 float distanceInfluenceForSnapDuration(float f) {
2160 f -= 0.5f; // center the values about 0.
2161 f *= 0.3f * Math.PI / 2.0f;
2162 return (float) Math.sin(f);
2163 }
2164
Michael Jurka0142d492010-08-25 17:46:15 -07002165 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002166 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002167 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002168
Adam Cohenedb40762013-07-18 16:45:45 -07002169 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002170 int delta = newX - mUnboundedScrollX;
2171 int duration = 0;
2172
Adam Cohen265b9a62011-12-07 14:37:18 -08002173 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002174 // If the velocity is low enough, then treat this more as an automatic page advance
2175 // as opposed to an apparent physical response to flinging
2176 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2177 return;
2178 }
2179
2180 // Here we compute a "distance" that will be used in the computation of the overall
2181 // snap duration. This is a function of the actual distance that needs to be traveled;
2182 // we keep this value close to half screen size in order to reduce the variance in snap
2183 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002184 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002185 float distance = halfScreenSize + halfScreenSize *
2186 distanceInfluenceForSnapDuration(distanceRatio);
2187
2188 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002189 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002190
2191 // we want the page's snap velocity to approximately match the velocity at which the
2192 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002193 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2194 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002195
2196 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002197 }
2198
2199 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002200 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002201 }
2202
Adam Cohen7d30a372013-07-01 17:03:59 -07002203 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002204 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002205 }
2206
Michael Jurka0142d492010-08-25 17:46:15 -07002207 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002208 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002209 }
2210
Adam Cohenf9618852013-11-08 06:45:03 -08002211 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2212 snapToPage(whichPage, duration, false, interpolator);
2213 }
2214
2215 protected void snapToPage(int whichPage, int duration, boolean immediate,
2216 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002217 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002218
Adam Cohenedb40762013-07-18 16:45:45 -07002219 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002220 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002221 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002222 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002223 }
2224
2225 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002226 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002227 }
Michael Jurka0142d492010-08-25 17:46:15 -07002228
Adam Cohenf9618852013-11-08 06:45:03 -08002229 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2230 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002231 whichPage = validateNewPage(whichPage);
2232
Adam Cohen7d30a372013-07-01 17:03:59 -07002233 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002234 View focusedChild = getFocusedChild();
2235 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002236 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002237 focusedChild.clearFocus();
2238 }
2239
Adam Cohen53805212013-10-01 10:39:23 -07002240 sendScrollAccessibilityEvent();
2241
Michael Jurka0142d492010-08-25 17:46:15 -07002242 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002243 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002244 if (immediate) {
2245 duration = 0;
2246 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002247 duration = Math.abs(delta);
2248 }
2249
Adam Cohenf358a4b2013-07-23 16:47:31 -07002250 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002251 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002252 }
Adam Cohenf9618852013-11-08 06:45:03 -08002253
2254 if (interpolator != null) {
2255 mScroller.setInterpolator(interpolator);
2256 } else {
2257 mScroller.setInterpolator(mDefaultInterpolator);
2258 }
2259
Adam Cohen68d73932010-11-15 10:50:58 -08002260 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002261
Adam Cohen674531f2013-12-13 15:07:14 -08002262 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002263
2264 // Trigger a compute() to finish switching pages if necessary
2265 if (immediate) {
2266 computeScroll();
2267 }
2268
Winson Chung9c0565f2013-07-19 13:49:06 -07002269 // Defer loading associated pages until the scroll settles
2270 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2271
Adam Cohen7d30a372013-07-01 17:03:59 -07002272 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002273 invalidate();
2274 }
2275
Winson Chung321e9ee2010-08-09 13:37:56 -07002276 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002277 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002278 }
2279
2280 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002281 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002282 }
2283
Winson Chung86f77532010-08-24 11:08:22 -07002284 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002285 int result = -1;
2286 if (v != null) {
2287 ViewParent vp = v.getParent();
2288 int count = getChildCount();
2289 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002290 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002291 return i;
2292 }
2293 }
2294 }
2295 return result;
2296 }
2297
2298 /**
2299 * @return True is long presses are still allowed for the current touch
2300 */
2301 public boolean allowLongPress() {
2302 return mAllowLongPress;
2303 }
2304
Adam Cohendbdff6b2013-09-18 19:09:15 -07002305 @Override
2306 public boolean performLongClick() {
2307 mCancelTap = true;
2308 return super.performLongClick();
2309 }
2310
Michael Jurka0142d492010-08-25 17:46:15 -07002311 /**
2312 * Set true to allow long-press events to be triggered, usually checked by
2313 * {@link Launcher} to accept or block dpad-initiated long-presses.
2314 */
2315 public void setAllowLongPress(boolean allowLongPress) {
2316 mAllowLongPress = allowLongPress;
2317 }
2318
Winson Chung321e9ee2010-08-09 13:37:56 -07002319 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002320 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002321
2322 SavedState(Parcelable superState) {
2323 super(superState);
2324 }
2325
2326 private SavedState(Parcel in) {
2327 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002328 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002329 }
2330
2331 @Override
2332 public void writeToParcel(Parcel out, int flags) {
2333 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002334 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002335 }
2336
2337 public static final Parcelable.Creator<SavedState> CREATOR =
2338 new Parcelable.Creator<SavedState>() {
2339 public SavedState createFromParcel(Parcel in) {
2340 return new SavedState(in);
2341 }
2342
2343 public SavedState[] newArray(int size) {
2344 return new SavedState[size];
2345 }
2346 };
2347 }
2348
Winson Chungf314b0e2011-08-16 11:54:27 -07002349 protected void loadAssociatedPages(int page) {
2350 loadAssociatedPages(page, false);
2351 }
2352 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002353 if (mContentIsRefreshable) {
2354 final int count = getChildCount();
2355 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002356 int lowerPageBound = getAssociatedLowerPageBound(page);
2357 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002358 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2359 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002360 // First, clear any pages that should no longer be loaded
2361 for (int i = 0; i < count; ++i) {
2362 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002363 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002364 if (layout.getPageChildCount() > 0) {
2365 layout.removeAllViewsOnPage();
2366 }
2367 mDirtyPageContent.set(i, true);
2368 }
2369 }
2370 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002371 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002372 if ((i != page) && immediateAndOnly) {
2373 continue;
2374 }
Michael Jurka0142d492010-08-25 17:46:15 -07002375 if (lowerPageBound <= i && i <= upperPageBound) {
2376 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002377 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002378 mDirtyPageContent.set(i, false);
2379 }
Winson Chung86f77532010-08-24 11:08:22 -07002380 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002381 }
2382 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002383 }
2384 }
2385
Winson Chunge3193b92010-09-10 11:44:42 -07002386 protected int getAssociatedLowerPageBound(int page) {
2387 return Math.max(0, page - 1);
2388 }
2389 protected int getAssociatedUpperPageBound(int page) {
2390 final int count = getChildCount();
2391 return Math.min(page + 1, count - 1);
2392 }
2393
Winson Chung86f77532010-08-24 11:08:22 -07002394 /**
2395 * This method is called ONLY to synchronize the number of pages that the paged view has.
2396 * To actually fill the pages with information, implement syncPageItems() below. It is
2397 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2398 * and therefore, individual page items do not need to be updated in this method.
2399 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002400 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002401
2402 /**
2403 * This method is called to synchronize the items that are on a particular page. If views on
2404 * the page can be reused, then they should be updated within this method.
2405 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002406 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002407
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002408 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002409 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002410 }
2411 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002412 invalidatePageData(currentPage, false);
2413 }
2414 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002415 if (!mIsDataReady) {
2416 return;
2417 }
2418
Michael Jurka0142d492010-08-25 17:46:15 -07002419 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002420 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002421 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002422
Michael Jurka0142d492010-08-25 17:46:15 -07002423 // Update all the pages
2424 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002425
Winson Chung5a808352011-06-27 19:08:49 -07002426 // We must force a measure after we've loaded the pages to update the content width and
2427 // to determine the full scroll width
2428 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2429 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2430
2431 // Set a new page as the current page if necessary
2432 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002433 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002434 }
2435
Michael Jurka0142d492010-08-25 17:46:15 -07002436 // Mark each of the pages as dirty
2437 final int count = getChildCount();
2438 mDirtyPageContent.clear();
2439 for (int i = 0; i < count; ++i) {
2440 mDirtyPageContent.add(true);
2441 }
2442
2443 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002444 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002445 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002446 }
Adam Cohen06559042013-09-29 18:30:56 -07002447 if (isPageMoving()) {
2448 // If the page is moving, then snap it to the final position to ensure we don't get
2449 // stuck between pages
2450 snapToDestination();
2451 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002452 }
Winson Chung007c6982011-06-14 13:27:53 -07002453
Adam Cohen7d30a372013-07-01 17:03:59 -07002454 // Animate the drag view back to the original position
2455 void animateDragViewToOriginalPosition() {
2456 if (mDragView != null) {
2457 AnimatorSet anim = new AnimatorSet();
2458 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2459 anim.playTogether(
2460 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002461 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2462 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2463 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002464 anim.addListener(new AnimatorListenerAdapter() {
2465 @Override
2466 public void onAnimationEnd(Animator animation) {
2467 onPostReorderingAnimationCompleted();
2468 }
2469 });
2470 anim.start();
2471 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002472 }
2473
Adam Cohen7d30a372013-07-01 17:03:59 -07002474 protected void onStartReordering() {
2475 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2476 mTouchState = TOUCH_STATE_REORDERING;
2477 mIsReordering = true;
2478
Adam Cohen7d30a372013-07-01 17:03:59 -07002479 // We must invalidate to trigger a redraw to update the layers such that the drag view
2480 // is always drawn on top
2481 invalidate();
2482 }
2483
2484 private void onPostReorderingAnimationCompleted() {
2485 // Trigger the callback when reordering has settled
2486 --mPostReorderingPreZoomInRemainingAnimationCount;
2487 if (mPostReorderingPreZoomInRunnable != null &&
2488 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2489 mPostReorderingPreZoomInRunnable.run();
2490 mPostReorderingPreZoomInRunnable = null;
2491 }
2492 }
2493
2494 protected void onEndReordering() {
2495 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002496 }
2497
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002499 int dragViewIndex = indexOfChild(v);
2500
Adam Cohen327acfe2014-06-06 11:52:52 -07002501 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002502
Adam Cohen7d30a372013-07-01 17:03:59 -07002503 mTempVisiblePagesRange[0] = 0;
2504 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002505 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002506 mReorderingStarted = true;
2507
2508 // Check if we are within the reordering range
2509 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002510 dragViewIndex <= mTempVisiblePagesRange[1]) {
2511 // Find the drag view under the pointer
2512 mDragView = getChildAt(dragViewIndex);
2513 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2514 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002515 snapToPage(getPageNearestToCenterOfScreen());
2516 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002517 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002518 return true;
2519 }
2520 return false;
2521 }
2522
2523 boolean isReordering(boolean testTouchState) {
2524 boolean state = mIsReordering;
2525 if (testTouchState) {
2526 state &= (mTouchState == TOUCH_STATE_REORDERING);
2527 }
2528 return state;
2529 }
2530 void endReordering() {
2531 // For simplicity, we call endReordering sometimes even if reordering was never started.
2532 // In that case, we don't want to do anything.
2533 if (!mReorderingStarted) return;
2534 mReorderingStarted = false;
2535
2536 // If we haven't flung-to-delete the current child, then we just animate the drag view
2537 // back into position
2538 final Runnable onCompleteRunnable = new Runnable() {
2539 @Override
2540 public void run() {
2541 onEndReordering();
2542 }
2543 };
2544 if (!mDeferringForDelete) {
2545 mPostReorderingPreZoomInRunnable = new Runnable() {
2546 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002547 onCompleteRunnable.run();
2548 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002549 };
2550 };
2551
2552 mPostReorderingPreZoomInRemainingAnimationCount =
2553 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2554 // Snap to the current page
2555 snapToPage(indexOfChild(mDragView), 0);
2556 // Animate the drag view back to the front position
2557 animateDragViewToOriginalPosition();
2558 } else {
2559 // Handled in post-delete-animation-callbacks
2560 }
2561 }
2562
Adam Cohen7d30a372013-07-01 17:03:59 -07002563 /*
2564 * Flinging to delete - IN PROGRESS
2565 */
2566 private PointF isFlingingToDelete() {
2567 ViewConfiguration config = ViewConfiguration.get(getContext());
2568 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2569
2570 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2571 // Do a quick dot product test to ensure that we are flinging upwards
2572 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2573 mVelocityTracker.getYVelocity());
2574 PointF upVec = new PointF(0f, -1f);
2575 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2576 (vel.length() * upVec.length()));
2577 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2578 return vel;
2579 }
2580 }
2581 return null;
2582 }
2583
2584 /**
2585 * Creates an animation from the current drag view along its current velocity vector.
2586 * For this animation, the alpha runs for a fixed duration and we update the position
2587 * progressively.
2588 */
2589 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2590 private View mDragView;
2591 private PointF mVelocity;
2592 private Rect mFrom;
2593 private long mPrevTime;
2594 private float mFriction;
2595
2596 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2597
2598 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2599 long startTime, float friction) {
2600 mDragView = dragView;
2601 mVelocity = vel;
2602 mFrom = from;
2603 mPrevTime = startTime;
2604 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2605 }
2606
2607 @Override
2608 public void onAnimationUpdate(ValueAnimator animation) {
2609 float t = ((Float) animation.getAnimatedValue()).floatValue();
2610 long curTime = AnimationUtils.currentAnimationTimeMillis();
2611
2612 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2613 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2614
2615 mDragView.setTranslationX(mFrom.left);
2616 mDragView.setTranslationY(mFrom.top);
2617 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2618
2619 mVelocity.x *= mFriction;
2620 mVelocity.y *= mFriction;
2621 mPrevTime = curTime;
2622 }
2623 };
2624
2625 private static final int ANIM_TAG_KEY = 100;
2626
2627 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2628 return new Runnable() {
2629 @Override
2630 public void run() {
2631 int dragViewIndex = indexOfChild(dragView);
2632
2633 // For each of the pages around the drag view, animate them from the previous
2634 // position to the new position in the layout (as a result of the drag view moving
2635 // in the layout)
2636 // NOTE: We can make an assumption here because we have side-bound pages that we
2637 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002638 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002639 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2640 boolean slideFromLeft = (isLastWidgetPage ||
2641 dragViewIndex > mTempVisiblePagesRange[0]);
2642
2643 // Setup the scroll to the correct page before we swap the views
2644 if (slideFromLeft) {
2645 snapToPageImmediately(dragViewIndex - 1);
2646 }
2647
2648 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2649 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2650 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2651 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2652 ArrayList<Animator> animations = new ArrayList<Animator>();
2653 for (int i = lowerIndex; i <= upperIndex; ++i) {
2654 View v = getChildAt(i);
2655 // dragViewIndex < pageUnderPointIndex, so after we remove the
2656 // drag view all subsequent views to pageUnderPointIndex will
2657 // shift down.
2658 int oldX = 0;
2659 int newX = 0;
2660 if (slideFromLeft) {
2661 if (i == 0) {
2662 // Simulate the page being offscreen with the page spacing
2663 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002664 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002665 } else {
2666 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2667 }
2668 newX = getViewportOffsetX() + getChildOffset(i);
2669 } else {
2670 oldX = getChildOffset(i) - getChildOffset(i - 1);
2671 newX = 0;
2672 }
2673
2674 // Animate the view translation from its old position to its new
2675 // position
2676 AnimatorSet anim = (AnimatorSet) v.getTag();
2677 if (anim != null) {
2678 anim.cancel();
2679 }
2680
2681 // Note: Hacky, but we want to skip any optimizations to not draw completely
2682 // hidden views
2683 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2684 v.setTranslationX(oldX - newX);
2685 anim = new AnimatorSet();
2686 anim.playTogether(
2687 ObjectAnimator.ofFloat(v, "translationX", 0f),
2688 ObjectAnimator.ofFloat(v, "alpha", 1f));
2689 animations.add(anim);
2690 v.setTag(ANIM_TAG_KEY, anim);
2691 }
2692
2693 AnimatorSet slideAnimations = new AnimatorSet();
2694 slideAnimations.playTogether(animations);
2695 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2696 slideAnimations.addListener(new AnimatorListenerAdapter() {
2697 @Override
2698 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002699 mDeferringForDelete = false;
2700 onEndReordering();
2701 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002702 }
2703 });
2704 slideAnimations.start();
2705
2706 removeView(dragView);
2707 onRemoveView(dragView, true);
2708 }
2709 };
2710 }
2711
2712 public void onFlingToDelete(PointF vel) {
2713 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2714
2715 // NOTE: Because it takes time for the first frame of animation to actually be
2716 // called and we expect the animation to be a continuation of the fling, we have
2717 // to account for the time that has elapsed since the fling finished. And since
2718 // we don't have a startDelay, we will always get call to update when we call
2719 // start() (which we want to ignore).
2720 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2721 private int mCount = -1;
2722 private long mStartTime;
2723 private float mOffset;
2724 /* Anonymous inner class ctor */ {
2725 mStartTime = startTime;
2726 }
2727
2728 @Override
2729 public float getInterpolation(float t) {
2730 if (mCount < 0) {
2731 mCount++;
2732 } else if (mCount == 0) {
2733 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2734 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2735 mCount++;
2736 }
2737 return Math.min(1f, mOffset + t);
2738 }
2739 };
2740
2741 final Rect from = new Rect();
2742 final View dragView = mDragView;
2743 from.left = (int) dragView.getTranslationX();
2744 from.top = (int) dragView.getTranslationY();
2745 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2746 from, startTime, FLING_TO_DELETE_FRICTION);
2747
2748 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2749
2750 // Create and start the animation
2751 ValueAnimator mDropAnim = new ValueAnimator();
2752 mDropAnim.setInterpolator(tInterpolator);
2753 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2754 mDropAnim.setFloatValues(0f, 1f);
2755 mDropAnim.addUpdateListener(updateCb);
2756 mDropAnim.addListener(new AnimatorListenerAdapter() {
2757 public void onAnimationEnd(Animator animation) {
2758 onAnimationEndRunnable.run();
2759 }
2760 });
2761 mDropAnim.start();
2762 mDeferringForDelete = true;
2763 }
2764
2765 /* Drag to delete */
2766 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2767 if (mDeleteDropTarget != null) {
2768 mAltTmpRect.set(0, 0, 0, 0);
2769 View parent = (View) mDeleteDropTarget.getParent();
2770 if (parent != null) {
2771 parent.getGlobalVisibleRect(mAltTmpRect);
2772 }
2773 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2774 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2775 return mTmpRect.contains(x, y);
2776 }
2777 return false;
2778 }
2779
2780 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2781
2782 private void onDropToDelete() {
2783 final View dragView = mDragView;
2784
2785 final float toScale = 0f;
2786 final float toAlpha = 0f;
2787
2788 // Create and start the complex animation
2789 ArrayList<Animator> animations = new ArrayList<Animator>();
2790 AnimatorSet motionAnim = new AnimatorSet();
2791 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2792 motionAnim.playTogether(
2793 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2794 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2795 animations.add(motionAnim);
2796
2797 AnimatorSet alphaAnim = new AnimatorSet();
2798 alphaAnim.setInterpolator(new LinearInterpolator());
2799 alphaAnim.playTogether(
2800 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2801 animations.add(alphaAnim);
2802
2803 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2804
2805 AnimatorSet anim = new AnimatorSet();
2806 anim.playTogether(animations);
2807 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2808 anim.addListener(new AnimatorListenerAdapter() {
2809 public void onAnimationEnd(Animator animation) {
2810 onAnimationEndRunnable.run();
2811 }
2812 });
2813 anim.start();
2814
2815 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002816 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002817
2818 /* Accessibility */
2819 @Override
2820 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2821 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002822 info.setScrollable(getPageCount() > 1);
2823 if (getCurrentPage() < getPageCount() - 1) {
2824 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2825 }
2826 if (getCurrentPage() > 0) {
2827 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2828 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002829 }
2830
2831 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002832 public void sendAccessibilityEvent(int eventType) {
2833 // Don't let the view send real scroll events.
2834 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2835 super.sendAccessibilityEvent(eventType);
2836 }
2837 }
2838
2839 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002840 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2841 super.onInitializeAccessibilityEvent(event);
2842 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002843 }
2844
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002845 @Override
2846 public boolean performAccessibilityAction(int action, Bundle arguments) {
2847 if (super.performAccessibilityAction(action, arguments)) {
2848 return true;
2849 }
2850 switch (action) {
2851 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2852 if (getCurrentPage() < getPageCount() - 1) {
2853 scrollRight();
2854 return true;
2855 }
2856 } break;
2857 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2858 if (getCurrentPage() > 0) {
2859 scrollLeft();
2860 return true;
2861 }
2862 } break;
2863 }
2864 return false;
2865 }
2866
Adam Cohen0ffac432013-07-10 11:19:26 -07002867 protected String getCurrentPageDescription() {
2868 return String.format(getContext().getString(R.string.default_scroll_format),
2869 getNextPage() + 1, getChildCount());
2870 }
2871
Winson Chungd11265e2011-08-30 13:37:23 -07002872 @Override
2873 public boolean onHoverEvent(android.view.MotionEvent event) {
2874 return true;
2875 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002876}