blob: 88295c084c77401a9a6cf026cb91d43d9d73216c [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 int getCurrentPage() {
481 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700483
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700484 /**
485 * Returns the index of page to be shown immediately afterwards.
486 */
Winson Chung360e63f2012-04-27 13:48:05 -0700487 int getNextPage() {
488 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
489 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700490
Winson Chung86f77532010-08-24 11:08:22 -0700491 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 return getChildCount();
493 }
494
Winson Chung86f77532010-08-24 11:08:22 -0700495 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 return getChildAt(index);
497 }
498
Adam Cohenae4f1552011-10-20 00:15:42 -0700499 protected int indexToPage(int index) {
500 return index;
501 }
502
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800504 * Updates the scroll of the current page immediately to its final scroll position. We use this
505 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
506 * the previous tab page.
507 */
508 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700509 // If the current page is invalid, just reset the scroll position to zero
510 int newX = 0;
511 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700512 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700513 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800514 scrollTo(newX, 0);
515 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700516 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800517 }
518
519 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700520 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700521 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
522 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700523 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700524 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700525 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800526 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700527 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700528 }
529
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700530 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700531 mScroller.abortAnimation();
532 // We need to clean up the next page here to avoid computeScrollHelper from
533 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700534 if (resetNextPage) {
535 mNextPage = INVALID_PAGE;
536 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700537 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700538
539 private void forceFinishScroller() {
540 mScroller.forceFinished(true);
541 // We need to clean up the next page here to avoid computeScrollHelper from
542 // updating current page on the pass.
543 mNextPage = INVALID_PAGE;
544 }
545
Adam Cohen6f127a62014-06-12 14:54:41 -0700546 private int validateNewPage(int newPage) {
547 int validatedPage = newPage;
548 // When in free scroll mode, we need to clamp to the free scroll page range.
549 if (mFreeScroll) {
550 getFreeScrollPageRange(mTempVisiblePagesRange);
551 validatedPage = Math.max(mTempVisiblePagesRange[0],
552 Math.min(newPage, mTempVisiblePagesRange[1]));
553 }
554 // Ensure that it is clamped by the actual set of children in all cases
555 validatedPage = Math.max(0, Math.min(validatedPage, getPageCount() - 1));
556 return validatedPage;
557 }
558
Chet Haasebc2f0822012-10-26 17:59:53 -0700559 /**
Winson Chung86f77532010-08-24 11:08:22 -0700560 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700561 */
Winson Chung86f77532010-08-24 11:08:22 -0700562 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800563 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700564 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800565 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800566 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
567 // the default
568 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800569 return;
570 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700571 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700572 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700573 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700574 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800575 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700576 }
577
Winson Chung8c87cd82013-07-23 16:20:10 -0700578 /**
579 * The restore page will be set in place of the current page at the next (likely first)
580 * layout.
581 */
582 void setRestorePage(int restorePage) {
583 mRestorePage = restorePage;
584 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800585 int getRestorePage() {
586 return mRestorePage;
587 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700588
Adam Cohen674531f2013-12-13 15:07:14 -0800589 /**
590 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
591 * has settled.
592 */
Michael Jurka0142d492010-08-25 17:46:15 -0700593 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700594 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800595 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700596 }
Winson Chungd2be3812013-07-16 11:11:32 -0700597
Adam Cohen674531f2013-12-13 15:07:14 -0800598 updatePageIndicator();
599 }
600
601 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700602 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700603 if (mPageIndicator != null) {
604 mPageIndicator.setContentDescription(getPageIndicatorDescription());
605 if (!isReordering(false)) {
606 mPageIndicator.setActiveMarker(getNextPage());
607 }
Winson Chungd2be3812013-07-16 11:11:32 -0700608 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800610 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700611 if (!mIsPageMoving) {
612 mIsPageMoving = true;
613 onPageBeginMoving();
614 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700615 }
616
Michael Jurkace7e05f2011-02-01 22:02:35 -0800617 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700618 if (mIsPageMoving) {
619 mIsPageMoving = false;
620 onPageEndMoving();
621 }
Michael Jurka0142d492010-08-25 17:46:15 -0700622 }
623
Adam Cohen26976d92011-03-22 15:33:33 -0700624 protected boolean isPageMoving() {
625 return mIsPageMoving;
626 }
627
Michael Jurka0142d492010-08-25 17:46:15 -0700628 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700629 protected void onPageBeginMoving() {
630 }
631
632 // a method that subclasses can override to add behavior
633 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700634 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700635 }
636
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 /**
Winson Chung86f77532010-08-24 11:08:22 -0700638 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 *
640 * @param l The listener used to respond to long clicks.
641 */
642 @Override
643 public void setOnLongClickListener(OnLongClickListener l) {
644 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700645 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700647 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 }
Adam Cohen1697b792013-09-17 19:08:21 -0700649 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 }
651
652 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800653 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700654 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800655 }
656
657 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700658 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700659 // In free scroll mode, we clamp the scrollX
660 if (mFreeScroll) {
661 x = Math.min(x, mFreeScrollMaxScrollX);
662 x = Math.max(x, mFreeScrollMinScrollX);
663 }
664
Adam Cohen0ffac432013-07-10 11:19:26 -0700665 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800666 mUnboundedScrollX = x;
667
Adam Cohen0ffac432013-07-10 11:19:26 -0700668 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
669 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
670 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800671 super.scrollTo(0, y);
672 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700673 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700674 if (isRtl) {
675 overScroll(x - mMaxScrollX);
676 } else {
677 overScroll(x);
678 }
Adam Cohen68d73932010-11-15 10:50:58 -0800679 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700680 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800681 super.scrollTo(mMaxScrollX, y);
682 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700683 mWasInOverscroll = true;
Adam Cohen0ffac432013-07-10 11:19:26 -0700684 if (isRtl) {
685 overScroll(x);
686 } else {
687 overScroll(x - mMaxScrollX);
688 }
Adam Cohen68d73932010-11-15 10:50:58 -0800689 }
690 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700691 if (mWasInOverscroll) {
692 overScroll(0);
693 mWasInOverscroll = false;
694 }
Adam Cohenebea84d2011-11-09 17:20:41 -0800695 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800696 super.scrollTo(x, y);
697 }
698
Michael Jurka0142d492010-08-25 17:46:15 -0700699 mTouchX = x;
700 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700701
702 // Update the last motion events when scrolling
703 if (isReordering(true)) {
704 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
705 mLastMotionX = p[0];
706 mLastMotionY = p[1];
707 updateDragViewTranslationDuringDrag();
708 }
Michael Jurka0142d492010-08-25 17:46:15 -0700709 }
710
Adam Cohen53805212013-10-01 10:39:23 -0700711 private void sendScrollAccessibilityEvent() {
712 AccessibilityManager am =
713 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
714 if (am.isEnabled()) {
715 AccessibilityEvent ev =
716 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700717 ev.setItemCount(getChildCount());
718 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700719 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700720
Alan Viverette254139a2013-10-08 13:13:46 -0700721 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700722 if (getNextPage() >= mCurrentPage) {
723 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
724 } else {
725 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
726 }
727
728 ev.setAction(action);
729 sendAccessibilityEventUnchecked(ev);
730 }
731 }
732
Michael Jurka0142d492010-08-25 17:46:15 -0700733 // we moved this functionality to a helper function so SmoothPagedView can reuse it
734 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700736 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 if (getScrollX() != mScroller.getCurrX()
738 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700739 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700740 float scaleX = mFreeScroll ? getScaleX() : 1f;
741 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
742 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700743 }
Michael Jurka0142d492010-08-25 17:46:15 -0700744 invalidate();
745 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700746 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700747 sendScrollAccessibilityEvent();
748
Adam Cohen6f127a62014-06-12 14:54:41 -0700749 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700750 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700751 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700752
Winson Chung9c0565f2013-07-19 13:49:06 -0700753 // Load the associated pages if necessary
754 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
755 loadAssociatedPages(mCurrentPage);
756 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
757 }
758
Adam Cohen73aa9752010-11-24 16:26:58 -0800759 // We don't want to trigger a page end moving unless the page has settled
760 // and the user has stopped scrolling
761 if (mTouchState == TOUCH_STATE_REST) {
762 pageEndMoving();
763 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700764
Adam Cohen7d30a372013-07-01 17:03:59 -0700765 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700766 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700767 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700768 if (am.isEnabled()) {
769 // Notify the user when the page changes
770 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700771 }
Michael Jurka0142d492010-08-25 17:46:15 -0700772 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 }
Michael Jurka0142d492010-08-25 17:46:15 -0700774 return false;
775 }
776
777 @Override
778 public void computeScroll() {
779 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 }
781
Adam Cohen7d30a372013-07-01 17:03:59 -0700782 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
783 return mTopAlignPageWhenShrinkingForBouncer;
784 }
785
Adam Cohen96d30a12013-07-16 18:13:21 -0700786 public static class LayoutParams extends ViewGroup.LayoutParams {
787 public boolean isFullScreenPage = false;
788
789 /**
790 * {@inheritDoc}
791 */
792 public LayoutParams(int width, int height) {
793 super(width, height);
794 }
795
796 public LayoutParams(ViewGroup.LayoutParams source) {
797 super(source);
798 }
799 }
800
801 protected LayoutParams generateDefaultLayoutParams() {
802 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
803 }
804
Adam Cohenedb40762013-07-18 16:45:45 -0700805 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700806 LayoutParams lp = generateDefaultLayoutParams();
807 lp.isFullScreenPage = true;
808 super.addView(page, 0, lp);
809 }
810
Adam Cohen410f3cd2013-09-22 12:09:32 -0700811 public int getNormalChildHeight() {
812 return mNormalChildHeight;
813 }
814
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 @Override
816 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700817 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700818 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
819 return;
820 }
821
Adam Cohen7d30a372013-07-01 17:03:59 -0700822 // We measure the dimensions of the PagedView to be larger than the pages so that when we
823 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700824 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
825 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
826 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700827 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800828 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700829 // viewport, we can be at most one and a half screens offset once we scale down
830 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700831 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
832 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700833
Winson Chungc82d2622013-11-06 13:23:29 -0800834 int parentWidthSize = (int) (2f * maxSize);
835 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700836 int scaledWidthSize, scaledHeightSize;
837 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700838 scaledWidthSize = (int) (parentWidthSize / mMinScale);
839 scaledHeightSize = (int) (parentHeightSize / mMinScale);
840 } else {
841 scaledWidthSize = widthSize;
842 scaledHeightSize = heightSize;
843 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700844 mViewport.set(0, 0, widthSize, heightSize);
845
846 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
847 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
848 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 }
850
Winson Chung8aad6102012-05-11 16:27:49 -0700851 // Return early if we aren't given a proper dimension
852 if (widthSize <= 0 || heightSize <= 0) {
853 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
854 return;
855 }
856
Adam Lesinski6b879f02010-11-04 16:15:23 -0700857 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
858 * of the All apps view on XLarge displays to not take up more space then it needs. Width
859 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
860 * each page to have the same width.
861 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700862 final int verticalPadding = getPaddingTop() + getPaddingBottom();
863 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700864
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700865 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700867 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700868 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700869 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
870 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
871 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
872 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 final int childCount = getChildCount();
874 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700875 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700876 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100877 if (child.getVisibility() != GONE) {
878 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700879
Vladimir Marko2824b072013-10-04 16:42:17 +0100880 int childWidthMode;
881 int childHeightMode;
882 int childWidth;
883 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700884
Vladimir Marko2824b072013-10-04 16:42:17 +0100885 if (!lp.isFullScreenPage) {
886 if (lp.width == LayoutParams.WRAP_CONTENT) {
887 childWidthMode = MeasureSpec.AT_MOST;
888 } else {
889 childWidthMode = MeasureSpec.EXACTLY;
890 }
891
892 if (lp.height == LayoutParams.WRAP_CONTENT) {
893 childHeightMode = MeasureSpec.AT_MOST;
894 } else {
895 childHeightMode = MeasureSpec.EXACTLY;
896 }
897
Adam Cohen3b185e22013-10-29 14:45:58 -0700898 childWidth = getViewportWidth() - horizontalPadding
899 - mInsets.left - mInsets.right;
900 childHeight = getViewportHeight() - verticalPadding
901 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100902 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700903 } else {
904 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700905 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100906
Adam Cohen3b185e22013-10-29 14:45:58 -0700907 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
908 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700909 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700910 if (referenceChildWidth == 0) {
911 referenceChildWidth = childWidth;
912 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700913
Vladimir Marko2824b072013-10-04 16:42:17 +0100914 final int childWidthMeasureSpec =
915 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
916 final int childHeightMeasureSpec =
917 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
918 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700919 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700920 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700921 if (mSpacePagesAutomatically) {
922 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
923 - referenceChildWidth) / 2;
924 if (spacing >= 0) {
925 setPageSpacing(spacing);
926 }
927 mSpacePagesAutomatically = false;
928 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700929 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800930 }
931
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700932 /**
933 * This method should be called once before first layout / measure pass.
934 */
935 protected void setSinglePageInViewport() {
936 mSpacePagesAutomatically = true;
937 }
938
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700940 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700942 return;
943 }
944
Winson Chung785d2eb2011-04-14 16:08:02 -0700945 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700947
Adam Cohen7d30a372013-07-01 17:03:59 -0700948 int offsetX = getViewportOffsetX();
949 int offsetY = getViewportOffsetY();
950
951 // Update the viewport offsets
952 mViewport.offset(offsetX, offsetY);
953
Adam Cohen0ffac432013-07-10 11:19:26 -0700954 final boolean isRtl = isLayoutRtl();
955
956 final int startIndex = isRtl ? childCount - 1 : 0;
957 final int endIndex = isRtl ? -1 : childCount;
958 final int delta = isRtl ? -1 : 1;
959
Adam Cohen7d30a372013-07-01 17:03:59 -0700960 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700961
Adam Cohen3b185e22013-10-29 14:45:58 -0700962 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000963 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700964
965 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700966 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
967 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700968 }
969
Adam Cohen0ffac432013-07-10 11:19:26 -0700970 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700971 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700973 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100974 int childTop;
975 if (lp.isFullScreenPage) {
976 childTop = offsetY;
977 } else {
978 childTop = offsetY + getPaddingTop() + mInsets.top;
979 if (mCenterPagesVertically) {
980 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
981 }
982 }
983
Adam Cohen96d30a12013-07-16 18:13:21 -0700984 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700985 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800986
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700988 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800989 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700990
Adam Cohen3b185e22013-10-29 14:45:58 -0700991 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
992 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000993
994 int pageGap = mPageSpacing;
995 int next = i + delta;
996 if (next != endIndex) {
997 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
998 } else {
999 nextLp = null;
1000 }
1001
1002 // Prevent full screen pages from showing in the viewport
1003 // when they are not the current page.
1004 if (lp.isFullScreenPage) {
1005 pageGap = getPaddingLeft();
1006 } else if (nextLp != null && nextLp.isFullScreenPage) {
1007 pageGap = getPaddingRight();
1008 }
1009
1010 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 }
1012 }
Winson Chungc3665fa2011-09-14 17:56:27 -07001013
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001014 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -08001015 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001016 mFirstLayout = false;
1017 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001018
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001019 final LayoutTransition transition = getLayoutTransition();
1020 // If the transition is running defer updating max scroll, as some empty pages could
1021 // still be present, and a max scroll change could cause sudden jumps in scroll.
1022 if (transition != null && transition.isRunning()) {
1023 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
1024
1025 @Override
1026 public void startTransition(LayoutTransition transition, ViewGroup container,
1027 View view, int transitionType) { }
1028
1029 @Override
1030 public void endTransition(LayoutTransition transition, ViewGroup container,
1031 View view, int transitionType) {
1032 // Wait until all transitions are complete.
1033 if (!transition.isRunning()) {
1034 transition.removeTransitionListener(this);
1035 updateMaxScrollX();
1036 }
1037 }
1038 });
Adam Cohenf698c6e2013-07-17 18:44:25 -07001039 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001040 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -07001041 }
1042
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001043 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount &&
Adam Cohenf698c6e2013-07-17 18:44:25 -07001044 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001045 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001046 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001047 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001048 } else {
1049 setCurrentPage(getNextPage());
1050 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001051 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001052 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001053
1054 if (isReordering(true)) {
1055 updateDragViewTranslationDuringDrag();
1056 }
Winson Chunge3193b92010-09-10 11:44:42 -07001057 }
1058
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001059 private void updateMaxScrollX() {
1060 int childCount = getChildCount();
1061 if (childCount > 0) {
1062 final int index = isLayoutRtl() ? 0 : childCount - 1;
1063 mMaxScrollX = getScrollForPage(index);
1064 } else {
1065 mMaxScrollX = 0;
1066 }
1067 }
1068
Adam Cohen3b185e22013-10-29 14:45:58 -07001069 public void setPageSpacing(int pageSpacing) {
1070 mPageSpacing = pageSpacing;
1071 requestLayout();
1072 }
1073
Adam Cohenf34bab52010-09-30 14:11:56 -07001074 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001075 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1076
1077 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001078 for (int i = 0; i < getChildCount(); i++) {
1079 View child = getChildAt(i);
1080 if (child != null) {
1081 float scrollProgress = getScrollProgress(screenCenter, child, i);
1082 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001083 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001084 }
1085 }
1086 invalidate();
1087 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001088 }
1089
Winson Chungc58497e2013-09-03 17:48:37 -07001090 protected void enablePagedViewAnimations() {
1091 mAllowPagedViewAnimations = true;
1092
1093 }
1094 protected void disablePagedViewAnimations() {
1095 mAllowPagedViewAnimations = false;
1096 }
1097
Winson Chunge3193b92010-09-10 11:44:42 -07001098 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001099 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001100 // Update the page indicator, we don't update the page indicator as we
1101 // add/remove pages
1102 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001103 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001104 mPageIndicator.addMarker(pageIndex,
1105 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001106 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001107 }
1108
Adam Cohen2591f6a2011-10-25 14:36:40 -07001109 // This ensures that when children are added, they get the correct transforms / alphas
1110 // in accordance with any scroll effects.
1111 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001112 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001113 invalidate();
1114 }
1115
Michael Jurka8b805b12012-04-18 14:23:14 -07001116 @Override
1117 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001118 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001119 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001120 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001121 }
1122
Winson Chungd2be3812013-07-16 11:11:32 -07001123 private void removeMarkerForView(int index) {
1124 // Update the page indicator, we don't update the page indicator as we
1125 // add/remove pages
1126 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001127 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001128 }
1129 }
1130
1131 @Override
1132 public void removeView(View v) {
1133 // XXX: We should find a better way to hook into this before the view
1134 // gets removed form its parent...
1135 removeMarkerForView(indexOfChild(v));
1136 super.removeView(v);
1137 }
1138 @Override
1139 public void removeViewInLayout(View v) {
1140 // XXX: We should find a better way to hook into this before the view
1141 // gets removed form its parent...
1142 removeMarkerForView(indexOfChild(v));
1143 super.removeViewInLayout(v);
1144 }
1145 @Override
1146 public void removeViewAt(int index) {
1147 // XXX: We should find a better way to hook into this before the view
1148 // gets removed form its parent...
1149 removeViewAt(index);
1150 super.removeViewAt(index);
1151 }
1152 @Override
1153 public void removeAllViewsInLayout() {
1154 // Update the page indicator, we don't update the page indicator as we
1155 // add/remove pages
1156 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001157 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001158 }
1159
1160 super.removeAllViewsInLayout();
1161 }
1162
Adam Cohen73894962011-10-31 13:17:17 -07001163 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001164 if (index < 0 || index > getChildCount() - 1) return 0;
1165
Adam Cohenf698c6e2013-07-17 18:44:25 -07001166 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001167
Adam Cohenf698c6e2013-07-17 18:44:25 -07001168 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001169 }
1170
Adam Cohen6f127a62014-06-12 14:54:41 -07001171 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001172 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001173 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001174 }
1175
Michael Jurkadde558b2011-11-09 22:09:06 -08001176 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001177 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001178 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001179
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001180 range[0] = -1;
1181 range[1] = -1;
1182
Michael Jurkac4fb9172010-09-02 17:19:20 -07001183 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001184 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001185 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001186
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001187 int count = getChildCount();
1188 for (int i = 0; i < count; i++) {
1189 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001190
Winson Chungc9ca2982013-07-19 12:07:38 -07001191 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001192 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001193 if (mTmpIntPoint[0] > viewportWidth) {
1194 if (range[0] == -1) {
1195 continue;
1196 } else {
1197 break;
1198 }
1199 }
1200
Adam Cohen6a678da2013-09-24 10:58:01 -07001201 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001202 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1203 if (mTmpIntPoint[0] < 0) {
1204 if (range[0] == -1) {
1205 continue;
1206 } else {
1207 break;
1208 }
1209 }
1210 curScreen = i;
1211 if (range[0] < 0) {
1212 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001213 }
1214 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001215
1216 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001217 } else {
1218 range[0] = -1;
1219 range[1] = -1;
1220 }
1221 }
1222
Michael Jurka920d7f42012-05-14 16:29:55 -07001223 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001224 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001225 }
1226
Michael Jurkadde558b2011-11-09 22:09:06 -08001227 @Override
1228 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001229 // Find out which screens are visible; as an optimization we only call draw on them
1230 final int pageCount = getChildCount();
1231 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001232 int halfScreenSize = getViewportWidth() / 2;
1233 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1234 // Otherwise it is equal to the scaled overscroll position.
1235 int screenCenter = mOverScrollX + halfScreenSize;
1236
1237 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1238 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1239 // set it for the next frame
1240 mForceScreenScrolled = false;
1241 screenScrolled(screenCenter);
1242 mLastScreenCenter = screenCenter;
1243 }
1244
Michael Jurkadde558b2011-11-09 22:09:06 -08001245 getVisiblePages(mTempVisiblePagesRange);
1246 final int leftScreen = mTempVisiblePagesRange[0];
1247 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001248 if (leftScreen != -1 && rightScreen != -1) {
1249 final long drawingTime = getDrawingTime();
1250 // Clip to the bounds
1251 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001252 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1253 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001254
Adam Cohen7d30a372013-07-01 17:03:59 -07001255 // Draw all the children, leaving the drag view for last
1256 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001257 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001258 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001259 if (mForceDrawAllChildrenNextFrame ||
1260 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001261 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001262 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001263 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001264 // Draw the drag view on top (if there is one)
1265 if (mDragView != null) {
1266 drawChild(canvas, mDragView, drawingTime);
1267 }
1268
Michael Jurka5e368ff2012-05-14 23:13:15 -07001269 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001270 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001271 }
Michael Jurka0142d492010-08-25 17:46:15 -07001272 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 }
1274
1275 @Override
1276 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001277 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001278 if (page != mCurrentPage || !mScroller.isFinished()) {
1279 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 return true;
1281 }
1282 return false;
1283 }
1284
1285 @Override
1286 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001287 int focusablePage;
1288 if (mNextPage != INVALID_PAGE) {
1289 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001291 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001292 }
Winson Chung86f77532010-08-24 11:08:22 -07001293 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001295 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 }
1297 return false;
1298 }
1299
1300 @Override
1301 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001302 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001303 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001304 if (getCurrentPage() > 0) {
1305 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 return true;
1307 }
1308 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001309 if (getCurrentPage() < getPageCount() - 1) {
1310 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 return true;
1312 }
1313 }
1314 return super.dispatchUnhandledMove(focused, direction);
1315 }
1316
1317 @Override
1318 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001319 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001320 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001321 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 }
1323 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001324 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001325 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 }
1327 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001328 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001329 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 }
1331 }
1332 }
1333
1334 /**
1335 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001336 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 *
Winson Chung86f77532010-08-24 11:08:22 -07001338 * This happens when live folders requery, and if they're off page, they
1339 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 */
1341 @Override
1342 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001343 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 View v = focused;
1345 while (true) {
1346 if (v == current) {
1347 super.focusableViewAvailable(focused);
1348 return;
1349 }
1350 if (v == this) {
1351 return;
1352 }
1353 ViewParent parent = v.getParent();
1354 if (parent instanceof View) {
1355 v = (View)v.getParent();
1356 } else {
1357 return;
1358 }
1359 }
1360 }
1361
1362 /**
1363 * {@inheritDoc}
1364 */
1365 @Override
1366 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1367 if (disallowIntercept) {
1368 // We need to make sure to cancel our long press if
1369 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001370 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001371 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 }
1373 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1374 }
1375
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001376 /**
1377 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1378 */
1379 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001380 if (isLayoutRtl()) {
1381 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001382 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001383 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001384 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001385 }
1386
1387 /**
1388 * Return true if a tap at (x, y) should trigger a flip to the next page.
1389 */
1390 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001391 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001392 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001393 }
1394 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001395 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001396 }
Winson Chung52aee602013-01-30 12:01:02 -08001397
Adam Cohen7d30a372013-07-01 17:03:59 -07001398 /** Returns whether x and y originated within the buffered viewport */
1399 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1400 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1401 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1402 return mTmpRect.contains(x, y);
1403 }
1404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 @Override
1406 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001407 if (DISABLE_TOUCH_INTERACTION) {
1408 return false;
1409 }
1410
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 /*
1412 * This method JUST determines whether we want to intercept the motion.
1413 * If we return true, onTouchEvent will be called and we do the actual
1414 * scrolling there.
1415 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001416 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001417
Winson Chung45e1d6e2010-11-09 17:19:49 -08001418 // Skip touch handling if there are no pages to swipe
1419 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1420
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 /*
1422 * Shortcut the most recurring case: the user is in the dragging
1423 * state and he is moving his finger. We want to intercept this
1424 * motion.
1425 */
1426 final int action = ev.getAction();
1427 if ((action == MotionEvent.ACTION_MOVE) &&
1428 (mTouchState == TOUCH_STATE_SCROLLING)) {
1429 return true;
1430 }
1431
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 switch (action & MotionEvent.ACTION_MASK) {
1433 case MotionEvent.ACTION_MOVE: {
1434 /*
1435 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1436 * whether the user has moved far enough from his original down touch.
1437 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001438 if (mActivePointerId != INVALID_POINTER) {
1439 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001440 }
1441 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1442 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1443 // i.e. fall through to the next case (don't break)
1444 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1445 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001446 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001447 }
1448
1449 case MotionEvent.ACTION_DOWN: {
1450 final float x = ev.getX();
1451 final float y = ev.getY();
1452 // Remember location of down touch
1453 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001454 mDownMotionY = y;
1455 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 mLastMotionX = x;
1457 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001458 float[] p = mapPointFromViewToParent(this, x, y);
1459 mParentDownMotionX = p[0];
1460 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001461 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001462 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001464
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 /*
1466 * If being flinged and user touches the screen, initiate drag;
1467 * otherwise don't. mScroller.isFinished should be false when
1468 * being flinged.
1469 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001470 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001471 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001472
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001473 if (finishedScrolling) {
1474 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001475 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001476 setCurrentPage(getNextPage());
1477 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001478 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001479 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001480 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1481 mTouchState = TOUCH_STATE_SCROLLING;
1482 } else {
1483 mTouchState = TOUCH_STATE_REST;
1484 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001485 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001486
Winson Chung86f77532010-08-24 11:08:22 -07001487 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001488 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001489 if (!DISABLE_TOUCH_SIDE_PAGES) {
1490 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1491 if (getChildCount() > 0) {
1492 if (hitsPreviousPage(x, y)) {
1493 mTouchState = TOUCH_STATE_PREV_PAGE;
1494 } else if (hitsNextPage(x, y)) {
1495 mTouchState = TOUCH_STATE_NEXT_PAGE;
1496 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 }
1498 }
1499 }
1500 break;
1501 }
1502
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001504 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001505 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 break;
1507
1508 case MotionEvent.ACTION_POINTER_UP:
1509 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001510 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 break;
1512 }
1513
1514 /*
1515 * The only time we want to intercept motion events is if we are in the
1516 * drag mode.
1517 */
1518 return mTouchState != TOUCH_STATE_REST;
1519 }
1520
Adam Cohenf8d28232011-02-01 21:47:00 -08001521 protected void determineScrollingStart(MotionEvent ev) {
1522 determineScrollingStart(ev, 1.0f);
1523 }
1524
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 /*
1526 * Determines if we should change the touch state to start scrolling after the
1527 * user moves their touch point too far.
1528 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001529 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001530 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001531 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001532 if (pointerIndex == -1) return;
1533
1534 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001535 final float x = ev.getX(pointerIndex);
1536 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001537 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1538
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 final int xDiff = (int) Math.abs(x - mLastMotionX);
1540 final int yDiff = (int) Math.abs(y - mLastMotionY);
1541
Adam Cohenf8d28232011-02-01 21:47:00 -08001542 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001543 boolean xPaged = xDiff > mPagingTouchSlop;
1544 boolean xMoved = xDiff > touchSlop;
1545 boolean yMoved = yDiff > touchSlop;
1546
Adam Cohenf8d28232011-02-01 21:47:00 -08001547 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001548 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 // Scroll if the user moved far enough along the X axis
1550 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001551 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001553 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001554 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001555 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001556 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001557 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001558 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001559 }
1560 }
1561
Adam Cohen7d30a372013-07-01 17:03:59 -07001562 protected float getMaxScrollProgress() {
1563 return 1.0f;
1564 }
1565
Adam Cohenf8d28232011-02-01 21:47:00 -08001566 protected void cancelCurrentPageLongPress() {
1567 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001568 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001569 // Try canceling the long press. It could also have been scheduled
1570 // by a distant descendant, so use the mAllowLongPress flag to block
1571 // everything
1572 final View currentPage = getPageAt(mCurrentPage);
1573 if (currentPage != null) {
1574 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001575 }
1576 }
1577 }
1578
Adam Cohen7d30a372013-07-01 17:03:59 -07001579 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1580 final int halfScreenSize = getViewportWidth() / 2;
1581
1582 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1583 screenCenter = Math.max(halfScreenSize, screenCenter);
1584
1585 return getScrollProgress(screenCenter, v, page);
1586 }
1587
Adam Cohenb5ba0972011-09-07 18:02:31 -07001588 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001589 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001590
Adam Cohenedb40762013-07-18 16:45:45 -07001591 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001592 int count = getChildCount();
1593
1594 final int totalDistance;
1595
1596 int adjacentPage = page + 1;
1597 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1598 adjacentPage = page - 1;
1599 }
1600
1601 if (adjacentPage < 0 || adjacentPage > count - 1) {
1602 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1603 } else {
1604 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1605 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001606
1607 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001608 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1609 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001610 return scrollProgress;
1611 }
1612
Adam Cohenedb40762013-07-18 16:45:45 -07001613 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001614 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001615 return 0;
1616 } else {
1617 return mPageScrolls[index];
1618 }
1619 }
1620
Adam Cohen564a2e72013-10-09 14:47:32 -07001621 // While layout transitions are occurring, a child's position may stray from its baseline
1622 // position. This method returns the magnitude of this stray at any given time.
1623 public int getLayoutTransitionOffsetForPage(int index) {
1624 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1625 return 0;
1626 } else {
1627 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001628
1629 int scrollOffset = 0;
1630 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1631 if (!lp.isFullScreenPage) {
1632 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1633 }
1634
Adam Cohen564a2e72013-10-09 14:47:32 -07001635 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1636 return (int) (child.getX() - baselineX);
1637 }
1638 }
1639
Adam Cohene0f66b52010-11-23 15:06:07 -08001640 // This curve determines how the effect of scrolling over the limits of the page dimishes
1641 // as the user pulls further and further from the bounds
1642 private float overScrollInfluenceCurve(float f) {
1643 f -= 1.0f;
1644 return f * f * f + 1.0f;
1645 }
1646
Adam Cohen1e4359c2014-08-18 13:12:16 -07001647 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001648 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001649
1650 // We want to reach the max over scroll effect when the user has
1651 // over scrolled half the size of the screen
1652 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1653
Adam Cohen1e4359c2014-08-18 13:12:16 -07001654 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001655
1656 // Clamp this factor, f, to -1 < f < 1
1657 if (Math.abs(f) >= 1) {
1658 f /= Math.abs(f);
1659 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001660 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001661 }
1662
1663 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001664 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001665
1666 float f = (amount / screenSize);
1667
1668 if (f == 0) return;
1669 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1670
Adam Cohen7bfc9792011-01-28 13:52:37 -08001671 // Clamp this factor, f, to -1 < f < 1
1672 if (Math.abs(f) >= 1) {
1673 f /= Math.abs(f);
1674 }
1675
Adam Cohene0f66b52010-11-23 15:06:07 -08001676 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001677 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001678 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001679 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001680 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001681 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001682 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001683 }
1684 invalidate();
1685 }
1686
Adam Cohenb5ba0972011-09-07 18:02:31 -07001687 protected void overScroll(float amount) {
1688 dampedOverScroll(amount);
1689 }
1690
Michael Jurkac5b262c2011-01-12 20:24:50 -08001691 protected float maxOverScroll() {
1692 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001693 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001694 float f = 1.0f;
1695 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1696 return OVERSCROLL_DAMP_FACTOR * f;
1697 }
1698
Adam Cohenf358a4b2013-07-23 16:47:31 -07001699 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001700 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001701 }
1702
Adam Cohenf9618852013-11-08 06:45:03 -08001703 protected void disableFreeScroll() {
1704 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001705 }
1706
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001707 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001708 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001709 if (isLayoutRtl()) {
1710 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1711 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1712 } else {
1713 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1714 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1715 }
1716 }
1717
Adam Cohenf9618852013-11-08 06:45:03 -08001718 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001719 mFreeScroll = freeScroll;
1720
Adam Cohenf9618852013-11-08 06:45:03 -08001721 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001722 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001723 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001724 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1725 setCurrentPage(mTempVisiblePagesRange[0]);
1726 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1727 setCurrentPage(mTempVisiblePagesRange[1]);
1728 }
1729 }
1730
1731 setEnableOverscroll(!freeScroll);
1732 }
1733
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001734 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001735 mAllowOverScroll = enable;
1736 }
1737
1738 int getNearestHoverOverPageIndex() {
1739 if (mDragView != null) {
1740 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1741 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001742 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001743 int minDistance = Integer.MAX_VALUE;
1744 int minIndex = indexOfChild(mDragView);
1745 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1746 View page = getPageAt(i);
1747 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1748 int d = Math.abs(dragX - pageX);
1749 if (d < minDistance) {
1750 minIndex = i;
1751 minDistance = d;
1752 }
1753 }
1754 return minIndex;
1755 }
1756 return -1;
1757 }
1758
Winson Chung321e9ee2010-08-09 13:37:56 -07001759 @Override
1760 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001761 if (DISABLE_TOUCH_INTERACTION) {
1762 return false;
1763 }
1764
Adam Cohen1697b792013-09-17 19:08:21 -07001765 super.onTouchEvent(ev);
1766
Winson Chung45e1d6e2010-11-09 17:19:49 -08001767 // Skip touch handling if there are no pages to swipe
1768 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1769
Michael Jurkab8f06722010-10-10 15:58:46 -07001770 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001771
1772 final int action = ev.getAction();
1773
1774 switch (action & MotionEvent.ACTION_MASK) {
1775 case MotionEvent.ACTION_DOWN:
1776 /*
1777 * If being flinged and user touches, stop the fling. isFinished
1778 * will be false if being flinged.
1779 */
1780 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001781 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001782 }
1783
1784 // Remember where the motion event started
1785 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001786 mDownMotionY = mLastMotionY = ev.getY();
1787 mDownScrollX = getScrollX();
1788 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1789 mParentDownMotionX = p[0];
1790 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001791 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001792 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001794
Michael Jurka0142d492010-08-25 17:46:15 -07001795 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001796 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001797 pageBeginMoving();
1798 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001799 break;
1800
1801 case MotionEvent.ACTION_MOVE:
1802 if (mTouchState == TOUCH_STATE_SCROLLING) {
1803 // Scroll to follow the motion event
1804 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001805
1806 if (pointerIndex == -1) return true;
1807
Winson Chung321e9ee2010-08-09 13:37:56 -07001808 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001809 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001810
Adam Cohenaefd4e12011-02-14 16:39:38 -08001811 mTotalMotionX += Math.abs(deltaX);
1812
Winson Chungc0844aa2011-02-02 15:25:58 -08001813 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1814 // keep the remainder because we are actually testing if we've moved from the last
1815 // scrolled position (which is discrete).
1816 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001817 mTouchX += deltaX;
1818 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1819 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001820 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001821 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001822 } else {
1823 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001824 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001825 mLastMotionX = x;
1826 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001827 } else {
1828 awakenScrollBars();
1829 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001830 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1831 // Update the last motion position
1832 mLastMotionX = ev.getX();
1833 mLastMotionY = ev.getY();
1834
1835 // Update the parent down so that our zoom animations take this new movement into
1836 // account
1837 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1838 mParentDownMotionX = pt[0];
1839 mParentDownMotionY = pt[1];
1840 updateDragViewTranslationDuringDrag();
1841
1842 // Find the closest page to the touch point
1843 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001844
1845 // Change the drag view if we are hovering over the drop target
1846 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1847 (int) mParentDownMotionX, (int) mParentDownMotionY);
1848 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1849
Adam Cohen7d30a372013-07-01 17:03:59 -07001850 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1851 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1852 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1853 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1854
Adam Cohenf358a4b2013-07-23 16:47:31 -07001855 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1856 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1857 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001858 mTempVisiblePagesRange[0] = 0;
1859 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001860 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001861 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1862 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1863 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1864 mSidePageHoverIndex = pageUnderPointIndex;
1865 mSidePageHoverRunnable = new Runnable() {
1866 @Override
1867 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001868 // Setup the scroll to the correct page before we swap the views
1869 snapToPage(pageUnderPointIndex);
1870
1871 // For each of the pages between the paged view and the drag view,
1872 // animate them from the previous position to the new position in
1873 // the layout (as a result of the drag view moving in the layout)
1874 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1875 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1876 dragViewIndex + 1 : pageUnderPointIndex;
1877 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1878 dragViewIndex - 1 : pageUnderPointIndex;
1879 for (int i = lowerIndex; i <= upperIndex; ++i) {
1880 View v = getChildAt(i);
1881 // dragViewIndex < pageUnderPointIndex, so after we remove the
1882 // drag view all subsequent views to pageUnderPointIndex will
1883 // shift down.
1884 int oldX = getViewportOffsetX() + getChildOffset(i);
1885 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1886
1887 // Animate the view translation from its old position to its new
1888 // position
1889 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1890 if (anim != null) {
1891 anim.cancel();
1892 }
1893
1894 v.setTranslationX(oldX - newX);
1895 anim = new AnimatorSet();
1896 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1897 anim.playTogether(
1898 ObjectAnimator.ofFloat(v, "translationX", 0f));
1899 anim.start();
1900 v.setTag(anim);
1901 }
1902
1903 removeView(mDragView);
1904 onRemoveView(mDragView, false);
1905 addView(mDragView, pageUnderPointIndex);
1906 onAddView(mDragView, pageUnderPointIndex);
1907 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001908 if (mPageIndicator != null) {
1909 mPageIndicator.setActiveMarker(getNextPage());
1910 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001911 }
1912 };
1913 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1914 }
1915 } else {
1916 removeCallbacks(mSidePageHoverRunnable);
1917 mSidePageHoverIndex = -1;
1918 }
Adam Cohen564976a2010-10-13 18:52:07 -07001919 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001920 determineScrollingStart(ev);
1921 }
1922 break;
1923
1924 case MotionEvent.ACTION_UP:
1925 if (mTouchState == TOUCH_STATE_SCROLLING) {
1926 final int activePointerId = mActivePointerId;
1927 final int pointerIndex = ev.findPointerIndex(activePointerId);
1928 final float x = ev.getX(pointerIndex);
1929 final VelocityTracker velocityTracker = mVelocityTracker;
1930 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1931 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001932 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001933 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001934 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1935 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001936
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001937 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1938
Adam Cohen00481b32011-11-18 12:03:48 -08001939 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001940 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001941
Adam Cohenf358a4b2013-07-23 16:47:31 -07001942 if (!mFreeScroll) {
1943 // In the case that the page is moved far to one direction and then is flung
1944 // in the opposite direction, we use a threshold to determine whether we should
1945 // just return to the starting page, or if we should skip one further.
1946 boolean returnToOriginalPage = false;
1947 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1948 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1949 returnToOriginalPage = true;
1950 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001951
Adam Cohenf358a4b2013-07-23 16:47:31 -07001952 int finalPage;
1953 // We give flings precedence over large moves, which is why we short-circuit our
1954 // test for a large move if a fling has been registered. That is, a large
1955 // move to the left and fling to the right will register as a fling to the right.
1956 final boolean isRtl = isLayoutRtl();
1957 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1958 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1959 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1960 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1961 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1962 snapToPageWithVelocity(finalPage, velocityX);
1963 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1964 (isFling && isVelocityXLeft)) &&
1965 mCurrentPage < getChildCount() - 1) {
1966 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1967 snapToPageWithVelocity(finalPage, velocityX);
1968 } else {
1969 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001970 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001971 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001972 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001973 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001974 }
1975
1976 float scaleX = getScaleX();
1977 int vX = (int) (-velocityX * scaleX);
1978 int initialScrollX = (int) (getScrollX() * scaleX);
1979
Adam Cohenf9618852013-11-08 06:45:03 -08001980 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001981 mScroller.fling(initialScrollX,
1982 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1983 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001985 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001986 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1987 // at this point we have not moved beyond the touch slop
1988 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1989 // we can just page
1990 int nextPage = Math.max(0, mCurrentPage - 1);
1991 if (nextPage != mCurrentPage) {
1992 snapToPage(nextPage);
1993 } else {
1994 snapToDestination();
1995 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001996 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001997 // at this point we have not moved beyond the touch slop
1998 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1999 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07002000 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
2001 if (nextPage != mCurrentPage) {
2002 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002003 } else {
2004 snapToDestination();
2005 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002006 } else if (mTouchState == TOUCH_STATE_REORDERING) {
2007 // Update the last motion position
2008 mLastMotionX = ev.getX();
2009 mLastMotionY = ev.getY();
2010
2011 // Update the parent down so that our zoom animations take this new movement into
2012 // account
2013 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
2014 mParentDownMotionX = pt[0];
2015 mParentDownMotionY = pt[1];
2016 updateDragViewTranslationDuringDrag();
2017 boolean handledFling = false;
2018 if (!DISABLE_FLING_TO_DELETE) {
2019 // Check the velocity and see if we are flinging-to-delete
2020 PointF flingToDeleteVector = isFlingingToDelete();
2021 if (flingToDeleteVector != null) {
2022 onFlingToDelete(flingToDeleteVector);
2023 handledFling = true;
2024 }
2025 }
2026 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
2027 (int) mParentDownMotionY)) {
2028 onDropToDelete();
2029 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002030 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07002031 if (!mCancelTap) {
2032 onUnhandledTap(ev);
2033 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002034 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002035
2036 // Remove the callback to wait for the side page hover timeout
2037 removeCallbacks(mSidePageHoverRunnable);
2038 // End any intermediate reordering states
2039 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002040 break;
2041
2042 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002043 if (mTouchState == TOUCH_STATE_SCROLLING) {
2044 snapToDestination();
2045 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002046 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002047 break;
2048
2049 case MotionEvent.ACTION_POINTER_UP:
2050 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002051 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002052 break;
2053 }
2054
2055 return true;
2056 }
2057
Adam Cohen7d30a372013-07-01 17:03:59 -07002058 public void onFlingToDelete(View v) {}
2059 public void onRemoveView(View v, boolean deletePermanently) {}
2060 public void onRemoveViewAnimationCompleted() {}
2061 public void onAddView(View v, int index) {}
2062
2063 private void resetTouchState() {
2064 releaseVelocityTracker();
2065 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002066 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002067 mTouchState = TOUCH_STATE_REST;
2068 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002069 }
2070
Adam Cohenc2d6e892014-10-16 09:49:24 -07002071 /**
2072 * Triggered by scrolling via touch
2073 */
2074 protected void onScrollInteractionBegin() {
2075 }
2076
2077 protected void onScrollInteractionEnd() {
2078 }
2079
Adam Cohen1697b792013-09-17 19:08:21 -07002080 protected void onUnhandledTap(MotionEvent ev) {
2081 ((Launcher) getContext()).onClick(this);
2082 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002083
Winson Chung185d7162011-02-28 13:47:29 -08002084 @Override
2085 public boolean onGenericMotionEvent(MotionEvent event) {
2086 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2087 switch (event.getAction()) {
2088 case MotionEvent.ACTION_SCROLL: {
2089 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2090 final float vscroll;
2091 final float hscroll;
2092 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2093 vscroll = 0;
2094 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2095 } else {
2096 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2097 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2098 }
2099 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002100 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2101 : (hscroll > 0 || vscroll > 0);
2102 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002103 scrollRight();
2104 } else {
2105 scrollLeft();
2106 }
2107 return true;
2108 }
2109 }
2110 }
2111 }
2112 return super.onGenericMotionEvent(event);
2113 }
2114
Michael Jurkab8f06722010-10-10 15:58:46 -07002115 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2116 if (mVelocityTracker == null) {
2117 mVelocityTracker = VelocityTracker.obtain();
2118 }
2119 mVelocityTracker.addMovement(ev);
2120 }
2121
2122 private void releaseVelocityTracker() {
2123 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002124 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002125 mVelocityTracker.recycle();
2126 mVelocityTracker = null;
2127 }
2128 }
2129
Winson Chung321e9ee2010-08-09 13:37:56 -07002130 private void onSecondaryPointerUp(MotionEvent ev) {
2131 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2132 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2133 final int pointerId = ev.getPointerId(pointerIndex);
2134 if (pointerId == mActivePointerId) {
2135 // This was our active pointer going up. Choose a new
2136 // active pointer and adjust accordingly.
2137 // TODO: Make this decision more intelligent.
2138 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2139 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2140 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002141 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002142 mActivePointerId = ev.getPointerId(newPointerIndex);
2143 if (mVelocityTracker != null) {
2144 mVelocityTracker.clear();
2145 }
2146 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002147 }
2148
Winson Chung321e9ee2010-08-09 13:37:56 -07002149 @Override
2150 public void requestChildFocus(View child, View focused) {
2151 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002152 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002153 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002154 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002155 }
2156 }
2157
Winson Chung1908d072011-02-24 18:09:44 -08002158 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002159 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002160 }
2161
Adam Cohen7d30a372013-07-01 17:03:59 -07002162 int getPageNearestToPoint(float x) {
2163 int index = 0;
2164 for (int i = 0; i < getChildCount(); ++i) {
2165 if (x < getChildAt(i).getRight() - getScrollX()) {
2166 return index;
2167 } else {
2168 index++;
2169 }
2170 }
2171 return Math.min(index, getChildCount() - 1);
2172 }
2173
Adam Cohend19d3ca2010-09-15 14:43:42 -07002174 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002175 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002176 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002177 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002178 final int childCount = getChildCount();
2179 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002180 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002181 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002182 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002183 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002184 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2185 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2186 minDistanceFromScreenCenter = distanceFromScreenCenter;
2187 minDistanceFromScreenCenterIndex = i;
2188 }
2189 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002190 return minDistanceFromScreenCenterIndex;
2191 }
2192
Adam Cohen1e4359c2014-08-18 13:12:16 -07002193 protected boolean isInOverScroll() {
2194 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2195 }
2196
2197 protected int getPageSnapDuration() {
2198 if (isInOverScroll()) {
2199 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2200 }
2201 return PAGE_SNAP_ANIMATION_DURATION;
2202
2203 }
2204
Adam Cohend19d3ca2010-09-15 14:43:42 -07002205 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002206 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002207 }
2208
Adam Cohene0f66b52010-11-23 15:06:07 -08002209 private static class ScrollInterpolator implements Interpolator {
2210 public ScrollInterpolator() {
2211 }
2212
2213 public float getInterpolation(float t) {
2214 t -= 1.0f;
2215 return t*t*t*t*t + 1;
2216 }
2217 }
2218
2219 // We want the duration of the page snap animation to be influenced by the distance that
2220 // the screen has to travel, however, we don't want this duration to be effected in a
2221 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2222 // of travel has on the overall snap duration.
2223 float distanceInfluenceForSnapDuration(float f) {
2224 f -= 0.5f; // center the values about 0.
2225 f *= 0.3f * Math.PI / 2.0f;
2226 return (float) Math.sin(f);
2227 }
2228
Michael Jurka0142d492010-08-25 17:46:15 -07002229 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002230 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002231 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002232
Adam Cohenedb40762013-07-18 16:45:45 -07002233 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002234 int delta = newX - mUnboundedScrollX;
2235 int duration = 0;
2236
Adam Cohen1e4359c2014-08-18 13:12:16 -07002237 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002238 // If the velocity is low enough, then treat this more as an automatic page advance
2239 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002240 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002241 return;
2242 }
2243
2244 // Here we compute a "distance" that will be used in the computation of the overall
2245 // snap duration. This is a function of the actual distance that needs to be traveled;
2246 // we keep this value close to half screen size in order to reduce the variance in snap
2247 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002248 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002249 float distance = halfScreenSize + halfScreenSize *
2250 distanceInfluenceForSnapDuration(distanceRatio);
2251
2252 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002253 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002254
2255 // we want the page's snap velocity to approximately match the velocity at which the
2256 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002257 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2258 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002259
2260 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002261 }
2262
2263 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002264 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002265 }
2266
Adam Cohen7d30a372013-07-01 17:03:59 -07002267 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002268 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002269 }
2270
Michael Jurka0142d492010-08-25 17:46:15 -07002271 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002272 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002273 }
2274
Adam Cohenf9618852013-11-08 06:45:03 -08002275 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2276 snapToPage(whichPage, duration, false, interpolator);
2277 }
2278
2279 protected void snapToPage(int whichPage, int duration, boolean immediate,
2280 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002281 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002282
Adam Cohenedb40762013-07-18 16:45:45 -07002283 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002284 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002285 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002286 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002287 }
2288
2289 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002290 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002291 }
Michael Jurka0142d492010-08-25 17:46:15 -07002292
Adam Cohenf9618852013-11-08 06:45:03 -08002293 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2294 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002295 whichPage = validateNewPage(whichPage);
2296
Adam Cohen7d30a372013-07-01 17:03:59 -07002297 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002298 View focusedChild = getFocusedChild();
2299 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002300 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002301 focusedChild.clearFocus();
2302 }
2303
Adam Cohen53805212013-10-01 10:39:23 -07002304 sendScrollAccessibilityEvent();
2305
Michael Jurka0142d492010-08-25 17:46:15 -07002306 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002307 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002308 if (immediate) {
2309 duration = 0;
2310 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002311 duration = Math.abs(delta);
2312 }
2313
Adam Cohenf358a4b2013-07-23 16:47:31 -07002314 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002315 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002316 }
Adam Cohenf9618852013-11-08 06:45:03 -08002317
2318 if (interpolator != null) {
2319 mScroller.setInterpolator(interpolator);
2320 } else {
2321 mScroller.setInterpolator(mDefaultInterpolator);
2322 }
2323
Adam Cohen68d73932010-11-15 10:50:58 -08002324 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002325
Adam Cohen674531f2013-12-13 15:07:14 -08002326 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002327
2328 // Trigger a compute() to finish switching pages if necessary
2329 if (immediate) {
2330 computeScroll();
2331 }
2332
Winson Chung9c0565f2013-07-19 13:49:06 -07002333 // Defer loading associated pages until the scroll settles
2334 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2335
Adam Cohen7d30a372013-07-01 17:03:59 -07002336 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002337 invalidate();
2338 }
2339
Winson Chung321e9ee2010-08-09 13:37:56 -07002340 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002341 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002342 }
2343
2344 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002345 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002346 }
2347
Winson Chung86f77532010-08-24 11:08:22 -07002348 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002349 int result = -1;
2350 if (v != null) {
2351 ViewParent vp = v.getParent();
2352 int count = getChildCount();
2353 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002354 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002355 return i;
2356 }
2357 }
2358 }
2359 return result;
2360 }
2361
2362 /**
2363 * @return True is long presses are still allowed for the current touch
2364 */
2365 public boolean allowLongPress() {
2366 return mAllowLongPress;
2367 }
2368
Adam Cohendbdff6b2013-09-18 19:09:15 -07002369 @Override
2370 public boolean performLongClick() {
2371 mCancelTap = true;
2372 return super.performLongClick();
2373 }
2374
Michael Jurka0142d492010-08-25 17:46:15 -07002375 /**
2376 * Set true to allow long-press events to be triggered, usually checked by
2377 * {@link Launcher} to accept or block dpad-initiated long-presses.
2378 */
2379 public void setAllowLongPress(boolean allowLongPress) {
2380 mAllowLongPress = allowLongPress;
2381 }
2382
Winson Chung321e9ee2010-08-09 13:37:56 -07002383 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002384 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002385
2386 SavedState(Parcelable superState) {
2387 super(superState);
2388 }
2389
Adam Cohen091440a2015-03-18 14:16:05 -07002390 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002391 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002392 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002393 }
2394
2395 @Override
2396 public void writeToParcel(Parcel out, int flags) {
2397 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002398 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002399 }
2400
2401 public static final Parcelable.Creator<SavedState> CREATOR =
2402 new Parcelable.Creator<SavedState>() {
2403 public SavedState createFromParcel(Parcel in) {
2404 return new SavedState(in);
2405 }
2406
2407 public SavedState[] newArray(int size) {
2408 return new SavedState[size];
2409 }
2410 };
2411 }
2412
Winson Chungf314b0e2011-08-16 11:54:27 -07002413 protected void loadAssociatedPages(int page) {
2414 loadAssociatedPages(page, false);
2415 }
2416 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002417 if (mContentIsRefreshable) {
2418 final int count = getChildCount();
2419 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002420 int lowerPageBound = getAssociatedLowerPageBound(page);
2421 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002422 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2423 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002424 // First, clear any pages that should no longer be loaded
2425 for (int i = 0; i < count; ++i) {
2426 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002427 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002428 if (layout.getPageChildCount() > 0) {
2429 layout.removeAllViewsOnPage();
2430 }
2431 mDirtyPageContent.set(i, true);
2432 }
2433 }
2434 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002435 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002436 if ((i != page) && immediateAndOnly) {
2437 continue;
2438 }
Michael Jurka0142d492010-08-25 17:46:15 -07002439 if (lowerPageBound <= i && i <= upperPageBound) {
2440 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002441 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002442 mDirtyPageContent.set(i, false);
2443 }
Winson Chung86f77532010-08-24 11:08:22 -07002444 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002445 }
2446 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002447 }
2448 }
2449
Winson Chunge3193b92010-09-10 11:44:42 -07002450 protected int getAssociatedLowerPageBound(int page) {
2451 return Math.max(0, page - 1);
2452 }
2453 protected int getAssociatedUpperPageBound(int page) {
2454 final int count = getChildCount();
2455 return Math.min(page + 1, count - 1);
2456 }
2457
Winson Chung86f77532010-08-24 11:08:22 -07002458 /**
2459 * This method is called ONLY to synchronize the number of pages that the paged view has.
2460 * To actually fill the pages with information, implement syncPageItems() below. It is
2461 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2462 * and therefore, individual page items do not need to be updated in this method.
2463 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002464 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002465
2466 /**
2467 * This method is called to synchronize the items that are on a particular page. If views on
2468 * the page can be reused, then they should be updated within this method.
2469 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002470 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002471
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002472 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002473 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002474 }
2475 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002476 invalidatePageData(currentPage, false);
2477 }
2478 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002479 if (!mIsDataReady) {
2480 return;
2481 }
2482
Michael Jurka0142d492010-08-25 17:46:15 -07002483 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002484 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002485 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002486
Michael Jurka0142d492010-08-25 17:46:15 -07002487 // Update all the pages
2488 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002489
Winson Chung5a808352011-06-27 19:08:49 -07002490 // We must force a measure after we've loaded the pages to update the content width and
2491 // to determine the full scroll width
2492 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2493 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2494
2495 // Set a new page as the current page if necessary
2496 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002497 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002498 }
2499
Michael Jurka0142d492010-08-25 17:46:15 -07002500 // Mark each of the pages as dirty
2501 final int count = getChildCount();
2502 mDirtyPageContent.clear();
2503 for (int i = 0; i < count; ++i) {
2504 mDirtyPageContent.add(true);
2505 }
2506
2507 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002508 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002509 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002510 }
Adam Cohen06559042013-09-29 18:30:56 -07002511 if (isPageMoving()) {
2512 // If the page is moving, then snap it to the final position to ensure we don't get
2513 // stuck between pages
2514 snapToDestination();
2515 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002516 }
Winson Chung007c6982011-06-14 13:27:53 -07002517
Adam Cohen7d30a372013-07-01 17:03:59 -07002518 // Animate the drag view back to the original position
2519 void animateDragViewToOriginalPosition() {
2520 if (mDragView != null) {
2521 AnimatorSet anim = new AnimatorSet();
2522 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2523 anim.playTogether(
2524 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002525 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2526 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2527 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002528 anim.addListener(new AnimatorListenerAdapter() {
2529 @Override
2530 public void onAnimationEnd(Animator animation) {
2531 onPostReorderingAnimationCompleted();
2532 }
2533 });
2534 anim.start();
2535 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002536 }
2537
Adam Cohen7d30a372013-07-01 17:03:59 -07002538 protected void onStartReordering() {
2539 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2540 mTouchState = TOUCH_STATE_REORDERING;
2541 mIsReordering = true;
2542
Adam Cohen7d30a372013-07-01 17:03:59 -07002543 // We must invalidate to trigger a redraw to update the layers such that the drag view
2544 // is always drawn on top
2545 invalidate();
2546 }
2547
Adam Cohen091440a2015-03-18 14:16:05 -07002548 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002549 // Trigger the callback when reordering has settled
2550 --mPostReorderingPreZoomInRemainingAnimationCount;
2551 if (mPostReorderingPreZoomInRunnable != null &&
2552 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2553 mPostReorderingPreZoomInRunnable.run();
2554 mPostReorderingPreZoomInRunnable = null;
2555 }
2556 }
2557
2558 protected void onEndReordering() {
2559 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002560 }
2561
Adam Cohenf358a4b2013-07-23 16:47:31 -07002562 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002563 int dragViewIndex = indexOfChild(v);
2564
Adam Cohen327acfe2014-06-06 11:52:52 -07002565 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002566
Adam Cohen7d30a372013-07-01 17:03:59 -07002567 mTempVisiblePagesRange[0] = 0;
2568 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002569 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002570 mReorderingStarted = true;
2571
2572 // Check if we are within the reordering range
2573 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002574 dragViewIndex <= mTempVisiblePagesRange[1]) {
2575 // Find the drag view under the pointer
2576 mDragView = getChildAt(dragViewIndex);
2577 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2578 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002579 snapToPage(getPageNearestToCenterOfScreen());
2580 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002581 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002582 return true;
2583 }
2584 return false;
2585 }
2586
2587 boolean isReordering(boolean testTouchState) {
2588 boolean state = mIsReordering;
2589 if (testTouchState) {
2590 state &= (mTouchState == TOUCH_STATE_REORDERING);
2591 }
2592 return state;
2593 }
2594 void endReordering() {
2595 // For simplicity, we call endReordering sometimes even if reordering was never started.
2596 // In that case, we don't want to do anything.
2597 if (!mReorderingStarted) return;
2598 mReorderingStarted = false;
2599
2600 // If we haven't flung-to-delete the current child, then we just animate the drag view
2601 // back into position
2602 final Runnable onCompleteRunnable = new Runnable() {
2603 @Override
2604 public void run() {
2605 onEndReordering();
2606 }
2607 };
2608 if (!mDeferringForDelete) {
2609 mPostReorderingPreZoomInRunnable = new Runnable() {
2610 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002611 onCompleteRunnable.run();
2612 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002613 };
2614 };
2615
2616 mPostReorderingPreZoomInRemainingAnimationCount =
2617 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2618 // Snap to the current page
2619 snapToPage(indexOfChild(mDragView), 0);
2620 // Animate the drag view back to the front position
2621 animateDragViewToOriginalPosition();
2622 } else {
2623 // Handled in post-delete-animation-callbacks
2624 }
2625 }
2626
Adam Cohen7d30a372013-07-01 17:03:59 -07002627 /*
2628 * Flinging to delete - IN PROGRESS
2629 */
2630 private PointF isFlingingToDelete() {
2631 ViewConfiguration config = ViewConfiguration.get(getContext());
2632 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2633
2634 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2635 // Do a quick dot product test to ensure that we are flinging upwards
2636 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2637 mVelocityTracker.getYVelocity());
2638 PointF upVec = new PointF(0f, -1f);
2639 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2640 (vel.length() * upVec.length()));
2641 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2642 return vel;
2643 }
2644 }
2645 return null;
2646 }
2647
2648 /**
2649 * Creates an animation from the current drag view along its current velocity vector.
2650 * For this animation, the alpha runs for a fixed duration and we update the position
2651 * progressively.
2652 */
2653 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2654 private View mDragView;
2655 private PointF mVelocity;
2656 private Rect mFrom;
2657 private long mPrevTime;
2658 private float mFriction;
2659
2660 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2661
2662 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2663 long startTime, float friction) {
2664 mDragView = dragView;
2665 mVelocity = vel;
2666 mFrom = from;
2667 mPrevTime = startTime;
2668 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2669 }
2670
2671 @Override
2672 public void onAnimationUpdate(ValueAnimator animation) {
2673 float t = ((Float) animation.getAnimatedValue()).floatValue();
2674 long curTime = AnimationUtils.currentAnimationTimeMillis();
2675
2676 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2677 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2678
2679 mDragView.setTranslationX(mFrom.left);
2680 mDragView.setTranslationY(mFrom.top);
2681 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2682
2683 mVelocity.x *= mFriction;
2684 mVelocity.y *= mFriction;
2685 mPrevTime = curTime;
2686 }
2687 };
2688
2689 private static final int ANIM_TAG_KEY = 100;
2690
2691 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2692 return new Runnable() {
2693 @Override
2694 public void run() {
2695 int dragViewIndex = indexOfChild(dragView);
2696
2697 // For each of the pages around the drag view, animate them from the previous
2698 // position to the new position in the layout (as a result of the drag view moving
2699 // in the layout)
2700 // NOTE: We can make an assumption here because we have side-bound pages that we
2701 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002702 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002703 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2704 boolean slideFromLeft = (isLastWidgetPage ||
2705 dragViewIndex > mTempVisiblePagesRange[0]);
2706
2707 // Setup the scroll to the correct page before we swap the views
2708 if (slideFromLeft) {
2709 snapToPageImmediately(dragViewIndex - 1);
2710 }
2711
2712 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2713 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2714 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2715 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2716 ArrayList<Animator> animations = new ArrayList<Animator>();
2717 for (int i = lowerIndex; i <= upperIndex; ++i) {
2718 View v = getChildAt(i);
2719 // dragViewIndex < pageUnderPointIndex, so after we remove the
2720 // drag view all subsequent views to pageUnderPointIndex will
2721 // shift down.
2722 int oldX = 0;
2723 int newX = 0;
2724 if (slideFromLeft) {
2725 if (i == 0) {
2726 // Simulate the page being offscreen with the page spacing
2727 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002728 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002729 } else {
2730 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2731 }
2732 newX = getViewportOffsetX() + getChildOffset(i);
2733 } else {
2734 oldX = getChildOffset(i) - getChildOffset(i - 1);
2735 newX = 0;
2736 }
2737
2738 // Animate the view translation from its old position to its new
2739 // position
2740 AnimatorSet anim = (AnimatorSet) v.getTag();
2741 if (anim != null) {
2742 anim.cancel();
2743 }
2744
2745 // Note: Hacky, but we want to skip any optimizations to not draw completely
2746 // hidden views
2747 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2748 v.setTranslationX(oldX - newX);
2749 anim = new AnimatorSet();
2750 anim.playTogether(
2751 ObjectAnimator.ofFloat(v, "translationX", 0f),
2752 ObjectAnimator.ofFloat(v, "alpha", 1f));
2753 animations.add(anim);
2754 v.setTag(ANIM_TAG_KEY, anim);
2755 }
2756
2757 AnimatorSet slideAnimations = new AnimatorSet();
2758 slideAnimations.playTogether(animations);
2759 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2760 slideAnimations.addListener(new AnimatorListenerAdapter() {
2761 @Override
2762 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002763 mDeferringForDelete = false;
2764 onEndReordering();
2765 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002766 }
2767 });
2768 slideAnimations.start();
2769
2770 removeView(dragView);
2771 onRemoveView(dragView, true);
2772 }
2773 };
2774 }
2775
2776 public void onFlingToDelete(PointF vel) {
2777 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2778
2779 // NOTE: Because it takes time for the first frame of animation to actually be
2780 // called and we expect the animation to be a continuation of the fling, we have
2781 // to account for the time that has elapsed since the fling finished. And since
2782 // we don't have a startDelay, we will always get call to update when we call
2783 // start() (which we want to ignore).
2784 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2785 private int mCount = -1;
2786 private long mStartTime;
2787 private float mOffset;
2788 /* Anonymous inner class ctor */ {
2789 mStartTime = startTime;
2790 }
2791
2792 @Override
2793 public float getInterpolation(float t) {
2794 if (mCount < 0) {
2795 mCount++;
2796 } else if (mCount == 0) {
2797 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2798 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2799 mCount++;
2800 }
2801 return Math.min(1f, mOffset + t);
2802 }
2803 };
2804
2805 final Rect from = new Rect();
2806 final View dragView = mDragView;
2807 from.left = (int) dragView.getTranslationX();
2808 from.top = (int) dragView.getTranslationY();
2809 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2810 from, startTime, FLING_TO_DELETE_FRICTION);
2811
2812 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2813
2814 // Create and start the animation
2815 ValueAnimator mDropAnim = new ValueAnimator();
2816 mDropAnim.setInterpolator(tInterpolator);
2817 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2818 mDropAnim.setFloatValues(0f, 1f);
2819 mDropAnim.addUpdateListener(updateCb);
2820 mDropAnim.addListener(new AnimatorListenerAdapter() {
2821 public void onAnimationEnd(Animator animation) {
2822 onAnimationEndRunnable.run();
2823 }
2824 });
2825 mDropAnim.start();
2826 mDeferringForDelete = true;
2827 }
2828
2829 /* Drag to delete */
2830 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2831 if (mDeleteDropTarget != null) {
2832 mAltTmpRect.set(0, 0, 0, 0);
2833 View parent = (View) mDeleteDropTarget.getParent();
2834 if (parent != null) {
2835 parent.getGlobalVisibleRect(mAltTmpRect);
2836 }
2837 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2838 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2839 return mTmpRect.contains(x, y);
2840 }
2841 return false;
2842 }
2843
2844 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2845
2846 private void onDropToDelete() {
2847 final View dragView = mDragView;
2848
2849 final float toScale = 0f;
2850 final float toAlpha = 0f;
2851
2852 // Create and start the complex animation
2853 ArrayList<Animator> animations = new ArrayList<Animator>();
2854 AnimatorSet motionAnim = new AnimatorSet();
2855 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2856 motionAnim.playTogether(
2857 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2858 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2859 animations.add(motionAnim);
2860
2861 AnimatorSet alphaAnim = new AnimatorSet();
2862 alphaAnim.setInterpolator(new LinearInterpolator());
2863 alphaAnim.playTogether(
2864 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2865 animations.add(alphaAnim);
2866
2867 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2868
2869 AnimatorSet anim = new AnimatorSet();
2870 anim.playTogether(animations);
2871 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2872 anim.addListener(new AnimatorListenerAdapter() {
2873 public void onAnimationEnd(Animator animation) {
2874 onAnimationEndRunnable.run();
2875 }
2876 });
2877 anim.start();
2878
2879 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002880 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002881
2882 /* Accessibility */
2883 @Override
2884 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2885 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002886 info.setScrollable(getPageCount() > 1);
2887 if (getCurrentPage() < getPageCount() - 1) {
2888 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2889 }
2890 if (getCurrentPage() > 0) {
2891 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2892 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002893 }
2894
2895 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002896 public void sendAccessibilityEvent(int eventType) {
2897 // Don't let the view send real scroll events.
2898 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2899 super.sendAccessibilityEvent(eventType);
2900 }
2901 }
2902
2903 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002904 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2905 super.onInitializeAccessibilityEvent(event);
2906 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002907 }
2908
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002909 @Override
2910 public boolean performAccessibilityAction(int action, Bundle arguments) {
2911 if (super.performAccessibilityAction(action, arguments)) {
2912 return true;
2913 }
2914 switch (action) {
2915 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2916 if (getCurrentPage() < getPageCount() - 1) {
2917 scrollRight();
2918 return true;
2919 }
2920 } break;
2921 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2922 if (getCurrentPage() > 0) {
2923 scrollLeft();
2924 return true;
2925 }
2926 } break;
2927 }
2928 return false;
2929 }
2930
Adam Cohen0ffac432013-07-10 11:19:26 -07002931 protected String getCurrentPageDescription() {
2932 return String.format(getContext().getString(R.string.default_scroll_format),
2933 getNextPage() + 1, getChildCount());
2934 }
2935
Winson Chungd11265e2011-08-30 13:37:23 -07002936 @Override
2937 public boolean onHoverEvent(android.view.MotionEvent event) {
2938 return true;
2939 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002940}