blob: fa52f52b10f5cd1c0cc52ddc362bff000b5c8170 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen7d30a372013-07-01 17:03:59 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070024import android.animation.ValueAnimator;
Adam Cohen7d30a372013-07-01 17:03:59 -070025import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
30import android.graphics.PointF;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070032import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.os.Parcel;
34import android.os.Parcelable;
Adam Cohen53805212013-10-01 10:39:23 -070035import android.support.v4.view.accessibility.AccessibilityEventCompat;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import 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 -070054import android.widget.Scroller;
55
Winson Chung6a0f57d2011-06-29 20:10:49 -070056import java.util.ArrayList;
57
Winson Chungc58497e2013-09-03 17:48:37 -070058interface Page {
59 public int getPageChildCount();
60 public View getChildOnPageAt(int i);
61 public void removeAllViewsOnPage();
62 public void removeViewOnPageAt(int i);
63 public int indexOfChildOnPage(View v);
64}
65
Winson Chung321e9ee2010-08-09 13:37:56 -070066/**
67 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070068 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070069 */
Michael Jurka8b805b12012-04-18 14:23:14 -070070public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070071 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070072 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070073 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070074
Winson Chung86f77532010-08-24 11:08:22 -070075 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070076 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070077
Adam Cohen7d30a372013-07-01 17:03:59 -070078 protected static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070079 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Adam Cohenb5ba0972011-09-07 18:02:31 -070082 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070083 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080084
Adam Cohenb64cb5a2011-02-15 13:53:42 -080085 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080086 // The page is moved more than halfway, automatically move to the next page on touch up.
87 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080088
Adam Cohen265b9a62011-12-07 14:37:18 -080089 // The following constants need to be scaled based on density. The scaled versions will be
90 // assigned to the corresponding member variables below.
91 private static final int FLING_THRESHOLD_VELOCITY = 500;
92 private static final int MIN_SNAP_VELOCITY = 1500;
93 private static final int MIN_FLING_VELOCITY = 250;
94
Adam Cohen7d30a372013-07-01 17:03:59 -070095 // We are disabling touch interaction of the widget region for factory ROM.
96 private static final boolean DISABLE_TOUCH_INTERACTION = false;
Adam Cohenf358a4b2013-07-23 16:47:31 -070097 private static final boolean DISABLE_TOUCH_SIDE_PAGES = true;
Adam Cohendedbd962013-07-11 14:21:49 -070098 private static final boolean DISABLE_FLING_TO_DELETE = true;
Adam Cohen7d30a372013-07-01 17:03:59 -070099
Adam Cohen21cd0022013-10-09 18:57:02 -0700100 public static final int INVALID_RESTORE_PAGE = -1001;
101
Adam Cohenf358a4b2013-07-23 16:47:31 -0700102 private boolean mFreeScroll = false;
103 private int mFreeScrollMinScrollX = -1;
104 private int mFreeScrollMaxScrollX = -1;
105
Winson Chung8aad6102012-05-11 16:27:49 -0700106 static final int AUTOMATIC_PAGE_SPACING = -1;
107
Adam Cohen265b9a62011-12-07 14:37:18 -0800108 protected int mFlingThresholdVelocity;
109 protected int mMinFlingVelocity;
110 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -0700111
Adam Cohenb5ba0972011-09-07 18:02:31 -0700112 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected float mSmoothingTime;
114 protected float mTouchX;
115
116 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -0700117 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -0700118
119 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -0700120 protected int mRestorePage = INVALID_RESTORE_PAGE;
Adam Cohenf698c6e2013-07-17 18:44:25 -0700121 protected int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700122
Michael Jurka0142d492010-08-25 17:46:15 -0700123 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -0800124 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800125 protected LauncherScroller mScroller;
126 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127 private VelocityTracker mVelocityTracker;
Adam Cohen3b185e22013-10-29 14:45:58 -0700128 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Adam Cohen7d30a372013-07-01 17:03:59 -0700130 private float mParentDownMotionX;
131 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700133 private float mDownMotionY;
134 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700135 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800137 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800138 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800139 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700140 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700141
Adam Cohendbdff6b2013-09-18 19:09:15 -0700142 private boolean mCancelTap;
143
Adam Cohenedb40762013-07-18 16:45:45 -0700144 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 protected final static int TOUCH_STATE_REST = 0;
147 protected final static int TOUCH_STATE_SCROLLING = 1;
148 protected final static int TOUCH_STATE_PREV_PAGE = 2;
149 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700150 protected final static int TOUCH_STATE_REORDERING = 4;
151
Adam Cohene45440e2010-10-14 18:33:38 -0700152 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700155 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800156
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Michael Jurka0142d492010-08-25 17:46:15 -0700158 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159
Michael Jurka7426c422010-11-11 15:23:47 -0800160 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 private int mPagingTouchSlop;
162 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700163 protected int mPageLayoutPaddingTop;
164 protected int mPageLayoutPaddingBottom;
165 protected int mPageLayoutPaddingLeft;
166 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700167 protected int mPageLayoutWidthGap;
168 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700169 protected int mCellCountX = 0;
170 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700171 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800172 protected boolean mAllowOverScroll = true;
173 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800174 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700175 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700176
Michael Jurka8b805b12012-04-18 14:23:14 -0700177 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800178 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
179 // the screens from continuing to translate beyond the normal bounds.
180 protected int mOverScrollX;
181
Michael Jurka5f1c5092010-09-03 14:15:02 -0700182 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Michael Jurka5f1c5092010-09-03 14:15:02 -0700184 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700185
Winson Chung86f77532010-08-24 11:08:22 -0700186 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700187
Michael Jurkae326f182011-11-21 14:05:46 -0800188 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700189
Michael Jurka0142d492010-08-25 17:46:15 -0700190 // If true, syncPages and syncPageItems will be called to refresh pages
191 protected boolean mContentIsRefreshable = true;
192
193 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700194 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700195
196 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
197 // to switch to a new page
198 protected boolean mUsePagingTouchSlop = true;
199
Michael Jurka8b805b12012-04-18 14:23:14 -0700200 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700201 // (SmoothPagedView does this)
202 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700203 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700204
Patrick Dubroy1262e362010-10-06 15:49:50 -0700205 protected boolean mIsPageMoving = false;
206
Winson Chungf0ea4d32011-06-06 14:27:16 -0700207 // All syncs and layout passes are deferred until data is ready.
208 protected boolean mIsDataReady = false;
209
Adam Cohen7d30a372013-07-01 17:03:59 -0700210 protected boolean mAllowLongPress = true;
211
Winson Chungd2be3812013-07-16 11:11:32 -0700212 // Page Indicator
213 private int mPageIndicatorViewId;
214 private 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;
233 private int mSidePageHoverIndex = -1;
234 // 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
252 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
253 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
258 private boolean mDeferringForDelete = false;
259 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
260 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
Adam Cohen9c4949e2010-10-05 12:27:22 -0700288 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800289 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700290 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800291 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700292 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800293 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700294 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800295 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700296 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700297 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700298 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700299 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700300 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700301 a.recycle();
302
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700304 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 }
306
307 /**
308 * Initializes various states for this workspace.
309 */
Michael Jurka0142d492010-08-25 17:46:15 -0700310 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700311 mDirtyPageContent = new ArrayList<Boolean>();
312 mDirtyPageContent.ensureCapacity(32);
Adam Cohenf9618852013-11-08 06:45:03 -0800313 mScroller = new LauncherScroller(getContext());
314 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700315 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700316 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700317
318 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700319 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
321 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700322 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800323
Adam Cohen7d30a372013-07-01 17:03:59 -0700324 // Scale the fling-to-delete threshold by the density
325 mFlingToDeleteThresholdVelocity =
326 (int) (mFlingToDeleteThresholdVelocity * mDensity);
327
Adam Cohen265b9a62011-12-07 14:37:18 -0800328 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
329 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
330 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700331 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700332 }
333
Adam Cohenf9618852013-11-08 06:45:03 -0800334 protected void setDefaultInterpolator(Interpolator interpolator) {
335 mDefaultInterpolator = interpolator;
336 mScroller.setInterpolator(mDefaultInterpolator);
337 }
338
Winson Chungd2be3812013-07-16 11:11:32 -0700339 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700340 super.onAttachedToWindow();
341
Winson Chungd2be3812013-07-16 11:11:32 -0700342 // Hook up the page indicator
343 ViewGroup parent = (ViewGroup) getParent();
344 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
345 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700346 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700347
Winson Chung7819a562013-09-19 15:55:45 -0700348 ArrayList<PageIndicator.PageMarkerResources> markers =
349 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700350 for (int i = 0; i < getChildCount(); ++i) {
351 markers.add(getPageIndicatorMarker(i));
352 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700353
Winson Chungc58497e2013-09-03 17:48:37 -0700354 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700355
356 OnClickListener listener = getPageIndicatorClickListener();
357 if (listener != null) {
358 mPageIndicator.setOnClickListener(listener);
359 }
Adam Cohen53805212013-10-01 10:39:23 -0700360 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700361 }
362 }
363
Adam Cohen53805212013-10-01 10:39:23 -0700364 protected String getPageIndicatorDescription() {
365 return getCurrentPageDescription();
366 }
367
368 protected OnClickListener getPageIndicatorClickListener() {
369 return null;
370 }
371
Winson Chungd2be3812013-07-16 11:11:32 -0700372 protected void onDetachedFromWindow() {
373 // Unhook the page indicator
374 mPageIndicator = null;
375 }
376
Adam Cohen7d30a372013-07-01 17:03:59 -0700377 void setDeleteDropTarget(View v) {
378 mDeleteDropTarget = v;
379 }
380
381 // Convenience methods to map points from self to parent and vice versa
382 float[] mapPointFromViewToParent(View v, float x, float y) {
383 mTmpPoint[0] = x;
384 mTmpPoint[1] = y;
385 v.getMatrix().mapPoints(mTmpPoint);
386 mTmpPoint[0] += v.getLeft();
387 mTmpPoint[1] += v.getTop();
388 return mTmpPoint;
389 }
390 float[] mapPointFromParentToView(View v, float x, float y) {
391 mTmpPoint[0] = x - v.getLeft();
392 mTmpPoint[1] = y - v.getTop();
393 v.getMatrix().invert(mTmpInvMatrix);
394 mTmpInvMatrix.mapPoints(mTmpPoint);
395 return mTmpPoint;
396 }
397
398 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700399 if (mDragView != null) {
400 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
401 (mDragViewBaselineLeft - mDragView.getLeft());
402 float y = mLastMotionY - mDownMotionY;
403 mDragView.setTranslationX(x);
404 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700405
Adam Cohenf358a4b2013-07-23 16:47:31 -0700406 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
407 + x + ", " + y);
408 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700409 }
410
411 public void setMinScale(float f) {
412 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700413 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700414 requestLayout();
415 }
416
417 @Override
418 public void setScaleX(float scaleX) {
419 super.setScaleX(scaleX);
420 if (isReordering(true)) {
421 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
422 mLastMotionX = p[0];
423 mLastMotionY = p[1];
424 updateDragViewTranslationDuringDrag();
425 }
426 }
427
428 // Convenience methods to get the actual width/height of the PagedView (since it is measured
429 // to be larger to account for the minimum possible scale)
430 int getViewportWidth() {
431 return mViewport.width();
432 }
433 int getViewportHeight() {
434 return mViewport.height();
435 }
436
437 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
438 // PagedView both horizontally and vertically
439 int getViewportOffsetX() {
440 return (getMeasuredWidth() - getViewportWidth()) / 2;
441 }
442
443 int getViewportOffsetY() {
444 return (getMeasuredHeight() - getViewportHeight()) / 2;
445 }
446
Adam Cohenedb40762013-07-18 16:45:45 -0700447 PageIndicator getPageIndicator() {
448 return mPageIndicator;
449 }
Winson Chung7819a562013-09-19 15:55:45 -0700450 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
451 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700452 }
Adam Cohenedb40762013-07-18 16:45:45 -0700453
Adam Cohen674531f2013-12-13 15:07:14 -0800454 /**
455 * Add a page change listener which will be called when a page is _finished_ listening.
456 *
457 */
Winson Chung86f77532010-08-24 11:08:22 -0700458 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
459 mPageSwitchListener = pageSwitchListener;
460 if (mPageSwitchListener != null) {
461 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 }
463 }
464
465 /**
Winson Chung52aee602013-01-30 12:01:02 -0800466 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
467 */
468 public boolean isLayoutRtl() {
469 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
470 }
471
472 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700473 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
474 * out pages.
475 */
476 protected void setDataIsReady() {
477 mIsDataReady = true;
478 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700479
Winson Chungf0ea4d32011-06-06 14:27:16 -0700480 protected boolean isDataReady() {
481 return mIsDataReady;
482 }
483
484 /**
Winson Chung86f77532010-08-24 11:08:22 -0700485 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 *
Winson Chung86f77532010-08-24 11:08:22 -0700487 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 */
Winson Chung86f77532010-08-24 11:08:22 -0700489 int getCurrentPage() {
490 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700492
Winson Chung360e63f2012-04-27 13:48:05 -0700493 int getNextPage() {
494 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
495 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700496
Winson Chung86f77532010-08-24 11:08:22 -0700497 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 return getChildCount();
499 }
500
Winson Chung86f77532010-08-24 11:08:22 -0700501 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 return getChildAt(index);
503 }
504
Adam Cohenae4f1552011-10-20 00:15:42 -0700505 protected int indexToPage(int index) {
506 return index;
507 }
508
Winson Chung321e9ee2010-08-09 13:37:56 -0700509 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800510 * Updates the scroll of the current page immediately to its final scroll position. We use this
511 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
512 * the previous tab page.
513 */
514 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700515 // If the current page is invalid, just reset the scroll position to zero
516 int newX = 0;
517 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700518 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700519 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800520 scrollTo(newX, 0);
521 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700522 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800523 }
524
525 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700526 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700527 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
528 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700529 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700530 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700531 mCurrentPage = getNextPage();
Adam Cohen674531f2013-12-13 15:07:14 -0800532 notifyPageSwitchListener();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700533 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700534 }
535
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700536 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700537 mScroller.abortAnimation();
538 // We need to clean up the next page here to avoid computeScrollHelper from
539 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700540 if (resetNextPage) {
541 mNextPage = INVALID_PAGE;
542 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700543 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700544
545 private void forceFinishScroller() {
546 mScroller.forceFinished(true);
547 // We need to clean up the next page here to avoid computeScrollHelper from
548 // updating current page on the pass.
549 mNextPage = INVALID_PAGE;
550 }
551
Chet Haasebc2f0822012-10-26 17:59:53 -0700552 /**
Winson Chung86f77532010-08-24 11:08:22 -0700553 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 */
Winson Chung86f77532010-08-24 11:08:22 -0700555 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800556 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700557 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800558 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
560 // the default
561 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800562 return;
563 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700564 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700565 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chung181c3dc2013-07-17 15:36:20 -0700566 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700567 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800568 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 }
570
Winson Chung8c87cd82013-07-23 16:20:10 -0700571 /**
572 * The restore page will be set in place of the current page at the next (likely first)
573 * layout.
574 */
575 void setRestorePage(int restorePage) {
576 mRestorePage = restorePage;
577 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800578 int getRestorePage() {
579 return mRestorePage;
580 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700581
Adam Cohen674531f2013-12-13 15:07:14 -0800582 /**
583 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
584 * has settled.
585 */
Michael Jurka0142d492010-08-25 17:46:15 -0700586 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700587 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800588 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 }
Winson Chungd2be3812013-07-16 11:11:32 -0700590
Adam Cohen674531f2013-12-13 15:07:14 -0800591 updatePageIndicator();
592 }
593
594 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700595 // Update the page indicator (when we aren't reordering)
596 if (mPageIndicator != null && !isReordering(false)) {
597 mPageIndicator.setActiveMarker(getNextPage());
598 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800600 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700601 if (!mIsPageMoving) {
602 mIsPageMoving = true;
603 onPageBeginMoving();
604 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700605 }
606
Michael Jurkace7e05f2011-02-01 22:02:35 -0800607 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700608 if (mIsPageMoving) {
609 mIsPageMoving = false;
610 onPageEndMoving();
611 }
Michael Jurka0142d492010-08-25 17:46:15 -0700612 }
613
Adam Cohen26976d92011-03-22 15:33:33 -0700614 protected boolean isPageMoving() {
615 return mIsPageMoving;
616 }
617
Michael Jurka0142d492010-08-25 17:46:15 -0700618 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700619 protected void onPageBeginMoving() {
620 }
621
622 // a method that subclasses can override to add behavior
623 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700624 }
625
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 /**
Winson Chung86f77532010-08-24 11:08:22 -0700627 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 *
629 * @param l The listener used to respond to long clicks.
630 */
631 @Override
632 public void setOnLongClickListener(OnLongClickListener l) {
633 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700634 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700636 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 }
Adam Cohen1697b792013-09-17 19:08:21 -0700638 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 }
640
641 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800642 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700643 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800644 }
645
646 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700647 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700648 // In free scroll mode, we clamp the scrollX
649 if (mFreeScroll) {
650 x = Math.min(x, mFreeScrollMaxScrollX);
651 x = Math.max(x, mFreeScrollMinScrollX);
652 }
653
Adam Cohen0ffac432013-07-10 11:19:26 -0700654 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800655 mUnboundedScrollX = x;
656
Adam Cohen0ffac432013-07-10 11:19:26 -0700657 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
658 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
659 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800660 super.scrollTo(0, y);
661 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700662 if (isRtl) {
663 overScroll(x - mMaxScrollX);
664 } else {
665 overScroll(x);
666 }
Adam Cohen68d73932010-11-15 10:50:58 -0800667 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700668 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800669 super.scrollTo(mMaxScrollX, y);
670 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700671 if (isRtl) {
672 overScroll(x);
673 } else {
674 overScroll(x - mMaxScrollX);
675 }
Adam Cohen68d73932010-11-15 10:50:58 -0800676 }
677 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800678 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800679 super.scrollTo(x, y);
680 }
681
Michael Jurka0142d492010-08-25 17:46:15 -0700682 mTouchX = x;
683 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700684
685 // Update the last motion events when scrolling
686 if (isReordering(true)) {
687 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
688 mLastMotionX = p[0];
689 mLastMotionY = p[1];
690 updateDragViewTranslationDuringDrag();
691 }
Michael Jurka0142d492010-08-25 17:46:15 -0700692 }
693
Adam Cohen53805212013-10-01 10:39:23 -0700694 private void sendScrollAccessibilityEvent() {
695 AccessibilityManager am =
696 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
697 if (am.isEnabled()) {
698 AccessibilityEvent ev =
699 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700700 ev.setItemCount(getChildCount());
701 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700702 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700703
Alan Viverette254139a2013-10-08 13:13:46 -0700704 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700705 if (getNextPage() >= mCurrentPage) {
706 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
707 } else {
708 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
709 }
710
711 ev.setAction(action);
712 sendAccessibilityEventUnchecked(ev);
713 }
714 }
715
Michael Jurka0142d492010-08-25 17:46:15 -0700716 // we moved this functionality to a helper function so SmoothPagedView can reuse it
717 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700719 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 if (getScrollX() != mScroller.getCurrX()
721 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700722 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700723 float scaleX = mFreeScroll ? getScaleX() : 1f;
724 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
725 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700726 }
Michael Jurka0142d492010-08-25 17:46:15 -0700727 invalidate();
728 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700729 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700730 sendScrollAccessibilityEvent();
731
Winson Chung86f77532010-08-24 11:08:22 -0700732 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700733 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700734 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700735
Winson Chung9c0565f2013-07-19 13:49:06 -0700736 // Load the associated pages if necessary
737 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
738 loadAssociatedPages(mCurrentPage);
739 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
740 }
741
Adam Cohen73aa9752010-11-24 16:26:58 -0800742 // We don't want to trigger a page end moving unless the page has settled
743 // and the user has stopped scrolling
744 if (mTouchState == TOUCH_STATE_REST) {
745 pageEndMoving();
746 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700747
Adam Cohen7d30a372013-07-01 17:03:59 -0700748 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700749 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700750 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700751 if (am.isEnabled()) {
752 // Notify the user when the page changes
753 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700754 }
Michael Jurka0142d492010-08-25 17:46:15 -0700755 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700756 }
Michael Jurka0142d492010-08-25 17:46:15 -0700757 return false;
758 }
759
760 @Override
761 public void computeScroll() {
762 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 }
764
Adam Cohen7d30a372013-07-01 17:03:59 -0700765 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
766 return mTopAlignPageWhenShrinkingForBouncer;
767 }
768
Adam Cohen96d30a12013-07-16 18:13:21 -0700769 public static class LayoutParams extends ViewGroup.LayoutParams {
770 public boolean isFullScreenPage = false;
771
772 /**
773 * {@inheritDoc}
774 */
775 public LayoutParams(int width, int height) {
776 super(width, height);
777 }
778
779 public LayoutParams(ViewGroup.LayoutParams source) {
780 super(source);
781 }
782 }
783
784 protected LayoutParams generateDefaultLayoutParams() {
785 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
786 }
787
Adam Cohenedb40762013-07-18 16:45:45 -0700788 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700789 LayoutParams lp = generateDefaultLayoutParams();
790 lp.isFullScreenPage = true;
791 super.addView(page, 0, lp);
792 }
793
Adam Cohen410f3cd2013-09-22 12:09:32 -0700794 public int getNormalChildHeight() {
795 return mNormalChildHeight;
796 }
797
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 @Override
799 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700801 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
802 return;
803 }
804
Adam Cohen7d30a372013-07-01 17:03:59 -0700805 // We measure the dimensions of the PagedView to be larger than the pages so that when we
806 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700807 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
808 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
809 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700810 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800811 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700812 // viewport, we can be at most one and a half screens offset once we scale down
813 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700814 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
815 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700816
Winson Chungc82d2622013-11-06 13:23:29 -0800817 int parentWidthSize = (int) (2f * maxSize);
818 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700819 int scaledWidthSize, scaledHeightSize;
820 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700821 scaledWidthSize = (int) (parentWidthSize / mMinScale);
822 scaledHeightSize = (int) (parentHeightSize / mMinScale);
823 } else {
824 scaledWidthSize = widthSize;
825 scaledHeightSize = heightSize;
826 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700827 mViewport.set(0, 0, widthSize, heightSize);
828
829 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
830 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
831 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 }
833
Winson Chung8aad6102012-05-11 16:27:49 -0700834 // Return early if we aren't given a proper dimension
835 if (widthSize <= 0 || heightSize <= 0) {
836 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
837 return;
838 }
839
Adam Lesinski6b879f02010-11-04 16:15:23 -0700840 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
841 * of the All apps view on XLarge displays to not take up more space then it needs. Width
842 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
843 * each page to have the same width.
844 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700845 final int verticalPadding = getPaddingTop() + getPaddingBottom();
846 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700847
848 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700849 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700851 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
852 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
853 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
854 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 final int childCount = getChildCount();
856 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700857 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700858 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100859 if (child.getVisibility() != GONE) {
860 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700861
Vladimir Marko2824b072013-10-04 16:42:17 +0100862 int childWidthMode;
863 int childHeightMode;
864 int childWidth;
865 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700866
Vladimir Marko2824b072013-10-04 16:42:17 +0100867 if (!lp.isFullScreenPage) {
868 if (lp.width == LayoutParams.WRAP_CONTENT) {
869 childWidthMode = MeasureSpec.AT_MOST;
870 } else {
871 childWidthMode = MeasureSpec.EXACTLY;
872 }
873
874 if (lp.height == LayoutParams.WRAP_CONTENT) {
875 childHeightMode = MeasureSpec.AT_MOST;
876 } else {
877 childHeightMode = MeasureSpec.EXACTLY;
878 }
879
Adam Cohen3b185e22013-10-29 14:45:58 -0700880 childWidth = getViewportWidth() - horizontalPadding
881 - mInsets.left - mInsets.right;
882 childHeight = getViewportHeight() - verticalPadding
883 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100884 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700885 } else {
886 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700887 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100888
Adam Cohen3b185e22013-10-29 14:45:58 -0700889 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
890 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700891 }
892
Vladimir Marko2824b072013-10-04 16:42:17 +0100893 final int childWidthMeasureSpec =
894 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
895 final int childHeightMeasureSpec =
896 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
897 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700898 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700899 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700900 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800901 }
902
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700904 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700905 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700906 return;
907 }
908
Winson Chung785d2eb2011-04-14 16:08:02 -0700909 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700911
Adam Cohen7d30a372013-07-01 17:03:59 -0700912 int offsetX = getViewportOffsetX();
913 int offsetY = getViewportOffsetY();
914
915 // Update the viewport offsets
916 mViewport.offset(offsetX, offsetY);
917
Adam Cohen0ffac432013-07-10 11:19:26 -0700918 final boolean isRtl = isLayoutRtl();
919
920 final int startIndex = isRtl ? childCount - 1 : 0;
921 final int endIndex = isRtl ? -1 : childCount;
922 final int delta = isRtl ? -1 : 1;
923
Adam Cohen7d30a372013-07-01 17:03:59 -0700924 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700925
Adam Cohen3b185e22013-10-29 14:45:58 -0700926 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000927 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700928
929 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700930 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
931 mPageScrolls = new int[getChildCount()];
932 }
933
Adam Cohen0ffac432013-07-10 11:19:26 -0700934 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700935 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700937 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100938 int childTop;
939 if (lp.isFullScreenPage) {
940 childTop = offsetY;
941 } else {
942 childTop = offsetY + getPaddingTop() + mInsets.top;
943 if (mCenterPagesVertically) {
944 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
945 }
946 }
947
Adam Cohen96d30a12013-07-16 18:13:21 -0700948 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700949 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800950
Winson Chung785d2eb2011-04-14 16:08:02 -0700951 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700952 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800953 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700954
Adam Cohen3b185e22013-10-29 14:45:58 -0700955 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
956 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000957
958 int pageGap = mPageSpacing;
959 int next = i + delta;
960 if (next != endIndex) {
961 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
962 } else {
963 nextLp = null;
964 }
965
966 // Prevent full screen pages from showing in the viewport
967 // when they are not the current page.
968 if (lp.isFullScreenPage) {
969 pageGap = getPaddingLeft();
970 } else if (nextLp != null && nextLp.isFullScreenPage) {
971 pageGap = getPaddingRight();
972 }
973
974 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 }
976 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700977
978 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
979 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800980 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700981 setHorizontalScrollBarEnabled(true);
982 mFirstLayout = false;
983 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700984
985 if (childCount > 0) {
986 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700987 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700988 } else {
989 mMaxScrollX = 0;
990 }
991
992 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
993 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700994 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700995 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700996 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700997 } else {
998 setCurrentPage(getNextPage());
999 }
Adam Cohenf698c6e2013-07-17 18:44:25 -07001000 }
1001 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001002
1003 if (isReordering(true)) {
1004 updateDragViewTranslationDuringDrag();
1005 }
Winson Chunge3193b92010-09-10 11:44:42 -07001006 }
1007
Adam Cohen3b185e22013-10-29 14:45:58 -07001008 public void setPageSpacing(int pageSpacing) {
1009 mPageSpacing = pageSpacing;
1010 requestLayout();
1011 }
1012
Adam Cohenf34bab52010-09-30 14:11:56 -07001013 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001014 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1015
1016 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001017 for (int i = 0; i < getChildCount(); i++) {
1018 View child = getChildAt(i);
1019 if (child != null) {
1020 float scrollProgress = getScrollProgress(screenCenter, child, i);
1021 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001022 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001023 }
1024 }
1025 invalidate();
1026 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001027 }
1028
Winson Chungc58497e2013-09-03 17:48:37 -07001029 protected void enablePagedViewAnimations() {
1030 mAllowPagedViewAnimations = true;
1031
1032 }
1033 protected void disablePagedViewAnimations() {
1034 mAllowPagedViewAnimations = false;
1035 }
1036
Winson Chunge3193b92010-09-10 11:44:42 -07001037 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001038 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001039 // Update the page indicator, we don't update the page indicator as we
1040 // add/remove pages
1041 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001042 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001043 mPageIndicator.addMarker(pageIndex,
1044 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001045 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001046 }
1047
Adam Cohen2591f6a2011-10-25 14:36:40 -07001048 // This ensures that when children are added, they get the correct transforms / alphas
1049 // in accordance with any scroll effects.
1050 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001051 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001052 invalidate();
1053 }
1054
Michael Jurka8b805b12012-04-18 14:23:14 -07001055 @Override
1056 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001057 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001058 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001059 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001060 }
1061
Winson Chungd2be3812013-07-16 11:11:32 -07001062 private void removeMarkerForView(int index) {
1063 // Update the page indicator, we don't update the page indicator as we
1064 // add/remove pages
1065 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001066 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001067 }
1068 }
1069
1070 @Override
1071 public void removeView(View v) {
1072 // XXX: We should find a better way to hook into this before the view
1073 // gets removed form its parent...
1074 removeMarkerForView(indexOfChild(v));
1075 super.removeView(v);
1076 }
1077 @Override
1078 public void removeViewInLayout(View v) {
1079 // XXX: We should find a better way to hook into this before the view
1080 // gets removed form its parent...
1081 removeMarkerForView(indexOfChild(v));
1082 super.removeViewInLayout(v);
1083 }
1084 @Override
1085 public void removeViewAt(int index) {
1086 // XXX: We should find a better way to hook into this before the view
1087 // gets removed form its parent...
1088 removeViewAt(index);
1089 super.removeViewAt(index);
1090 }
1091 @Override
1092 public void removeAllViewsInLayout() {
1093 // Update the page indicator, we don't update the page indicator as we
1094 // add/remove pages
1095 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001096 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001097 }
1098
1099 super.removeAllViewsInLayout();
1100 }
1101
Adam Cohen73894962011-10-31 13:17:17 -07001102 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001103 if (index < 0 || index > getChildCount() - 1) return 0;
1104
Adam Cohenf698c6e2013-07-17 18:44:25 -07001105 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001106
Adam Cohenf698c6e2013-07-17 18:44:25 -07001107 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001108 }
1109
Adam Cohenf358a4b2013-07-23 16:47:31 -07001110 protected void getOverviewModePages(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001111 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001112 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001113 }
1114
Michael Jurkadde558b2011-11-09 22:09:06 -08001115 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001116 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001117 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001118
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001119 range[0] = -1;
1120 range[1] = -1;
1121
Michael Jurkac4fb9172010-09-02 17:19:20 -07001122 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001123 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001124 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001125
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001126 int count = getChildCount();
1127 for (int i = 0; i < count; i++) {
1128 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001129
Winson Chungc9ca2982013-07-19 12:07:38 -07001130 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001131 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001132 if (mTmpIntPoint[0] > viewportWidth) {
1133 if (range[0] == -1) {
1134 continue;
1135 } else {
1136 break;
1137 }
1138 }
1139
Adam Cohen6a678da2013-09-24 10:58:01 -07001140 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001141 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1142 if (mTmpIntPoint[0] < 0) {
1143 if (range[0] == -1) {
1144 continue;
1145 } else {
1146 break;
1147 }
1148 }
1149 curScreen = i;
1150 if (range[0] < 0) {
1151 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001152 }
1153 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001154
1155 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001156 } else {
1157 range[0] = -1;
1158 range[1] = -1;
1159 }
1160 }
1161
Michael Jurka920d7f42012-05-14 16:29:55 -07001162 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001163 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001164 }
1165
Michael Jurkadde558b2011-11-09 22:09:06 -08001166 @Override
1167 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001168 // Find out which screens are visible; as an optimization we only call draw on them
1169 final int pageCount = getChildCount();
1170 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001171 int halfScreenSize = getViewportWidth() / 2;
1172 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1173 // Otherwise it is equal to the scaled overscroll position.
1174 int screenCenter = mOverScrollX + halfScreenSize;
1175
1176 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1177 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1178 // set it for the next frame
1179 mForceScreenScrolled = false;
1180 screenScrolled(screenCenter);
1181 mLastScreenCenter = screenCenter;
1182 }
1183
Michael Jurkadde558b2011-11-09 22:09:06 -08001184 getVisiblePages(mTempVisiblePagesRange);
1185 final int leftScreen = mTempVisiblePagesRange[0];
1186 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001187 if (leftScreen != -1 && rightScreen != -1) {
1188 final long drawingTime = getDrawingTime();
1189 // Clip to the bounds
1190 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001191 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1192 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001193
Adam Cohen7d30a372013-07-01 17:03:59 -07001194 // Draw all the children, leaving the drag view for last
1195 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001196 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001197 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001198 if (mForceDrawAllChildrenNextFrame ||
1199 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001200 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001201 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001202 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001203 // Draw the drag view on top (if there is one)
1204 if (mDragView != null) {
1205 drawChild(canvas, mDragView, drawingTime);
1206 }
1207
Michael Jurka5e368ff2012-05-14 23:13:15 -07001208 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001209 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001210 }
Michael Jurka0142d492010-08-25 17:46:15 -07001211 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 }
1213
1214 @Override
1215 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001216 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (page != mCurrentPage || !mScroller.isFinished()) {
1218 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 return true;
1220 }
1221 return false;
1222 }
1223
1224 @Override
1225 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001226 int focusablePage;
1227 if (mNextPage != INVALID_PAGE) {
1228 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001230 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 }
Winson Chung86f77532010-08-24 11:08:22 -07001232 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001234 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 }
1236 return false;
1237 }
1238
1239 @Override
1240 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001241 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001243 if (getCurrentPage() > 0) {
1244 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 return true;
1246 }
1247 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001248 if (getCurrentPage() < getPageCount() - 1) {
1249 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 return true;
1251 }
1252 }
1253 return super.dispatchUnhandledMove(focused, direction);
1254 }
1255
1256 @Override
1257 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001258 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001259 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001260 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 }
1262 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001263 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001264 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 }
1266 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001267 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001268 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 }
1270 }
1271 }
1272
1273 /**
1274 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001275 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 *
Winson Chung86f77532010-08-24 11:08:22 -07001277 * This happens when live folders requery, and if they're off page, they
1278 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 */
1280 @Override
1281 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001282 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 View v = focused;
1284 while (true) {
1285 if (v == current) {
1286 super.focusableViewAvailable(focused);
1287 return;
1288 }
1289 if (v == this) {
1290 return;
1291 }
1292 ViewParent parent = v.getParent();
1293 if (parent instanceof View) {
1294 v = (View)v.getParent();
1295 } else {
1296 return;
1297 }
1298 }
1299 }
1300
1301 /**
1302 * {@inheritDoc}
1303 */
1304 @Override
1305 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1306 if (disallowIntercept) {
1307 // We need to make sure to cancel our long press if
1308 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001309 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001310 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 }
1312 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1313 }
1314
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001315 /**
1316 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1317 */
1318 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001319 if (isLayoutRtl()) {
1320 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001321 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001322 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001323 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001324 }
1325
1326 /**
1327 * Return true if a tap at (x, y) should trigger a flip to the next page.
1328 */
1329 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001330 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001331 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001332 }
1333 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001334 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001335 }
Winson Chung52aee602013-01-30 12:01:02 -08001336
Adam Cohen7d30a372013-07-01 17:03:59 -07001337 /** Returns whether x and y originated within the buffered viewport */
1338 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1339 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1340 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1341 return mTmpRect.contains(x, y);
1342 }
1343
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 @Override
1345 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001346 if (DISABLE_TOUCH_INTERACTION) {
1347 return false;
1348 }
1349
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 /*
1351 * This method JUST determines whether we want to intercept the motion.
1352 * If we return true, onTouchEvent will be called and we do the actual
1353 * scrolling there.
1354 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001355 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001356
Winson Chung45e1d6e2010-11-09 17:19:49 -08001357 // Skip touch handling if there are no pages to swipe
1358 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1359
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 /*
1361 * Shortcut the most recurring case: the user is in the dragging
1362 * state and he is moving his finger. We want to intercept this
1363 * motion.
1364 */
1365 final int action = ev.getAction();
1366 if ((action == MotionEvent.ACTION_MOVE) &&
1367 (mTouchState == TOUCH_STATE_SCROLLING)) {
1368 return true;
1369 }
1370
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 switch (action & MotionEvent.ACTION_MASK) {
1372 case MotionEvent.ACTION_MOVE: {
1373 /*
1374 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1375 * whether the user has moved far enough from his original down touch.
1376 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001377 if (mActivePointerId != INVALID_POINTER) {
1378 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001379 }
1380 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1381 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1382 // i.e. fall through to the next case (don't break)
1383 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1384 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001385 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 }
1387
1388 case MotionEvent.ACTION_DOWN: {
1389 final float x = ev.getX();
1390 final float y = ev.getY();
1391 // Remember location of down touch
1392 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001393 mDownMotionY = y;
1394 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 mLastMotionX = x;
1396 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 float[] p = mapPointFromViewToParent(this, x, y);
1398 mParentDownMotionX = p[0];
1399 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001400 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001401 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001403
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 /*
1405 * If being flinged and user touches the screen, initiate drag;
1406 * otherwise don't. mScroller.isFinished should be false when
1407 * being flinged.
1408 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001409 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001410 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001411
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001412 if (finishedScrolling) {
1413 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001414 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001415 setCurrentPage(getNextPage());
1416 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001417 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001418 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001419 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1420 mTouchState = TOUCH_STATE_SCROLLING;
1421 } else {
1422 mTouchState = TOUCH_STATE_REST;
1423 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001424 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001425
Winson Chung86f77532010-08-24 11:08:22 -07001426 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001428 if (!DISABLE_TOUCH_SIDE_PAGES) {
1429 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1430 if (getChildCount() > 0) {
1431 if (hitsPreviousPage(x, y)) {
1432 mTouchState = TOUCH_STATE_PREV_PAGE;
1433 } else if (hitsNextPage(x, y)) {
1434 mTouchState = TOUCH_STATE_NEXT_PAGE;
1435 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 }
1437 }
1438 }
1439 break;
1440 }
1441
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001443 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001444 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 break;
1446
1447 case MotionEvent.ACTION_POINTER_UP:
1448 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001449 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001450 break;
1451 }
1452
1453 /*
1454 * The only time we want to intercept motion events is if we are in the
1455 * drag mode.
1456 */
1457 return mTouchState != TOUCH_STATE_REST;
1458 }
1459
Adam Cohenf8d28232011-02-01 21:47:00 -08001460 protected void determineScrollingStart(MotionEvent ev) {
1461 determineScrollingStart(ev, 1.0f);
1462 }
1463
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 /*
1465 * Determines if we should change the touch state to start scrolling after the
1466 * user moves their touch point too far.
1467 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001468 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001469 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001471 if (pointerIndex == -1) return;
1472
1473 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 final float x = ev.getX(pointerIndex);
1475 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001476 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1477
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 final int xDiff = (int) Math.abs(x - mLastMotionX);
1479 final int yDiff = (int) Math.abs(y - mLastMotionY);
1480
Adam Cohenf8d28232011-02-01 21:47:00 -08001481 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 boolean xPaged = xDiff > mPagingTouchSlop;
1483 boolean xMoved = xDiff > touchSlop;
1484 boolean yMoved = yDiff > touchSlop;
1485
Adam Cohenf8d28232011-02-01 21:47:00 -08001486 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001487 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001488 // Scroll if the user moved far enough along the X axis
1489 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001490 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001492 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001493 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001494 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1495 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001497 }
1498 }
1499
Adam Cohen7d30a372013-07-01 17:03:59 -07001500 protected float getMaxScrollProgress() {
1501 return 1.0f;
1502 }
1503
Adam Cohenf8d28232011-02-01 21:47:00 -08001504 protected void cancelCurrentPageLongPress() {
1505 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001506 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001507 // Try canceling the long press. It could also have been scheduled
1508 // by a distant descendant, so use the mAllowLongPress flag to block
1509 // everything
1510 final View currentPage = getPageAt(mCurrentPage);
1511 if (currentPage != null) {
1512 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 }
1514 }
1515 }
1516
Adam Cohen7d30a372013-07-01 17:03:59 -07001517 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1518 final int halfScreenSize = getViewportWidth() / 2;
1519
1520 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1521 screenCenter = Math.max(halfScreenSize, screenCenter);
1522
1523 return getScrollProgress(screenCenter, v, page);
1524 }
1525
Adam Cohenb5ba0972011-09-07 18:02:31 -07001526 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001527 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001528
Adam Cohenedb40762013-07-18 16:45:45 -07001529 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001530 int count = getChildCount();
1531
1532 final int totalDistance;
1533
1534 int adjacentPage = page + 1;
1535 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1536 adjacentPage = page - 1;
1537 }
1538
1539 if (adjacentPage < 0 || adjacentPage > count - 1) {
1540 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1541 } else {
1542 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1543 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001544
1545 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001546 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1547 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001548 return scrollProgress;
1549 }
1550
Adam Cohenedb40762013-07-18 16:45:45 -07001551 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001552 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001553 return 0;
1554 } else {
1555 return mPageScrolls[index];
1556 }
1557 }
1558
Adam Cohen564a2e72013-10-09 14:47:32 -07001559 // While layout transitions are occurring, a child's position may stray from its baseline
1560 // position. This method returns the magnitude of this stray at any given time.
1561 public int getLayoutTransitionOffsetForPage(int index) {
1562 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1563 return 0;
1564 } else {
1565 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001566
1567 int scrollOffset = 0;
1568 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1569 if (!lp.isFullScreenPage) {
1570 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1571 }
1572
Adam Cohen564a2e72013-10-09 14:47:32 -07001573 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1574 return (int) (child.getX() - baselineX);
1575 }
1576 }
1577
Adam Cohene0f66b52010-11-23 15:06:07 -08001578 // This curve determines how the effect of scrolling over the limits of the page dimishes
1579 // as the user pulls further and further from the bounds
1580 private float overScrollInfluenceCurve(float f) {
1581 f -= 1.0f;
1582 return f * f * f + 1.0f;
1583 }
1584
Adam Cohenb5ba0972011-09-07 18:02:31 -07001585 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001586 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001587
1588 // We want to reach the max over scroll effect when the user has
1589 // over scrolled half the size of the screen
1590 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1591
1592 if (f == 0) return;
1593
1594 // Clamp this factor, f, to -1 < f < 1
1595 if (Math.abs(f) >= 1) {
1596 f /= Math.abs(f);
1597 }
1598
1599 int overScrollAmount = (int) Math.round(f * screenSize);
1600 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001601 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001602 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001603 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001604 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001605 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001606 }
1607 invalidate();
1608 }
1609
1610 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001611 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001612
1613 float f = (amount / screenSize);
1614
1615 if (f == 0) return;
1616 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1617
Adam Cohen7bfc9792011-01-28 13:52:37 -08001618 // Clamp this factor, f, to -1 < f < 1
1619 if (Math.abs(f) >= 1) {
1620 f /= Math.abs(f);
1621 }
1622
Adam Cohene0f66b52010-11-23 15:06:07 -08001623 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001624 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001625 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001626 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001627 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001628 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001629 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001630 }
1631 invalidate();
1632 }
1633
Adam Cohenb5ba0972011-09-07 18:02:31 -07001634 protected void overScroll(float amount) {
1635 dampedOverScroll(amount);
1636 }
1637
Michael Jurkac5b262c2011-01-12 20:24:50 -08001638 protected float maxOverScroll() {
1639 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001640 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001641 float f = 1.0f;
1642 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1643 return OVERSCROLL_DAMP_FACTOR * f;
1644 }
1645
Adam Cohenf358a4b2013-07-23 16:47:31 -07001646 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001647 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001648 }
1649
Adam Cohenf9618852013-11-08 06:45:03 -08001650 protected void disableFreeScroll() {
1651 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001652 }
1653
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001654 void updateFreescrollBounds() {
1655 getOverviewModePages(mTempVisiblePagesRange);
1656 if (isLayoutRtl()) {
1657 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1658 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1659 } else {
1660 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1661 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1662 }
1663 }
1664
Adam Cohenf9618852013-11-08 06:45:03 -08001665 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001666 mFreeScroll = freeScroll;
1667
Adam Cohenf9618852013-11-08 06:45:03 -08001668 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001669 updateFreescrollBounds();
1670 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001671 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1672 setCurrentPage(mTempVisiblePagesRange[0]);
1673 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1674 setCurrentPage(mTempVisiblePagesRange[1]);
1675 }
1676 }
1677
1678 setEnableOverscroll(!freeScroll);
1679 }
1680
1681 private void setEnableOverscroll(boolean enable) {
1682 mAllowOverScroll = enable;
1683 }
1684
1685 int getNearestHoverOverPageIndex() {
1686 if (mDragView != null) {
1687 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1688 + mDragView.getTranslationX());
1689 getOverviewModePages(mTempVisiblePagesRange);
1690 int minDistance = Integer.MAX_VALUE;
1691 int minIndex = indexOfChild(mDragView);
1692 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1693 View page = getPageAt(i);
1694 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1695 int d = Math.abs(dragX - pageX);
1696 if (d < minDistance) {
1697 minIndex = i;
1698 minDistance = d;
1699 }
1700 }
1701 return minIndex;
1702 }
1703 return -1;
1704 }
1705
Winson Chung321e9ee2010-08-09 13:37:56 -07001706 @Override
1707 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001708 if (DISABLE_TOUCH_INTERACTION) {
1709 return false;
1710 }
1711
Adam Cohen1697b792013-09-17 19:08:21 -07001712 super.onTouchEvent(ev);
1713
Winson Chung45e1d6e2010-11-09 17:19:49 -08001714 // Skip touch handling if there are no pages to swipe
1715 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1716
Michael Jurkab8f06722010-10-10 15:58:46 -07001717 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001718
1719 final int action = ev.getAction();
1720
1721 switch (action & MotionEvent.ACTION_MASK) {
1722 case MotionEvent.ACTION_DOWN:
1723 /*
1724 * If being flinged and user touches, stop the fling. isFinished
1725 * will be false if being flinged.
1726 */
1727 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001728 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001729 }
1730
1731 // Remember where the motion event started
1732 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001733 mDownMotionY = mLastMotionY = ev.getY();
1734 mDownScrollX = getScrollX();
1735 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1736 mParentDownMotionX = p[0];
1737 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001738 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001739 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001740 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001741
Michael Jurka0142d492010-08-25 17:46:15 -07001742 if (mTouchState == TOUCH_STATE_SCROLLING) {
1743 pageBeginMoving();
1744 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001745 break;
1746
1747 case MotionEvent.ACTION_MOVE:
1748 if (mTouchState == TOUCH_STATE_SCROLLING) {
1749 // Scroll to follow the motion event
1750 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001751
1752 if (pointerIndex == -1) return true;
1753
Winson Chung321e9ee2010-08-09 13:37:56 -07001754 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001755 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001756
Adam Cohenaefd4e12011-02-14 16:39:38 -08001757 mTotalMotionX += Math.abs(deltaX);
1758
Winson Chungc0844aa2011-02-02 15:25:58 -08001759 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1760 // keep the remainder because we are actually testing if we've moved from the last
1761 // scrolled position (which is discrete).
1762 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001763 mTouchX += deltaX;
1764 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1765 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001766 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001767 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001768 } else {
1769 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001770 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001771 mLastMotionX = x;
1772 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001773 } else {
1774 awakenScrollBars();
1775 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001776 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1777 // Update the last motion position
1778 mLastMotionX = ev.getX();
1779 mLastMotionY = ev.getY();
1780
1781 // Update the parent down so that our zoom animations take this new movement into
1782 // account
1783 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1784 mParentDownMotionX = pt[0];
1785 mParentDownMotionY = pt[1];
1786 updateDragViewTranslationDuringDrag();
1787
1788 // Find the closest page to the touch point
1789 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001790
1791 // Change the drag view if we are hovering over the drop target
1792 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1793 (int) mParentDownMotionX, (int) mParentDownMotionY);
1794 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1795
Adam Cohen7d30a372013-07-01 17:03:59 -07001796 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1797 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1798 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1799 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1800
Adam Cohenf358a4b2013-07-23 16:47:31 -07001801 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1802 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1803 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001804 mTempVisiblePagesRange[0] = 0;
1805 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001806 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001807 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1808 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1809 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1810 mSidePageHoverIndex = pageUnderPointIndex;
1811 mSidePageHoverRunnable = new Runnable() {
1812 @Override
1813 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001814 // Setup the scroll to the correct page before we swap the views
1815 snapToPage(pageUnderPointIndex);
1816
1817 // For each of the pages between the paged view and the drag view,
1818 // animate them from the previous position to the new position in
1819 // the layout (as a result of the drag view moving in the layout)
1820 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1821 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1822 dragViewIndex + 1 : pageUnderPointIndex;
1823 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1824 dragViewIndex - 1 : pageUnderPointIndex;
1825 for (int i = lowerIndex; i <= upperIndex; ++i) {
1826 View v = getChildAt(i);
1827 // dragViewIndex < pageUnderPointIndex, so after we remove the
1828 // drag view all subsequent views to pageUnderPointIndex will
1829 // shift down.
1830 int oldX = getViewportOffsetX() + getChildOffset(i);
1831 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1832
1833 // Animate the view translation from its old position to its new
1834 // position
1835 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1836 if (anim != null) {
1837 anim.cancel();
1838 }
1839
1840 v.setTranslationX(oldX - newX);
1841 anim = new AnimatorSet();
1842 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1843 anim.playTogether(
1844 ObjectAnimator.ofFloat(v, "translationX", 0f));
1845 anim.start();
1846 v.setTag(anim);
1847 }
1848
1849 removeView(mDragView);
1850 onRemoveView(mDragView, false);
1851 addView(mDragView, pageUnderPointIndex);
1852 onAddView(mDragView, pageUnderPointIndex);
1853 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001854 if (mPageIndicator != null) {
1855 mPageIndicator.setActiveMarker(getNextPage());
1856 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001857 }
1858 };
1859 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1860 }
1861 } else {
1862 removeCallbacks(mSidePageHoverRunnable);
1863 mSidePageHoverIndex = -1;
1864 }
Adam Cohen564976a2010-10-13 18:52:07 -07001865 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001866 determineScrollingStart(ev);
1867 }
1868 break;
1869
1870 case MotionEvent.ACTION_UP:
1871 if (mTouchState == TOUCH_STATE_SCROLLING) {
1872 final int activePointerId = mActivePointerId;
1873 final int pointerIndex = ev.findPointerIndex(activePointerId);
1874 final float x = ev.getX(pointerIndex);
1875 final VelocityTracker velocityTracker = mVelocityTracker;
1876 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1877 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001878 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001879 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001880 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1881 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001882
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001883 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1884
Adam Cohen00481b32011-11-18 12:03:48 -08001885 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001886 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001887
Adam Cohenf358a4b2013-07-23 16:47:31 -07001888 if (!mFreeScroll) {
1889 // In the case that the page is moved far to one direction and then is flung
1890 // in the opposite direction, we use a threshold to determine whether we should
1891 // just return to the starting page, or if we should skip one further.
1892 boolean returnToOriginalPage = false;
1893 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1894 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1895 returnToOriginalPage = true;
1896 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001897
Adam Cohenf358a4b2013-07-23 16:47:31 -07001898 int finalPage;
1899 // We give flings precedence over large moves, which is why we short-circuit our
1900 // test for a large move if a fling has been registered. That is, a large
1901 // move to the left and fling to the right will register as a fling to the right.
1902 final boolean isRtl = isLayoutRtl();
1903 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1904 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1905 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1906 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1907 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1908 snapToPageWithVelocity(finalPage, velocityX);
1909 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1910 (isFling && isVelocityXLeft)) &&
1911 mCurrentPage < getChildCount() - 1) {
1912 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1913 snapToPageWithVelocity(finalPage, velocityX);
1914 } else {
1915 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001916 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001917 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001918 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001919 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001920 }
1921
1922 float scaleX = getScaleX();
1923 int vX = (int) (-velocityX * scaleX);
1924 int initialScrollX = (int) (getScrollX() * scaleX);
1925
Adam Cohenf9618852013-11-08 06:45:03 -08001926 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001927 mScroller.fling(initialScrollX,
1928 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1929 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001930 }
Adam Cohencae7f572013-11-04 14:42:52 -08001931 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1932 // at this point we have not moved beyond the touch slop
1933 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1934 // we can just page
1935 int nextPage = Math.max(0, mCurrentPage - 1);
1936 if (nextPage != mCurrentPage) {
1937 snapToPage(nextPage);
1938 } else {
1939 snapToDestination();
1940 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001941 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001942 // at this point we have not moved beyond the touch slop
1943 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1944 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001945 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1946 if (nextPage != mCurrentPage) {
1947 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 } else {
1949 snapToDestination();
1950 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001951 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1952 // Update the last motion position
1953 mLastMotionX = ev.getX();
1954 mLastMotionY = ev.getY();
1955
1956 // Update the parent down so that our zoom animations take this new movement into
1957 // account
1958 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1959 mParentDownMotionX = pt[0];
1960 mParentDownMotionY = pt[1];
1961 updateDragViewTranslationDuringDrag();
1962 boolean handledFling = false;
1963 if (!DISABLE_FLING_TO_DELETE) {
1964 // Check the velocity and see if we are flinging-to-delete
1965 PointF flingToDeleteVector = isFlingingToDelete();
1966 if (flingToDeleteVector != null) {
1967 onFlingToDelete(flingToDeleteVector);
1968 handledFling = true;
1969 }
1970 }
1971 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1972 (int) mParentDownMotionY)) {
1973 onDropToDelete();
1974 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001975 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001976 if (!mCancelTap) {
1977 onUnhandledTap(ev);
1978 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001979 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001980
1981 // Remove the callback to wait for the side page hover timeout
1982 removeCallbacks(mSidePageHoverRunnable);
1983 // End any intermediate reordering states
1984 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001985 break;
1986
1987 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001988 if (mTouchState == TOUCH_STATE_SCROLLING) {
1989 snapToDestination();
1990 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001991 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001992 break;
1993
1994 case MotionEvent.ACTION_POINTER_UP:
1995 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001996 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001997 break;
1998 }
1999
2000 return true;
2001 }
2002
Adam Cohen7d30a372013-07-01 17:03:59 -07002003 public void onFlingToDelete(View v) {}
2004 public void onRemoveView(View v, boolean deletePermanently) {}
2005 public void onRemoveViewAnimationCompleted() {}
2006 public void onAddView(View v, int index) {}
2007
2008 private void resetTouchState() {
2009 releaseVelocityTracker();
2010 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07002011 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002012 mTouchState = TOUCH_STATE_REST;
2013 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002014 }
2015
Adam Cohen1697b792013-09-17 19:08:21 -07002016 protected void onUnhandledTap(MotionEvent ev) {
2017 ((Launcher) getContext()).onClick(this);
2018 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002019
Winson Chung185d7162011-02-28 13:47:29 -08002020 @Override
2021 public boolean onGenericMotionEvent(MotionEvent event) {
2022 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2023 switch (event.getAction()) {
2024 case MotionEvent.ACTION_SCROLL: {
2025 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2026 final float vscroll;
2027 final float hscroll;
2028 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2029 vscroll = 0;
2030 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2031 } else {
2032 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2033 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2034 }
2035 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002036 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2037 : (hscroll > 0 || vscroll > 0);
2038 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002039 scrollRight();
2040 } else {
2041 scrollLeft();
2042 }
2043 return true;
2044 }
2045 }
2046 }
2047 }
2048 return super.onGenericMotionEvent(event);
2049 }
2050
Michael Jurkab8f06722010-10-10 15:58:46 -07002051 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2052 if (mVelocityTracker == null) {
2053 mVelocityTracker = VelocityTracker.obtain();
2054 }
2055 mVelocityTracker.addMovement(ev);
2056 }
2057
2058 private void releaseVelocityTracker() {
2059 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002060 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002061 mVelocityTracker.recycle();
2062 mVelocityTracker = null;
2063 }
2064 }
2065
Winson Chung321e9ee2010-08-09 13:37:56 -07002066 private void onSecondaryPointerUp(MotionEvent ev) {
2067 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2068 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2069 final int pointerId = ev.getPointerId(pointerIndex);
2070 if (pointerId == mActivePointerId) {
2071 // This was our active pointer going up. Choose a new
2072 // active pointer and adjust accordingly.
2073 // TODO: Make this decision more intelligent.
2074 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2075 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2076 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002077 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002078 mActivePointerId = ev.getPointerId(newPointerIndex);
2079 if (mVelocityTracker != null) {
2080 mVelocityTracker.clear();
2081 }
2082 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002083 }
2084
Winson Chung321e9ee2010-08-09 13:37:56 -07002085 @Override
2086 public void requestChildFocus(View child, View focused) {
2087 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002088 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002089 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002090 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002091 }
2092 }
2093
Winson Chung1908d072011-02-24 18:09:44 -08002094 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002095 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002096 }
2097
Adam Cohen7d30a372013-07-01 17:03:59 -07002098 int getPageNearestToPoint(float x) {
2099 int index = 0;
2100 for (int i = 0; i < getChildCount(); ++i) {
2101 if (x < getChildAt(i).getRight() - getScrollX()) {
2102 return index;
2103 } else {
2104 index++;
2105 }
2106 }
2107 return Math.min(index, getChildCount() - 1);
2108 }
2109
Adam Cohend19d3ca2010-09-15 14:43:42 -07002110 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002111 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002112 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002113 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002114 final int childCount = getChildCount();
2115 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002116 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002117 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002119 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002120 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2121 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2122 minDistanceFromScreenCenter = distanceFromScreenCenter;
2123 minDistanceFromScreenCenterIndex = i;
2124 }
2125 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002126 return minDistanceFromScreenCenterIndex;
2127 }
2128
2129 protected void snapToDestination() {
2130 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002131 }
2132
Adam Cohene0f66b52010-11-23 15:06:07 -08002133 private static class ScrollInterpolator implements Interpolator {
2134 public ScrollInterpolator() {
2135 }
2136
2137 public float getInterpolation(float t) {
2138 t -= 1.0f;
2139 return t*t*t*t*t + 1;
2140 }
2141 }
2142
2143 // We want the duration of the page snap animation to be influenced by the distance that
2144 // the screen has to travel, however, we don't want this duration to be effected in a
2145 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2146 // of travel has on the overall snap duration.
2147 float distanceInfluenceForSnapDuration(float f) {
2148 f -= 0.5f; // center the values about 0.
2149 f *= 0.3f * Math.PI / 2.0f;
2150 return (float) Math.sin(f);
2151 }
2152
Michael Jurka0142d492010-08-25 17:46:15 -07002153 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002154 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
Adam Cohen7d30a372013-07-01 17:03:59 -07002155 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002156
Adam Cohenedb40762013-07-18 16:45:45 -07002157 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002158 int delta = newX - mUnboundedScrollX;
2159 int duration = 0;
2160
Adam Cohen265b9a62011-12-07 14:37:18 -08002161 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002162 // If the velocity is low enough, then treat this more as an automatic page advance
2163 // as opposed to an apparent physical response to flinging
2164 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2165 return;
2166 }
2167
2168 // Here we compute a "distance" that will be used in the computation of the overall
2169 // snap duration. This is a function of the actual distance that needs to be traveled;
2170 // we keep this value close to half screen size in order to reduce the variance in snap
2171 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002172 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002173 float distance = halfScreenSize + halfScreenSize *
2174 distanceInfluenceForSnapDuration(distanceRatio);
2175
2176 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002177 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002178
2179 // we want the page's snap velocity to approximately match the velocity at which the
2180 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002181 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2182 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002183
2184 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002185 }
2186
2187 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002188 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002189 }
2190
Adam Cohen7d30a372013-07-01 17:03:59 -07002191 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002192 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002193 }
2194
Michael Jurka0142d492010-08-25 17:46:15 -07002195 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002196 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002197 }
2198
Adam Cohenf9618852013-11-08 06:45:03 -08002199 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2200 snapToPage(whichPage, duration, false, interpolator);
2201 }
2202
2203 protected void snapToPage(int whichPage, int duration, boolean immediate,
2204 TimeInterpolator interpolator) {
Winson Chung86f77532010-08-24 11:08:22 -07002205 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07002206
Adam Cohenedb40762013-07-18 16:45:45 -07002207 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002208 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002209 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002210 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002211 }
2212
2213 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002214 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002215 }
Michael Jurka0142d492010-08-25 17:46:15 -07002216
Adam Cohenf9618852013-11-08 06:45:03 -08002217 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2218 TimeInterpolator interpolator) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002219 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002220 View focusedChild = getFocusedChild();
2221 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002222 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002223 focusedChild.clearFocus();
2224 }
2225
Adam Cohen53805212013-10-01 10:39:23 -07002226 sendScrollAccessibilityEvent();
2227
Michael Jurka0142d492010-08-25 17:46:15 -07002228 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002229 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002230 if (immediate) {
2231 duration = 0;
2232 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002233 duration = Math.abs(delta);
2234 }
2235
Adam Cohenf358a4b2013-07-23 16:47:31 -07002236 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002237 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002238 }
Adam Cohenf9618852013-11-08 06:45:03 -08002239
2240 if (interpolator != null) {
2241 mScroller.setInterpolator(interpolator);
2242 } else {
2243 mScroller.setInterpolator(mDefaultInterpolator);
2244 }
2245
Adam Cohen68d73932010-11-15 10:50:58 -08002246 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002247
Adam Cohen674531f2013-12-13 15:07:14 -08002248 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002249
2250 // Trigger a compute() to finish switching pages if necessary
2251 if (immediate) {
2252 computeScroll();
2253 }
2254
Winson Chung9c0565f2013-07-19 13:49:06 -07002255 // Defer loading associated pages until the scroll settles
2256 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2257
Adam Cohen7d30a372013-07-01 17:03:59 -07002258 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002259 invalidate();
2260 }
2261
Winson Chung321e9ee2010-08-09 13:37:56 -07002262 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002263 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002264 }
2265
2266 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002267 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002268 }
2269
Winson Chung86f77532010-08-24 11:08:22 -07002270 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002271 int result = -1;
2272 if (v != null) {
2273 ViewParent vp = v.getParent();
2274 int count = getChildCount();
2275 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002276 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002277 return i;
2278 }
2279 }
2280 }
2281 return result;
2282 }
2283
2284 /**
2285 * @return True is long presses are still allowed for the current touch
2286 */
2287 public boolean allowLongPress() {
2288 return mAllowLongPress;
2289 }
2290
Adam Cohendbdff6b2013-09-18 19:09:15 -07002291 @Override
2292 public boolean performLongClick() {
2293 mCancelTap = true;
2294 return super.performLongClick();
2295 }
2296
Michael Jurka0142d492010-08-25 17:46:15 -07002297 /**
2298 * Set true to allow long-press events to be triggered, usually checked by
2299 * {@link Launcher} to accept or block dpad-initiated long-presses.
2300 */
2301 public void setAllowLongPress(boolean allowLongPress) {
2302 mAllowLongPress = allowLongPress;
2303 }
2304
Winson Chung321e9ee2010-08-09 13:37:56 -07002305 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002306 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002307
2308 SavedState(Parcelable superState) {
2309 super(superState);
2310 }
2311
2312 private SavedState(Parcel in) {
2313 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002314 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002315 }
2316
2317 @Override
2318 public void writeToParcel(Parcel out, int flags) {
2319 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002320 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002321 }
2322
2323 public static final Parcelable.Creator<SavedState> CREATOR =
2324 new Parcelable.Creator<SavedState>() {
2325 public SavedState createFromParcel(Parcel in) {
2326 return new SavedState(in);
2327 }
2328
2329 public SavedState[] newArray(int size) {
2330 return new SavedState[size];
2331 }
2332 };
2333 }
2334
Winson Chungf314b0e2011-08-16 11:54:27 -07002335 protected void loadAssociatedPages(int page) {
2336 loadAssociatedPages(page, false);
2337 }
2338 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002339 if (mContentIsRefreshable) {
2340 final int count = getChildCount();
2341 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002342 int lowerPageBound = getAssociatedLowerPageBound(page);
2343 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002344 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2345 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002346 // First, clear any pages that should no longer be loaded
2347 for (int i = 0; i < count; ++i) {
2348 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002349 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002350 if (layout.getPageChildCount() > 0) {
2351 layout.removeAllViewsOnPage();
2352 }
2353 mDirtyPageContent.set(i, true);
2354 }
2355 }
2356 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002357 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002358 if ((i != page) && immediateAndOnly) {
2359 continue;
2360 }
Michael Jurka0142d492010-08-25 17:46:15 -07002361 if (lowerPageBound <= i && i <= upperPageBound) {
2362 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002363 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002364 mDirtyPageContent.set(i, false);
2365 }
Winson Chung86f77532010-08-24 11:08:22 -07002366 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002367 }
2368 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002369 }
2370 }
2371
Winson Chunge3193b92010-09-10 11:44:42 -07002372 protected int getAssociatedLowerPageBound(int page) {
2373 return Math.max(0, page - 1);
2374 }
2375 protected int getAssociatedUpperPageBound(int page) {
2376 final int count = getChildCount();
2377 return Math.min(page + 1, count - 1);
2378 }
2379
Winson Chung86f77532010-08-24 11:08:22 -07002380 /**
2381 * This method is called ONLY to synchronize the number of pages that the paged view has.
2382 * To actually fill the pages with information, implement syncPageItems() below. It is
2383 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2384 * and therefore, individual page items do not need to be updated in this method.
2385 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002386 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002387
2388 /**
2389 * This method is called to synchronize the items that are on a particular page. If views on
2390 * the page can be reused, then they should be updated within this method.
2391 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002392 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002393
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002394 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002395 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002396 }
2397 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002398 invalidatePageData(currentPage, false);
2399 }
2400 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002401 if (!mIsDataReady) {
2402 return;
2403 }
2404
Michael Jurka0142d492010-08-25 17:46:15 -07002405 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002406 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002407 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002408
Michael Jurka0142d492010-08-25 17:46:15 -07002409 // Update all the pages
2410 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002411
Winson Chung5a808352011-06-27 19:08:49 -07002412 // We must force a measure after we've loaded the pages to update the content width and
2413 // to determine the full scroll width
2414 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2415 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2416
2417 // Set a new page as the current page if necessary
2418 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002419 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002420 }
2421
Michael Jurka0142d492010-08-25 17:46:15 -07002422 // Mark each of the pages as dirty
2423 final int count = getChildCount();
2424 mDirtyPageContent.clear();
2425 for (int i = 0; i < count; ++i) {
2426 mDirtyPageContent.add(true);
2427 }
2428
2429 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002430 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002431 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002432 }
Adam Cohen06559042013-09-29 18:30:56 -07002433 if (isPageMoving()) {
2434 // If the page is moving, then snap it to the final position to ensure we don't get
2435 // stuck between pages
2436 snapToDestination();
2437 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002438 }
Winson Chung007c6982011-06-14 13:27:53 -07002439
Adam Cohen7d30a372013-07-01 17:03:59 -07002440 // Animate the drag view back to the original position
2441 void animateDragViewToOriginalPosition() {
2442 if (mDragView != null) {
2443 AnimatorSet anim = new AnimatorSet();
2444 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2445 anim.playTogether(
2446 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002447 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2448 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2449 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002450 anim.addListener(new AnimatorListenerAdapter() {
2451 @Override
2452 public void onAnimationEnd(Animator animation) {
2453 onPostReorderingAnimationCompleted();
2454 }
2455 });
2456 anim.start();
2457 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002458 }
2459
Adam Cohen7d30a372013-07-01 17:03:59 -07002460 protected void onStartReordering() {
2461 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2462 mTouchState = TOUCH_STATE_REORDERING;
2463 mIsReordering = true;
2464
Adam Cohen7d30a372013-07-01 17:03:59 -07002465 // We must invalidate to trigger a redraw to update the layers such that the drag view
2466 // is always drawn on top
2467 invalidate();
2468 }
2469
2470 private void onPostReorderingAnimationCompleted() {
2471 // Trigger the callback when reordering has settled
2472 --mPostReorderingPreZoomInRemainingAnimationCount;
2473 if (mPostReorderingPreZoomInRunnable != null &&
2474 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2475 mPostReorderingPreZoomInRunnable.run();
2476 mPostReorderingPreZoomInRunnable = null;
2477 }
2478 }
2479
2480 protected void onEndReordering() {
2481 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002482 }
2483
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002485 int dragViewIndex = indexOfChild(v);
2486
2487 if (mTouchState != TOUCH_STATE_REST) return false;
2488
Adam Cohen7d30a372013-07-01 17:03:59 -07002489 mTempVisiblePagesRange[0] = 0;
2490 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002491 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002492 mReorderingStarted = true;
2493
2494 // Check if we are within the reordering range
2495 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 dragViewIndex <= mTempVisiblePagesRange[1]) {
2497 // Find the drag view under the pointer
2498 mDragView = getChildAt(dragViewIndex);
2499 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2500 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002501 snapToPage(getPageNearestToCenterOfScreen());
2502 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002504 return true;
2505 }
2506 return false;
2507 }
2508
2509 boolean isReordering(boolean testTouchState) {
2510 boolean state = mIsReordering;
2511 if (testTouchState) {
2512 state &= (mTouchState == TOUCH_STATE_REORDERING);
2513 }
2514 return state;
2515 }
2516 void endReordering() {
2517 // For simplicity, we call endReordering sometimes even if reordering was never started.
2518 // In that case, we don't want to do anything.
2519 if (!mReorderingStarted) return;
2520 mReorderingStarted = false;
2521
2522 // If we haven't flung-to-delete the current child, then we just animate the drag view
2523 // back into position
2524 final Runnable onCompleteRunnable = new Runnable() {
2525 @Override
2526 public void run() {
2527 onEndReordering();
2528 }
2529 };
2530 if (!mDeferringForDelete) {
2531 mPostReorderingPreZoomInRunnable = new Runnable() {
2532 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 onCompleteRunnable.run();
2534 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002535 };
2536 };
2537
2538 mPostReorderingPreZoomInRemainingAnimationCount =
2539 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2540 // Snap to the current page
2541 snapToPage(indexOfChild(mDragView), 0);
2542 // Animate the drag view back to the front position
2543 animateDragViewToOriginalPosition();
2544 } else {
2545 // Handled in post-delete-animation-callbacks
2546 }
2547 }
2548
Adam Cohen7d30a372013-07-01 17:03:59 -07002549 /*
2550 * Flinging to delete - IN PROGRESS
2551 */
2552 private PointF isFlingingToDelete() {
2553 ViewConfiguration config = ViewConfiguration.get(getContext());
2554 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2555
2556 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2557 // Do a quick dot product test to ensure that we are flinging upwards
2558 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2559 mVelocityTracker.getYVelocity());
2560 PointF upVec = new PointF(0f, -1f);
2561 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2562 (vel.length() * upVec.length()));
2563 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2564 return vel;
2565 }
2566 }
2567 return null;
2568 }
2569
2570 /**
2571 * Creates an animation from the current drag view along its current velocity vector.
2572 * For this animation, the alpha runs for a fixed duration and we update the position
2573 * progressively.
2574 */
2575 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2576 private View mDragView;
2577 private PointF mVelocity;
2578 private Rect mFrom;
2579 private long mPrevTime;
2580 private float mFriction;
2581
2582 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2583
2584 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2585 long startTime, float friction) {
2586 mDragView = dragView;
2587 mVelocity = vel;
2588 mFrom = from;
2589 mPrevTime = startTime;
2590 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2591 }
2592
2593 @Override
2594 public void onAnimationUpdate(ValueAnimator animation) {
2595 float t = ((Float) animation.getAnimatedValue()).floatValue();
2596 long curTime = AnimationUtils.currentAnimationTimeMillis();
2597
2598 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2599 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2600
2601 mDragView.setTranslationX(mFrom.left);
2602 mDragView.setTranslationY(mFrom.top);
2603 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2604
2605 mVelocity.x *= mFriction;
2606 mVelocity.y *= mFriction;
2607 mPrevTime = curTime;
2608 }
2609 };
2610
2611 private static final int ANIM_TAG_KEY = 100;
2612
2613 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2614 return new Runnable() {
2615 @Override
2616 public void run() {
2617 int dragViewIndex = indexOfChild(dragView);
2618
2619 // For each of the pages around the drag view, animate them from the previous
2620 // position to the new position in the layout (as a result of the drag view moving
2621 // in the layout)
2622 // NOTE: We can make an assumption here because we have side-bound pages that we
2623 // will always have pages to animate in from the left
Adam Cohenf358a4b2013-07-23 16:47:31 -07002624 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002625 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2626 boolean slideFromLeft = (isLastWidgetPage ||
2627 dragViewIndex > mTempVisiblePagesRange[0]);
2628
2629 // Setup the scroll to the correct page before we swap the views
2630 if (slideFromLeft) {
2631 snapToPageImmediately(dragViewIndex - 1);
2632 }
2633
2634 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2635 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2636 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2637 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2638 ArrayList<Animator> animations = new ArrayList<Animator>();
2639 for (int i = lowerIndex; i <= upperIndex; ++i) {
2640 View v = getChildAt(i);
2641 // dragViewIndex < pageUnderPointIndex, so after we remove the
2642 // drag view all subsequent views to pageUnderPointIndex will
2643 // shift down.
2644 int oldX = 0;
2645 int newX = 0;
2646 if (slideFromLeft) {
2647 if (i == 0) {
2648 // Simulate the page being offscreen with the page spacing
2649 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002650 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002651 } else {
2652 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2653 }
2654 newX = getViewportOffsetX() + getChildOffset(i);
2655 } else {
2656 oldX = getChildOffset(i) - getChildOffset(i - 1);
2657 newX = 0;
2658 }
2659
2660 // Animate the view translation from its old position to its new
2661 // position
2662 AnimatorSet anim = (AnimatorSet) v.getTag();
2663 if (anim != null) {
2664 anim.cancel();
2665 }
2666
2667 // Note: Hacky, but we want to skip any optimizations to not draw completely
2668 // hidden views
2669 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2670 v.setTranslationX(oldX - newX);
2671 anim = new AnimatorSet();
2672 anim.playTogether(
2673 ObjectAnimator.ofFloat(v, "translationX", 0f),
2674 ObjectAnimator.ofFloat(v, "alpha", 1f));
2675 animations.add(anim);
2676 v.setTag(ANIM_TAG_KEY, anim);
2677 }
2678
2679 AnimatorSet slideAnimations = new AnimatorSet();
2680 slideAnimations.playTogether(animations);
2681 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2682 slideAnimations.addListener(new AnimatorListenerAdapter() {
2683 @Override
2684 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002685 mDeferringForDelete = false;
2686 onEndReordering();
2687 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002688 }
2689 });
2690 slideAnimations.start();
2691
2692 removeView(dragView);
2693 onRemoveView(dragView, true);
2694 }
2695 };
2696 }
2697
2698 public void onFlingToDelete(PointF vel) {
2699 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2700
2701 // NOTE: Because it takes time for the first frame of animation to actually be
2702 // called and we expect the animation to be a continuation of the fling, we have
2703 // to account for the time that has elapsed since the fling finished. And since
2704 // we don't have a startDelay, we will always get call to update when we call
2705 // start() (which we want to ignore).
2706 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2707 private int mCount = -1;
2708 private long mStartTime;
2709 private float mOffset;
2710 /* Anonymous inner class ctor */ {
2711 mStartTime = startTime;
2712 }
2713
2714 @Override
2715 public float getInterpolation(float t) {
2716 if (mCount < 0) {
2717 mCount++;
2718 } else if (mCount == 0) {
2719 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2720 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2721 mCount++;
2722 }
2723 return Math.min(1f, mOffset + t);
2724 }
2725 };
2726
2727 final Rect from = new Rect();
2728 final View dragView = mDragView;
2729 from.left = (int) dragView.getTranslationX();
2730 from.top = (int) dragView.getTranslationY();
2731 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2732 from, startTime, FLING_TO_DELETE_FRICTION);
2733
2734 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2735
2736 // Create and start the animation
2737 ValueAnimator mDropAnim = new ValueAnimator();
2738 mDropAnim.setInterpolator(tInterpolator);
2739 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2740 mDropAnim.setFloatValues(0f, 1f);
2741 mDropAnim.addUpdateListener(updateCb);
2742 mDropAnim.addListener(new AnimatorListenerAdapter() {
2743 public void onAnimationEnd(Animator animation) {
2744 onAnimationEndRunnable.run();
2745 }
2746 });
2747 mDropAnim.start();
2748 mDeferringForDelete = true;
2749 }
2750
2751 /* Drag to delete */
2752 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2753 if (mDeleteDropTarget != null) {
2754 mAltTmpRect.set(0, 0, 0, 0);
2755 View parent = (View) mDeleteDropTarget.getParent();
2756 if (parent != null) {
2757 parent.getGlobalVisibleRect(mAltTmpRect);
2758 }
2759 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2760 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2761 return mTmpRect.contains(x, y);
2762 }
2763 return false;
2764 }
2765
2766 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2767
2768 private void onDropToDelete() {
2769 final View dragView = mDragView;
2770
2771 final float toScale = 0f;
2772 final float toAlpha = 0f;
2773
2774 // Create and start the complex animation
2775 ArrayList<Animator> animations = new ArrayList<Animator>();
2776 AnimatorSet motionAnim = new AnimatorSet();
2777 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2778 motionAnim.playTogether(
2779 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2780 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2781 animations.add(motionAnim);
2782
2783 AnimatorSet alphaAnim = new AnimatorSet();
2784 alphaAnim.setInterpolator(new LinearInterpolator());
2785 alphaAnim.playTogether(
2786 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2787 animations.add(alphaAnim);
2788
2789 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2790
2791 AnimatorSet anim = new AnimatorSet();
2792 anim.playTogether(animations);
2793 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2794 anim.addListener(new AnimatorListenerAdapter() {
2795 public void onAnimationEnd(Animator animation) {
2796 onAnimationEndRunnable.run();
2797 }
2798 });
2799 anim.start();
2800
2801 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002802 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002803
2804 /* Accessibility */
2805 @Override
2806 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2807 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002808 info.setScrollable(getPageCount() > 1);
2809 if (getCurrentPage() < getPageCount() - 1) {
2810 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2811 }
2812 if (getCurrentPage() > 0) {
2813 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2814 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002815 }
2816
2817 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002818 public void sendAccessibilityEvent(int eventType) {
2819 // Don't let the view send real scroll events.
2820 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2821 super.sendAccessibilityEvent(eventType);
2822 }
2823 }
2824
2825 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002826 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2827 super.onInitializeAccessibilityEvent(event);
2828 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002829 }
2830
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002831 @Override
2832 public boolean performAccessibilityAction(int action, Bundle arguments) {
2833 if (super.performAccessibilityAction(action, arguments)) {
2834 return true;
2835 }
2836 switch (action) {
2837 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2838 if (getCurrentPage() < getPageCount() - 1) {
2839 scrollRight();
2840 return true;
2841 }
2842 } break;
2843 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2844 if (getCurrentPage() > 0) {
2845 scrollLeft();
2846 return true;
2847 }
2848 } break;
2849 }
2850 return false;
2851 }
2852
Adam Cohen0ffac432013-07-10 11:19:26 -07002853 protected String getCurrentPageDescription() {
2854 return String.format(getContext().getString(R.string.default_scroll_format),
2855 getNextPage() + 1, getChildCount());
2856 }
2857
Winson Chungd11265e2011-08-30 13:37:23 -07002858 @Override
2859 public boolean onHoverEvent(android.view.MotionEvent event) {
2860 return true;
2861 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002862}