blob: f77ad0564fa68a2b2375e031d424ea23fcff2b6d [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 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700562 public 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()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700715 if (mCurrentPage != getNextPage()) {
716 AccessibilityEvent ev =
717 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
718 ev.setItemCount(getChildCount());
719 ev.setFromIndex(getNextPage());
720 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700721
Vadim Tryshevf4715972015-05-08 17:21:03 -0700722 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700723 }
Adam Cohen53805212013-10-01 10:39:23 -0700724 }
725 }
726
Michael Jurka0142d492010-08-25 17:46:15 -0700727 // we moved this functionality to a helper function so SmoothPagedView can reuse it
728 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700730 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (getScrollX() != mScroller.getCurrX()
732 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700733 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700734 float scaleX = mFreeScroll ? getScaleX() : 1f;
735 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
736 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700737 }
Michael Jurka0142d492010-08-25 17:46:15 -0700738 invalidate();
739 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700740 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700741 sendScrollAccessibilityEvent();
742
Adam Cohen6f127a62014-06-12 14:54:41 -0700743 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700744 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700745 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700746
Winson Chung9c0565f2013-07-19 13:49:06 -0700747 // Load the associated pages if necessary
748 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
749 loadAssociatedPages(mCurrentPage);
750 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
751 }
752
Adam Cohen73aa9752010-11-24 16:26:58 -0800753 // We don't want to trigger a page end moving unless the page has settled
754 // and the user has stopped scrolling
755 if (mTouchState == TOUCH_STATE_REST) {
756 pageEndMoving();
757 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700758
Adam Cohen7d30a372013-07-01 17:03:59 -0700759 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700760 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700761 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700762 if (am.isEnabled()) {
763 // Notify the user when the page changes
764 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700765 }
Michael Jurka0142d492010-08-25 17:46:15 -0700766 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 }
Michael Jurka0142d492010-08-25 17:46:15 -0700768 return false;
769 }
770
771 @Override
772 public void computeScroll() {
773 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 }
775
Adam Cohen7d30a372013-07-01 17:03:59 -0700776 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
777 return mTopAlignPageWhenShrinkingForBouncer;
778 }
779
Adam Cohen96d30a12013-07-16 18:13:21 -0700780 public static class LayoutParams extends ViewGroup.LayoutParams {
781 public boolean isFullScreenPage = false;
782
783 /**
784 * {@inheritDoc}
785 */
786 public LayoutParams(int width, int height) {
787 super(width, height);
788 }
789
790 public LayoutParams(ViewGroup.LayoutParams source) {
791 super(source);
792 }
793 }
794
795 protected LayoutParams generateDefaultLayoutParams() {
796 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
797 }
798
Adam Cohenedb40762013-07-18 16:45:45 -0700799 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700800 LayoutParams lp = generateDefaultLayoutParams();
801 lp.isFullScreenPage = true;
802 super.addView(page, 0, lp);
803 }
804
Adam Cohen410f3cd2013-09-22 12:09:32 -0700805 public int getNormalChildHeight() {
806 return mNormalChildHeight;
807 }
808
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 @Override
810 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700812 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
813 return;
814 }
815
Adam Cohen7d30a372013-07-01 17:03:59 -0700816 // We measure the dimensions of the PagedView to be larger than the pages so that when we
817 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700818 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
819 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
820 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700821 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800822 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700823 // viewport, we can be at most one and a half screens offset once we scale down
824 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700825 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
826 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700827
Winson Chungc82d2622013-11-06 13:23:29 -0800828 int parentWidthSize = (int) (2f * maxSize);
829 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700830 int scaledWidthSize, scaledHeightSize;
831 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700832 scaledWidthSize = (int) (parentWidthSize / mMinScale);
833 scaledHeightSize = (int) (parentHeightSize / mMinScale);
834 } else {
835 scaledWidthSize = widthSize;
836 scaledHeightSize = heightSize;
837 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700838 mViewport.set(0, 0, widthSize, heightSize);
839
840 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
841 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
842 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844
Winson Chung8aad6102012-05-11 16:27:49 -0700845 // Return early if we aren't given a proper dimension
846 if (widthSize <= 0 || heightSize <= 0) {
847 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
848 return;
849 }
850
Adam Lesinski6b879f02010-11-04 16:15:23 -0700851 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
852 * of the All apps view on XLarge displays to not take up more space then it needs. Width
853 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
854 * each page to have the same width.
855 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700856 final int verticalPadding = getPaddingTop() + getPaddingBottom();
857 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700858
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700859 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700861 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700862 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700863 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
864 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
865 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
866 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 final int childCount = getChildCount();
868 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700869 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700870 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100871 if (child.getVisibility() != GONE) {
872 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700873
Vladimir Marko2824b072013-10-04 16:42:17 +0100874 int childWidthMode;
875 int childHeightMode;
876 int childWidth;
877 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700878
Vladimir Marko2824b072013-10-04 16:42:17 +0100879 if (!lp.isFullScreenPage) {
880 if (lp.width == LayoutParams.WRAP_CONTENT) {
881 childWidthMode = MeasureSpec.AT_MOST;
882 } else {
883 childWidthMode = MeasureSpec.EXACTLY;
884 }
885
886 if (lp.height == LayoutParams.WRAP_CONTENT) {
887 childHeightMode = MeasureSpec.AT_MOST;
888 } else {
889 childHeightMode = MeasureSpec.EXACTLY;
890 }
891
Adam Cohen3b185e22013-10-29 14:45:58 -0700892 childWidth = getViewportWidth() - horizontalPadding
893 - mInsets.left - mInsets.right;
894 childHeight = getViewportHeight() - verticalPadding
895 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100896 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700897 } else {
898 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700899 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100900
Adam Cohen3b185e22013-10-29 14:45:58 -0700901 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
902 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700903 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700904 if (referenceChildWidth == 0) {
905 referenceChildWidth = childWidth;
906 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700907
Vladimir Marko2824b072013-10-04 16:42:17 +0100908 final int childWidthMeasureSpec =
909 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
910 final int childHeightMeasureSpec =
911 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
912 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700913 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700914 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700915 if (mSpacePagesAutomatically) {
916 int spacing = (getViewportWidth() - mInsets.left - mInsets.right
917 - referenceChildWidth) / 2;
918 if (spacing >= 0) {
919 setPageSpacing(spacing);
920 }
921 mSpacePagesAutomatically = false;
922 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700923 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800924 }
925
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700926 /**
927 * This method should be called once before first layout / measure pass.
928 */
929 protected void setSinglePageInViewport() {
930 mSpacePagesAutomatically = true;
931 }
932
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700934 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700936 return;
937 }
938
Winson Chung785d2eb2011-04-14 16:08:02 -0700939 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700941
Adam Cohen7d30a372013-07-01 17:03:59 -0700942 int offsetX = getViewportOffsetX();
943 int offsetY = getViewportOffsetY();
944
945 // Update the viewport offsets
946 mViewport.offset(offsetX, offsetY);
947
Adam Cohen0ffac432013-07-10 11:19:26 -0700948 final boolean isRtl = isLayoutRtl();
949
950 final int startIndex = isRtl ? childCount - 1 : 0;
951 final int endIndex = isRtl ? -1 : childCount;
952 final int delta = isRtl ? -1 : 1;
953
Adam Cohen7d30a372013-07-01 17:03:59 -0700954 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700955
Adam Cohen3b185e22013-10-29 14:45:58 -0700956 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000957 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700958
959 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700960 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
961 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700962 }
963
Adam Cohen0ffac432013-07-10 11:19:26 -0700964 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700965 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700967 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100968 int childTop;
969 if (lp.isFullScreenPage) {
970 childTop = offsetY;
971 } else {
972 childTop = offsetY + getPaddingTop() + mInsets.top;
973 if (mCenterPagesVertically) {
974 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
975 }
976 }
977
Adam Cohen96d30a12013-07-16 18:13:21 -0700978 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700979 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800980
Winson Chung785d2eb2011-04-14 16:08:02 -0700981 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700982 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800983 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700984
Adam Cohen3b185e22013-10-29 14:45:58 -0700985 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
986 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000987
988 int pageGap = mPageSpacing;
989 int next = i + delta;
990 if (next != endIndex) {
991 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
992 } else {
993 nextLp = null;
994 }
995
996 // Prevent full screen pages from showing in the viewport
997 // when they are not the current page.
998 if (lp.isFullScreenPage) {
999 pageGap = getPaddingLeft();
1000 } else if (nextLp != null && nextLp.isFullScreenPage) {
1001 pageGap = getPaddingRight();
1002 }
1003
1004 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 }
1006 }
Winson Chungc3665fa2011-09-14 17:56:27 -07001007
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001008 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
Michael Jurkadd6c0912012-01-16 06:46:20 -08001009 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -07001010 mFirstLayout = false;
1011 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001012
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001013 final LayoutTransition transition = getLayoutTransition();
1014 // If the transition is running defer updating max scroll, as some empty pages could
1015 // still be present, and a max scroll change could cause sudden jumps in scroll.
1016 if (transition != null && transition.isRunning()) {
1017 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
1018
1019 @Override
1020 public void startTransition(LayoutTransition transition, ViewGroup container,
1021 View view, int transitionType) { }
1022
1023 @Override
1024 public void endTransition(LayoutTransition transition, ViewGroup container,
1025 View view, int transitionType) {
1026 // Wait until all transitions are complete.
1027 if (!transition.isRunning()) {
1028 transition.removeTransitionListener(this);
1029 updateMaxScrollX();
1030 }
1031 }
1032 });
Adam Cohenf698c6e2013-07-17 18:44:25 -07001033 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001034 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -07001035 }
1036
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001037 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount &&
Adam Cohenf698c6e2013-07-17 18:44:25 -07001038 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001039 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001040 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -07001041 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -07001042 } else {
1043 setCurrentPage(getNextPage());
1044 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001045 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001046 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001047
1048 if (isReordering(true)) {
1049 updateDragViewTranslationDuringDrag();
1050 }
Winson Chunge3193b92010-09-10 11:44:42 -07001051 }
1052
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -07001053 private void updateMaxScrollX() {
1054 int childCount = getChildCount();
1055 if (childCount > 0) {
1056 final int index = isLayoutRtl() ? 0 : childCount - 1;
1057 mMaxScrollX = getScrollForPage(index);
1058 } else {
1059 mMaxScrollX = 0;
1060 }
1061 }
1062
Adam Cohen3b185e22013-10-29 14:45:58 -07001063 public void setPageSpacing(int pageSpacing) {
1064 mPageSpacing = pageSpacing;
1065 requestLayout();
1066 }
1067
Adam Cohenf34bab52010-09-30 14:11:56 -07001068 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001069 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1070
1071 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001072 for (int i = 0; i < getChildCount(); i++) {
1073 View child = getChildAt(i);
1074 if (child != null) {
1075 float scrollProgress = getScrollProgress(screenCenter, child, i);
1076 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001077 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001078 }
1079 }
1080 invalidate();
1081 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001082 }
1083
Winson Chungc58497e2013-09-03 17:48:37 -07001084 protected void enablePagedViewAnimations() {
1085 mAllowPagedViewAnimations = true;
1086
1087 }
1088 protected void disablePagedViewAnimations() {
1089 mAllowPagedViewAnimations = false;
1090 }
1091
Winson Chunge3193b92010-09-10 11:44:42 -07001092 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001093 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001094 // Update the page indicator, we don't update the page indicator as we
1095 // add/remove pages
1096 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001097 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001098 mPageIndicator.addMarker(pageIndex,
1099 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001100 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001101 }
1102
Adam Cohen2591f6a2011-10-25 14:36:40 -07001103 // This ensures that when children are added, they get the correct transforms / alphas
1104 // in accordance with any scroll effects.
1105 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001106 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001107 invalidate();
1108 }
1109
Michael Jurka8b805b12012-04-18 14:23:14 -07001110 @Override
1111 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001112 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001113 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001114 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001115 }
1116
Winson Chungd2be3812013-07-16 11:11:32 -07001117 private void removeMarkerForView(int index) {
1118 // Update the page indicator, we don't update the page indicator as we
1119 // add/remove pages
1120 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001121 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001122 }
1123 }
1124
1125 @Override
1126 public void removeView(View v) {
1127 // XXX: We should find a better way to hook into this before the view
1128 // gets removed form its parent...
1129 removeMarkerForView(indexOfChild(v));
1130 super.removeView(v);
1131 }
1132 @Override
1133 public void removeViewInLayout(View v) {
1134 // XXX: We should find a better way to hook into this before the view
1135 // gets removed form its parent...
1136 removeMarkerForView(indexOfChild(v));
1137 super.removeViewInLayout(v);
1138 }
1139 @Override
1140 public void removeViewAt(int index) {
1141 // XXX: We should find a better way to hook into this before the view
1142 // gets removed form its parent...
1143 removeViewAt(index);
1144 super.removeViewAt(index);
1145 }
1146 @Override
1147 public void removeAllViewsInLayout() {
1148 // Update the page indicator, we don't update the page indicator as we
1149 // add/remove pages
1150 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001151 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001152 }
1153
1154 super.removeAllViewsInLayout();
1155 }
1156
Adam Cohen73894962011-10-31 13:17:17 -07001157 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001158 if (index < 0 || index > getChildCount() - 1) return 0;
1159
Adam Cohenf698c6e2013-07-17 18:44:25 -07001160 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001161
Adam Cohenf698c6e2013-07-17 18:44:25 -07001162 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001163 }
1164
Adam Cohen6f127a62014-06-12 14:54:41 -07001165 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001166 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001167 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001168 }
1169
Michael Jurkadde558b2011-11-09 22:09:06 -08001170 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001171 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001172 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001173
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001174 range[0] = -1;
1175 range[1] = -1;
1176
Michael Jurkac4fb9172010-09-02 17:19:20 -07001177 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001178 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001179 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001180
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001181 int count = getChildCount();
1182 for (int i = 0; i < count; i++) {
1183 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001184
Winson Chungc9ca2982013-07-19 12:07:38 -07001185 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001186 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001187 if (mTmpIntPoint[0] > viewportWidth) {
1188 if (range[0] == -1) {
1189 continue;
1190 } else {
1191 break;
1192 }
1193 }
1194
Adam Cohen6a678da2013-09-24 10:58:01 -07001195 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001196 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1197 if (mTmpIntPoint[0] < 0) {
1198 if (range[0] == -1) {
1199 continue;
1200 } else {
1201 break;
1202 }
1203 }
1204 curScreen = i;
1205 if (range[0] < 0) {
1206 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001207 }
1208 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001209
1210 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001211 } else {
1212 range[0] = -1;
1213 range[1] = -1;
1214 }
1215 }
1216
Michael Jurka920d7f42012-05-14 16:29:55 -07001217 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001218 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001219 }
1220
Michael Jurkadde558b2011-11-09 22:09:06 -08001221 @Override
1222 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001223 // Find out which screens are visible; as an optimization we only call draw on them
1224 final int pageCount = getChildCount();
1225 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001226 int halfScreenSize = getViewportWidth() / 2;
1227 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1228 // Otherwise it is equal to the scaled overscroll position.
1229 int screenCenter = mOverScrollX + halfScreenSize;
1230
1231 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1232 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1233 // set it for the next frame
1234 mForceScreenScrolled = false;
1235 screenScrolled(screenCenter);
1236 mLastScreenCenter = screenCenter;
1237 }
1238
Michael Jurkadde558b2011-11-09 22:09:06 -08001239 getVisiblePages(mTempVisiblePagesRange);
1240 final int leftScreen = mTempVisiblePagesRange[0];
1241 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001242 if (leftScreen != -1 && rightScreen != -1) {
1243 final long drawingTime = getDrawingTime();
1244 // Clip to the bounds
1245 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001246 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1247 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001248
Adam Cohen7d30a372013-07-01 17:03:59 -07001249 // Draw all the children, leaving the drag view for last
1250 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001251 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001252 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001253 if (mForceDrawAllChildrenNextFrame ||
1254 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001255 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001256 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001257 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001258 // Draw the drag view on top (if there is one)
1259 if (mDragView != null) {
1260 drawChild(canvas, mDragView, drawingTime);
1261 }
1262
Michael Jurka5e368ff2012-05-14 23:13:15 -07001263 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001264 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001265 }
Michael Jurka0142d492010-08-25 17:46:15 -07001266 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 }
1268
1269 @Override
1270 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001271 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001272 if (page != mCurrentPage || !mScroller.isFinished()) {
1273 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 return true;
1275 }
1276 return false;
1277 }
1278
1279 @Override
1280 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001281 int focusablePage;
1282 if (mNextPage != INVALID_PAGE) {
1283 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001284 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001285 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 }
Winson Chung86f77532010-08-24 11:08:22 -07001287 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001289 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 }
1291 return false;
1292 }
1293
1294 @Override
1295 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001296 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001298 if (getCurrentPage() > 0) {
1299 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 return true;
1301 }
1302 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001303 if (getCurrentPage() < getPageCount() - 1) {
1304 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 return true;
1306 }
1307 }
1308 return super.dispatchUnhandledMove(focused, direction);
1309 }
1310
1311 @Override
1312 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001313 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001314 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001315 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 }
1317 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001318 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001319 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 }
1321 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001322 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001323 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 }
1325 }
1326 }
1327
1328 /**
1329 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001330 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 *
Winson Chung86f77532010-08-24 11:08:22 -07001332 * This happens when live folders requery, and if they're off page, they
1333 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001334 */
1335 @Override
1336 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001337 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 View v = focused;
1339 while (true) {
1340 if (v == current) {
1341 super.focusableViewAvailable(focused);
1342 return;
1343 }
1344 if (v == this) {
1345 return;
1346 }
1347 ViewParent parent = v.getParent();
1348 if (parent instanceof View) {
1349 v = (View)v.getParent();
1350 } else {
1351 return;
1352 }
1353 }
1354 }
1355
1356 /**
1357 * {@inheritDoc}
1358 */
1359 @Override
1360 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1361 if (disallowIntercept) {
1362 // We need to make sure to cancel our long press if
1363 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001364 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001365 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 }
1367 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1368 }
1369
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001370 /**
1371 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1372 */
1373 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001374 if (isLayoutRtl()) {
1375 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001376 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001377 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001378 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001379 }
1380
1381 /**
1382 * Return true if a tap at (x, y) should trigger a flip to the next page.
1383 */
1384 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001385 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001386 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001387 }
1388 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001389 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001390 }
Winson Chung52aee602013-01-30 12:01:02 -08001391
Adam Cohen7d30a372013-07-01 17:03:59 -07001392 /** Returns whether x and y originated within the buffered viewport */
1393 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1394 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1395 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1396 return mTmpRect.contains(x, y);
1397 }
1398
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 @Override
1400 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001401 if (DISABLE_TOUCH_INTERACTION) {
1402 return false;
1403 }
1404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 /*
1406 * This method JUST determines whether we want to intercept the motion.
1407 * If we return true, onTouchEvent will be called and we do the actual
1408 * scrolling there.
1409 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001410 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001411
Winson Chung45e1d6e2010-11-09 17:19:49 -08001412 // Skip touch handling if there are no pages to swipe
1413 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 /*
1416 * Shortcut the most recurring case: the user is in the dragging
1417 * state and he is moving his finger. We want to intercept this
1418 * motion.
1419 */
1420 final int action = ev.getAction();
1421 if ((action == MotionEvent.ACTION_MOVE) &&
1422 (mTouchState == TOUCH_STATE_SCROLLING)) {
1423 return true;
1424 }
1425
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 switch (action & MotionEvent.ACTION_MASK) {
1427 case MotionEvent.ACTION_MOVE: {
1428 /*
1429 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1430 * whether the user has moved far enough from his original down touch.
1431 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001432 if (mActivePointerId != INVALID_POINTER) {
1433 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001434 }
1435 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1436 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1437 // i.e. fall through to the next case (don't break)
1438 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1439 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001440 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 }
1442
1443 case MotionEvent.ACTION_DOWN: {
1444 final float x = ev.getX();
1445 final float y = ev.getY();
1446 // Remember location of down touch
1447 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001448 mDownMotionY = y;
1449 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001450 mLastMotionX = x;
1451 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001452 float[] p = mapPointFromViewToParent(this, x, y);
1453 mParentDownMotionX = p[0];
1454 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001455 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001456 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001458
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 /*
1460 * If being flinged and user touches the screen, initiate drag;
1461 * otherwise don't. mScroller.isFinished should be false when
1462 * being flinged.
1463 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001464 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001465 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001466
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001467 if (finishedScrolling) {
1468 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001469 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001470 setCurrentPage(getNextPage());
1471 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001472 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001473 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001474 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1475 mTouchState = TOUCH_STATE_SCROLLING;
1476 } else {
1477 mTouchState = TOUCH_STATE_REST;
1478 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001479 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001480
Winson Chung86f77532010-08-24 11:08:22 -07001481 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001483 if (!DISABLE_TOUCH_SIDE_PAGES) {
1484 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1485 if (getChildCount() > 0) {
1486 if (hitsPreviousPage(x, y)) {
1487 mTouchState = TOUCH_STATE_PREV_PAGE;
1488 } else if (hitsNextPage(x, y)) {
1489 mTouchState = TOUCH_STATE_NEXT_PAGE;
1490 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 }
1492 }
1493 }
1494 break;
1495 }
1496
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001498 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001499 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 break;
1501
1502 case MotionEvent.ACTION_POINTER_UP:
1503 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001504 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 break;
1506 }
1507
1508 /*
1509 * The only time we want to intercept motion events is if we are in the
1510 * drag mode.
1511 */
1512 return mTouchState != TOUCH_STATE_REST;
1513 }
1514
Adam Cohenf8d28232011-02-01 21:47:00 -08001515 protected void determineScrollingStart(MotionEvent ev) {
1516 determineScrollingStart(ev, 1.0f);
1517 }
1518
Winson Chung321e9ee2010-08-09 13:37:56 -07001519 /*
1520 * Determines if we should change the touch state to start scrolling after the
1521 * user moves their touch point too far.
1522 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001523 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001524 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001526 if (pointerIndex == -1) return;
1527
1528 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001529 final float x = ev.getX(pointerIndex);
1530 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001531 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1532
Winson Chung321e9ee2010-08-09 13:37:56 -07001533 final int xDiff = (int) Math.abs(x - mLastMotionX);
1534 final int yDiff = (int) Math.abs(y - mLastMotionY);
1535
Adam Cohenf8d28232011-02-01 21:47:00 -08001536 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001537 boolean xPaged = xDiff > mPagingTouchSlop;
1538 boolean xMoved = xDiff > touchSlop;
1539 boolean yMoved = yDiff > touchSlop;
1540
Adam Cohenf8d28232011-02-01 21:47:00 -08001541 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001542 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001543 // Scroll if the user moved far enough along the X axis
1544 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001545 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001546 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001547 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001548 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001549 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohenc2d6e892014-10-16 09:49:24 -07001550 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001551 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001553 }
1554 }
1555
Adam Cohen7d30a372013-07-01 17:03:59 -07001556 protected float getMaxScrollProgress() {
1557 return 1.0f;
1558 }
1559
Adam Cohenf8d28232011-02-01 21:47:00 -08001560 protected void cancelCurrentPageLongPress() {
1561 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001562 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001563 // Try canceling the long press. It could also have been scheduled
1564 // by a distant descendant, so use the mAllowLongPress flag to block
1565 // everything
1566 final View currentPage = getPageAt(mCurrentPage);
1567 if (currentPage != null) {
1568 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 }
1570 }
1571 }
1572
Adam Cohen7d30a372013-07-01 17:03:59 -07001573 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1574 final int halfScreenSize = getViewportWidth() / 2;
1575
1576 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1577 screenCenter = Math.max(halfScreenSize, screenCenter);
1578
1579 return getScrollProgress(screenCenter, v, page);
1580 }
1581
Adam Cohenb5ba0972011-09-07 18:02:31 -07001582 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001583 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001584
Adam Cohenedb40762013-07-18 16:45:45 -07001585 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001586 int count = getChildCount();
1587
1588 final int totalDistance;
1589
1590 int adjacentPage = page + 1;
1591 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1592 adjacentPage = page - 1;
1593 }
1594
1595 if (adjacentPage < 0 || adjacentPage > count - 1) {
1596 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1597 } else {
1598 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1599 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001600
1601 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001602 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1603 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001604 return scrollProgress;
1605 }
1606
Adam Cohenedb40762013-07-18 16:45:45 -07001607 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001608 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001609 return 0;
1610 } else {
1611 return mPageScrolls[index];
1612 }
1613 }
1614
Adam Cohen564a2e72013-10-09 14:47:32 -07001615 // While layout transitions are occurring, a child's position may stray from its baseline
1616 // position. This method returns the magnitude of this stray at any given time.
1617 public int getLayoutTransitionOffsetForPage(int index) {
1618 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1619 return 0;
1620 } else {
1621 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001622
1623 int scrollOffset = 0;
1624 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1625 if (!lp.isFullScreenPage) {
1626 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1627 }
1628
Adam Cohen564a2e72013-10-09 14:47:32 -07001629 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1630 return (int) (child.getX() - baselineX);
1631 }
1632 }
1633
Adam Cohene0f66b52010-11-23 15:06:07 -08001634 // This curve determines how the effect of scrolling over the limits of the page dimishes
1635 // as the user pulls further and further from the bounds
1636 private float overScrollInfluenceCurve(float f) {
1637 f -= 1.0f;
1638 return f * f * f + 1.0f;
1639 }
1640
Adam Cohen1e4359c2014-08-18 13:12:16 -07001641 protected float acceleratedOverFactor(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001642 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001643
1644 // We want to reach the max over scroll effect when the user has
1645 // over scrolled half the size of the screen
1646 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1647
Adam Cohen1e4359c2014-08-18 13:12:16 -07001648 if (f == 0) return 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001649
1650 // Clamp this factor, f, to -1 < f < 1
1651 if (Math.abs(f) >= 1) {
1652 f /= Math.abs(f);
1653 }
Adam Cohen1e4359c2014-08-18 13:12:16 -07001654 return f;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001655 }
1656
1657 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001658 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001659
1660 float f = (amount / screenSize);
1661
1662 if (f == 0) return;
1663 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1664
Adam Cohen7bfc9792011-01-28 13:52:37 -08001665 // Clamp this factor, f, to -1 < f < 1
1666 if (Math.abs(f) >= 1) {
1667 f /= Math.abs(f);
1668 }
1669
Adam Cohene0f66b52010-11-23 15:06:07 -08001670 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001671 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001672 mOverScrollX = overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001673 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001674 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001675 mOverScrollX = mMaxScrollX + overScrollAmount;
Adam Cohen1e4359c2014-08-18 13:12:16 -07001676 super.scrollTo(mOverScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001677 }
1678 invalidate();
1679 }
1680
Adam Cohenb5ba0972011-09-07 18:02:31 -07001681 protected void overScroll(float amount) {
1682 dampedOverScroll(amount);
1683 }
1684
Michael Jurkac5b262c2011-01-12 20:24:50 -08001685 protected float maxOverScroll() {
1686 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001687 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001688 float f = 1.0f;
1689 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1690 return OVERSCROLL_DAMP_FACTOR * f;
1691 }
1692
Winson Chungdc61c4d2015-04-20 18:26:57 -07001693 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001694 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001695 }
1696
Winson Chungdc61c4d2015-04-20 18:26:57 -07001697 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001698 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001699 }
1700
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001701 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001702 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001703 if (isLayoutRtl()) {
1704 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1705 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1706 } else {
1707 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1708 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1709 }
1710 }
1711
Adam Cohenf9618852013-11-08 06:45:03 -08001712 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001713 mFreeScroll = freeScroll;
1714
Adam Cohenf9618852013-11-08 06:45:03 -08001715 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001716 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001717 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001718 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1719 setCurrentPage(mTempVisiblePagesRange[0]);
1720 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1721 setCurrentPage(mTempVisiblePagesRange[1]);
1722 }
1723 }
1724
1725 setEnableOverscroll(!freeScroll);
1726 }
1727
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001728 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001729 mAllowOverScroll = enable;
1730 }
1731
1732 int getNearestHoverOverPageIndex() {
1733 if (mDragView != null) {
1734 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1735 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001736 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001737 int minDistance = Integer.MAX_VALUE;
1738 int minIndex = indexOfChild(mDragView);
1739 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1740 View page = getPageAt(i);
1741 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1742 int d = Math.abs(dragX - pageX);
1743 if (d < minDistance) {
1744 minIndex = i;
1745 minDistance = d;
1746 }
1747 }
1748 return minIndex;
1749 }
1750 return -1;
1751 }
1752
Winson Chung321e9ee2010-08-09 13:37:56 -07001753 @Override
1754 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001755 if (DISABLE_TOUCH_INTERACTION) {
1756 return false;
1757 }
1758
Adam Cohen1697b792013-09-17 19:08:21 -07001759 super.onTouchEvent(ev);
1760
Winson Chung45e1d6e2010-11-09 17:19:49 -08001761 // Skip touch handling if there are no pages to swipe
1762 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1763
Michael Jurkab8f06722010-10-10 15:58:46 -07001764 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001765
1766 final int action = ev.getAction();
1767
1768 switch (action & MotionEvent.ACTION_MASK) {
1769 case MotionEvent.ACTION_DOWN:
1770 /*
1771 * If being flinged and user touches, stop the fling. isFinished
1772 * will be false if being flinged.
1773 */
1774 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001775 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001776 }
1777
1778 // Remember where the motion event started
1779 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001780 mDownMotionY = mLastMotionY = ev.getY();
1781 mDownScrollX = getScrollX();
1782 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1783 mParentDownMotionX = p[0];
1784 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001785 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001786 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001787 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001788
Michael Jurka0142d492010-08-25 17:46:15 -07001789 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001790 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001791 pageBeginMoving();
1792 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001793 break;
1794
1795 case MotionEvent.ACTION_MOVE:
1796 if (mTouchState == TOUCH_STATE_SCROLLING) {
1797 // Scroll to follow the motion event
1798 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001799
1800 if (pointerIndex == -1) return true;
1801
Winson Chung321e9ee2010-08-09 13:37:56 -07001802 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001803 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001804
Adam Cohenaefd4e12011-02-14 16:39:38 -08001805 mTotalMotionX += Math.abs(deltaX);
1806
Winson Chungc0844aa2011-02-02 15:25:58 -08001807 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1808 // keep the remainder because we are actually testing if we've moved from the last
1809 // scrolled position (which is discrete).
1810 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001811 mTouchX += deltaX;
1812 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1813 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001814 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001815 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001816 } else {
1817 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001818 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001819 mLastMotionX = x;
1820 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001821 } else {
1822 awakenScrollBars();
1823 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001824 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1825 // Update the last motion position
1826 mLastMotionX = ev.getX();
1827 mLastMotionY = ev.getY();
1828
1829 // Update the parent down so that our zoom animations take this new movement into
1830 // account
1831 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1832 mParentDownMotionX = pt[0];
1833 mParentDownMotionY = pt[1];
1834 updateDragViewTranslationDuringDrag();
1835
1836 // Find the closest page to the touch point
1837 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001838
1839 // Change the drag view if we are hovering over the drop target
1840 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1841 (int) mParentDownMotionX, (int) mParentDownMotionY);
1842 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1843
Adam Cohen7d30a372013-07-01 17:03:59 -07001844 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1845 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1846 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1847 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1848
Adam Cohenf358a4b2013-07-23 16:47:31 -07001849 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1850 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1851 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001852 mTempVisiblePagesRange[0] = 0;
1853 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001854 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001855 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1856 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1857 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1858 mSidePageHoverIndex = pageUnderPointIndex;
1859 mSidePageHoverRunnable = new Runnable() {
1860 @Override
1861 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001862 // Setup the scroll to the correct page before we swap the views
1863 snapToPage(pageUnderPointIndex);
1864
1865 // For each of the pages between the paged view and the drag view,
1866 // animate them from the previous position to the new position in
1867 // the layout (as a result of the drag view moving in the layout)
1868 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1869 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1870 dragViewIndex + 1 : pageUnderPointIndex;
1871 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1872 dragViewIndex - 1 : pageUnderPointIndex;
1873 for (int i = lowerIndex; i <= upperIndex; ++i) {
1874 View v = getChildAt(i);
1875 // dragViewIndex < pageUnderPointIndex, so after we remove the
1876 // drag view all subsequent views to pageUnderPointIndex will
1877 // shift down.
1878 int oldX = getViewportOffsetX() + getChildOffset(i);
1879 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1880
1881 // Animate the view translation from its old position to its new
1882 // position
1883 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1884 if (anim != null) {
1885 anim.cancel();
1886 }
1887
1888 v.setTranslationX(oldX - newX);
1889 anim = new AnimatorSet();
1890 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1891 anim.playTogether(
1892 ObjectAnimator.ofFloat(v, "translationX", 0f));
1893 anim.start();
1894 v.setTag(anim);
1895 }
1896
1897 removeView(mDragView);
1898 onRemoveView(mDragView, false);
1899 addView(mDragView, pageUnderPointIndex);
1900 onAddView(mDragView, pageUnderPointIndex);
1901 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001902 if (mPageIndicator != null) {
1903 mPageIndicator.setActiveMarker(getNextPage());
1904 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001905 }
1906 };
1907 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1908 }
1909 } else {
1910 removeCallbacks(mSidePageHoverRunnable);
1911 mSidePageHoverIndex = -1;
1912 }
Adam Cohen564976a2010-10-13 18:52:07 -07001913 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001914 determineScrollingStart(ev);
1915 }
1916 break;
1917
1918 case MotionEvent.ACTION_UP:
1919 if (mTouchState == TOUCH_STATE_SCROLLING) {
1920 final int activePointerId = mActivePointerId;
1921 final int pointerIndex = ev.findPointerIndex(activePointerId);
1922 final float x = ev.getX(pointerIndex);
1923 final VelocityTracker velocityTracker = mVelocityTracker;
1924 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1925 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001926 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001927 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001928 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1929 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001930
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001931 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1932
Adam Cohen00481b32011-11-18 12:03:48 -08001933 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001934 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001935
Adam Cohenf358a4b2013-07-23 16:47:31 -07001936 if (!mFreeScroll) {
1937 // In the case that the page is moved far to one direction and then is flung
1938 // in the opposite direction, we use a threshold to determine whether we should
1939 // just return to the starting page, or if we should skip one further.
1940 boolean returnToOriginalPage = false;
1941 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1942 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1943 returnToOriginalPage = true;
1944 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001945
Adam Cohenf358a4b2013-07-23 16:47:31 -07001946 int finalPage;
1947 // We give flings precedence over large moves, which is why we short-circuit our
1948 // test for a large move if a fling has been registered. That is, a large
1949 // move to the left and fling to the right will register as a fling to the right.
1950 final boolean isRtl = isLayoutRtl();
1951 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1952 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1953 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1954 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1955 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1956 snapToPageWithVelocity(finalPage, velocityX);
1957 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1958 (isFling && isVelocityXLeft)) &&
1959 mCurrentPage < getChildCount() - 1) {
1960 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1961 snapToPageWithVelocity(finalPage, velocityX);
1962 } else {
1963 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001964 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001965 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001966 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001967 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001968 }
1969
1970 float scaleX = getScaleX();
1971 int vX = (int) (-velocityX * scaleX);
1972 int initialScrollX = (int) (getScrollX() * scaleX);
1973
Adam Cohenf9618852013-11-08 06:45:03 -08001974 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001975 mScroller.fling(initialScrollX,
1976 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1977 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001978 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001979 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001980 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1981 // at this point we have not moved beyond the touch slop
1982 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1983 // we can just page
1984 int nextPage = Math.max(0, mCurrentPage - 1);
1985 if (nextPage != mCurrentPage) {
1986 snapToPage(nextPage);
1987 } else {
1988 snapToDestination();
1989 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001990 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001991 // at this point we have not moved beyond the touch slop
1992 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1993 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001994 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1995 if (nextPage != mCurrentPage) {
1996 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001997 } else {
1998 snapToDestination();
1999 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002000 } else if (mTouchState == TOUCH_STATE_REORDERING) {
2001 // Update the last motion position
2002 mLastMotionX = ev.getX();
2003 mLastMotionY = ev.getY();
2004
2005 // Update the parent down so that our zoom animations take this new movement into
2006 // account
2007 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
2008 mParentDownMotionX = pt[0];
2009 mParentDownMotionY = pt[1];
2010 updateDragViewTranslationDuringDrag();
2011 boolean handledFling = false;
2012 if (!DISABLE_FLING_TO_DELETE) {
2013 // Check the velocity and see if we are flinging-to-delete
2014 PointF flingToDeleteVector = isFlingingToDelete();
2015 if (flingToDeleteVector != null) {
2016 onFlingToDelete(flingToDeleteVector);
2017 handledFling = true;
2018 }
2019 }
2020 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
2021 (int) mParentDownMotionY)) {
2022 onDropToDelete();
2023 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002024 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07002025 if (!mCancelTap) {
2026 onUnhandledTap(ev);
2027 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002028 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002029
2030 // Remove the callback to wait for the side page hover timeout
2031 removeCallbacks(mSidePageHoverRunnable);
2032 // End any intermediate reordering states
2033 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002034 break;
2035
2036 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07002037 if (mTouchState == TOUCH_STATE_SCROLLING) {
2038 snapToDestination();
2039 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002040 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07002041 break;
2042
2043 case MotionEvent.ACTION_POINTER_UP:
2044 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002045 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07002046 break;
2047 }
2048
2049 return true;
2050 }
2051
Adam Cohen7d30a372013-07-01 17:03:59 -07002052 public void onFlingToDelete(View v) {}
2053 public void onRemoveView(View v, boolean deletePermanently) {}
2054 public void onRemoveViewAnimationCompleted() {}
2055 public void onAddView(View v, int index) {}
2056
2057 private void resetTouchState() {
2058 releaseVelocityTracker();
2059 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002060 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002061 mTouchState = TOUCH_STATE_REST;
2062 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002063 }
2064
Adam Cohenc2d6e892014-10-16 09:49:24 -07002065 /**
2066 * Triggered by scrolling via touch
2067 */
2068 protected void onScrollInteractionBegin() {
2069 }
2070
2071 protected void onScrollInteractionEnd() {
2072 }
2073
Adam Cohen1697b792013-09-17 19:08:21 -07002074 protected void onUnhandledTap(MotionEvent ev) {
2075 ((Launcher) getContext()).onClick(this);
2076 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002077
Winson Chung185d7162011-02-28 13:47:29 -08002078 @Override
2079 public boolean onGenericMotionEvent(MotionEvent event) {
2080 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2081 switch (event.getAction()) {
2082 case MotionEvent.ACTION_SCROLL: {
2083 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2084 final float vscroll;
2085 final float hscroll;
2086 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2087 vscroll = 0;
2088 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2089 } else {
2090 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2091 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2092 }
2093 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002094 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2095 : (hscroll > 0 || vscroll > 0);
2096 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002097 scrollRight();
2098 } else {
2099 scrollLeft();
2100 }
2101 return true;
2102 }
2103 }
2104 }
2105 }
2106 return super.onGenericMotionEvent(event);
2107 }
2108
Michael Jurkab8f06722010-10-10 15:58:46 -07002109 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2110 if (mVelocityTracker == null) {
2111 mVelocityTracker = VelocityTracker.obtain();
2112 }
2113 mVelocityTracker.addMovement(ev);
2114 }
2115
2116 private void releaseVelocityTracker() {
2117 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002118 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002119 mVelocityTracker.recycle();
2120 mVelocityTracker = null;
2121 }
2122 }
2123
Winson Chung321e9ee2010-08-09 13:37:56 -07002124 private void onSecondaryPointerUp(MotionEvent ev) {
2125 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2126 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2127 final int pointerId = ev.getPointerId(pointerIndex);
2128 if (pointerId == mActivePointerId) {
2129 // This was our active pointer going up. Choose a new
2130 // active pointer and adjust accordingly.
2131 // TODO: Make this decision more intelligent.
2132 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2133 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2134 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002135 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002136 mActivePointerId = ev.getPointerId(newPointerIndex);
2137 if (mVelocityTracker != null) {
2138 mVelocityTracker.clear();
2139 }
2140 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002141 }
2142
Winson Chung321e9ee2010-08-09 13:37:56 -07002143 @Override
2144 public void requestChildFocus(View child, View focused) {
2145 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002146 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002147 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002148 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002149 }
2150 }
2151
Winson Chung1908d072011-02-24 18:09:44 -08002152 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002153 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002154 }
2155
Adam Cohen7d30a372013-07-01 17:03:59 -07002156 int getPageNearestToPoint(float x) {
2157 int index = 0;
2158 for (int i = 0; i < getChildCount(); ++i) {
2159 if (x < getChildAt(i).getRight() - getScrollX()) {
2160 return index;
2161 } else {
2162 index++;
2163 }
2164 }
2165 return Math.min(index, getChildCount() - 1);
2166 }
2167
Adam Cohend19d3ca2010-09-15 14:43:42 -07002168 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002169 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002170 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002171 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002172 final int childCount = getChildCount();
2173 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002174 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002175 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002176 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002177 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002178 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2179 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2180 minDistanceFromScreenCenter = distanceFromScreenCenter;
2181 minDistanceFromScreenCenterIndex = i;
2182 }
2183 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002184 return minDistanceFromScreenCenterIndex;
2185 }
2186
Adam Cohen1e4359c2014-08-18 13:12:16 -07002187 protected boolean isInOverScroll() {
2188 return (mOverScrollX > mMaxScrollX || mOverScrollX < 0);
2189 }
2190
2191 protected int getPageSnapDuration() {
2192 if (isInOverScroll()) {
2193 return OVER_SCROLL_PAGE_SNAP_ANIMATION_DURATION;
2194 }
2195 return PAGE_SNAP_ANIMATION_DURATION;
2196
2197 }
2198
Adam Cohend19d3ca2010-09-15 14:43:42 -07002199 protected void snapToDestination() {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002200 snapToPage(getPageNearestToCenterOfScreen(), getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002201 }
2202
Adam Cohene0f66b52010-11-23 15:06:07 -08002203 private static class ScrollInterpolator implements Interpolator {
2204 public ScrollInterpolator() {
2205 }
2206
2207 public float getInterpolation(float t) {
2208 t -= 1.0f;
2209 return t*t*t*t*t + 1;
2210 }
2211 }
2212
2213 // We want the duration of the page snap animation to be influenced by the distance that
2214 // the screen has to travel, however, we don't want this duration to be effected in a
2215 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2216 // of travel has on the overall snap duration.
2217 float distanceInfluenceForSnapDuration(float f) {
2218 f -= 0.5f; // center the values about 0.
2219 f *= 0.3f * Math.PI / 2.0f;
2220 return (float) Math.sin(f);
2221 }
2222
Michael Jurka0142d492010-08-25 17:46:15 -07002223 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002224 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002225 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002226
Adam Cohenedb40762013-07-18 16:45:45 -07002227 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002228 int delta = newX - mUnboundedScrollX;
2229 int duration = 0;
2230
Adam Cohen1e4359c2014-08-18 13:12:16 -07002231 if (Math.abs(velocity) < mMinFlingVelocity || isInOverScroll()) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002232 // If the velocity is low enough, then treat this more as an automatic page advance
2233 // as opposed to an apparent physical response to flinging
Adam Cohen1e4359c2014-08-18 13:12:16 -07002234 snapToPage(whichPage, getPageSnapDuration());
Adam Cohene0f66b52010-11-23 15:06:07 -08002235 return;
2236 }
2237
2238 // Here we compute a "distance" that will be used in the computation of the overall
2239 // snap duration. This is a function of the actual distance that needs to be traveled;
2240 // we keep this value close to half screen size in order to reduce the variance in snap
2241 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002242 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002243 float distance = halfScreenSize + halfScreenSize *
2244 distanceInfluenceForSnapDuration(distanceRatio);
2245
2246 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002247 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002248
2249 // we want the page's snap velocity to approximately match the velocity at which the
2250 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002251 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2252 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002253
2254 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002255 }
2256
2257 protected void snapToPage(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002258 snapToPage(whichPage, getPageSnapDuration());
Winson Chung321e9ee2010-08-09 13:37:56 -07002259 }
2260
Adam Cohen7d30a372013-07-01 17:03:59 -07002261 protected void snapToPageImmediately(int whichPage) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07002262 snapToPage(whichPage, getPageSnapDuration(), true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002263 }
2264
Michael Jurka0142d492010-08-25 17:46:15 -07002265 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002266 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002267 }
2268
Adam Cohenf9618852013-11-08 06:45:03 -08002269 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2270 snapToPage(whichPage, duration, false, interpolator);
2271 }
2272
2273 protected void snapToPage(int whichPage, int duration, boolean immediate,
2274 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002275 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002276
Adam Cohenedb40762013-07-18 16:45:45 -07002277 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002278 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002279 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002280 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002281 }
2282
2283 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002284 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002285 }
Michael Jurka0142d492010-08-25 17:46:15 -07002286
Adam Cohenf9618852013-11-08 06:45:03 -08002287 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2288 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002289 whichPage = validateNewPage(whichPage);
2290
Adam Cohen7d30a372013-07-01 17:03:59 -07002291 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002292 View focusedChild = getFocusedChild();
2293 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002294 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002295 focusedChild.clearFocus();
2296 }
2297
2298 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002299 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002300 if (immediate) {
2301 duration = 0;
2302 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002303 duration = Math.abs(delta);
2304 }
2305
Adam Cohenf358a4b2013-07-23 16:47:31 -07002306 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002307 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002308 }
Adam Cohenf9618852013-11-08 06:45:03 -08002309
2310 if (interpolator != null) {
2311 mScroller.setInterpolator(interpolator);
2312 } else {
2313 mScroller.setInterpolator(mDefaultInterpolator);
2314 }
2315
Adam Cohen68d73932010-11-15 10:50:58 -08002316 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002317
Adam Cohen674531f2013-12-13 15:07:14 -08002318 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002319
2320 // Trigger a compute() to finish switching pages if necessary
2321 if (immediate) {
2322 computeScroll();
2323 }
2324
Winson Chung9c0565f2013-07-19 13:49:06 -07002325 // Defer loading associated pages until the scroll settles
2326 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2327
Adam Cohen7d30a372013-07-01 17:03:59 -07002328 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002329 invalidate();
2330 }
2331
Winson Chung321e9ee2010-08-09 13:37:56 -07002332 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002333 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002334 }
2335
2336 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002337 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002338 }
2339
Winson Chung86f77532010-08-24 11:08:22 -07002340 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002341 int result = -1;
2342 if (v != null) {
2343 ViewParent vp = v.getParent();
2344 int count = getChildCount();
2345 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002346 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002347 return i;
2348 }
2349 }
2350 }
2351 return result;
2352 }
2353
2354 /**
2355 * @return True is long presses are still allowed for the current touch
2356 */
2357 public boolean allowLongPress() {
2358 return mAllowLongPress;
2359 }
2360
Adam Cohendbdff6b2013-09-18 19:09:15 -07002361 @Override
2362 public boolean performLongClick() {
2363 mCancelTap = true;
2364 return super.performLongClick();
2365 }
2366
Michael Jurka0142d492010-08-25 17:46:15 -07002367 /**
2368 * Set true to allow long-press events to be triggered, usually checked by
2369 * {@link Launcher} to accept or block dpad-initiated long-presses.
2370 */
2371 public void setAllowLongPress(boolean allowLongPress) {
2372 mAllowLongPress = allowLongPress;
2373 }
2374
Winson Chung321e9ee2010-08-09 13:37:56 -07002375 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002376 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002377
2378 SavedState(Parcelable superState) {
2379 super(superState);
2380 }
2381
Adam Cohen091440a2015-03-18 14:16:05 -07002382 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002383 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002384 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002385 }
2386
2387 @Override
2388 public void writeToParcel(Parcel out, int flags) {
2389 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002390 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002391 }
2392
2393 public static final Parcelable.Creator<SavedState> CREATOR =
2394 new Parcelable.Creator<SavedState>() {
2395 public SavedState createFromParcel(Parcel in) {
2396 return new SavedState(in);
2397 }
2398
2399 public SavedState[] newArray(int size) {
2400 return new SavedState[size];
2401 }
2402 };
2403 }
2404
Winson Chungf314b0e2011-08-16 11:54:27 -07002405 protected void loadAssociatedPages(int page) {
2406 loadAssociatedPages(page, false);
2407 }
2408 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002409 if (mContentIsRefreshable) {
2410 final int count = getChildCount();
2411 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002412 int lowerPageBound = getAssociatedLowerPageBound(page);
2413 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002414 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2415 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002416 // First, clear any pages that should no longer be loaded
2417 for (int i = 0; i < count; ++i) {
2418 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002419 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002420 if (layout.getPageChildCount() > 0) {
2421 layout.removeAllViewsOnPage();
2422 }
2423 mDirtyPageContent.set(i, true);
2424 }
2425 }
2426 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002427 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002428 if ((i != page) && immediateAndOnly) {
2429 continue;
2430 }
Michael Jurka0142d492010-08-25 17:46:15 -07002431 if (lowerPageBound <= i && i <= upperPageBound) {
2432 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002433 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002434 mDirtyPageContent.set(i, false);
2435 }
Winson Chung86f77532010-08-24 11:08:22 -07002436 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002437 }
2438 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002439 }
2440 }
2441
Winson Chunge3193b92010-09-10 11:44:42 -07002442 protected int getAssociatedLowerPageBound(int page) {
2443 return Math.max(0, page - 1);
2444 }
2445 protected int getAssociatedUpperPageBound(int page) {
2446 final int count = getChildCount();
2447 return Math.min(page + 1, count - 1);
2448 }
2449
Winson Chung86f77532010-08-24 11:08:22 -07002450 /**
2451 * This method is called ONLY to synchronize the number of pages that the paged view has.
2452 * To actually fill the pages with information, implement syncPageItems() below. It is
2453 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2454 * and therefore, individual page items do not need to be updated in this method.
2455 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002456 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002457
2458 /**
2459 * This method is called to synchronize the items that are on a particular page. If views on
2460 * the page can be reused, then they should be updated within this method.
2461 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002462 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002463
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002464 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002465 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002466 }
2467 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002468 invalidatePageData(currentPage, false);
2469 }
2470 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002471 if (!mIsDataReady) {
2472 return;
2473 }
2474
Michael Jurka0142d492010-08-25 17:46:15 -07002475 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002476 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002477 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002478
Michael Jurka0142d492010-08-25 17:46:15 -07002479 // Update all the pages
2480 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002481
Winson Chung5a808352011-06-27 19:08:49 -07002482 // We must force a measure after we've loaded the pages to update the content width and
2483 // to determine the full scroll width
2484 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2485 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2486
2487 // Set a new page as the current page if necessary
2488 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002489 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002490 }
2491
Michael Jurka0142d492010-08-25 17:46:15 -07002492 // Mark each of the pages as dirty
2493 final int count = getChildCount();
2494 mDirtyPageContent.clear();
2495 for (int i = 0; i < count; ++i) {
2496 mDirtyPageContent.add(true);
2497 }
2498
2499 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002500 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002501 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002502 }
Adam Cohen06559042013-09-29 18:30:56 -07002503 if (isPageMoving()) {
2504 // If the page is moving, then snap it to the final position to ensure we don't get
2505 // stuck between pages
2506 snapToDestination();
2507 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002508 }
Winson Chung007c6982011-06-14 13:27:53 -07002509
Adam Cohen7d30a372013-07-01 17:03:59 -07002510 // Animate the drag view back to the original position
2511 void animateDragViewToOriginalPosition() {
2512 if (mDragView != null) {
2513 AnimatorSet anim = new AnimatorSet();
2514 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2515 anim.playTogether(
2516 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002517 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2518 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2519 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002520 anim.addListener(new AnimatorListenerAdapter() {
2521 @Override
2522 public void onAnimationEnd(Animator animation) {
2523 onPostReorderingAnimationCompleted();
2524 }
2525 });
2526 anim.start();
2527 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002528 }
2529
Sunny Goyal1d08f702015-05-04 15:50:25 -07002530 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002531 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2532 mTouchState = TOUCH_STATE_REORDERING;
2533 mIsReordering = true;
2534
Adam Cohen7d30a372013-07-01 17:03:59 -07002535 // We must invalidate to trigger a redraw to update the layers such that the drag view
2536 // is always drawn on top
2537 invalidate();
2538 }
2539
Adam Cohen091440a2015-03-18 14:16:05 -07002540 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002541 // Trigger the callback when reordering has settled
2542 --mPostReorderingPreZoomInRemainingAnimationCount;
2543 if (mPostReorderingPreZoomInRunnable != null &&
2544 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2545 mPostReorderingPreZoomInRunnable.run();
2546 mPostReorderingPreZoomInRunnable = null;
2547 }
2548 }
2549
Sunny Goyal1d08f702015-05-04 15:50:25 -07002550 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002551 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002552 }
2553
Adam Cohenf358a4b2013-07-23 16:47:31 -07002554 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002555 int dragViewIndex = indexOfChild(v);
2556
Adam Cohen327acfe2014-06-06 11:52:52 -07002557 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002558
Adam Cohen7d30a372013-07-01 17:03:59 -07002559 mTempVisiblePagesRange[0] = 0;
2560 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002561 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002562 mReorderingStarted = true;
2563
2564 // Check if we are within the reordering range
2565 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002566 dragViewIndex <= mTempVisiblePagesRange[1]) {
2567 // Find the drag view under the pointer
2568 mDragView = getChildAt(dragViewIndex);
2569 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2570 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002571 snapToPage(getPageNearestToCenterOfScreen());
2572 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002573 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002574 return true;
2575 }
2576 return false;
2577 }
2578
2579 boolean isReordering(boolean testTouchState) {
2580 boolean state = mIsReordering;
2581 if (testTouchState) {
2582 state &= (mTouchState == TOUCH_STATE_REORDERING);
2583 }
2584 return state;
2585 }
2586 void endReordering() {
2587 // For simplicity, we call endReordering sometimes even if reordering was never started.
2588 // In that case, we don't want to do anything.
2589 if (!mReorderingStarted) return;
2590 mReorderingStarted = false;
2591
2592 // If we haven't flung-to-delete the current child, then we just animate the drag view
2593 // back into position
2594 final Runnable onCompleteRunnable = new Runnable() {
2595 @Override
2596 public void run() {
2597 onEndReordering();
2598 }
2599 };
2600 if (!mDeferringForDelete) {
2601 mPostReorderingPreZoomInRunnable = new Runnable() {
2602 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002603 onCompleteRunnable.run();
2604 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002605 };
2606 };
2607
2608 mPostReorderingPreZoomInRemainingAnimationCount =
2609 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2610 // Snap to the current page
2611 snapToPage(indexOfChild(mDragView), 0);
2612 // Animate the drag view back to the front position
2613 animateDragViewToOriginalPosition();
2614 } else {
2615 // Handled in post-delete-animation-callbacks
2616 }
2617 }
2618
Adam Cohen7d30a372013-07-01 17:03:59 -07002619 /*
2620 * Flinging to delete - IN PROGRESS
2621 */
2622 private PointF isFlingingToDelete() {
2623 ViewConfiguration config = ViewConfiguration.get(getContext());
2624 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2625
2626 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2627 // Do a quick dot product test to ensure that we are flinging upwards
2628 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2629 mVelocityTracker.getYVelocity());
2630 PointF upVec = new PointF(0f, -1f);
2631 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2632 (vel.length() * upVec.length()));
2633 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2634 return vel;
2635 }
2636 }
2637 return null;
2638 }
2639
2640 /**
2641 * Creates an animation from the current drag view along its current velocity vector.
2642 * For this animation, the alpha runs for a fixed duration and we update the position
2643 * progressively.
2644 */
2645 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2646 private View mDragView;
2647 private PointF mVelocity;
2648 private Rect mFrom;
2649 private long mPrevTime;
2650 private float mFriction;
2651
2652 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2653
2654 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2655 long startTime, float friction) {
2656 mDragView = dragView;
2657 mVelocity = vel;
2658 mFrom = from;
2659 mPrevTime = startTime;
2660 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2661 }
2662
2663 @Override
2664 public void onAnimationUpdate(ValueAnimator animation) {
2665 float t = ((Float) animation.getAnimatedValue()).floatValue();
2666 long curTime = AnimationUtils.currentAnimationTimeMillis();
2667
2668 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2669 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2670
2671 mDragView.setTranslationX(mFrom.left);
2672 mDragView.setTranslationY(mFrom.top);
2673 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2674
2675 mVelocity.x *= mFriction;
2676 mVelocity.y *= mFriction;
2677 mPrevTime = curTime;
2678 }
2679 };
2680
2681 private static final int ANIM_TAG_KEY = 100;
2682
2683 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2684 return new Runnable() {
2685 @Override
2686 public void run() {
2687 int dragViewIndex = indexOfChild(dragView);
2688
2689 // For each of the pages around the drag view, animate them from the previous
2690 // position to the new position in the layout (as a result of the drag view moving
2691 // in the layout)
2692 // NOTE: We can make an assumption here because we have side-bound pages that we
2693 // will always have pages to animate in from the left
Adam Cohen6f127a62014-06-12 14:54:41 -07002694 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002695 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2696 boolean slideFromLeft = (isLastWidgetPage ||
2697 dragViewIndex > mTempVisiblePagesRange[0]);
2698
2699 // Setup the scroll to the correct page before we swap the views
2700 if (slideFromLeft) {
2701 snapToPageImmediately(dragViewIndex - 1);
2702 }
2703
2704 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2705 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2706 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2707 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2708 ArrayList<Animator> animations = new ArrayList<Animator>();
2709 for (int i = lowerIndex; i <= upperIndex; ++i) {
2710 View v = getChildAt(i);
2711 // dragViewIndex < pageUnderPointIndex, so after we remove the
2712 // drag view all subsequent views to pageUnderPointIndex will
2713 // shift down.
2714 int oldX = 0;
2715 int newX = 0;
2716 if (slideFromLeft) {
2717 if (i == 0) {
2718 // Simulate the page being offscreen with the page spacing
2719 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002720 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002721 } else {
2722 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2723 }
2724 newX = getViewportOffsetX() + getChildOffset(i);
2725 } else {
2726 oldX = getChildOffset(i) - getChildOffset(i - 1);
2727 newX = 0;
2728 }
2729
2730 // Animate the view translation from its old position to its new
2731 // position
2732 AnimatorSet anim = (AnimatorSet) v.getTag();
2733 if (anim != null) {
2734 anim.cancel();
2735 }
2736
2737 // Note: Hacky, but we want to skip any optimizations to not draw completely
2738 // hidden views
2739 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2740 v.setTranslationX(oldX - newX);
2741 anim = new AnimatorSet();
2742 anim.playTogether(
2743 ObjectAnimator.ofFloat(v, "translationX", 0f),
2744 ObjectAnimator.ofFloat(v, "alpha", 1f));
2745 animations.add(anim);
2746 v.setTag(ANIM_TAG_KEY, anim);
2747 }
2748
2749 AnimatorSet slideAnimations = new AnimatorSet();
2750 slideAnimations.playTogether(animations);
2751 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2752 slideAnimations.addListener(new AnimatorListenerAdapter() {
2753 @Override
2754 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002755 mDeferringForDelete = false;
2756 onEndReordering();
2757 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002758 }
2759 });
2760 slideAnimations.start();
2761
2762 removeView(dragView);
2763 onRemoveView(dragView, true);
2764 }
2765 };
2766 }
2767
2768 public void onFlingToDelete(PointF vel) {
2769 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2770
2771 // NOTE: Because it takes time for the first frame of animation to actually be
2772 // called and we expect the animation to be a continuation of the fling, we have
2773 // to account for the time that has elapsed since the fling finished. And since
2774 // we don't have a startDelay, we will always get call to update when we call
2775 // start() (which we want to ignore).
2776 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2777 private int mCount = -1;
2778 private long mStartTime;
2779 private float mOffset;
2780 /* Anonymous inner class ctor */ {
2781 mStartTime = startTime;
2782 }
2783
2784 @Override
2785 public float getInterpolation(float t) {
2786 if (mCount < 0) {
2787 mCount++;
2788 } else if (mCount == 0) {
2789 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2790 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2791 mCount++;
2792 }
2793 return Math.min(1f, mOffset + t);
2794 }
2795 };
2796
2797 final Rect from = new Rect();
2798 final View dragView = mDragView;
2799 from.left = (int) dragView.getTranslationX();
2800 from.top = (int) dragView.getTranslationY();
2801 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2802 from, startTime, FLING_TO_DELETE_FRICTION);
2803
2804 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2805
2806 // Create and start the animation
2807 ValueAnimator mDropAnim = new ValueAnimator();
2808 mDropAnim.setInterpolator(tInterpolator);
2809 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2810 mDropAnim.setFloatValues(0f, 1f);
2811 mDropAnim.addUpdateListener(updateCb);
2812 mDropAnim.addListener(new AnimatorListenerAdapter() {
2813 public void onAnimationEnd(Animator animation) {
2814 onAnimationEndRunnable.run();
2815 }
2816 });
2817 mDropAnim.start();
2818 mDeferringForDelete = true;
2819 }
2820
2821 /* Drag to delete */
2822 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2823 if (mDeleteDropTarget != null) {
2824 mAltTmpRect.set(0, 0, 0, 0);
2825 View parent = (View) mDeleteDropTarget.getParent();
2826 if (parent != null) {
2827 parent.getGlobalVisibleRect(mAltTmpRect);
2828 }
2829 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2830 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2831 return mTmpRect.contains(x, y);
2832 }
2833 return false;
2834 }
2835
2836 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2837
2838 private void onDropToDelete() {
2839 final View dragView = mDragView;
2840
2841 final float toScale = 0f;
2842 final float toAlpha = 0f;
2843
2844 // Create and start the complex animation
2845 ArrayList<Animator> animations = new ArrayList<Animator>();
2846 AnimatorSet motionAnim = new AnimatorSet();
2847 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2848 motionAnim.playTogether(
2849 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2850 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2851 animations.add(motionAnim);
2852
2853 AnimatorSet alphaAnim = new AnimatorSet();
2854 alphaAnim.setInterpolator(new LinearInterpolator());
2855 alphaAnim.playTogether(
2856 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2857 animations.add(alphaAnim);
2858
2859 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2860
2861 AnimatorSet anim = new AnimatorSet();
2862 anim.playTogether(animations);
2863 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2864 anim.addListener(new AnimatorListenerAdapter() {
2865 public void onAnimationEnd(Animator animation) {
2866 onAnimationEndRunnable.run();
2867 }
2868 });
2869 anim.start();
2870
2871 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002872 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002873
2874 /* Accessibility */
2875 @Override
2876 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2877 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002878 info.setScrollable(getPageCount() > 1);
2879 if (getCurrentPage() < getPageCount() - 1) {
2880 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2881 }
2882 if (getCurrentPage() > 0) {
2883 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2884 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002885 }
2886
2887 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002888 public void sendAccessibilityEvent(int eventType) {
2889 // Don't let the view send real scroll events.
2890 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2891 super.sendAccessibilityEvent(eventType);
2892 }
2893 }
2894
2895 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002896 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2897 super.onInitializeAccessibilityEvent(event);
2898 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002899 }
2900
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002901 @Override
2902 public boolean performAccessibilityAction(int action, Bundle arguments) {
2903 if (super.performAccessibilityAction(action, arguments)) {
2904 return true;
2905 }
2906 switch (action) {
2907 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2908 if (getCurrentPage() < getPageCount() - 1) {
2909 scrollRight();
2910 return true;
2911 }
2912 } break;
2913 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2914 if (getCurrentPage() > 0) {
2915 scrollLeft();
2916 return true;
2917 }
2918 } break;
2919 }
2920 return false;
2921 }
2922
Adam Cohen0ffac432013-07-10 11:19:26 -07002923 protected String getCurrentPageDescription() {
2924 return String.format(getContext().getString(R.string.default_scroll_format),
2925 getNextPage() + 1, getChildCount());
2926 }
2927
Winson Chungd11265e2011-08-30 13:37:23 -07002928 @Override
2929 public boolean onHoverEvent(android.view.MotionEvent event) {
2930 return true;
2931 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002932}