blob: 6e9f27a0addcea1be720ce81c92375142d591a6d [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;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070022import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070023import android.animation.ObjectAnimator;
24import android.animation.TimeInterpolator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070025import android.animation.ValueAnimator;
Adam Cohen7d30a372013-07-01 17:03:59 -070026import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070028import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070030import android.graphics.Matrix;
31import android.graphics.PointF;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
45import android.view.ViewGroup;
46import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070048import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070049import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohen7d30a372013-07-01 17:03:59 -070050import android.view.animation.AnimationUtils;
51import android.view.animation.DecelerateInterpolator;
Adam Cohene0f66b52010-11-23 15:06:07 -080052import android.view.animation.Interpolator;
Adam Cohen7d30a372013-07-01 17:03:59 -070053import android.view.animation.LinearInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Adam Cohen091440a2015-03-18 14:16:05 -070055import com.android.launcher3.util.Thunk;
56
Winson Chung6a0f57d2011-06-29 20:10:49 -070057import java.util.ArrayList;
58
Winson Chungc58497e2013-09-03 17:48:37 -070059interface Page {
60 public int getPageChildCount();
61 public View getChildOnPageAt(int i);
62 public void removeAllViewsOnPage();
63 public void removeViewOnPageAt(int i);
64 public int indexOfChildOnPage(View v);
65}
66
Winson Chung321e9ee2010-08-09 13:37:56 -070067/**
68 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070069 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070070 */
Michael Jurka8b805b12012-04-18 14:23:14 -070071public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070073 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070074 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070075
Winson Chung86f77532010-08-24 11:08:22 -070076 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070077 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070078
Adam Cohen7d30a372013-07-01 17:03:59 -070079 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Adam Cohen1e4359c2014-08-18 13:12:16 -070080 protected static final int OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION = 350;
Winson Chungf0c6ae02012-03-21 16:10:31 -070081 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070082 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070083
Adam Cohenb5ba0972011-09-07 18:02:31 -070084 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Adam Cohen1e4359c2014-08-18 13:12:16 -070085 private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
Winson Chung867ca622012-02-21 15:48:35 -080086
Adam Cohenb64cb5a2011-02-15 13:53:42 -080087 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080088 // The page is moved more than halfway, automatically move to the next page on touch up.
89 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080090
Adam Cohen265b9a62011-12-07 14:37:18 -080091 // The following constants need to be scaled based on density. The scaled versions will be
92 // assigned to the corresponding member variables below.
93 private static final int FLING_THRESHOLD_VELOCITY = 500;
94 private static final int MIN_SNAP_VELOCITY = 1500;
95 private static final int MIN_FLING_VELOCITY = 250;
96
Adam Cohen7d30a372013-07-01 17:03:59 -070097 // We are disabling touch interaction of the widget region for factory ROM.
98 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070099 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -0700100 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700101
Adam Cohen21cd0022013-10-09 18:57:02 -0700102 public static final int INVALID_RESTORE_PAGE = -1001;
103
Adam Cohenf358a4b2013-07-23 16:47:31 -0700104 private boolean mFreeScroll = false;
105 private int mFreeScrollMinScrollX = -1;
106 private int mFreeScrollMaxScrollX = -1;
107
Winson Chung8aad6102012-05-11 16:27:49 -0700108 static final int AUTOMATIC_PAGE_SPACING = -1;
109
Adam Cohen265b9a62011-12-07 14:37:18 -0800110 protected int mFlingThresholdVelocity;
111 protected int mMinFlingVelocity;
112 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700113
Adam Cohenb5ba0972011-09-07 18:02:31 -0700114 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected float mSmoothingTime;
116 protected float mTouchX;
117
118 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700119 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700120
121 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700122 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700123 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800126 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800127 protected LauncherScroller mScroller;
128 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 private float mParentDownMotionX;
133 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700135 private float mDownMotionY;
136 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700137 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800138 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800139 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800140 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800141 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700142 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700143
Adam Cohendbdff6b2013-09-18 19:09:15 -0700144 private boolean mCancelTap;
145
Adam Cohenedb40762013-07-18 16:45:45 -0700146 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka0142d492010-08-25 17:46:15 -0700148 protected final static int TOUCH_STATE_REST = 0;
149 protected final static int TOUCH_STATE_SCROLLING = 1;
150 protected final static int TOUCH_STATE_PREV_PAGE = 2;
151 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700152 protected final static int TOUCH_STATE_REORDERING = 4;
153
Adam Cohene45440e2010-10-14 18:33:38 -0700154 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700155
Michael Jurka0142d492010-08-25 17:46:15 -0700156 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700157 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800158
Michael Jurka0142d492010-08-25 17:46:15 -0700159 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700160
Michael Jurka7426c422010-11-11 15:23:47 -0800161 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 private int mPagingTouchSlop;
163 private int mMaximumVelocity;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700164 protected int mPageLayoutWidthGap;
165 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700166 protected int mCellCountX = 0;
167 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700168 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800169 protected boolean mAllowOverScroll = true;
170 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800171 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700172 protected boolean mForceDrawAllChildrenNextFrame;
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700173 private boolean mSpacePagesAutomatically = false;
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
Adam Cohenc2d6e892014-10-16 09:49:24 -0700210 private boolean mWasInOverscroll = false;
211
Winson Chungd2be3812013-07-16 11:11:32 -0700212 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk int mPageIndicatorViewId;
214 @Thunk PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700215 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700216
Adam Cohen7d30a372013-07-01 17:03:59 -0700217 // The viewport whether the pages are to be contained (the actual view may be larger than the
218 // viewport)
219 private Rect mViewport = new Rect();
220
221 // Reordering
222 // We use the min scale to determine how much to expand the actually PagedView measured
223 // dimensions such that when we are zoomed out, the view is not clipped
224 private int REORDERING_DROP_REPOSITION_DURATION = 200;
225 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
226 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700227 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700228 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700229 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700230 protected View mDragView;
231 protected AnimatorSet mZoomInOutAnim;
232 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700234 // This variable's scope is only for the duration of startReordering() and endReordering()
235 private boolean mReorderingStarted = false;
236 // This variable's scope is for the duration of startReordering() and after the zoomIn()
237 // animation after endReordering()
238 private boolean mIsReordering;
239 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
240 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
241 private int mPostReorderingPreZoomInRemainingAnimationCount;
242 private Runnable mPostReorderingPreZoomInRunnable;
243
Adam Cohen7d30a372013-07-01 17:03:59 -0700244 // Convenience/caching
245 private Matrix mTmpInvMatrix = new Matrix();
246 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700247 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700248 private Rect mTmpRect = new Rect();
249 private Rect mAltTmpRect = new Rect();
250
251 // Fling to delete
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
Adam Cohen7d30a372013-07-01 17:03:59 -0700253 private float FLING_TO_DELETE_FRICTION = 0.035f;
254 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
255 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
256 protected int mFlingToDeleteThresholdVelocity = -1400;
257 // Drag to delete
Adam Cohen091440a2015-03-18 14:16:05 -0700258 @Thunk boolean mDeferringForDelete = false;
259 @Thunk int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
Adam Cohen7d30a372013-07-01 17:03:59 -0700260 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
261
262 // Drop to delete
263 private View mDeleteDropTarget;
264
Adam Cohen7d30a372013-07-01 17:03:59 -0700265 // Bouncer
266 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700267
John Spurlock77e1f472013-09-11 10:09:51 -0400268 protected final Rect mInsets = new Rect();
269
Winson Chung86f77532010-08-24 11:08:22 -0700270 public interface PageSwitchListener {
271 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 }
273
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 public PagedView(Context context) {
275 this(context, null);
276 }
277
278 public PagedView(Context context, AttributeSet attrs) {
279 this(context, attrs, 0);
280 }
281
282 public PagedView(Context context, AttributeSet attrs, int defStyle) {
283 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700284
Adam Cohen9c4949e2010-10-05 12:27:22 -0700285 TypedArray a = context.obtainStyledAttributes(attrs,
286 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700287
Winson Chungdf4b83d2010-10-20 17:49:27 -0700288 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700289 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700290 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700291 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700292 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700293 a.recycle();
294
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700296 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 }
298
299 /**
300 * Initializes various states for this workspace.
301 */
Michael Jurka0142d492010-08-25 17:46:15 -0700302 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700303 mDirtyPageContent = new ArrayList<Boolean>();
304 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800305 mScroller = new LauncherScroller(getContext());
306 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700307 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700308 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700309
310 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700311 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
313 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700314 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800315
Adam Cohen7d30a372013-07-01 17:03:59 -0700316 // Scale the fling-to-delete threshold by the density
317 mFlingToDeleteThresholdVelocity =
318 (int) (mFlingToDeleteThresholdVelocity * mDensity);
319
Adam Cohen265b9a62011-12-07 14:37:18 -0800320 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
321 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
322 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700323 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 }
325
Adam Cohenf9618852013-11-08 06:45:03 -0800326 protected void setDefaultInterpolator(Interpolator interpolator) {
327 mDefaultInterpolator = interpolator;
328 mScroller.setInterpolator(mDefaultInterpolator);
329 }
330
Winson Chungd2be3812013-07-16 11:11:32 -0700331 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700332 super.onAttachedToWindow();
333
Winson Chungd2be3812013-07-16 11:11:32 -0700334 // Hook up the page indicator
335 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700336 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700337 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700338 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700339 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700340
Winson Chung7819a562013-09-19 15:55:45 -0700341 ArrayList<PageIndicator.PageMarkerResources> markers =
342 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700343 for (int i = 0; i < getChildCount(); ++i) {
344 markers.add(getPageIndicatorMarker(i));
345 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700346
Winson Chungc58497e2013-09-03 17:48:37 -0700347 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700348
349 OnClickListener listener = getPageIndicatorClickListener();
350 if (listener != null) {
351 mPageIndicator.setOnClickListener(listener);
352 }
Adam Cohen53805212013-10-01 10:39:23 -0700353 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700354 }
355 }
356
Adam Cohen53805212013-10-01 10:39:23 -0700357 protected String getPageIndicatorDescription() {
358 return getCurrentPageDescription();
359 }
360
361 protected OnClickListener getPageIndicatorClickListener() {
362 return null;
363 }
364
Winson Chungd2be3812013-07-16 11:11:32 -0700365 protected void onDetachedFromWindow() {
366 // Unhook the page indicator
367 mPageIndicator = null;
368 }
369
Adam Cohen7d30a372013-07-01 17:03:59 -0700370 void setDeleteDropTarget(View v) {
371 mDeleteDropTarget = v;
372 }
373
374 // Convenience methods to map points from self to parent and vice versa
375 float[] mapPointFromViewToParent(View v, float x, float y) {
376 mTmpPoint[0] = x;
377 mTmpPoint[1] = y;
378 v.getMatrix().mapPoints(mTmpPoint);
379 mTmpPoint[0] += v.getLeft();
380 mTmpPoint[1] += v.getTop();
381 return mTmpPoint;
382 }
383 float[] mapPointFromParentToView(View v, float x, float y) {
384 mTmpPoint[0] = x - v.getLeft();
385 mTmpPoint[1] = y - v.getTop();
386 v.getMatrix().invert(mTmpInvMatrix);
387 mTmpInvMatrix.mapPoints(mTmpPoint);
388 return mTmpPoint;
389 }
390
391 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700392 if (mDragView != null) {
393 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
394 (mDragViewBaselineLeft - mDragView.getLeft());
395 float y = mLastMotionY - mDownMotionY;
396 mDragView.setTranslationX(x);
397 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700398
Adam Cohenf358a4b2013-07-23 16:47:31 -0700399 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
400 + x + ", " + y);
401 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700402 }
403
404 public void setMinScale(float f) {
405 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700406 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700407 requestLayout();
408 }
409
410 @Override
411 public void setScaleX(float scaleX) {
412 super.setScaleX(scaleX);
413 if (isReordering(true)) {
414 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
415 mLastMotionX = p[0];
416 mLastMotionY = p[1];
417 updateDragViewTranslationDuringDrag();
418 }
419 }
420
421 // Convenience methods to get the actual width/height of the PagedView (since it is measured
422 // to be larger to account for the minimum possible scale)
423 int getViewportWidth() {
424 return mViewport.width();
425 }
426 int getViewportHeight() {
427 return mViewport.height();
428 }
429
430 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
431 // PagedView both horizontally and vertically
432 int getViewportOffsetX() {
433 return (getMeasuredWidth() - getViewportWidth()) / 2;
434 }
435
436 int getViewportOffsetY() {
437 return (getMeasuredHeight() - getViewportHeight()) / 2;
438 }
439
Adam Cohenedb40762013-07-18 16:45:45 -0700440 PageIndicator getPageIndicator() {
441 return mPageIndicator;
442 }
Winson Chung7819a562013-09-19 15:55:45 -0700443 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
444 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700445 }
Adam Cohenedb40762013-07-18 16:45:45 -0700446
Adam Cohen674531f2013-12-13 15:07:14 -0800447 /**
448 * Add a page change listener which will be called when a page is _finished_ listening.
449 *
450 */
Winson Chung86f77532010-08-24 11:08:22 -0700451 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
452 mPageSwitchListener = pageSwitchListener;
453 if (mPageSwitchListener != null) {
454 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 }
456 }
457
458 /**
Winson Chung52aee602013-01-30 12:01:02 -0800459 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
460 */
461 public boolean isLayoutRtl() {
462 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
463 }
464
465 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700466 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
467 * out pages.
468 */
469 protected void setDataIsReady() {
470 mIsDataReady = true;
471 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700472
Winson Chungf0ea4d32011-06-06 14:27:16 -0700473 protected boolean isDataReady() {
474 return mIsDataReady;
475 }
476
477 /**
Winson Chung86f77532010-08-24 11:08:22 -0700478 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 *
Winson Chung86f77532010-08-24 11:08:22 -0700480 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 */
Winson Chung86f77532010-08-24 11:08:22 -0700482 int getCurrentPage() {
483 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700485
Winson Chung360e63f2012-04-27 13:48:05 -0700486 int getNextPage() {
487 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
488 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700489
Winson Chung86f77532010-08-24 11:08:22 -0700490 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 return getChildCount();
492 }
493
Winson Chung86f77532010-08-24 11:08:22 -0700494 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 return getChildAt(index);
496 }
497
Adam Cohenae4f1552011-10-20 00:15:42 -0700498 protected int indexToPage(int index) {
499 return index;
500 }
501
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800503 * Updates the scroll of the current page immediately to its final scroll position. We use this
504 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
505 * the previous tab page.
506 */
507 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700508 // If the current page is invalid, just reset the scroll position to zero
509 int newX = 0;
510 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700511 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700512 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800513 scrollTo(newX, 0);
514 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700515 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800516 }
517
518 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700519 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700520 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
521 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700522 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700523 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700524 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800525 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700526 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700527 }
528
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700529 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700530 mScroller.abortAnimation();
531 // We need to clean up the next page here to avoid computeScrollHelper from
532 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700533 if (resetNextPage) {
534 mNextPage = INVALID_PAGE;
535 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700536 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700537
538 private void forceFinishScroller() {
539 mScroller.forceFinished(true);
540 // We need to clean up the next page here to avoid computeScrollHelper from
541 // updating current page on the pass.
542 mNextPage = INVALID_PAGE;
543 }
544
Adam Cohen6f127a62014-06-12 14:54:41 -0700545 private int validateNewPage(int newPage) {
546 int validatedPage = newPage;
547 // When in free scroll mode, we need to clamp to the free scroll page range.
548 if (mFreeScroll) {
549 getFreeScrollPageRange(mTempVisiblePagesRange);
550 validatedPage = Math.max(mTempVisiblePagesRange[0],
551 Math.min(newPage, mTempVisiblePagesRange[1]));
552 }
553 // Ensure that it is clamped by the actual set of children in all cases
554 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
555 return validatedPage;
556 }
557
Chet Haasebc2f0822012-10-26 17:59:53 -0700558 /**
Winson Chung86f77532010-08-24 11:08:22 -0700559 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 */
Winson Chung86f77532010-08-24 11:08:22 -0700561 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800562 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700563 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800564 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800565 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
566 // the default
567 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800568 return;
569 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700570 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700571 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700572 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700573 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800574 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 }
576
Winson Chung8c87cd82013-07-23 16:20:10 -0700577 /**
578 * The restore page will be set in place of the current page at the next (likely first)
579 * layout.
580 */
581 void setRestorePage(int restorePage) {
582 mRestorePage = restorePage;
583 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800584 int getRestorePage() {
585 return mRestorePage;
586 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700587
Adam Cohen674531f2013-12-13 15:07:14 -0800588 /**
589 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
590 * has settled.
591 */
Michael Jurka0142d492010-08-25 17:46:15 -0700592 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700593 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800594 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 }
Winson Chungd2be3812013-07-16 11:11:32 -0700596
Adam Cohen674531f2013-12-13 15:07:14 -0800597 updatePageIndicator();
598 }
599
600 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700601 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700602 if (mPageIndicator != null) {
603 mPageIndicator.setContentDescription(getPageIndicatorDescription());
604 if (!isReordering(false)) {
605 mPageIndicator.setActiveMarker(getNextPage());
606 }
Winson Chungd2be3812013-07-16 11:11:32 -0700607 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800609 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700610 if (!mIsPageMoving) {
611 mIsPageMoving = true;
612 onPageBeginMoving();
613 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700614 }
615
Michael Jurkace7e05f2011-02-01 22:02:35 -0800616 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700617 if (mIsPageMoving) {
618 mIsPageMoving = false;
619 onPageEndMoving();
620 }
Michael Jurka0142d492010-08-25 17:46:15 -0700621 }
622
Adam Cohen26976d92011-03-22 15:33:33 -0700623 protected boolean isPageMoving() {
624 return mIsPageMoving;
625 }
626
Michael Jurka0142d492010-08-25 17:46:15 -0700627 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700628 protected void onPageBeginMoving() {
629 }
630
631 // a method that subclasses can override to add behavior
632 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700633 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700634 }
635
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 /**
Winson Chung86f77532010-08-24 11:08:22 -0700637 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 *
639 * @param l The listener used to respond to long clicks.
640 */
641 @Override
642 public void setOnLongClickListener(OnLongClickListener l) {
643 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700644 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700646 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 }
Adam Cohen1697b792013-09-17 19:08:21 -0700648 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
650
651 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800652 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700653 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800654 }
655
656 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700657 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700658 // In free scroll mode, we clamp the scrollX
659 if (mFreeScroll) {
660 x = Math.min(x, mFreeScrollMaxScrollX);
661 x = Math.max(x, mFreeScrollMinScrollX);
662 }
663
Adam Cohen0ffac432013-07-10 11:19:26 -0700664 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800665 mUnboundedScrollX = x;
666
Adam Cohen0ffac432013-07-10 11:19:26 -0700667 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
668 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
669 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800670 super.scrollTo(0, y);
671 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700672 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700673 if (isRtl) {
674 overScroll(x - mMaxScrollX);
675 } else {
676 overScroll(x);
677 }
Adam Cohen68d73932010-11-15 10:50:58 -0800678 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700679 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800680 super.scrollTo(mMaxScrollX, y);
681 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700682 mWasInOverscroll = true;
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 Cohenc2d6e892014-10-16 09:49:24 -0700690 if (mWasInOverscroll) {
691 overScroll(0);
692 mWasInOverscroll = false;
693 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800694 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800695 super.scrollTo(x, y);
696 }
697
Michael Jurka0142d492010-08-25 17:46:15 -0700698 mTouchX = x;
699 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700700
701 // Update the last motion events when scrolling
702 if (isReordering(true)) {
703 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
704 mLastMotionX = p[0];
705 mLastMotionY = p[1];
706 updateDragViewTranslationDuringDrag();
707 }
Michael Jurka0142d492010-08-25 17:46:15 -0700708 }
709
Adam Cohen53805212013-10-01 10:39:23 -0700710 private void sendScrollAccessibilityEvent() {
711 AccessibilityManager am =
712 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
713 if (am.isEnabled()) {
714 AccessibilityEvent ev =
715 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700716 ev.setItemCount(getChildCount());
717 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700718 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700719
Alan Viverette254139a2013-10-08 13:13:46 -0700720 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700721 if (getNextPage() >= mCurrentPage) {
722 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
723 } else {
724 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
725 }
726
727 ev.setAction(action);
728 sendAccessibilityEventUnchecked(ev);
729 }
730 }
731
Michael Jurka0142d492010-08-25 17:46:15 -0700732 // we moved this functionality to a helper function so SmoothPagedView can reuse it
733 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700735 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 if (getScrollX() != mScroller.getCurrX()
737 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700738 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700739 float scaleX = mFreeScroll ? getScaleX() : 1f;
740 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
741 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700742 }
Michael Jurka0142d492010-08-25 17:46:15 -0700743 invalidate();
744 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700745 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700746 sendScrollAccessibilityEvent();
747
Adam Cohen6f127a62014-06-12 14:54:41 -0700748 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700749 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700750 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700751
Winson Chung9c0565f2013-07-19 13:49:06 -0700752 // Load the associated pages if necessary
753 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
754 loadAssociatedPages(mCurrentPage);
755 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
756 }
757
Adam Cohen73aa9752010-11-24 16:26:58 -0800758 // We don't want to trigger a page end moving unless the page has settled
759 // and the user has stopped scrolling
760 if (mTouchState == TOUCH_STATE_REST) {
761 pageEndMoving();
762 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700763
Adam Cohen7d30a372013-07-01 17:03:59 -0700764 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700765 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700766 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700767 if (am.isEnabled()) {
768 // Notify the user when the page changes
769 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700770 }
Michael Jurka0142d492010-08-25 17:46:15 -0700771 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 }
Michael Jurka0142d492010-08-25 17:46:15 -0700773 return false;
774 }
775
776 @Override
777 public void computeScroll() {
778 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 }
780
Adam Cohen7d30a372013-07-01 17:03:59 -0700781 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
782 return mTopAlignPageWhenShrinkingForBouncer;
783 }
784
Adam Cohen96d30a12013-07-16 18:13:21 -0700785 public static class LayoutParams extends ViewGroup.LayoutParams {
786 public boolean isFullScreenPage = false;
787
788 /**
789 * {@inheritDoc}
790 */
791 public LayoutParams(int width, int height) {
792 super(width, height);
793 }
794
795 public LayoutParams(ViewGroup.LayoutParams source) {
796 super(source);
797 }
798 }
799
800 protected LayoutParams generateDefaultLayoutParams() {
801 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
802 }
803
Adam Cohenedb40762013-07-18 16:45:45 -0700804 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700805 LayoutParams lp = generateDefaultLayoutParams();
806 lp.isFullScreenPage = true;
807 super.addView(page, 0, lp);
808 }
809
Adam Cohen410f3cd2013-09-22 12:09:32 -0700810 public int getNormalChildHeight() {
811 return mNormalChildHeight;
812 }
813
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 @Override
815 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700817 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
818 return;
819 }
820
Adam Cohen7d30a372013-07-01 17:03:59 -0700821 // We measure the dimensions of the PagedView to be larger than the pages so that when we
822 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700823 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
824 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
825 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700826 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800827 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700828 // viewport, we can be at most one and a half screens offset once we scale down
829 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700830 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
831 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700832
Winson Chungc82d2622013-11-06 13:23:29 -0800833 int parentWidthSize = (int) (2f * maxSize);
834 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700835 int scaledWidthSize, scaledHeightSize;
836 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700837 scaledWidthSize = (int) (parentWidthSize / mMinScale);
838 scaledHeightSize = (int) (parentHeightSize / mMinScale);
839 } else {
840 scaledWidthSize = widthSize;
841 scaledHeightSize = heightSize;
842 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700843 mViewport.set(0, 0, widthSize, heightSize);
844
845 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
846 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
847 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 }
849
Winson Chung8aad6102012-05-11 16:27:49 -0700850 // Return early if we aren't given a proper dimension
851 if (widthSize <= 0 || heightSize <= 0) {
852 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
853 return;
854 }
855
Adam Lesinski6b879f02010-11-04 16:15:23 -0700856 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
857 * of the All apps view on XLarge displays to not take up more space then it needs. Width
858 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
859 * each page to have the same width.
860 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700861 final int verticalPadding = getPaddingTop() + getPaddingBottom();
862 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700863
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700864 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700866 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700867 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700868 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
869 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
870 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
871 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 final int childCount = getChildCount();
873 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700874 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700875 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100876 if (child.getVisibility() != GONE) {
877 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878
Vladimir Marko2824b072013-10-04 16:42:17 +0100879 int childWidthMode;
880 int childHeightMode;
881 int childWidth;
882 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700883
Vladimir Marko2824b072013-10-04 16:42:17 +0100884 if (!lp.isFullScreenPage) {
885 if (lp.width == LayoutParams.WRAP_CONTENT) {
886 childWidthMode = MeasureSpec.AT_MOST;
887 } else {
888 childWidthMode = MeasureSpec.EXACTLY;
889 }
890
891 if (lp.height == LayoutParams.WRAP_CONTENT) {
892 childHeightMode = MeasureSpec.AT_MOST;
893 } else {
894 childHeightMode = MeasureSpec.EXACTLY;
895 }
896
Adam Cohen3b185e22013-10-29 14:45:58 -0700897 childWidth = getViewportWidth() - horizontalPadding
898 - mInsets.left - mInsets.right;
899 childHeight = getViewportHeight() - verticalPadding
900 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100901 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700902 } else {
903 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700904 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100905
Adam Cohen3b185e22013-10-29 14:45:58 -0700906 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
907 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700908 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700909 if (referenceChildWidth == 0) {
910 referenceChildWidth = childWidth;
911 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700912
Vladimir Marko2824b072013-10-04 16:42:17 +0100913 final int childWidthMeasureSpec =
914 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
915 final int childHeightMeasureSpec =
916 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
917 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700918 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700919 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700920 if (mSpacePagesAutomatically) {
921 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
922 - referenceChildWidth) / 2;
923 if (spacing >= 0) {
924 setPageSpacing(spacing);
925 }
926 mSpacePagesAutomatically = false;
927 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700928 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800929 }
930
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700931 /**
932 * This method should be called once before first layout / measure pass.
933 */
934 protected void setSinglePageInViewport() {
935 mSpacePagesAutomatically = true;
936 }
937
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700939 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700941 return;
942 }
943
Winson Chung785d2eb2011-04-14 16:08:02 -0700944 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700946
Adam Cohen7d30a372013-07-01 17:03:59 -0700947 int offsetX = getViewportOffsetX();
948 int offsetY = getViewportOffsetY();
949
950 // Update the viewport offsets
951 mViewport.offset(offsetX, offsetY);
952
Adam Cohen0ffac432013-07-10 11:19:26 -0700953 final boolean isRtl = isLayoutRtl();
954
955 final int startIndex = isRtl ? childCount - 1 : 0;
956 final int endIndex = isRtl ? -1 : childCount;
957 final int delta = isRtl ? -1 : 1;
958
Adam Cohen7d30a372013-07-01 17:03:59 -0700959 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700960
Adam Cohen3b185e22013-10-29 14:45:58 -0700961 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000962 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700963
964 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700965 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
966 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700967 }
968
Adam Cohen0ffac432013-07-10 11:19:26 -0700969 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700970 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700972 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100973 int childTop;
974 if (lp.isFullScreenPage) {
975 childTop = offsetY;
976 } else {
977 childTop = offsetY + getPaddingTop() + mInsets.top;
978 if (mCenterPagesVertically) {
979 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
980 }
981 }
982
Adam Cohen96d30a12013-07-16 18:13:21 -0700983 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700984 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800985
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700987 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800988 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700989
Adam Cohen3b185e22013-10-29 14:45:58 -0700990 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
991 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000992
993 int pageGap = mPageSpacing;
994 int next = i + delta;
995 if (next != endIndex) {
996 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
997 } else {
998 nextLp = null;
999 }
1000
1001 // Prevent full screen pages from showing in the viewport
1002 // when they are not the current page.
1003 if (lp.isFullScreenPage) {
1004 pageGap = getPaddingLeft();
1005 } else if (nextLp != null && nextLp.isFullScreenPage) {
1006 pageGap = getPaddingRight();
1007 }
1008
1009 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 }
1011 }
Winson Chungc3665fa2011-09-14 17:56:27 -07001012
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001013 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -08001014 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001015 mFirstLayout = false;
1016 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001017
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001018 final LayoutTransition transition = getLayoutTransition();
1019 // If the transition is running defer updating max scroll, as some empty pages could
1020 // still be present, and a max scroll change could cause sudden jumps in scroll.
1021 if (transition != null && transition.isRunning()) {
1022 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
1023
1024 @Override
1025 public void startTransition(LayoutTransition transition, ViewGroup container,
1026 View view, int transitionType) { }
1027
1028 @Override
1029 public void endTransition(LayoutTransition transition, ViewGroup container,
1030 View view, int transitionType) {
1031 // Wait until all transitions are complete.
1032 if (!transition.isRunning()) {
1033 transition.removeTransitionListener(this);
1034 updateMaxScrollX();
1035 }
1036 }
1037 });
Adam Cohenf698c6e2013-07-17 18:44:25 -07001038 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001039 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -07001040 }
1041
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001042 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount &&
Adam Cohenf698c6e2013-07-17 18:44:25 -07001043 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001044 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001045 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001046 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001047 } else {
1048 setCurrentPage(getNextPage());
1049 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001050 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001051 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001052
1053 if (isReordering(true)) {
1054 updateDragViewTranslationDuringDrag();
1055 }
Winson Chunge3193b92010-09-10 11:44:42 -07001056 }
1057
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001058 private void updateMaxScrollX() {
1059 int childCount = getChildCount();
1060 if (childCount > 0) {
1061 final int index = isLayoutRtl() ? 0 : childCount - 1;
1062 mMaxScrollX = getScrollForPage(index);
1063 } else {
1064 mMaxScrollX = 0;
1065 }
1066 }
1067
Adam Cohen3b185e22013-10-29 14:45:58 -07001068 public void setPageSpacing(int pageSpacing) {
1069 mPageSpacing = pageSpacing;
1070 requestLayout();
1071 }
1072
Adam Cohenf34bab52010-09-30 14:11:56 -07001073 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001074 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1075
1076 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001077 for (int i = 0; i < getChildCount(); i++) {
1078 View child = getChildAt(i);
1079 if (child != null) {
1080 float scrollProgress = getScrollProgress(screenCenter, child, i);
1081 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001082 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001083 }
1084 }
1085 invalidate();
1086 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001087 }
1088
Winson Chungc58497e2013-09-03 17:48:37 -07001089 protected void enablePagedViewAnimations() {
1090 mAllowPagedViewAnimations = true;
1091
1092 }
1093 protected void disablePagedViewAnimations() {
1094 mAllowPagedViewAnimations = false;
1095 }
1096
Winson Chunge3193b92010-09-10 11:44:42 -07001097 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001098 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001099 // Update the page indicator, we don't update the page indicator as we
1100 // add/remove pages
1101 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001102 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001103 mPageIndicator.addMarker(pageIndex,
1104 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001105 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001106 }
1107
Adam Cohen2591f6a2011-10-25 14:36:40 -07001108 // This ensures that when children are added, they get the correct transforms / alphas
1109 // in accordance with any scroll effects.
1110 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001111 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001112 invalidate();
1113 }
1114
Michael Jurka8b805b12012-04-18 14:23:14 -07001115 @Override
1116 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001117 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001118 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001119 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001120 }
1121
Winson Chungd2be3812013-07-16 11:11:32 -07001122 private void removeMarkerForView(int index) {
1123 // Update the page indicator, we don't update the page indicator as we
1124 // add/remove pages
1125 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001126 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001127 }
1128 }
1129
1130 @Override
1131 public void removeView(View v) {
1132 // XXX: We should find a better way to hook into this before the view
1133 // gets removed form its parent...
1134 removeMarkerForView(indexOfChild(v));
1135 super.removeView(v);
1136 }
1137 @Override
1138 public void removeViewInLayout(View v) {
1139 // XXX: We should find a better way to hook into this before the view
1140 // gets removed form its parent...
1141 removeMarkerForView(indexOfChild(v));
1142 super.removeViewInLayout(v);
1143 }
1144 @Override
1145 public void removeViewAt(int index) {
1146 // XXX: We should find a better way to hook into this before the view
1147 // gets removed form its parent...
1148 removeViewAt(index);
1149 super.removeViewAt(index);
1150 }
1151 @Override
1152 public void removeAllViewsInLayout() {
1153 // Update the page indicator, we don't update the page indicator as we
1154 // add/remove pages
1155 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001156 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001157 }
1158
1159 super.removeAllViewsInLayout();
1160 }
1161
Adam Cohen73894962011-10-31 13:17:17 -07001162 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001163 if (index < 0 || index > getChildCount() - 1) return 0;
1164
Adam Cohenf698c6e2013-07-17 18:44:25 -07001165 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001166
Adam Cohenf698c6e2013-07-17 18:44:25 -07001167 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001168 }
1169
Adam Cohen6f127a62014-06-12 14:54:41 -07001170 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001171 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001172 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001173 }
1174
Michael Jurkadde558b2011-11-09 22:09:06 -08001175 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001176 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001177 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001178
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001179 range[0] = -1;
1180 range[1] = -1;
1181
Michael Jurkac4fb9172010-09-02 17:19:20 -07001182 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001183 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001184 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001185
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001186 int count = getChildCount();
1187 for (int i = 0; i < count; i++) {
1188 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001189
Winson Chungc9ca2982013-07-19 12:07:38 -07001190 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001191 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001192 if (mTmpIntPoint[0] > viewportWidth) {
1193 if (range[0] == -1) {
1194 continue;
1195 } else {
1196 break;
1197 }
1198 }
1199
Adam Cohen6a678da2013-09-24 10:58:01 -07001200 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001201 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1202 if (mTmpIntPoint[0] < 0) {
1203 if (range[0] == -1) {
1204 continue;
1205 } else {
1206 break;
1207 }
1208 }
1209 curScreen = i;
1210 if (range[0] < 0) {
1211 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001212 }
1213 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001214
1215 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001216 } else {
1217 range[0] = -1;
1218 range[1] = -1;
1219 }
1220 }
1221
Michael Jurka920d7f42012-05-14 16:29:55 -07001222 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001223 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001224 }
1225
Michael Jurkadde558b2011-11-09 22:09:06 -08001226 @Override
1227 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001228 // Find out which screens are visible; as an optimization we only call draw on them
1229 final int pageCount = getChildCount();
1230 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001231 int halfScreenSize = getViewportWidth() / 2;
1232 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1233 // Otherwise it is equal to the scaled overscroll position.
1234 int screenCenter = mOverScrollX + halfScreenSize;
1235
1236 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1237 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1238 // set it for the next frame
1239 mForceScreenScrolled = false;
1240 screenScrolled(screenCenter);
1241 mLastScreenCenter = screenCenter;
1242 }
1243
Michael Jurkadde558b2011-11-09 22:09:06 -08001244 getVisiblePages(mTempVisiblePagesRange);
1245 final int leftScreen = mTempVisiblePagesRange[0];
1246 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001247 if (leftScreen != -1 && rightScreen != -1) {
1248 final long drawingTime = getDrawingTime();
1249 // Clip to the bounds
1250 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001251 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1252 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001253
Adam Cohen7d30a372013-07-01 17:03:59 -07001254 // Draw all the children, leaving the drag view for last
1255 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001256 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001257 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001258 if (mForceDrawAllChildrenNextFrame ||
1259 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001260 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001261 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001262 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001263 // Draw the drag view on top (if there is one)
1264 if (mDragView != null) {
1265 drawChild(canvas, mDragView, drawingTime);
1266 }
1267
Michael Jurka5e368ff2012-05-14 23:13:15 -07001268 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001269 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001270 }
Michael Jurka0142d492010-08-25 17:46:15 -07001271 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273
1274 @Override
1275 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001276 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001277 if (page != mCurrentPage || !mScroller.isFinished()) {
1278 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 return true;
1280 }
1281 return false;
1282 }
1283
1284 @Override
1285 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001286 int focusablePage;
1287 if (mNextPage != INVALID_PAGE) {
1288 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001289 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001290 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 }
Winson Chung86f77532010-08-24 11:08:22 -07001292 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001294 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 }
1296 return false;
1297 }
1298
1299 @Override
1300 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001301 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001303 if (getCurrentPage() > 0) {
1304 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 return true;
1306 }
1307 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001308 if (getCurrentPage() < getPageCount() - 1) {
1309 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 return true;
1311 }
1312 }
1313 return super.dispatchUnhandledMove(focused, direction);
1314 }
1315
1316 @Override
1317 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001318 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001319 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001320 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001321 }
1322 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001323 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001324 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 }
1326 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001327 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001328 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 }
1330 }
1331 }
1332
1333 /**
1334 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001335 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 *
Winson Chung86f77532010-08-24 11:08:22 -07001337 * This happens when live folders requery, and if they're off page, they
1338 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 */
1340 @Override
1341 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001342 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 View v = focused;
1344 while (true) {
1345 if (v == current) {
1346 super.focusableViewAvailable(focused);
1347 return;
1348 }
1349 if (v == this) {
1350 return;
1351 }
1352 ViewParent parent = v.getParent();
1353 if (parent instanceof View) {
1354 v = (View)v.getParent();
1355 } else {
1356 return;
1357 }
1358 }
1359 }
1360
1361 /**
1362 * {@inheritDoc}
1363 */
1364 @Override
1365 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1366 if (disallowIntercept) {
1367 // We need to make sure to cancel our long press if
1368 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001369 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001370 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 }
1372 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1373 }
1374
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001375 /**
1376 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1377 */
1378 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001379 if (isLayoutRtl()) {
1380 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001381 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001382 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001383 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001384 }
1385
1386 /**
1387 * Return true if a tap at (x, y) should trigger a flip to the next page.
1388 */
1389 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001390 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001391 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001392 }
1393 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001394 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 }
Winson Chung52aee602013-01-30 12:01:02 -08001396
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 /** Returns whether x and y originated within the buffered viewport */
1398 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1399 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1400 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1401 return mTmpRect.contains(x, y);
1402 }
1403
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 @Override
1405 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001406 if (DISABLE_TOUCH_INTERACTION) {
1407 return false;
1408 }
1409
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 /*
1411 * This method JUST determines whether we want to intercept the motion.
1412 * If we return true, onTouchEvent will be called and we do the actual
1413 * scrolling there.
1414 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001415 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001416
Winson Chung45e1d6e2010-11-09 17:19:49 -08001417 // Skip touch handling if there are no pages to swipe
1418 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1419
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 /*
1421 * Shortcut the most recurring case: the user is in the dragging
1422 * state and he is moving his finger. We want to intercept this
1423 * motion.
1424 */
1425 final int action = ev.getAction();
1426 if ((action == MotionEvent.ACTION_MOVE) &&
1427 (mTouchState == TOUCH_STATE_SCROLLING)) {
1428 return true;
1429 }
1430
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 switch (action & MotionEvent.ACTION_MASK) {
1432 case MotionEvent.ACTION_MOVE: {
1433 /*
1434 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1435 * whether the user has moved far enough from his original down touch.
1436 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001437 if (mActivePointerId != INVALID_POINTER) {
1438 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001439 }
1440 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1441 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1442 // i.e. fall through to the next case (don't break)
1443 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1444 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001445 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 }
1447
1448 case MotionEvent.ACTION_DOWN: {
1449 final float x = ev.getX();
1450 final float y = ev.getY();
1451 // Remember location of down touch
1452 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001453 mDownMotionY = y;
1454 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001455 mLastMotionX = x;
1456 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001457 float[] p = mapPointFromViewToParent(this, x, y);
1458 mParentDownMotionX = p[0];
1459 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001460 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001461 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001463
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 /*
1465 * If being flinged and user touches the screen, initiate drag;
1466 * otherwise don't. mScroller.isFinished should be false when
1467 * being flinged.
1468 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001469 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001470 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001471
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001472 if (finishedScrolling) {
1473 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001474 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001475 setCurrentPage(getNextPage());
1476 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001477 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001478 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001479 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1480 mTouchState = TOUCH_STATE_SCROLLING;
1481 } else {
1482 mTouchState = TOUCH_STATE_REST;
1483 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001484 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001485
Winson Chung86f77532010-08-24 11:08:22 -07001486 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001488 if (!DISABLE_TOUCH_SIDE_PAGES) {
1489 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1490 if (getChildCount() > 0) {
1491 if (hitsPreviousPage(x, y)) {
1492 mTouchState = TOUCH_STATE_PREV_PAGE;
1493 } else if (hitsNextPage(x, y)) {
1494 mTouchState = TOUCH_STATE_NEXT_PAGE;
1495 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 }
1497 }
1498 }
1499 break;
1500 }
1501
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001503 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001504 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 break;
1506
1507 case MotionEvent.ACTION_POINTER_UP:
1508 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001509 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001510 break;
1511 }
1512
1513 /*
1514 * The only time we want to intercept motion events is if we are in the
1515 * drag mode.
1516 */
1517 return mTouchState != TOUCH_STATE_REST;
1518 }
1519
Adam Cohenf8d28232011-02-01 21:47:00 -08001520 protected void determineScrollingStart(MotionEvent ev) {
1521 determineScrollingStart(ev, 1.0f);
1522 }
1523
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 /*
1525 * Determines if we should change the touch state to start scrolling after the
1526 * user moves their touch point too far.
1527 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001528 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001529 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 if (pointerIndex == -1) return;
1532
1533 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 final float x = ev.getX(pointerIndex);
1535 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001536 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1537
Winson Chung321e9ee2010-08-09 13:37:56 -07001538 final int xDiff = (int) Math.abs(x - mLastMotionX);
1539 final int yDiff = (int) Math.abs(y - mLastMotionY);
1540
Adam Cohenf8d28232011-02-01 21:47:00 -08001541 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 boolean xPaged = xDiff > mPagingTouchSlop;
1543 boolean xMoved = xDiff > touchSlop;
1544 boolean yMoved = yDiff > touchSlop;
1545
Adam Cohenf8d28232011-02-01 21:47:00 -08001546 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001547 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001548 // Scroll if the user moved far enough along the X axis
1549 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001550 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001551 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001552 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001553 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001554 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001555 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001556 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001558 }
1559 }
1560
Adam Cohen7d30a372013-07-01 17:03:59 -07001561 protected float getMaxScrollProgress() {
1562 return 1.0f;
1563 }
1564
Adam Cohenf8d28232011-02-01 21:47:00 -08001565 protected void cancelCurrentPageLongPress() {
1566 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001567 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001568 // Try canceling the long press. It could also have been scheduled
1569 // by a distant descendant, so use the mAllowLongPress flag to block
1570 // everything
1571 final View currentPage = getPageAt(mCurrentPage);
1572 if (currentPage != null) {
1573 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001574 }
1575 }
1576 }
1577
Adam Cohen7d30a372013-07-01 17:03:59 -07001578 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1579 final int halfScreenSize = getViewportWidth() / 2;
1580
1581 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1582 screenCenter = Math.max(halfScreenSize, screenCenter);
1583
1584 return getScrollProgress(screenCenter, v, page);
1585 }
1586
Adam Cohenb5ba0972011-09-07 18:02:31 -07001587 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001588 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001589
Adam Cohenedb40762013-07-18 16:45:45 -07001590 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001591 int count = getChildCount();
1592
1593 final int totalDistance;
1594
1595 int adjacentPage = page + 1;
1596 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1597 adjacentPage = page - 1;
1598 }
1599
1600 if (adjacentPage < 0 || adjacentPage > count - 1) {
1601 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1602 } else {
1603 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1604 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001605
1606 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001607 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1608 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001609 return scrollProgress;
1610 }
1611
Adam Cohenedb40762013-07-18 16:45:45 -07001612 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001613 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001614 return 0;
1615 } else {
1616 return mPageScrolls[index];
1617 }
1618 }
1619
Adam Cohen564a2e72013-10-09 14:47:32 -07001620 // While layout transitions are occurring, a child's position may stray from its baseline
1621 // position. This method returns the magnitude of this stray at any given time.
1622 public int getLayoutTransitionOffsetForPage(int index) {
1623 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1624 return 0;
1625 } else {
1626 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001627
1628 int scrollOffset = 0;
1629 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1630 if (!lp.isFullScreenPage) {
1631 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1632 }
1633
Adam Cohen564a2e72013-10-09 14:47:32 -07001634 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1635 return (int) (child.getX() - baselineX);
1636 }
1637 }
1638
Adam Cohene0f66b52010-11-23 15:06:07 -08001639 // This curve determines how the effect of scrolling over the limits of the page dimishes
1640 // as the user pulls further and further from the bounds
1641 private float overScrollInfluenceCurve(float f) {
1642 f -= 1.0f;
1643 return f * f * f + 1.0f;
1644 }
1645
Adam Cohen1e4359c2014-08-18 13:12:16 -07001646 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001647 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001648
1649 // We want to reach the max over scroll effect when the user has
1650 // over scrolled half the size of the screen
1651 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1652
Adam Cohen1e4359c2014-08-18 13:12:16 -07001653 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001654
1655 // Clamp this factor, f, to -1 < f < 1
1656 if (Math.abs(f) >= 1) {
1657 f /= Math.abs(f);
1658 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001659 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001660 }
1661
1662 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001663 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001664
1665 float f = (amount / screenSize);
1666
1667 if (f == 0) return;
1668 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1669
Adam Cohen7bfc9792011-01-28 13:52:37 -08001670 // Clamp this factor, f, to -1 < f < 1
1671 if (Math.abs(f) >= 1) {
1672 f /= Math.abs(f);
1673 }
1674
Adam Cohene0f66b52010-11-23 15:06:07 -08001675 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001676 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001677 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001678 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001679 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001680 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001681 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001682 }
1683 invalidate();
1684 }
1685
Adam Cohenb5ba0972011-09-07 18:02:31 -07001686 protected void overScroll(float amount) {
1687 dampedOverScroll(amount);
1688 }
1689
Michael Jurkac5b262c2011-01-12 20:24:50 -08001690 protected float maxOverScroll() {
1691 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001692 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001693 float f = 1.0f;
1694 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1695 return OVERSCROLL_DAMP_FACTOR * f;
1696 }
1697
Adam Cohenf358a4b2013-07-23 16:47:31 -07001698 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001699 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001700 }
1701
Adam Cohenf9618852013-11-08 06:45:03 -08001702 protected void disableFreeScroll() {
1703 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001704 }
1705
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001706 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001707 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001708 if (isLayoutRtl()) {
1709 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1710 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1711 } else {
1712 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1713 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1714 }
1715 }
1716
Adam Cohenf9618852013-11-08 06:45:03 -08001717 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001718 mFreeScroll = freeScroll;
1719
Adam Cohenf9618852013-11-08 06:45:03 -08001720 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001721 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001722 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001723 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1724 setCurrentPage(mTempVisiblePagesRange[0]);
1725 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1726 setCurrentPage(mTempVisiblePagesRange[1]);
1727 }
1728 }
1729
1730 setEnableOverscroll(!freeScroll);
1731 }
1732
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001733 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001734 mAllowOverScroll = enable;
1735 }
1736
1737 int getNearestHoverOverPageIndex() {
1738 if (mDragView != null) {
1739 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1740 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001741 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001742 int minDistance = Integer.MAX_VALUE;
1743 int minIndex = indexOfChild(mDragView);
1744 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1745 View page = getPageAt(i);
1746 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1747 int d = Math.abs(dragX - pageX);
1748 if (d < minDistance) {
1749 minIndex = i;
1750 minDistance = d;
1751 }
1752 }
1753 return minIndex;
1754 }
1755 return -1;
1756 }
1757
Winson Chung321e9ee2010-08-09 13:37:56 -07001758 @Override
1759 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001760 if (DISABLE_TOUCH_INTERACTION) {
1761 return false;
1762 }
1763
Adam Cohen1697b792013-09-17 19:08:21 -07001764 super.onTouchEvent(ev);
1765
Winson Chung45e1d6e2010-11-09 17:19:49 -08001766 // Skip touch handling if there are no pages to swipe
1767 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1768
Michael Jurkab8f06722010-10-10 15:58:46 -07001769 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001770
1771 final int action = ev.getAction();
1772
1773 switch (action & MotionEvent.ACTION_MASK) {
1774 case MotionEvent.ACTION_DOWN:
1775 /*
1776 * If being flinged and user touches, stop the fling. isFinished
1777 * will be false if being flinged.
1778 */
1779 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001780 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 }
1782
1783 // Remember where the motion event started
1784 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001785 mDownMotionY = mLastMotionY = ev.getY();
1786 mDownScrollX = getScrollX();
1787 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1788 mParentDownMotionX = p[0];
1789 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001790 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001791 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001792 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001793
Michael Jurka0142d492010-08-25 17:46:15 -07001794 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001795 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001796 pageBeginMoving();
1797 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001798 break;
1799
1800 case MotionEvent.ACTION_MOVE:
1801 if (mTouchState == TOUCH_STATE_SCROLLING) {
1802 // Scroll to follow the motion event
1803 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001804
1805 if (pointerIndex == -1) return true;
1806
Winson Chung321e9ee2010-08-09 13:37:56 -07001807 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001808 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001809
Adam Cohenaefd4e12011-02-14 16:39:38 -08001810 mTotalMotionX += Math.abs(deltaX);
1811
Winson Chungc0844aa2011-02-02 15:25:58 -08001812 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1813 // keep the remainder because we are actually testing if we've moved from the last
1814 // scrolled position (which is discrete).
1815 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001816 mTouchX += deltaX;
1817 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1818 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001819 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001820 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001821 } else {
1822 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001823 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001824 mLastMotionX = x;
1825 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001826 } else {
1827 awakenScrollBars();
1828 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001829 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1830 // Update the last motion position
1831 mLastMotionX = ev.getX();
1832 mLastMotionY = ev.getY();
1833
1834 // Update the parent down so that our zoom animations take this new movement into
1835 // account
1836 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1837 mParentDownMotionX = pt[0];
1838 mParentDownMotionY = pt[1];
1839 updateDragViewTranslationDuringDrag();
1840
1841 // Find the closest page to the touch point
1842 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001843
1844 // Change the drag view if we are hovering over the drop target
1845 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1846 (int) mParentDownMotionX, (int) mParentDownMotionY);
1847 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1848
Adam Cohen7d30a372013-07-01 17:03:59 -07001849 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1850 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1851 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1852 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1853
Adam Cohenf358a4b2013-07-23 16:47:31 -07001854 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1855 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1856 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001857 mTempVisiblePagesRange[0] = 0;
1858 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001859 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001860 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1861 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1862 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1863 mSidePageHoverIndex = pageUnderPointIndex;
1864 mSidePageHoverRunnable = new Runnable() {
1865 @Override
1866 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001867 // Setup the scroll to the correct page before we swap the views
1868 snapToPage(pageUnderPointIndex);
1869
1870 // For each of the pages between the paged view and the drag view,
1871 // animate them from the previous position to the new position in
1872 // the layout (as a result of the drag view moving in the layout)
1873 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1874 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1875 dragViewIndex + 1 : pageUnderPointIndex;
1876 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1877 dragViewIndex - 1 : pageUnderPointIndex;
1878 for (int i = lowerIndex; i <= upperIndex; ++i) {
1879 View v = getChildAt(i);
1880 // dragViewIndex < pageUnderPointIndex, so after we remove the
1881 // drag view all subsequent views to pageUnderPointIndex will
1882 // shift down.
1883 int oldX = getViewportOffsetX() + getChildOffset(i);
1884 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1885
1886 // Animate the view translation from its old position to its new
1887 // position
1888 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1889 if (anim != null) {
1890 anim.cancel();
1891 }
1892
1893 v.setTranslationX(oldX - newX);
1894 anim = new AnimatorSet();
1895 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1896 anim.playTogether(
1897 ObjectAnimator.ofFloat(v, "translationX", 0f));
1898 anim.start();
1899 v.setTag(anim);
1900 }
1901
1902 removeView(mDragView);
1903 onRemoveView(mDragView, false);
1904 addView(mDragView, pageUnderPointIndex);
1905 onAddView(mDragView, pageUnderPointIndex);
1906 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001907 if (mPageIndicator != null) {
1908 mPageIndicator.setActiveMarker(getNextPage());
1909 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001910 }
1911 };
1912 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1913 }
1914 } else {
1915 removeCallbacks(mSidePageHoverRunnable);
1916 mSidePageHoverIndex = -1;
1917 }
Adam Cohen564976a2010-10-13 18:52:07 -07001918 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001919 determineScrollingStart(ev);
1920 }
1921 break;
1922
1923 case MotionEvent.ACTION_UP:
1924 if (mTouchState == TOUCH_STATE_SCROLLING) {
1925 final int activePointerId = mActivePointerId;
1926 final int pointerIndex = ev.findPointerIndex(activePointerId);
1927 final float x = ev.getX(pointerIndex);
1928 final VelocityTracker velocityTracker = mVelocityTracker;
1929 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1930 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001931 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001932 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001933 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1934 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001935
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001936 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1937
Adam Cohen00481b32011-11-18 12:03:48 -08001938 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001939 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001940
Adam Cohenf358a4b2013-07-23 16:47:31 -07001941 if (!mFreeScroll) {
1942 // In the case that the page is moved far to one direction and then is flung
1943 // in the opposite direction, we use a threshold to determine whether we should
1944 // just return to the starting page, or if we should skip one further.
1945 boolean returnToOriginalPage = false;
1946 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1947 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1948 returnToOriginalPage = true;
1949 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001950
Adam Cohenf358a4b2013-07-23 16:47:31 -07001951 int finalPage;
1952 // We give flings precedence over large moves, which is why we short-circuit our
1953 // test for a large move if a fling has been registered. That is, a large
1954 // move to the left and fling to the right will register as a fling to the right.
1955 final boolean isRtl = isLayoutRtl();
1956 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1957 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1958 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1959 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1960 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1961 snapToPageWithVelocity(finalPage, velocityX);
1962 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1963 (isFling && isVelocityXLeft)) &&
1964 mCurrentPage < getChildCount() - 1) {
1965 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1966 snapToPageWithVelocity(finalPage, velocityX);
1967 } else {
1968 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001969 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001970 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001971 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001972 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001973 }
1974
1975 float scaleX = getScaleX();
1976 int vX = (int) (-velocityX * scaleX);
1977 int initialScrollX = (int) (getScrollX() * scaleX);
1978
Adam Cohenf9618852013-11-08 06:45:03 -08001979 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001980 mScroller.fling(initialScrollX,
1981 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1982 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001983 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001984 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001985 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1986 // at this point we have not moved beyond the touch slop
1987 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1988 // we can just page
1989 int nextPage = Math.max(0, mCurrentPage - 1);
1990 if (nextPage != mCurrentPage) {
1991 snapToPage(nextPage);
1992 } else {
1993 snapToDestination();
1994 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001995 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001996 // at this point we have not moved beyond the touch slop
1997 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1998 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001999 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
2000 if (nextPage != mCurrentPage) {
2001 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002002 } else {
2003 snapToDestination();
2004 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002005 } else if (mTouchState == TOUCH_STATE_REORDERING) {
2006 // Update the last motion position
2007 mLastMotionX = ev.getX();
2008 mLastMotionY = ev.getY();
2009
2010 // Update the parent down so that our zoom animations take this new movement into
2011 // account
2012 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
2013 mParentDownMotionX = pt[0];
2014 mParentDownMotionY = pt[1];
2015 updateDragViewTranslationDuringDrag();
2016 boolean handledFling = false;
2017 if (!DISABLE_FLING_TO_DELETE) {
2018 // Check the velocity and see if we are flinging-to-delete
2019 PointF flingToDeleteVector = isFlingingToDelete();
2020 if (flingToDeleteVector != null) {
2021 onFlingToDelete(flingToDeleteVector);
2022 handledFling = true;
2023 }
2024 }
2025 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
2026 (int) mParentDownMotionY)) {
2027 onDropToDelete();
2028 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002029 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07002030 if (!mCancelTap) {
2031 onUnhandledTap(ev);
2032 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002033 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002034
2035 // Remove the callback to wait for the side page hover timeout
2036 removeCallbacks(mSidePageHoverRunnable);
2037 // End any intermediate reordering states
2038 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002039 break;
2040
2041 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002042 if (mTouchState == TOUCH_STATE_SCROLLING) {
2043 snapToDestination();
2044 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002045 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002046 break;
2047
2048 case MotionEvent.ACTION_POINTER_UP:
2049 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002050 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002051 break;
2052 }
2053
2054 return true;
2055 }
2056
Adam Cohen7d30a372013-07-01 17:03:59 -07002057 public void onFlingToDelete(View v) {}
2058 public void onRemoveView(View v, boolean deletePermanently) {}
2059 public void onRemoveViewAnimationCompleted() {}
2060 public void onAddView(View v, int index) {}
2061
2062 private void resetTouchState() {
2063 releaseVelocityTracker();
2064 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002065 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002066 mTouchState = TOUCH_STATE_REST;
2067 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002068 }
2069
Adam Cohenc2d6e892014-10-16 09:49:24 -07002070 /**
2071 * Triggered by scrolling via touch
2072 */
2073 protected void onScrollInteractionBegin() {
2074 }
2075
2076 protected void onScrollInteractionEnd() {
2077 }
2078
Adam Cohen1697b792013-09-17 19:08:21 -07002079 protected void onUnhandledTap(MotionEvent ev) {
2080 ((Launcher) getContext()).onClick(this);
2081 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002082
Winson Chung185d7162011-02-28 13:47:29 -08002083 @Override
2084 public boolean onGenericMotionEvent(MotionEvent event) {
2085 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2086 switch (event.getAction()) {
2087 case MotionEvent.ACTION_SCROLL: {
2088 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2089 final float vscroll;
2090 final float hscroll;
2091 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2092 vscroll = 0;
2093 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2094 } else {
2095 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2096 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2097 }
2098 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002099 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2100 : (hscroll > 0 || vscroll > 0);
2101 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002102 scrollRight();
2103 } else {
2104 scrollLeft();
2105 }
2106 return true;
2107 }
2108 }
2109 }
2110 }
2111 return super.onGenericMotionEvent(event);
2112 }
2113
Michael Jurkab8f06722010-10-10 15:58:46 -07002114 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2115 if (mVelocityTracker == null) {
2116 mVelocityTracker = VelocityTracker.obtain();
2117 }
2118 mVelocityTracker.addMovement(ev);
2119 }
2120
2121 private void releaseVelocityTracker() {
2122 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002123 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002124 mVelocityTracker.recycle();
2125 mVelocityTracker = null;
2126 }
2127 }
2128
Winson Chung321e9ee2010-08-09 13:37:56 -07002129 private void onSecondaryPointerUp(MotionEvent ev) {
2130 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2131 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2132 final int pointerId = ev.getPointerId(pointerIndex);
2133 if (pointerId == mActivePointerId) {
2134 // This was our active pointer going up. Choose a new
2135 // active pointer and adjust accordingly.
2136 // TODO: Make this decision more intelligent.
2137 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2138 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2139 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002140 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002141 mActivePointerId = ev.getPointerId(newPointerIndex);
2142 if (mVelocityTracker != null) {
2143 mVelocityTracker.clear();
2144 }
2145 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002146 }
2147
Winson Chung321e9ee2010-08-09 13:37:56 -07002148 @Override
2149 public void requestChildFocus(View child, View focused) {
2150 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002151 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002152 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002153 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002154 }
2155 }
2156
Winson Chung1908d072011-02-24 18:09:44 -08002157 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002158 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002159 }
2160
Adam Cohen7d30a372013-07-01 17:03:59 -07002161 int getPageNearestToPoint(float x) {
2162 int index = 0;
2163 for (int i = 0; i < getChildCount(); ++i) {
2164 if (x < getChildAt(i).getRight() - getScrollX()) {
2165 return index;
2166 } else {
2167 index++;
2168 }
2169 }
2170 return Math.min(index, getChildCount() - 1);
2171 }
2172
Adam Cohend19d3ca2010-09-15 14:43:42 -07002173 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002174 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002175 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002176 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002177 final int childCount = getChildCount();
2178 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002179 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002180 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002181 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002182 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002183 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2184 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2185 minDistanceFromScreenCenter = distanceFromScreenCenter;
2186 minDistanceFromScreenCenterIndex = i;
2187 }
2188 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002189 return minDistanceFromScreenCenterIndex;
2190 }
2191
Adam Cohen1e4359c2014-08-18 13:12:16 -07002192 protected boolean isInOverScroll() {
2193 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2194 }
2195
2196 protected int getPageSnapDuration() {
2197 if (isInOverScroll()) {
2198 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2199 }
2200 return PAGE_SNAP_ANIMATION_DURATION;
2201
2202 }
2203
Adam Cohend19d3ca2010-09-15 14:43:42 -07002204 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002205 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002206 }
2207
Adam Cohene0f66b52010-11-23 15:06:07 -08002208 private static class ScrollInterpolator implements Interpolator {
2209 public ScrollInterpolator() {
2210 }
2211
2212 public float getInterpolation(float t) {
2213 t -= 1.0f;
2214 return t*t*t*t*t + 1;
2215 }
2216 }
2217
2218 // We want the duration of the page snap animation to be influenced by the distance that
2219 // the screen has to travel, however, we don't want this duration to be effected in a
2220 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2221 // of travel has on the overall snap duration.
2222 float distanceInfluenceForSnapDuration(float f) {
2223 f -= 0.5f; // center the values about 0.
2224 f *= 0.3f * Math.PI / 2.0f;
2225 return (float) Math.sin(f);
2226 }
2227
Michael Jurka0142d492010-08-25 17:46:15 -07002228 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002229 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002230 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002231
Adam Cohenedb40762013-07-18 16:45:45 -07002232 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002233 int delta = newX - mUnboundedScrollX;
2234 int duration = 0;
2235
Adam Cohen1e4359c2014-08-18 13:12:16 -07002236 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002237 // If the velocity is low enough, then treat this more as an automatic page advance
2238 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002239 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002240 return;
2241 }
2242
2243 // Here we compute a "distance" that will be used in the computation of the overall
2244 // snap duration. This is a function of the actual distance that needs to be traveled;
2245 // we keep this value close to half screen size in order to reduce the variance in snap
2246 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002247 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002248 float distance = halfScreenSize + halfScreenSize *
2249 distanceInfluenceForSnapDuration(distanceRatio);
2250
2251 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002252 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002253
2254 // we want the page's snap velocity to approximately match the velocity at which the
2255 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002256 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2257 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002258
2259 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002260 }
2261
2262 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002263 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002264 }
2265
Adam Cohen7d30a372013-07-01 17:03:59 -07002266 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002267 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002268 }
2269
Michael Jurka0142d492010-08-25 17:46:15 -07002270 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002271 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002272 }
2273
Adam Cohenf9618852013-11-08 06:45:03 -08002274 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2275 snapToPage(whichPage, duration, false, interpolator);
2276 }
2277
2278 protected void snapToPage(int whichPage, int duration, boolean immediate,
2279 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002280 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002281
Adam Cohenedb40762013-07-18 16:45:45 -07002282 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002283 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002284 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002285 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002286 }
2287
2288 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002289 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002290 }
Michael Jurka0142d492010-08-25 17:46:15 -07002291
Adam Cohenf9618852013-11-08 06:45:03 -08002292 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2293 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002294 whichPage = validateNewPage(whichPage);
2295
Adam Cohen7d30a372013-07-01 17:03:59 -07002296 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002297 View focusedChild = getFocusedChild();
2298 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002299 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002300 focusedChild.clearFocus();
2301 }
2302
Adam Cohen53805212013-10-01 10:39:23 -07002303 sendScrollAccessibilityEvent();
2304
Michael Jurka0142d492010-08-25 17:46:15 -07002305 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002306 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002307 if (immediate) {
2308 duration = 0;
2309 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002310 duration = Math.abs(delta);
2311 }
2312
Adam Cohenf358a4b2013-07-23 16:47:31 -07002313 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002314 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002315 }
Adam Cohenf9618852013-11-08 06:45:03 -08002316
2317 if (interpolator != null) {
2318 mScroller.setInterpolator(interpolator);
2319 } else {
2320 mScroller.setInterpolator(mDefaultInterpolator);
2321 }
2322
Adam Cohen68d73932010-11-15 10:50:58 -08002323 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002324
Adam Cohen674531f2013-12-13 15:07:14 -08002325 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002326
2327 // Trigger a compute() to finish switching pages if necessary
2328 if (immediate) {
2329 computeScroll();
2330 }
2331
Winson Chung9c0565f2013-07-19 13:49:06 -07002332 // Defer loading associated pages until the scroll settles
2333 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2334
Adam Cohen7d30a372013-07-01 17:03:59 -07002335 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002336 invalidate();
2337 }
2338
Winson Chung321e9ee2010-08-09 13:37:56 -07002339 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002340 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002341 }
2342
2343 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002344 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002345 }
2346
Winson Chung86f77532010-08-24 11:08:22 -07002347 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002348 int result = -1;
2349 if (v != null) {
2350 ViewParent vp = v.getParent();
2351 int count = getChildCount();
2352 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002353 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002354 return i;
2355 }
2356 }
2357 }
2358 return result;
2359 }
2360
2361 /**
2362 * @return True is long presses are still allowed for the current touch
2363 */
2364 public boolean allowLongPress() {
2365 return mAllowLongPress;
2366 }
2367
Adam Cohendbdff6b2013-09-18 19:09:15 -07002368 @Override
2369 public boolean performLongClick() {
2370 mCancelTap = true;
2371 return super.performLongClick();
2372 }
2373
Michael Jurka0142d492010-08-25 17:46:15 -07002374 /**
2375 * Set true to allow long-press events to be triggered, usually checked by
2376 * {@link Launcher} to accept or block dpad-initiated long-presses.
2377 */
2378 public void setAllowLongPress(boolean allowLongPress) {
2379 mAllowLongPress = allowLongPress;
2380 }
2381
Winson Chung321e9ee2010-08-09 13:37:56 -07002382 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002383 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002384
2385 SavedState(Parcelable superState) {
2386 super(superState);
2387 }
2388
Adam Cohen091440a2015-03-18 14:16:05 -07002389 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002390 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002391 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002392 }
2393
2394 @Override
2395 public void writeToParcel(Parcel out, int flags) {
2396 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002397 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002398 }
2399
2400 public static final Parcelable.Creator<SavedState> CREATOR =
2401 new Parcelable.Creator<SavedState>() {
2402 public SavedState createFromParcel(Parcel in) {
2403 return new SavedState(in);
2404 }
2405
2406 public SavedState[] newArray(int size) {
2407 return new SavedState[size];
2408 }
2409 };
2410 }
2411
Winson Chungf314b0e2011-08-16 11:54:27 -07002412 protected void loadAssociatedPages(int page) {
2413 loadAssociatedPages(page, false);
2414 }
2415 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002416 if (mContentIsRefreshable) {
2417 final int count = getChildCount();
2418 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002419 int lowerPageBound = getAssociatedLowerPageBound(page);
2420 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002421 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2422 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002423 // First, clear any pages that should no longer be loaded
2424 for (int i = 0; i < count; ++i) {
2425 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002426 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002427 if (layout.getPageChildCount() > 0) {
2428 layout.removeAllViewsOnPage();
2429 }
2430 mDirtyPageContent.set(i, true);
2431 }
2432 }
2433 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002434 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002435 if ((i != page) && immediateAndOnly) {
2436 continue;
2437 }
Michael Jurka0142d492010-08-25 17:46:15 -07002438 if (lowerPageBound <= i && i <= upperPageBound) {
2439 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002440 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002441 mDirtyPageContent.set(i, false);
2442 }
Winson Chung86f77532010-08-24 11:08:22 -07002443 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002444 }
2445 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002446 }
2447 }
2448
Winson Chunge3193b92010-09-10 11:44:42 -07002449 protected int getAssociatedLowerPageBound(int page) {
2450 return Math.max(0, page - 1);
2451 }
2452 protected int getAssociatedUpperPageBound(int page) {
2453 final int count = getChildCount();
2454 return Math.min(page + 1, count - 1);
2455 }
2456
Winson Chung86f77532010-08-24 11:08:22 -07002457 /**
2458 * This method is called ONLY to synchronize the number of pages that the paged view has.
2459 * To actually fill the pages with information, implement syncPageItems() below. It is
2460 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2461 * and therefore, individual page items do not need to be updated in this method.
2462 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002463 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002464
2465 /**
2466 * This method is called to synchronize the items that are on a particular page. If views on
2467 * the page can be reused, then they should be updated within this method.
2468 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002469 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002470
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002471 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002472 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002473 }
2474 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002475 invalidatePageData(currentPage, false);
2476 }
2477 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002478 if (!mIsDataReady) {
2479 return;
2480 }
2481
Michael Jurka0142d492010-08-25 17:46:15 -07002482 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002483 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002484 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002485
Michael Jurka0142d492010-08-25 17:46:15 -07002486 // Update all the pages
2487 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002488
Winson Chung5a808352011-06-27 19:08:49 -07002489 // We must force a measure after we've loaded the pages to update the content width and
2490 // to determine the full scroll width
2491 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2492 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2493
2494 // Set a new page as the current page if necessary
2495 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002496 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002497 }
2498
Michael Jurka0142d492010-08-25 17:46:15 -07002499 // Mark each of the pages as dirty
2500 final int count = getChildCount();
2501 mDirtyPageContent.clear();
2502 for (int i = 0; i < count; ++i) {
2503 mDirtyPageContent.add(true);
2504 }
2505
2506 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002507 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002508 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002509 }
Adam Cohen06559042013-09-29 18:30:56 -07002510 if (isPageMoving()) {
2511 // If the page is moving, then snap it to the final position to ensure we don't get
2512 // stuck between pages
2513 snapToDestination();
2514 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002515 }
Winson Chung007c6982011-06-14 13:27:53 -07002516
Adam Cohen7d30a372013-07-01 17:03:59 -07002517 // Animate the drag view back to the original position
2518 void animateDragViewToOriginalPosition() {
2519 if (mDragView != null) {
2520 AnimatorSet anim = new AnimatorSet();
2521 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2522 anim.playTogether(
2523 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002524 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2525 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2526 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002527 anim.addListener(new AnimatorListenerAdapter() {
2528 @Override
2529 public void onAnimationEnd(Animator animation) {
2530 onPostReorderingAnimationCompleted();
2531 }
2532 });
2533 anim.start();
2534 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002535 }
2536
Adam Cohen7d30a372013-07-01 17:03:59 -07002537 protected void onStartReordering() {
2538 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2539 mTouchState = TOUCH_STATE_REORDERING;
2540 mIsReordering = true;
2541
Adam Cohen7d30a372013-07-01 17:03:59 -07002542 // We must invalidate to trigger a redraw to update the layers such that the drag view
2543 // is always drawn on top
2544 invalidate();
2545 }
2546
Adam Cohen091440a2015-03-18 14:16:05 -07002547 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002548 // Trigger the callback when reordering has settled
2549 --mPostReorderingPreZoomInRemainingAnimationCount;
2550 if (mPostReorderingPreZoomInRunnable != null &&
2551 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2552 mPostReorderingPreZoomInRunnable.run();
2553 mPostReorderingPreZoomInRunnable = null;
2554 }
2555 }
2556
2557 protected void onEndReordering() {
2558 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002559 }
2560
Adam Cohenf358a4b2013-07-23 16:47:31 -07002561 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002562 int dragViewIndex = indexOfChild(v);
2563
Adam Cohen327acfe2014-06-06 11:52:52 -07002564 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002565
Adam Cohen7d30a372013-07-01 17:03:59 -07002566 mTempVisiblePagesRange[0] = 0;
2567 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002568 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002569 mReorderingStarted = true;
2570
2571 // Check if we are within the reordering range
2572 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002573 dragViewIndex <= mTempVisiblePagesRange[1]) {
2574 // Find the drag view under the pointer
2575 mDragView = getChildAt(dragViewIndex);
2576 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2577 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002578 snapToPage(getPageNearestToCenterOfScreen());
2579 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002580 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002581 return true;
2582 }
2583 return false;
2584 }
2585
2586 boolean isReordering(boolean testTouchState) {
2587 boolean state = mIsReordering;
2588 if (testTouchState) {
2589 state &= (mTouchState == TOUCH_STATE_REORDERING);
2590 }
2591 return state;
2592 }
2593 void endReordering() {
2594 // For simplicity, we call endReordering sometimes even if reordering was never started.
2595 // In that case, we don't want to do anything.
2596 if (!mReorderingStarted) return;
2597 mReorderingStarted = false;
2598
2599 // If we haven't flung-to-delete the current child, then we just animate the drag view
2600 // back into position
2601 final Runnable onCompleteRunnable = new Runnable() {
2602 @Override
2603 public void run() {
2604 onEndReordering();
2605 }
2606 };
2607 if (!mDeferringForDelete) {
2608 mPostReorderingPreZoomInRunnable = new Runnable() {
2609 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002610 onCompleteRunnable.run();
2611 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002612 };
2613 };
2614
2615 mPostReorderingPreZoomInRemainingAnimationCount =
2616 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2617 // Snap to the current page
2618 snapToPage(indexOfChild(mDragView), 0);
2619 // Animate the drag view back to the front position
2620 animateDragViewToOriginalPosition();
2621 } else {
2622 // Handled in post-delete-animation-callbacks
2623 }
2624 }
2625
Adam Cohen7d30a372013-07-01 17:03:59 -07002626 /*
2627 * Flinging to delete - IN PROGRESS
2628 */
2629 private PointF isFlingingToDelete() {
2630 ViewConfiguration config = ViewConfiguration.get(getContext());
2631 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2632
2633 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2634 // Do a quick dot product test to ensure that we are flinging upwards
2635 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2636 mVelocityTracker.getYVelocity());
2637 PointF upVec = new PointF(0f, -1f);
2638 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2639 (vel.length() * upVec.length()));
2640 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2641 return vel;
2642 }
2643 }
2644 return null;
2645 }
2646
2647 /**
2648 * Creates an animation from the current drag view along its current velocity vector.
2649 * For this animation, the alpha runs for a fixed duration and we update the position
2650 * progressively.
2651 */
2652 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2653 private View mDragView;
2654 private PointF mVelocity;
2655 private Rect mFrom;
2656 private long mPrevTime;
2657 private float mFriction;
2658
2659 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2660
2661 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2662 long startTime, float friction) {
2663 mDragView = dragView;
2664 mVelocity = vel;
2665 mFrom = from;
2666 mPrevTime = startTime;
2667 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2668 }
2669
2670 @Override
2671 public void onAnimationUpdate(ValueAnimator animation) {
2672 float t = ((Float) animation.getAnimatedValue()).floatValue();
2673 long curTime = AnimationUtils.currentAnimationTimeMillis();
2674
2675 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2676 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2677
2678 mDragView.setTranslationX(mFrom.left);
2679 mDragView.setTranslationY(mFrom.top);
2680 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2681
2682 mVelocity.x *= mFriction;
2683 mVelocity.y *= mFriction;
2684 mPrevTime = curTime;
2685 }
2686 };
2687
2688 private static final int ANIM_TAG_KEY = 100;
2689
2690 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2691 return new Runnable() {
2692 @Override
2693 public void run() {
2694 int dragViewIndex = indexOfChild(dragView);
2695
2696 // For each of the pages around the drag view, animate them from the previous
2697 // position to the new position in the layout (as a result of the drag view moving
2698 // in the layout)
2699 // NOTE: We can make an assumption here because we have side-bound pages that we
2700 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002701 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002702 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2703 boolean slideFromLeft = (isLastWidgetPage ||
2704 dragViewIndex > mTempVisiblePagesRange[0]);
2705
2706 // Setup the scroll to the correct page before we swap the views
2707 if (slideFromLeft) {
2708 snapToPageImmediately(dragViewIndex - 1);
2709 }
2710
2711 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2712 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2713 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2714 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2715 ArrayList<Animator> animations = new ArrayList<Animator>();
2716 for (int i = lowerIndex; i <= upperIndex; ++i) {
2717 View v = getChildAt(i);
2718 // dragViewIndex < pageUnderPointIndex, so after we remove the
2719 // drag view all subsequent views to pageUnderPointIndex will
2720 // shift down.
2721 int oldX = 0;
2722 int newX = 0;
2723 if (slideFromLeft) {
2724 if (i == 0) {
2725 // Simulate the page being offscreen with the page spacing
2726 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002727 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002728 } else {
2729 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2730 }
2731 newX = getViewportOffsetX() + getChildOffset(i);
2732 } else {
2733 oldX = getChildOffset(i) - getChildOffset(i - 1);
2734 newX = 0;
2735 }
2736
2737 // Animate the view translation from its old position to its new
2738 // position
2739 AnimatorSet anim = (AnimatorSet) v.getTag();
2740 if (anim != null) {
2741 anim.cancel();
2742 }
2743
2744 // Note: Hacky, but we want to skip any optimizations to not draw completely
2745 // hidden views
2746 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2747 v.setTranslationX(oldX - newX);
2748 anim = new AnimatorSet();
2749 anim.playTogether(
2750 ObjectAnimator.ofFloat(v, "translationX", 0f),
2751 ObjectAnimator.ofFloat(v, "alpha", 1f));
2752 animations.add(anim);
2753 v.setTag(ANIM_TAG_KEY, anim);
2754 }
2755
2756 AnimatorSet slideAnimations = new AnimatorSet();
2757 slideAnimations.playTogether(animations);
2758 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2759 slideAnimations.addListener(new AnimatorListenerAdapter() {
2760 @Override
2761 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002762 mDeferringForDelete = false;
2763 onEndReordering();
2764 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002765 }
2766 });
2767 slideAnimations.start();
2768
2769 removeView(dragView);
2770 onRemoveView(dragView, true);
2771 }
2772 };
2773 }
2774
2775 public void onFlingToDelete(PointF vel) {
2776 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2777
2778 // NOTE: Because it takes time for the first frame of animation to actually be
2779 // called and we expect the animation to be a continuation of the fling, we have
2780 // to account for the time that has elapsed since the fling finished. And since
2781 // we don't have a startDelay, we will always get call to update when we call
2782 // start() (which we want to ignore).
2783 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2784 private int mCount = -1;
2785 private long mStartTime;
2786 private float mOffset;
2787 /* Anonymous inner class ctor */ {
2788 mStartTime = startTime;
2789 }
2790
2791 @Override
2792 public float getInterpolation(float t) {
2793 if (mCount < 0) {
2794 mCount++;
2795 } else if (mCount == 0) {
2796 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2797 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2798 mCount++;
2799 }
2800 return Math.min(1f, mOffset + t);
2801 }
2802 };
2803
2804 final Rect from = new Rect();
2805 final View dragView = mDragView;
2806 from.left = (int) dragView.getTranslationX();
2807 from.top = (int) dragView.getTranslationY();
2808 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2809 from, startTime, FLING_TO_DELETE_FRICTION);
2810
2811 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2812
2813 // Create and start the animation
2814 ValueAnimator mDropAnim = new ValueAnimator();
2815 mDropAnim.setInterpolator(tInterpolator);
2816 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2817 mDropAnim.setFloatValues(0f, 1f);
2818 mDropAnim.addUpdateListener(updateCb);
2819 mDropAnim.addListener(new AnimatorListenerAdapter() {
2820 public void onAnimationEnd(Animator animation) {
2821 onAnimationEndRunnable.run();
2822 }
2823 });
2824 mDropAnim.start();
2825 mDeferringForDelete = true;
2826 }
2827
2828 /* Drag to delete */
2829 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2830 if (mDeleteDropTarget != null) {
2831 mAltTmpRect.set(0, 0, 0, 0);
2832 View parent = (View) mDeleteDropTarget.getParent();
2833 if (parent != null) {
2834 parent.getGlobalVisibleRect(mAltTmpRect);
2835 }
2836 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2837 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2838 return mTmpRect.contains(x, y);
2839 }
2840 return false;
2841 }
2842
2843 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2844
2845 private void onDropToDelete() {
2846 final View dragView = mDragView;
2847
2848 final float toScale = 0f;
2849 final float toAlpha = 0f;
2850
2851 // Create and start the complex animation
2852 ArrayList<Animator> animations = new ArrayList<Animator>();
2853 AnimatorSet motionAnim = new AnimatorSet();
2854 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2855 motionAnim.playTogether(
2856 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2857 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2858 animations.add(motionAnim);
2859
2860 AnimatorSet alphaAnim = new AnimatorSet();
2861 alphaAnim.setInterpolator(new LinearInterpolator());
2862 alphaAnim.playTogether(
2863 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2864 animations.add(alphaAnim);
2865
2866 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2867
2868 AnimatorSet anim = new AnimatorSet();
2869 anim.playTogether(animations);
2870 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2871 anim.addListener(new AnimatorListenerAdapter() {
2872 public void onAnimationEnd(Animator animation) {
2873 onAnimationEndRunnable.run();
2874 }
2875 });
2876 anim.start();
2877
2878 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002879 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002880
2881 /* Accessibility */
2882 @Override
2883 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2884 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002885 info.setScrollable(getPageCount() > 1);
2886 if (getCurrentPage() < getPageCount() - 1) {
2887 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2888 }
2889 if (getCurrentPage() > 0) {
2890 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2891 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002892 }
2893
2894 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002895 public void sendAccessibilityEvent(int eventType) {
2896 // Don't let the view send real scroll events.
2897 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2898 super.sendAccessibilityEvent(eventType);
2899 }
2900 }
2901
2902 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002903 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2904 super.onInitializeAccessibilityEvent(event);
2905 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002906 }
2907
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002908 @Override
2909 public boolean performAccessibilityAction(int action, Bundle arguments) {
2910 if (super.performAccessibilityAction(action, arguments)) {
2911 return true;
2912 }
2913 switch (action) {
2914 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2915 if (getCurrentPage() < getPageCount() - 1) {
2916 scrollRight();
2917 return true;
2918 }
2919 } break;
2920 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2921 if (getCurrentPage() > 0) {
2922 scrollLeft();
2923 return true;
2924 }
2925 } break;
2926 }
2927 return false;
2928 }
2929
Adam Cohen0ffac432013-07-10 11:19:26 -07002930 protected String getCurrentPageDescription() {
2931 return String.format(getContext().getString(R.string.default_scroll_format),
2932 getNextPage() + 1, getChildCount());
2933 }
2934
Winson Chungd11265e2011-08-30 13:37:23 -07002935 @Override
2936 public boolean onHoverEvent(android.view.MotionEvent event) {
2937 return true;
2938 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002939}