blob: c216f926fd70ba1004c6231470b1e288e6096583 [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
Winson Chung86f77532010-08-24 11:08:22 -0700454 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
455 mPageSwitchListener = pageSwitchListener;
456 if (mPageSwitchListener != null) {
457 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 }
459 }
460
461 /**
Winson Chung52aee602013-01-30 12:01:02 -0800462 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
463 */
464 public boolean isLayoutRtl() {
465 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
466 }
467
468 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700469 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
470 * out pages.
471 */
472 protected void setDataIsReady() {
473 mIsDataReady = true;
474 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700475
Winson Chungf0ea4d32011-06-06 14:27:16 -0700476 protected boolean isDataReady() {
477 return mIsDataReady;
478 }
479
480 /**
Winson Chung86f77532010-08-24 11:08:22 -0700481 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 *
Winson Chung86f77532010-08-24 11:08:22 -0700483 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 */
Winson Chung86f77532010-08-24 11:08:22 -0700485 int getCurrentPage() {
486 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700488
Winson Chung360e63f2012-04-27 13:48:05 -0700489 int getNextPage() {
490 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
491 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700492
Winson Chung86f77532010-08-24 11:08:22 -0700493 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 return getChildCount();
495 }
496
Winson Chung86f77532010-08-24 11:08:22 -0700497 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 return getChildAt(index);
499 }
500
Adam Cohenae4f1552011-10-20 00:15:42 -0700501 protected int indexToPage(int index) {
502 return index;
503 }
504
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800506 * Updates the scroll of the current page immediately to its final scroll position. We use this
507 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
508 * the previous tab page.
509 */
510 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700511 // If the current page is invalid, just reset the scroll position to zero
512 int newX = 0;
513 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700514 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700515 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800516 scrollTo(newX, 0);
517 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700518 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800519 }
520
521 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700522 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700523 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
524 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700525 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700526 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700527 mCurrentPage = getNextPage();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700528 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700529 }
530
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700531 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700532 mScroller.abortAnimation();
533 // We need to clean up the next page here to avoid computeScrollHelper from
534 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700535 if (resetNextPage) {
536 mNextPage = INVALID_PAGE;
537 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700538 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700539
540 private void forceFinishScroller() {
541 mScroller.forceFinished(true);
542 // We need to clean up the next page here to avoid computeScrollHelper from
543 // updating current page on the pass.
544 mNextPage = INVALID_PAGE;
545 }
546
Chet Haasebc2f0822012-10-26 17:59:53 -0700547 /**
Winson Chung86f77532010-08-24 11:08:22 -0700548 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 */
Winson Chung86f77532010-08-24 11:08:22 -0700550 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800551 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700552 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800553 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800554 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
555 // the default
556 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800557 return;
558 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700559 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700560 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chung181c3dc2013-07-17 15:36:20 -0700561 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700562 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800563 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 }
565
Winson Chung8c87cd82013-07-23 16:20:10 -0700566 /**
567 * The restore page will be set in place of the current page at the next (likely first)
568 * layout.
569 */
570 void setRestorePage(int restorePage) {
571 mRestorePage = restorePage;
572 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800573 int getRestorePage() {
574 return mRestorePage;
575 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700576
Michael Jurka0142d492010-08-25 17:46:15 -0700577 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700578 if (mPageSwitchListener != null) {
579 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 }
Winson Chungd2be3812013-07-16 11:11:32 -0700581
582 // Update the page indicator (when we aren't reordering)
583 if (mPageIndicator != null && !isReordering(false)) {
584 mPageIndicator.setActiveMarker(getNextPage());
585 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700586 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800587 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700588 if (!mIsPageMoving) {
589 mIsPageMoving = true;
590 onPageBeginMoving();
591 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700592 }
593
Michael Jurkace7e05f2011-02-01 22:02:35 -0800594 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700595 if (mIsPageMoving) {
596 mIsPageMoving = false;
597 onPageEndMoving();
598 }
Michael Jurka0142d492010-08-25 17:46:15 -0700599 }
600
Adam Cohen26976d92011-03-22 15:33:33 -0700601 protected boolean isPageMoving() {
602 return mIsPageMoving;
603 }
604
Michael Jurka0142d492010-08-25 17:46:15 -0700605 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700606 protected void onPageBeginMoving() {
607 }
608
609 // a method that subclasses can override to add behavior
610 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700611 }
612
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 /**
Winson Chung86f77532010-08-24 11:08:22 -0700614 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 *
616 * @param l The listener used to respond to long clicks.
617 */
618 @Override
619 public void setOnLongClickListener(OnLongClickListener l) {
620 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700621 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700622 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700623 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 }
Adam Cohen1697b792013-09-17 19:08:21 -0700625 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 }
627
628 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800629 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800631 }
632
633 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700634 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700635 // In free scroll mode, we clamp the scrollX
636 if (mFreeScroll) {
637 x = Math.min(x, mFreeScrollMaxScrollX);
638 x = Math.max(x, mFreeScrollMinScrollX);
639 }
640
Adam Cohen0ffac432013-07-10 11:19:26 -0700641 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800642 mUnboundedScrollX = x;
643
Adam Cohen0ffac432013-07-10 11:19:26 -0700644 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
645 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
646 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800647 super.scrollTo(0, y);
648 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700649 if (isRtl) {
650 overScroll(x - mMaxScrollX);
651 } else {
652 overScroll(x);
653 }
Adam Cohen68d73932010-11-15 10:50:58 -0800654 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700655 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800656 super.scrollTo(mMaxScrollX, y);
657 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700658 if (isRtl) {
659 overScroll(x);
660 } else {
661 overScroll(x - mMaxScrollX);
662 }
Adam Cohen68d73932010-11-15 10:50:58 -0800663 }
664 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800665 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800666 super.scrollTo(x, y);
667 }
668
Michael Jurka0142d492010-08-25 17:46:15 -0700669 mTouchX = x;
670 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700671
672 // Update the last motion events when scrolling
673 if (isReordering(true)) {
674 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
675 mLastMotionX = p[0];
676 mLastMotionY = p[1];
677 updateDragViewTranslationDuringDrag();
678 }
Michael Jurka0142d492010-08-25 17:46:15 -0700679 }
680
Adam Cohen53805212013-10-01 10:39:23 -0700681 private void sendScrollAccessibilityEvent() {
682 AccessibilityManager am =
683 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
684 if (am.isEnabled()) {
685 AccessibilityEvent ev =
686 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700687 ev.setItemCount(getChildCount());
688 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700689 ev.setToIndex(getNextPage());
Adam Cohen53805212013-10-01 10:39:23 -0700690
Alan Viverette254139a2013-10-08 13:13:46 -0700691 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700692 if (getNextPage() >= mCurrentPage) {
693 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
694 } else {
695 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
696 }
697
698 ev.setAction(action);
699 sendAccessibilityEventUnchecked(ev);
700 }
701 }
702
Michael Jurka0142d492010-08-25 17:46:15 -0700703 // we moved this functionality to a helper function so SmoothPagedView can reuse it
704 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700706 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 if (getScrollX() != mScroller.getCurrX()
708 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700709 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700710 float scaleX = mFreeScroll ? getScaleX() : 1f;
711 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
712 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700713 }
Michael Jurka0142d492010-08-25 17:46:15 -0700714 invalidate();
715 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700716 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700717 sendScrollAccessibilityEvent();
718
Winson Chung86f77532010-08-24 11:08:22 -0700719 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700720 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700721 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700722
Winson Chung9c0565f2013-07-19 13:49:06 -0700723 // Load the associated pages if necessary
724 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
725 loadAssociatedPages(mCurrentPage);
726 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
727 }
728
Adam Cohen73aa9752010-11-24 16:26:58 -0800729 // We don't want to trigger a page end moving unless the page has settled
730 // and the user has stopped scrolling
731 if (mTouchState == TOUCH_STATE_REST) {
732 pageEndMoving();
733 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700734
Adam Cohen7d30a372013-07-01 17:03:59 -0700735 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700736 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700737 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700738 if (am.isEnabled()) {
739 // Notify the user when the page changes
740 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700741 }
Michael Jurka0142d492010-08-25 17:46:15 -0700742 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 }
Michael Jurka0142d492010-08-25 17:46:15 -0700744 return false;
745 }
746
747 @Override
748 public void computeScroll() {
749 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 }
751
Adam Cohen7d30a372013-07-01 17:03:59 -0700752 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
753 return mTopAlignPageWhenShrinkingForBouncer;
754 }
755
Adam Cohen96d30a12013-07-16 18:13:21 -0700756 public static class LayoutParams extends ViewGroup.LayoutParams {
757 public boolean isFullScreenPage = false;
758
759 /**
760 * {@inheritDoc}
761 */
762 public LayoutParams(int width, int height) {
763 super(width, height);
764 }
765
766 public LayoutParams(ViewGroup.LayoutParams source) {
767 super(source);
768 }
769 }
770
771 protected LayoutParams generateDefaultLayoutParams() {
772 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
773 }
774
Adam Cohenedb40762013-07-18 16:45:45 -0700775 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700776 LayoutParams lp = generateDefaultLayoutParams();
777 lp.isFullScreenPage = true;
778 super.addView(page, 0, lp);
779 }
780
Adam Cohen410f3cd2013-09-22 12:09:32 -0700781 public int getNormalChildHeight() {
782 return mNormalChildHeight;
783 }
784
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 @Override
786 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700788 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
789 return;
790 }
791
Adam Cohen7d30a372013-07-01 17:03:59 -0700792 // We measure the dimensions of the PagedView to be larger than the pages so that when we
793 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700794 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
795 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
796 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700797 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800798 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700799 // viewport, we can be at most one and a half screens offset once we scale down
800 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700801 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
802 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700803
Winson Chungc82d2622013-11-06 13:23:29 -0800804 int parentWidthSize = (int) (2f * maxSize);
805 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700806 int scaledWidthSize, scaledHeightSize;
807 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700808 scaledWidthSize = (int) (parentWidthSize / mMinScale);
809 scaledHeightSize = (int) (parentHeightSize / mMinScale);
810 } else {
811 scaledWidthSize = widthSize;
812 scaledHeightSize = heightSize;
813 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700814 mViewport.set(0, 0, widthSize, heightSize);
815
816 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
817 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
818 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 }
820
Winson Chung8aad6102012-05-11 16:27:49 -0700821 // Return early if we aren't given a proper dimension
822 if (widthSize <= 0 || heightSize <= 0) {
823 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
824 return;
825 }
826
Adam Lesinski6b879f02010-11-04 16:15:23 -0700827 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
828 * of the All apps view on XLarge displays to not take up more space then it needs. Width
829 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
830 * each page to have the same width.
831 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700832 final int verticalPadding = getPaddingTop() + getPaddingBottom();
833 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700834
835 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700836 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700837 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700838 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
839 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
840 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
841 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 final int childCount = getChildCount();
843 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700844 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700845 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100846 if (child.getVisibility() != GONE) {
847 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700848
Vladimir Marko2824b072013-10-04 16:42:17 +0100849 int childWidthMode;
850 int childHeightMode;
851 int childWidth;
852 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700853
Vladimir Marko2824b072013-10-04 16:42:17 +0100854 if (!lp.isFullScreenPage) {
855 if (lp.width == LayoutParams.WRAP_CONTENT) {
856 childWidthMode = MeasureSpec.AT_MOST;
857 } else {
858 childWidthMode = MeasureSpec.EXACTLY;
859 }
860
861 if (lp.height == LayoutParams.WRAP_CONTENT) {
862 childHeightMode = MeasureSpec.AT_MOST;
863 } else {
864 childHeightMode = MeasureSpec.EXACTLY;
865 }
866
Adam Cohen3b185e22013-10-29 14:45:58 -0700867 childWidth = getViewportWidth() - horizontalPadding
868 - mInsets.left - mInsets.right;
869 childHeight = getViewportHeight() - verticalPadding
870 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100871 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700872 } else {
873 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700874 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100875
Adam Cohen3b185e22013-10-29 14:45:58 -0700876 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
877 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700878 }
879
Vladimir Marko2824b072013-10-04 16:42:17 +0100880 final int childWidthMeasureSpec =
881 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
882 final int childHeightMeasureSpec =
883 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
884 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700885 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700886 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700887 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800888 }
889
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700891 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700892 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700893 return;
894 }
895
Winson Chung785d2eb2011-04-14 16:08:02 -0700896 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700898
Adam Cohen7d30a372013-07-01 17:03:59 -0700899 int offsetX = getViewportOffsetX();
900 int offsetY = getViewportOffsetY();
901
902 // Update the viewport offsets
903 mViewport.offset(offsetX, offsetY);
904
Adam Cohen0ffac432013-07-10 11:19:26 -0700905 final boolean isRtl = isLayoutRtl();
906
907 final int startIndex = isRtl ? childCount - 1 : 0;
908 final int endIndex = isRtl ? -1 : childCount;
909 final int delta = isRtl ? -1 : 1;
910
Adam Cohen7d30a372013-07-01 17:03:59 -0700911 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700912
Adam Cohen3b185e22013-10-29 14:45:58 -0700913 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000914 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700915
916 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700917 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
918 mPageScrolls = new int[getChildCount()];
919 }
920
Adam Cohen0ffac432013-07-10 11:19:26 -0700921 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700922 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700924 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100925 int childTop;
926 if (lp.isFullScreenPage) {
927 childTop = offsetY;
928 } else {
929 childTop = offsetY + getPaddingTop() + mInsets.top;
930 if (mCenterPagesVertically) {
931 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
932 }
933 }
934
Adam Cohen96d30a12013-07-16 18:13:21 -0700935 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700936 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800937
Winson Chung785d2eb2011-04-14 16:08:02 -0700938 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700939 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800940 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700941
Adam Cohen3b185e22013-10-29 14:45:58 -0700942 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
943 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000944
945 int pageGap = mPageSpacing;
946 int next = i + delta;
947 if (next != endIndex) {
948 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
949 } else {
950 nextLp = null;
951 }
952
953 // Prevent full screen pages from showing in the viewport
954 // when they are not the current page.
955 if (lp.isFullScreenPage) {
956 pageGap = getPaddingLeft();
957 } else if (nextLp != null && nextLp.isFullScreenPage) {
958 pageGap = getPaddingRight();
959 }
960
961 childLeft += childWidth + pageGap;
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 }
963 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700964
965 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
966 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800967 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700968 setHorizontalScrollBarEnabled(true);
969 mFirstLayout = false;
970 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700971
972 if (childCount > 0) {
973 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700974 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700975 } else {
976 mMaxScrollX = 0;
977 }
978
979 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
980 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700981 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700982 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700983 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700984 } else {
985 setCurrentPage(getNextPage());
986 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700987 }
988 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -0700989
990 if (isReordering(true)) {
991 updateDragViewTranslationDuringDrag();
992 }
Winson Chunge3193b92010-09-10 11:44:42 -0700993 }
994
Adam Cohen3b185e22013-10-29 14:45:58 -0700995 public void setPageSpacing(int pageSpacing) {
996 mPageSpacing = pageSpacing;
997 requestLayout();
998 }
999
Adam Cohenf34bab52010-09-30 14:11:56 -07001000 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001001 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1002
1003 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -07001004 for (int i = 0; i < getChildCount(); i++) {
1005 View child = getChildAt(i);
1006 if (child != null) {
1007 float scrollProgress = getScrollProgress(screenCenter, child, i);
1008 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001009 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001010 }
1011 }
1012 invalidate();
1013 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001014 }
1015
Winson Chungc58497e2013-09-03 17:48:37 -07001016 protected void enablePagedViewAnimations() {
1017 mAllowPagedViewAnimations = true;
1018
1019 }
1020 protected void disablePagedViewAnimations() {
1021 mAllowPagedViewAnimations = false;
1022 }
1023
Winson Chunge3193b92010-09-10 11:44:42 -07001024 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001025 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001026 // Update the page indicator, we don't update the page indicator as we
1027 // add/remove pages
1028 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001029 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001030 mPageIndicator.addMarker(pageIndex,
1031 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001032 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001033 }
1034
Adam Cohen2591f6a2011-10-25 14:36:40 -07001035 // This ensures that when children are added, they get the correct transforms / alphas
1036 // in accordance with any scroll effects.
1037 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001038 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001039 invalidate();
1040 }
1041
Michael Jurka8b805b12012-04-18 14:23:14 -07001042 @Override
1043 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001044 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001045 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001046 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001047 }
1048
Winson Chungd2be3812013-07-16 11:11:32 -07001049 private void removeMarkerForView(int index) {
1050 // Update the page indicator, we don't update the page indicator as we
1051 // add/remove pages
1052 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001053 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001054 }
1055 }
1056
1057 @Override
1058 public void removeView(View v) {
1059 // XXX: We should find a better way to hook into this before the view
1060 // gets removed form its parent...
1061 removeMarkerForView(indexOfChild(v));
1062 super.removeView(v);
1063 }
1064 @Override
1065 public void removeViewInLayout(View v) {
1066 // XXX: We should find a better way to hook into this before the view
1067 // gets removed form its parent...
1068 removeMarkerForView(indexOfChild(v));
1069 super.removeViewInLayout(v);
1070 }
1071 @Override
1072 public void removeViewAt(int index) {
1073 // XXX: We should find a better way to hook into this before the view
1074 // gets removed form its parent...
1075 removeViewAt(index);
1076 super.removeViewAt(index);
1077 }
1078 @Override
1079 public void removeAllViewsInLayout() {
1080 // Update the page indicator, we don't update the page indicator as we
1081 // add/remove pages
1082 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001083 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001084 }
1085
1086 super.removeAllViewsInLayout();
1087 }
1088
Adam Cohen73894962011-10-31 13:17:17 -07001089 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001090 if (index < 0 || index > getChildCount() - 1) return 0;
1091
Adam Cohenf698c6e2013-07-17 18:44:25 -07001092 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001093
Adam Cohenf698c6e2013-07-17 18:44:25 -07001094 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001095 }
1096
Adam Cohenf358a4b2013-07-23 16:47:31 -07001097 protected void getOverviewModePages(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001098 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001099 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001100 }
1101
Michael Jurkadde558b2011-11-09 22:09:06 -08001102 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001103 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001104 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001105
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001106 range[0] = -1;
1107 range[1] = -1;
1108
Michael Jurkac4fb9172010-09-02 17:19:20 -07001109 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001110 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001111 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001112
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001113 int count = getChildCount();
1114 for (int i = 0; i < count; i++) {
1115 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001116
Winson Chungc9ca2982013-07-19 12:07:38 -07001117 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001118 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001119 if (mTmpIntPoint[0] > viewportWidth) {
1120 if (range[0] == -1) {
1121 continue;
1122 } else {
1123 break;
1124 }
1125 }
1126
Adam Cohen6a678da2013-09-24 10:58:01 -07001127 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001128 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1129 if (mTmpIntPoint[0] < 0) {
1130 if (range[0] == -1) {
1131 continue;
1132 } else {
1133 break;
1134 }
1135 }
1136 curScreen = i;
1137 if (range[0] < 0) {
1138 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001139 }
1140 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001141
1142 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001143 } else {
1144 range[0] = -1;
1145 range[1] = -1;
1146 }
1147 }
1148
Michael Jurka920d7f42012-05-14 16:29:55 -07001149 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001150 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001151 }
1152
Michael Jurkadde558b2011-11-09 22:09:06 -08001153 @Override
1154 protected void dispatchDraw(Canvas canvas) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001155 int halfScreenSize = getViewportWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -07001156 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1157 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -08001158 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -08001159
1160 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -07001161 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1162 // set it for the next frame
1163 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -08001164 screenScrolled(screenCenter);
1165 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -08001166 }
1167
1168 // Find out which screens are visible; as an optimization we only call draw on them
1169 final int pageCount = getChildCount();
1170 if (pageCount > 0) {
1171 getVisiblePages(mTempVisiblePagesRange);
1172 final int leftScreen = mTempVisiblePagesRange[0];
1173 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001174 if (leftScreen != -1 && rightScreen != -1) {
1175 final long drawingTime = getDrawingTime();
1176 // Clip to the bounds
1177 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001178 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1179 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001180
Adam Cohen7d30a372013-07-01 17:03:59 -07001181 // Draw all the children, leaving the drag view for last
1182 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001183 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001184 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001185 if (mForceDrawAllChildrenNextFrame ||
1186 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001187 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001188 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001189 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001190 // Draw the drag view on top (if there is one)
1191 if (mDragView != null) {
1192 drawChild(canvas, mDragView, drawingTime);
1193 }
1194
Michael Jurka5e368ff2012-05-14 23:13:15 -07001195 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001196 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001197 }
Michael Jurka0142d492010-08-25 17:46:15 -07001198 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 }
1200
1201 @Override
1202 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001203 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001204 if (page != mCurrentPage || !mScroller.isFinished()) {
1205 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 return true;
1207 }
1208 return false;
1209 }
1210
1211 @Override
1212 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001213 int focusablePage;
1214 if (mNextPage != INVALID_PAGE) {
1215 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001217 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 }
Winson Chung86f77532010-08-24 11:08:22 -07001219 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001221 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 }
1223 return false;
1224 }
1225
1226 @Override
1227 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001228 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001230 if (getCurrentPage() > 0) {
1231 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 return true;
1233 }
1234 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001235 if (getCurrentPage() < getPageCount() - 1) {
1236 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 return true;
1238 }
1239 }
1240 return super.dispatchUnhandledMove(focused, direction);
1241 }
1242
1243 @Override
1244 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001245 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001246 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001247 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 }
1249 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001250 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001251 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 }
1253 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001254 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001255 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 }
1257 }
1258 }
1259
1260 /**
1261 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001262 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 *
Winson Chung86f77532010-08-24 11:08:22 -07001264 * This happens when live folders requery, and if they're off page, they
1265 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 */
1267 @Override
1268 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001269 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 View v = focused;
1271 while (true) {
1272 if (v == current) {
1273 super.focusableViewAvailable(focused);
1274 return;
1275 }
1276 if (v == this) {
1277 return;
1278 }
1279 ViewParent parent = v.getParent();
1280 if (parent instanceof View) {
1281 v = (View)v.getParent();
1282 } else {
1283 return;
1284 }
1285 }
1286 }
1287
1288 /**
1289 * {@inheritDoc}
1290 */
1291 @Override
1292 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1293 if (disallowIntercept) {
1294 // We need to make sure to cancel our long press if
1295 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001296 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001297 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 }
1299 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1300 }
1301
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001302 /**
1303 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1304 */
1305 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001306 if (isLayoutRtl()) {
1307 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001308 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001309 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001310 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001311 }
1312
1313 /**
1314 * Return true if a tap at (x, y) should trigger a flip to the next page.
1315 */
1316 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001317 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001318 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001319 }
1320 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001321 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 }
Winson Chung52aee602013-01-30 12:01:02 -08001323
Adam Cohen7d30a372013-07-01 17:03:59 -07001324 /** Returns whether x and y originated within the buffered viewport */
1325 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1326 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1327 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1328 return mTmpRect.contains(x, y);
1329 }
1330
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 @Override
1332 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001333 if (DISABLE_TOUCH_INTERACTION) {
1334 return false;
1335 }
1336
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 /*
1338 * This method JUST determines whether we want to intercept the motion.
1339 * If we return true, onTouchEvent will be called and we do the actual
1340 * scrolling there.
1341 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001342 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001343
Winson Chung45e1d6e2010-11-09 17:19:49 -08001344 // Skip touch handling if there are no pages to swipe
1345 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1346
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 /*
1348 * Shortcut the most recurring case: the user is in the dragging
1349 * state and he is moving his finger. We want to intercept this
1350 * motion.
1351 */
1352 final int action = ev.getAction();
1353 if ((action == MotionEvent.ACTION_MOVE) &&
1354 (mTouchState == TOUCH_STATE_SCROLLING)) {
1355 return true;
1356 }
1357
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 switch (action & MotionEvent.ACTION_MASK) {
1359 case MotionEvent.ACTION_MOVE: {
1360 /*
1361 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1362 * whether the user has moved far enough from his original down touch.
1363 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001364 if (mActivePointerId != INVALID_POINTER) {
1365 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001366 }
1367 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1368 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1369 // i.e. fall through to the next case (don't break)
1370 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1371 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001372 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001373 }
1374
1375 case MotionEvent.ACTION_DOWN: {
1376 final float x = ev.getX();
1377 final float y = ev.getY();
1378 // Remember location of down touch
1379 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001380 mDownMotionY = y;
1381 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 mLastMotionX = x;
1383 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001384 float[] p = mapPointFromViewToParent(this, x, y);
1385 mParentDownMotionX = p[0];
1386 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001387 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001388 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001390
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 /*
1392 * If being flinged and user touches the screen, initiate drag;
1393 * otherwise don't. mScroller.isFinished should be false when
1394 * being flinged.
1395 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001396 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001397 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001398
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001399 if (finishedScrolling) {
1400 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001401 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001402 setCurrentPage(getNextPage());
1403 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001404 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001405 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001406 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1407 mTouchState = TOUCH_STATE_SCROLLING;
1408 } else {
1409 mTouchState = TOUCH_STATE_REST;
1410 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001411 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001412
Winson Chung86f77532010-08-24 11:08:22 -07001413 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001415 if (!DISABLE_TOUCH_SIDE_PAGES) {
1416 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1417 if (getChildCount() > 0) {
1418 if (hitsPreviousPage(x, y)) {
1419 mTouchState = TOUCH_STATE_PREV_PAGE;
1420 } else if (hitsNextPage(x, y)) {
1421 mTouchState = TOUCH_STATE_NEXT_PAGE;
1422 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001423 }
1424 }
1425 }
1426 break;
1427 }
1428
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001430 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001431 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 break;
1433
1434 case MotionEvent.ACTION_POINTER_UP:
1435 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001436 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 break;
1438 }
1439
1440 /*
1441 * The only time we want to intercept motion events is if we are in the
1442 * drag mode.
1443 */
1444 return mTouchState != TOUCH_STATE_REST;
1445 }
1446
Adam Cohenf8d28232011-02-01 21:47:00 -08001447 protected void determineScrollingStart(MotionEvent ev) {
1448 determineScrollingStart(ev, 1.0f);
1449 }
1450
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 /*
1452 * Determines if we should change the touch state to start scrolling after the
1453 * user moves their touch point too far.
1454 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001455 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001456 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001458 if (pointerIndex == -1) return;
1459
1460 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 final float x = ev.getX(pointerIndex);
1462 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001463 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1464
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 final int xDiff = (int) Math.abs(x - mLastMotionX);
1466 final int yDiff = (int) Math.abs(y - mLastMotionY);
1467
Adam Cohenf8d28232011-02-01 21:47:00 -08001468 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001469 boolean xPaged = xDiff > mPagingTouchSlop;
1470 boolean xMoved = xDiff > touchSlop;
1471 boolean yMoved = yDiff > touchSlop;
1472
Adam Cohenf8d28232011-02-01 21:47:00 -08001473 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001474 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 // Scroll if the user moved far enough along the X axis
1476 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001477 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001479 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001480 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001481 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1482 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001484 }
1485 }
1486
Adam Cohen7d30a372013-07-01 17:03:59 -07001487 protected float getMaxScrollProgress() {
1488 return 1.0f;
1489 }
1490
Adam Cohenf8d28232011-02-01 21:47:00 -08001491 protected void cancelCurrentPageLongPress() {
1492 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001493 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001494 // Try canceling the long press. It could also have been scheduled
1495 // by a distant descendant, so use the mAllowLongPress flag to block
1496 // everything
1497 final View currentPage = getPageAt(mCurrentPage);
1498 if (currentPage != null) {
1499 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 }
1501 }
1502 }
1503
Adam Cohen7d30a372013-07-01 17:03:59 -07001504 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1505 final int halfScreenSize = getViewportWidth() / 2;
1506
1507 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1508 screenCenter = Math.max(halfScreenSize, screenCenter);
1509
1510 return getScrollProgress(screenCenter, v, page);
1511 }
1512
Adam Cohenb5ba0972011-09-07 18:02:31 -07001513 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001514 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001515
Adam Cohenedb40762013-07-18 16:45:45 -07001516 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001517 int count = getChildCount();
1518
1519 final int totalDistance;
1520
1521 int adjacentPage = page + 1;
1522 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1523 adjacentPage = page - 1;
1524 }
1525
1526 if (adjacentPage < 0 || adjacentPage > count - 1) {
1527 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1528 } else {
1529 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1530 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001531
1532 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001533 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1534 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001535 return scrollProgress;
1536 }
1537
Adam Cohenedb40762013-07-18 16:45:45 -07001538 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001539 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001540 return 0;
1541 } else {
1542 return mPageScrolls[index];
1543 }
1544 }
1545
Adam Cohen564a2e72013-10-09 14:47:32 -07001546 // While layout transitions are occurring, a child's position may stray from its baseline
1547 // position. This method returns the magnitude of this stray at any given time.
1548 public int getLayoutTransitionOffsetForPage(int index) {
1549 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1550 return 0;
1551 } else {
1552 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001553
1554 int scrollOffset = 0;
1555 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1556 if (!lp.isFullScreenPage) {
1557 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1558 }
1559
Adam Cohen564a2e72013-10-09 14:47:32 -07001560 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1561 return (int) (child.getX() - baselineX);
1562 }
1563 }
1564
Adam Cohene0f66b52010-11-23 15:06:07 -08001565 // This curve determines how the effect of scrolling over the limits of the page dimishes
1566 // as the user pulls further and further from the bounds
1567 private float overScrollInfluenceCurve(float f) {
1568 f -= 1.0f;
1569 return f * f * f + 1.0f;
1570 }
1571
Adam Cohenb5ba0972011-09-07 18:02:31 -07001572 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001573 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001574
1575 // We want to reach the max over scroll effect when the user has
1576 // over scrolled half the size of the screen
1577 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1578
1579 if (f == 0) return;
1580
1581 // Clamp this factor, f, to -1 < f < 1
1582 if (Math.abs(f) >= 1) {
1583 f /= Math.abs(f);
1584 }
1585
1586 int overScrollAmount = (int) Math.round(f * screenSize);
1587 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001588 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001589 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001590 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001591 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001592 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001593 }
1594 invalidate();
1595 }
1596
1597 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001598 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001599
1600 float f = (amount / screenSize);
1601
1602 if (f == 0) return;
1603 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1604
Adam Cohen7bfc9792011-01-28 13:52:37 -08001605 // Clamp this factor, f, to -1 < f < 1
1606 if (Math.abs(f) >= 1) {
1607 f /= Math.abs(f);
1608 }
1609
Adam Cohene0f66b52010-11-23 15:06:07 -08001610 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001611 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001612 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001613 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001614 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001615 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001616 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001617 }
1618 invalidate();
1619 }
1620
Adam Cohenb5ba0972011-09-07 18:02:31 -07001621 protected void overScroll(float amount) {
1622 dampedOverScroll(amount);
1623 }
1624
Michael Jurkac5b262c2011-01-12 20:24:50 -08001625 protected float maxOverScroll() {
1626 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001627 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001628 float f = 1.0f;
1629 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1630 return OVERSCROLL_DAMP_FACTOR * f;
1631 }
1632
Adam Cohenf358a4b2013-07-23 16:47:31 -07001633 protected void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001634 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001635 }
1636
Adam Cohenf9618852013-11-08 06:45:03 -08001637 protected void disableFreeScroll() {
1638 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001639 }
1640
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001641 void updateFreescrollBounds() {
1642 getOverviewModePages(mTempVisiblePagesRange);
1643 if (isLayoutRtl()) {
1644 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1645 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1646 } else {
1647 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1648 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1649 }
1650 }
1651
Adam Cohenf9618852013-11-08 06:45:03 -08001652 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001653 mFreeScroll = freeScroll;
1654
Adam Cohenf9618852013-11-08 06:45:03 -08001655 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001656 updateFreescrollBounds();
1657 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001658 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1659 setCurrentPage(mTempVisiblePagesRange[0]);
1660 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1661 setCurrentPage(mTempVisiblePagesRange[1]);
1662 }
1663 }
1664
1665 setEnableOverscroll(!freeScroll);
1666 }
1667
1668 private void setEnableOverscroll(boolean enable) {
1669 mAllowOverScroll = enable;
1670 }
1671
1672 int getNearestHoverOverPageIndex() {
1673 if (mDragView != null) {
1674 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1675 + mDragView.getTranslationX());
1676 getOverviewModePages(mTempVisiblePagesRange);
1677 int minDistance = Integer.MAX_VALUE;
1678 int minIndex = indexOfChild(mDragView);
1679 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1680 View page = getPageAt(i);
1681 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1682 int d = Math.abs(dragX - pageX);
1683 if (d < minDistance) {
1684 minIndex = i;
1685 minDistance = d;
1686 }
1687 }
1688 return minIndex;
1689 }
1690 return -1;
1691 }
1692
Winson Chung321e9ee2010-08-09 13:37:56 -07001693 @Override
1694 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001695 if (DISABLE_TOUCH_INTERACTION) {
1696 return false;
1697 }
1698
Adam Cohen1697b792013-09-17 19:08:21 -07001699 super.onTouchEvent(ev);
1700
Winson Chung45e1d6e2010-11-09 17:19:49 -08001701 // Skip touch handling if there are no pages to swipe
1702 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1703
Michael Jurkab8f06722010-10-10 15:58:46 -07001704 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001705
1706 final int action = ev.getAction();
1707
1708 switch (action & MotionEvent.ACTION_MASK) {
1709 case MotionEvent.ACTION_DOWN:
1710 /*
1711 * If being flinged and user touches, stop the fling. isFinished
1712 * will be false if being flinged.
1713 */
1714 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001715 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001716 }
1717
1718 // Remember where the motion event started
1719 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001720 mDownMotionY = mLastMotionY = ev.getY();
1721 mDownScrollX = getScrollX();
1722 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1723 mParentDownMotionX = p[0];
1724 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001725 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001726 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001727 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001728
Michael Jurka0142d492010-08-25 17:46:15 -07001729 if (mTouchState == TOUCH_STATE_SCROLLING) {
1730 pageBeginMoving();
1731 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001732 break;
1733
1734 case MotionEvent.ACTION_MOVE:
1735 if (mTouchState == TOUCH_STATE_SCROLLING) {
1736 // Scroll to follow the motion event
1737 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001738
1739 if (pointerIndex == -1) return true;
1740
Winson Chung321e9ee2010-08-09 13:37:56 -07001741 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001742 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001743
Adam Cohenaefd4e12011-02-14 16:39:38 -08001744 mTotalMotionX += Math.abs(deltaX);
1745
Winson Chungc0844aa2011-02-02 15:25:58 -08001746 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1747 // keep the remainder because we are actually testing if we've moved from the last
1748 // scrolled position (which is discrete).
1749 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001750 mTouchX += deltaX;
1751 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1752 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001753 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001754 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001755 } else {
1756 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001757 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001758 mLastMotionX = x;
1759 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001760 } else {
1761 awakenScrollBars();
1762 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001763 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1764 // Update the last motion position
1765 mLastMotionX = ev.getX();
1766 mLastMotionY = ev.getY();
1767
1768 // Update the parent down so that our zoom animations take this new movement into
1769 // account
1770 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1771 mParentDownMotionX = pt[0];
1772 mParentDownMotionY = pt[1];
1773 updateDragViewTranslationDuringDrag();
1774
1775 // Find the closest page to the touch point
1776 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001777
1778 // Change the drag view if we are hovering over the drop target
1779 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1780 (int) mParentDownMotionX, (int) mParentDownMotionY);
1781 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1782
Adam Cohen7d30a372013-07-01 17:03:59 -07001783 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1784 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1785 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1786 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1787
Adam Cohenf358a4b2013-07-23 16:47:31 -07001788 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1789 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1790 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001791 mTempVisiblePagesRange[0] = 0;
1792 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001793 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001794 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1795 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1796 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1797 mSidePageHoverIndex = pageUnderPointIndex;
1798 mSidePageHoverRunnable = new Runnable() {
1799 @Override
1800 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001801 // Setup the scroll to the correct page before we swap the views
1802 snapToPage(pageUnderPointIndex);
1803
1804 // For each of the pages between the paged view and the drag view,
1805 // animate them from the previous position to the new position in
1806 // the layout (as a result of the drag view moving in the layout)
1807 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1808 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1809 dragViewIndex + 1 : pageUnderPointIndex;
1810 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1811 dragViewIndex - 1 : pageUnderPointIndex;
1812 for (int i = lowerIndex; i <= upperIndex; ++i) {
1813 View v = getChildAt(i);
1814 // dragViewIndex < pageUnderPointIndex, so after we remove the
1815 // drag view all subsequent views to pageUnderPointIndex will
1816 // shift down.
1817 int oldX = getViewportOffsetX() + getChildOffset(i);
1818 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1819
1820 // Animate the view translation from its old position to its new
1821 // position
1822 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1823 if (anim != null) {
1824 anim.cancel();
1825 }
1826
1827 v.setTranslationX(oldX - newX);
1828 anim = new AnimatorSet();
1829 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1830 anim.playTogether(
1831 ObjectAnimator.ofFloat(v, "translationX", 0f));
1832 anim.start();
1833 v.setTag(anim);
1834 }
1835
1836 removeView(mDragView);
1837 onRemoveView(mDragView, false);
1838 addView(mDragView, pageUnderPointIndex);
1839 onAddView(mDragView, pageUnderPointIndex);
1840 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001841 if (mPageIndicator != null) {
1842 mPageIndicator.setActiveMarker(getNextPage());
1843 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001844 }
1845 };
1846 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1847 }
1848 } else {
1849 removeCallbacks(mSidePageHoverRunnable);
1850 mSidePageHoverIndex = -1;
1851 }
Adam Cohen564976a2010-10-13 18:52:07 -07001852 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001853 determineScrollingStart(ev);
1854 }
1855 break;
1856
1857 case MotionEvent.ACTION_UP:
1858 if (mTouchState == TOUCH_STATE_SCROLLING) {
1859 final int activePointerId = mActivePointerId;
1860 final int pointerIndex = ev.findPointerIndex(activePointerId);
1861 final float x = ev.getX(pointerIndex);
1862 final VelocityTracker velocityTracker = mVelocityTracker;
1863 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1864 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001865 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001866 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001867 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1868 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001869
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001870 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1871
Adam Cohen00481b32011-11-18 12:03:48 -08001872 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001873 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001874
Adam Cohenf358a4b2013-07-23 16:47:31 -07001875 if (!mFreeScroll) {
1876 // In the case that the page is moved far to one direction and then is flung
1877 // in the opposite direction, we use a threshold to determine whether we should
1878 // just return to the starting page, or if we should skip one further.
1879 boolean returnToOriginalPage = false;
1880 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1881 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1882 returnToOriginalPage = true;
1883 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001884
Adam Cohenf358a4b2013-07-23 16:47:31 -07001885 int finalPage;
1886 // We give flings precedence over large moves, which is why we short-circuit our
1887 // test for a large move if a fling has been registered. That is, a large
1888 // move to the left and fling to the right will register as a fling to the right.
1889 final boolean isRtl = isLayoutRtl();
1890 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1891 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1892 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1893 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1894 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1895 snapToPageWithVelocity(finalPage, velocityX);
1896 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1897 (isFling && isVelocityXLeft)) &&
1898 mCurrentPage < getChildCount() - 1) {
1899 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1900 snapToPageWithVelocity(finalPage, velocityX);
1901 } else {
1902 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001903 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001904 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001905 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001906 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001907 }
1908
1909 float scaleX = getScaleX();
1910 int vX = (int) (-velocityX * scaleX);
1911 int initialScrollX = (int) (getScrollX() * scaleX);
1912
Adam Cohenf9618852013-11-08 06:45:03 -08001913 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001914 mScroller.fling(initialScrollX,
1915 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1916 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001917 }
Adam Cohencae7f572013-11-04 14:42:52 -08001918 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1919 // at this point we have not moved beyond the touch slop
1920 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1921 // we can just page
1922 int nextPage = Math.max(0, mCurrentPage - 1);
1923 if (nextPage != mCurrentPage) {
1924 snapToPage(nextPage);
1925 } else {
1926 snapToDestination();
1927 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001928 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001929 // at this point we have not moved beyond the touch slop
1930 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1931 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001932 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1933 if (nextPage != mCurrentPage) {
1934 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 } else {
1936 snapToDestination();
1937 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001938 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1939 // Update the last motion position
1940 mLastMotionX = ev.getX();
1941 mLastMotionY = ev.getY();
1942
1943 // Update the parent down so that our zoom animations take this new movement into
1944 // account
1945 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1946 mParentDownMotionX = pt[0];
1947 mParentDownMotionY = pt[1];
1948 updateDragViewTranslationDuringDrag();
1949 boolean handledFling = false;
1950 if (!DISABLE_FLING_TO_DELETE) {
1951 // Check the velocity and see if we are flinging-to-delete
1952 PointF flingToDeleteVector = isFlingingToDelete();
1953 if (flingToDeleteVector != null) {
1954 onFlingToDelete(flingToDeleteVector);
1955 handledFling = true;
1956 }
1957 }
1958 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1959 (int) mParentDownMotionY)) {
1960 onDropToDelete();
1961 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001962 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001963 if (!mCancelTap) {
1964 onUnhandledTap(ev);
1965 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001966 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001967
1968 // Remove the callback to wait for the side page hover timeout
1969 removeCallbacks(mSidePageHoverRunnable);
1970 // End any intermediate reordering states
1971 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001972 break;
1973
1974 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001975 if (mTouchState == TOUCH_STATE_SCROLLING) {
1976 snapToDestination();
1977 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001978 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001979 break;
1980
1981 case MotionEvent.ACTION_POINTER_UP:
1982 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001983 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001984 break;
1985 }
1986
1987 return true;
1988 }
1989
Adam Cohen7d30a372013-07-01 17:03:59 -07001990 public void onFlingToDelete(View v) {}
1991 public void onRemoveView(View v, boolean deletePermanently) {}
1992 public void onRemoveViewAnimationCompleted() {}
1993 public void onAddView(View v, int index) {}
1994
1995 private void resetTouchState() {
1996 releaseVelocityTracker();
1997 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001998 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001999 mTouchState = TOUCH_STATE_REST;
2000 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07002001 }
2002
Adam Cohen1697b792013-09-17 19:08:21 -07002003 protected void onUnhandledTap(MotionEvent ev) {
2004 ((Launcher) getContext()).onClick(this);
2005 }
Adam Cohen7d30a372013-07-01 17:03:59 -07002006
Winson Chung185d7162011-02-28 13:47:29 -08002007 @Override
2008 public boolean onGenericMotionEvent(MotionEvent event) {
2009 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
2010 switch (event.getAction()) {
2011 case MotionEvent.ACTION_SCROLL: {
2012 // Handle mouse (or ext. device) by shifting the page depending on the scroll
2013 final float vscroll;
2014 final float hscroll;
2015 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
2016 vscroll = 0;
2017 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2018 } else {
2019 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
2020 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
2021 }
2022 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07002023 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
2024 : (hscroll > 0 || vscroll > 0);
2025 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08002026 scrollRight();
2027 } else {
2028 scrollLeft();
2029 }
2030 return true;
2031 }
2032 }
2033 }
2034 }
2035 return super.onGenericMotionEvent(event);
2036 }
2037
Michael Jurkab8f06722010-10-10 15:58:46 -07002038 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2039 if (mVelocityTracker == null) {
2040 mVelocityTracker = VelocityTracker.obtain();
2041 }
2042 mVelocityTracker.addMovement(ev);
2043 }
2044
2045 private void releaseVelocityTracker() {
2046 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002047 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002048 mVelocityTracker.recycle();
2049 mVelocityTracker = null;
2050 }
2051 }
2052
Winson Chung321e9ee2010-08-09 13:37:56 -07002053 private void onSecondaryPointerUp(MotionEvent ev) {
2054 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2055 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2056 final int pointerId = ev.getPointerId(pointerIndex);
2057 if (pointerId == mActivePointerId) {
2058 // This was our active pointer going up. Choose a new
2059 // active pointer and adjust accordingly.
2060 // TODO: Make this decision more intelligent.
2061 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2062 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2063 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002064 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002065 mActivePointerId = ev.getPointerId(newPointerIndex);
2066 if (mVelocityTracker != null) {
2067 mVelocityTracker.clear();
2068 }
2069 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002070 }
2071
Winson Chung321e9ee2010-08-09 13:37:56 -07002072 @Override
2073 public void requestChildFocus(View child, View focused) {
2074 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002075 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002076 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002077 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002078 }
2079 }
2080
Winson Chung1908d072011-02-24 18:09:44 -08002081 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002082 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002083 }
2084
Adam Cohen7d30a372013-07-01 17:03:59 -07002085 int getPageNearestToPoint(float x) {
2086 int index = 0;
2087 for (int i = 0; i < getChildCount(); ++i) {
2088 if (x < getChildAt(i).getRight() - getScrollX()) {
2089 return index;
2090 } else {
2091 index++;
2092 }
2093 }
2094 return Math.min(index, getChildCount() - 1);
2095 }
2096
Adam Cohend19d3ca2010-09-15 14:43:42 -07002097 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002098 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002099 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002100 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002101 final int childCount = getChildCount();
2102 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002103 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002104 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002105 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002106 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002107 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2108 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2109 minDistanceFromScreenCenter = distanceFromScreenCenter;
2110 minDistanceFromScreenCenterIndex = i;
2111 }
2112 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002113 return minDistanceFromScreenCenterIndex;
2114 }
2115
2116 protected void snapToDestination() {
2117 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002118 }
2119
Adam Cohene0f66b52010-11-23 15:06:07 -08002120 private static class ScrollInterpolator implements Interpolator {
2121 public ScrollInterpolator() {
2122 }
2123
2124 public float getInterpolation(float t) {
2125 t -= 1.0f;
2126 return t*t*t*t*t + 1;
2127 }
2128 }
2129
2130 // We want the duration of the page snap animation to be influenced by the distance that
2131 // the screen has to travel, however, we don't want this duration to be effected in a
2132 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2133 // of travel has on the overall snap duration.
2134 float distanceInfluenceForSnapDuration(float f) {
2135 f -= 0.5f; // center the values about 0.
2136 f *= 0.3f * Math.PI / 2.0f;
2137 return (float) Math.sin(f);
2138 }
2139
Michael Jurka0142d492010-08-25 17:46:15 -07002140 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002141 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
Adam Cohen7d30a372013-07-01 17:03:59 -07002142 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002143
Adam Cohenedb40762013-07-18 16:45:45 -07002144 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002145 int delta = newX - mUnboundedScrollX;
2146 int duration = 0;
2147
Adam Cohen265b9a62011-12-07 14:37:18 -08002148 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002149 // If the velocity is low enough, then treat this more as an automatic page advance
2150 // as opposed to an apparent physical response to flinging
2151 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2152 return;
2153 }
2154
2155 // Here we compute a "distance" that will be used in the computation of the overall
2156 // snap duration. This is a function of the actual distance that needs to be traveled;
2157 // we keep this value close to half screen size in order to reduce the variance in snap
2158 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002159 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002160 float distance = halfScreenSize + halfScreenSize *
2161 distanceInfluenceForSnapDuration(distanceRatio);
2162
2163 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002164 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002165
2166 // we want the page's snap velocity to approximately match the velocity at which the
2167 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002168 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2169 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002170
2171 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002172 }
2173
2174 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002175 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002176 }
2177
Adam Cohen7d30a372013-07-01 17:03:59 -07002178 protected void snapToPageImmediately(int whichPage) {
Adam Cohenf9618852013-11-08 06:45:03 -08002179 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002180 }
2181
Michael Jurka0142d492010-08-25 17:46:15 -07002182 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002183 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002184 }
2185
Adam Cohenf9618852013-11-08 06:45:03 -08002186 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2187 snapToPage(whichPage, duration, false, interpolator);
2188 }
2189
2190 protected void snapToPage(int whichPage, int duration, boolean immediate,
2191 TimeInterpolator interpolator) {
Winson Chung86f77532010-08-24 11:08:22 -07002192 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07002193
Adam Cohenedb40762013-07-18 16:45:45 -07002194 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002195 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002196 final int delta = newX - sX;
Adam Cohenf9618852013-11-08 06:45:03 -08002197 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002198 }
2199
2200 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002201 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002202 }
Michael Jurka0142d492010-08-25 17:46:15 -07002203
Adam Cohenf9618852013-11-08 06:45:03 -08002204 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2205 TimeInterpolator interpolator) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002206 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002207 View focusedChild = getFocusedChild();
2208 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002209 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002210 focusedChild.clearFocus();
2211 }
2212
Adam Cohen53805212013-10-01 10:39:23 -07002213 sendScrollAccessibilityEvent();
2214
Michael Jurka0142d492010-08-25 17:46:15 -07002215 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002216 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002217 if (immediate) {
2218 duration = 0;
2219 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002220 duration = Math.abs(delta);
2221 }
2222
Adam Cohenf358a4b2013-07-23 16:47:31 -07002223 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002224 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002225 }
Adam Cohenf9618852013-11-08 06:45:03 -08002226
2227 if (interpolator != null) {
2228 mScroller.setInterpolator(interpolator);
2229 } else {
2230 mScroller.setInterpolator(mDefaultInterpolator);
2231 }
2232
Adam Cohen68d73932010-11-15 10:50:58 -08002233 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002234
Michael Jurka0142d492010-08-25 17:46:15 -07002235 notifyPageSwitchListener();
Adam Cohen7d30a372013-07-01 17:03:59 -07002236
2237 // Trigger a compute() to finish switching pages if necessary
2238 if (immediate) {
2239 computeScroll();
2240 }
2241
Winson Chung9c0565f2013-07-19 13:49:06 -07002242 // Defer loading associated pages until the scroll settles
2243 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2244
Adam Cohen7d30a372013-07-01 17:03:59 -07002245 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002246 invalidate();
2247 }
2248
Winson Chung321e9ee2010-08-09 13:37:56 -07002249 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002250 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002251 }
2252
2253 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002254 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002255 }
2256
Winson Chung86f77532010-08-24 11:08:22 -07002257 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002258 int result = -1;
2259 if (v != null) {
2260 ViewParent vp = v.getParent();
2261 int count = getChildCount();
2262 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002263 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002264 return i;
2265 }
2266 }
2267 }
2268 return result;
2269 }
2270
2271 /**
2272 * @return True is long presses are still allowed for the current touch
2273 */
2274 public boolean allowLongPress() {
2275 return mAllowLongPress;
2276 }
2277
Adam Cohendbdff6b2013-09-18 19:09:15 -07002278 @Override
2279 public boolean performLongClick() {
2280 mCancelTap = true;
2281 return super.performLongClick();
2282 }
2283
Michael Jurka0142d492010-08-25 17:46:15 -07002284 /**
2285 * Set true to allow long-press events to be triggered, usually checked by
2286 * {@link Launcher} to accept or block dpad-initiated long-presses.
2287 */
2288 public void setAllowLongPress(boolean allowLongPress) {
2289 mAllowLongPress = allowLongPress;
2290 }
2291
Winson Chung321e9ee2010-08-09 13:37:56 -07002292 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002293 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002294
2295 SavedState(Parcelable superState) {
2296 super(superState);
2297 }
2298
2299 private SavedState(Parcel in) {
2300 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002301 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002302 }
2303
2304 @Override
2305 public void writeToParcel(Parcel out, int flags) {
2306 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002307 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002308 }
2309
2310 public static final Parcelable.Creator<SavedState> CREATOR =
2311 new Parcelable.Creator<SavedState>() {
2312 public SavedState createFromParcel(Parcel in) {
2313 return new SavedState(in);
2314 }
2315
2316 public SavedState[] newArray(int size) {
2317 return new SavedState[size];
2318 }
2319 };
2320 }
2321
Winson Chungf314b0e2011-08-16 11:54:27 -07002322 protected void loadAssociatedPages(int page) {
2323 loadAssociatedPages(page, false);
2324 }
2325 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002326 if (mContentIsRefreshable) {
2327 final int count = getChildCount();
2328 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002329 int lowerPageBound = getAssociatedLowerPageBound(page);
2330 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002331 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2332 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002333 // First, clear any pages that should no longer be loaded
2334 for (int i = 0; i < count; ++i) {
2335 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002336 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002337 if (layout.getPageChildCount() > 0) {
2338 layout.removeAllViewsOnPage();
2339 }
2340 mDirtyPageContent.set(i, true);
2341 }
2342 }
2343 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002344 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002345 if ((i != page) && immediateAndOnly) {
2346 continue;
2347 }
Michael Jurka0142d492010-08-25 17:46:15 -07002348 if (lowerPageBound <= i && i <= upperPageBound) {
2349 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002350 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002351 mDirtyPageContent.set(i, false);
2352 }
Winson Chung86f77532010-08-24 11:08:22 -07002353 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002354 }
2355 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002356 }
2357 }
2358
Winson Chunge3193b92010-09-10 11:44:42 -07002359 protected int getAssociatedLowerPageBound(int page) {
2360 return Math.max(0, page - 1);
2361 }
2362 protected int getAssociatedUpperPageBound(int page) {
2363 final int count = getChildCount();
2364 return Math.min(page + 1, count - 1);
2365 }
2366
Winson Chung86f77532010-08-24 11:08:22 -07002367 /**
2368 * This method is called ONLY to synchronize the number of pages that the paged view has.
2369 * To actually fill the pages with information, implement syncPageItems() below. It is
2370 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2371 * and therefore, individual page items do not need to be updated in this method.
2372 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002373 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002374
2375 /**
2376 * This method is called to synchronize the items that are on a particular page. If views on
2377 * the page can be reused, then they should be updated within this method.
2378 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002379 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002380
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002381 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002382 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002383 }
2384 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002385 invalidatePageData(currentPage, false);
2386 }
2387 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 if (!mIsDataReady) {
2389 return;
2390 }
2391
Michael Jurka0142d492010-08-25 17:46:15 -07002392 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002393 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002394 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002395
Michael Jurka0142d492010-08-25 17:46:15 -07002396 // Update all the pages
2397 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002398
Winson Chung5a808352011-06-27 19:08:49 -07002399 // We must force a measure after we've loaded the pages to update the content width and
2400 // to determine the full scroll width
2401 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2402 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2403
2404 // Set a new page as the current page if necessary
2405 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002406 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002407 }
2408
Michael Jurka0142d492010-08-25 17:46:15 -07002409 // Mark each of the pages as dirty
2410 final int count = getChildCount();
2411 mDirtyPageContent.clear();
2412 for (int i = 0; i < count; ++i) {
2413 mDirtyPageContent.add(true);
2414 }
2415
2416 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002417 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002418 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002419 }
Adam Cohen06559042013-09-29 18:30:56 -07002420 if (isPageMoving()) {
2421 // If the page is moving, then snap it to the final position to ensure we don't get
2422 // stuck between pages
2423 snapToDestination();
2424 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002425 }
Winson Chung007c6982011-06-14 13:27:53 -07002426
Adam Cohen7d30a372013-07-01 17:03:59 -07002427 // Animate the drag view back to the original position
2428 void animateDragViewToOriginalPosition() {
2429 if (mDragView != null) {
2430 AnimatorSet anim = new AnimatorSet();
2431 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2432 anim.playTogether(
2433 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002434 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2435 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2436 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002437 anim.addListener(new AnimatorListenerAdapter() {
2438 @Override
2439 public void onAnimationEnd(Animator animation) {
2440 onPostReorderingAnimationCompleted();
2441 }
2442 });
2443 anim.start();
2444 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002445 }
2446
Adam Cohen7d30a372013-07-01 17:03:59 -07002447 protected void onStartReordering() {
2448 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2449 mTouchState = TOUCH_STATE_REORDERING;
2450 mIsReordering = true;
2451
Adam Cohen7d30a372013-07-01 17:03:59 -07002452 // We must invalidate to trigger a redraw to update the layers such that the drag view
2453 // is always drawn on top
2454 invalidate();
2455 }
2456
2457 private void onPostReorderingAnimationCompleted() {
2458 // Trigger the callback when reordering has settled
2459 --mPostReorderingPreZoomInRemainingAnimationCount;
2460 if (mPostReorderingPreZoomInRunnable != null &&
2461 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2462 mPostReorderingPreZoomInRunnable.run();
2463 mPostReorderingPreZoomInRunnable = null;
2464 }
2465 }
2466
2467 protected void onEndReordering() {
2468 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002469 }
2470
Adam Cohenf358a4b2013-07-23 16:47:31 -07002471 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002472 int dragViewIndex = indexOfChild(v);
2473
2474 if (mTouchState != TOUCH_STATE_REST) return false;
2475
Adam Cohen7d30a372013-07-01 17:03:59 -07002476 mTempVisiblePagesRange[0] = 0;
2477 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002478 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002479 mReorderingStarted = true;
2480
2481 // Check if we are within the reordering range
2482 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 dragViewIndex <= mTempVisiblePagesRange[1]) {
2484 // Find the drag view under the pointer
2485 mDragView = getChildAt(dragViewIndex);
2486 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2487 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002488 snapToPage(getPageNearestToCenterOfScreen());
2489 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002490 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002491 return true;
2492 }
2493 return false;
2494 }
2495
2496 boolean isReordering(boolean testTouchState) {
2497 boolean state = mIsReordering;
2498 if (testTouchState) {
2499 state &= (mTouchState == TOUCH_STATE_REORDERING);
2500 }
2501 return state;
2502 }
2503 void endReordering() {
2504 // For simplicity, we call endReordering sometimes even if reordering was never started.
2505 // In that case, we don't want to do anything.
2506 if (!mReorderingStarted) return;
2507 mReorderingStarted = false;
2508
2509 // If we haven't flung-to-delete the current child, then we just animate the drag view
2510 // back into position
2511 final Runnable onCompleteRunnable = new Runnable() {
2512 @Override
2513 public void run() {
2514 onEndReordering();
2515 }
2516 };
2517 if (!mDeferringForDelete) {
2518 mPostReorderingPreZoomInRunnable = new Runnable() {
2519 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002520 onCompleteRunnable.run();
2521 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002522 };
2523 };
2524
2525 mPostReorderingPreZoomInRemainingAnimationCount =
2526 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2527 // Snap to the current page
2528 snapToPage(indexOfChild(mDragView), 0);
2529 // Animate the drag view back to the front position
2530 animateDragViewToOriginalPosition();
2531 } else {
2532 // Handled in post-delete-animation-callbacks
2533 }
2534 }
2535
Adam Cohen7d30a372013-07-01 17:03:59 -07002536 /*
2537 * Flinging to delete - IN PROGRESS
2538 */
2539 private PointF isFlingingToDelete() {
2540 ViewConfiguration config = ViewConfiguration.get(getContext());
2541 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2542
2543 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2544 // Do a quick dot product test to ensure that we are flinging upwards
2545 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2546 mVelocityTracker.getYVelocity());
2547 PointF upVec = new PointF(0f, -1f);
2548 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2549 (vel.length() * upVec.length()));
2550 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2551 return vel;
2552 }
2553 }
2554 return null;
2555 }
2556
2557 /**
2558 * Creates an animation from the current drag view along its current velocity vector.
2559 * For this animation, the alpha runs for a fixed duration and we update the position
2560 * progressively.
2561 */
2562 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2563 private View mDragView;
2564 private PointF mVelocity;
2565 private Rect mFrom;
2566 private long mPrevTime;
2567 private float mFriction;
2568
2569 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2570
2571 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2572 long startTime, float friction) {
2573 mDragView = dragView;
2574 mVelocity = vel;
2575 mFrom = from;
2576 mPrevTime = startTime;
2577 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2578 }
2579
2580 @Override
2581 public void onAnimationUpdate(ValueAnimator animation) {
2582 float t = ((Float) animation.getAnimatedValue()).floatValue();
2583 long curTime = AnimationUtils.currentAnimationTimeMillis();
2584
2585 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2586 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2587
2588 mDragView.setTranslationX(mFrom.left);
2589 mDragView.setTranslationY(mFrom.top);
2590 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2591
2592 mVelocity.x *= mFriction;
2593 mVelocity.y *= mFriction;
2594 mPrevTime = curTime;
2595 }
2596 };
2597
2598 private static final int ANIM_TAG_KEY = 100;
2599
2600 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2601 return new Runnable() {
2602 @Override
2603 public void run() {
2604 int dragViewIndex = indexOfChild(dragView);
2605
2606 // For each of the pages around the drag view, animate them from the previous
2607 // position to the new position in the layout (as a result of the drag view moving
2608 // in the layout)
2609 // NOTE: We can make an assumption here because we have side-bound pages that we
2610 // will always have pages to animate in from the left
Adam Cohenf358a4b2013-07-23 16:47:31 -07002611 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002612 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2613 boolean slideFromLeft = (isLastWidgetPage ||
2614 dragViewIndex > mTempVisiblePagesRange[0]);
2615
2616 // Setup the scroll to the correct page before we swap the views
2617 if (slideFromLeft) {
2618 snapToPageImmediately(dragViewIndex - 1);
2619 }
2620
2621 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2622 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2623 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2624 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2625 ArrayList<Animator> animations = new ArrayList<Animator>();
2626 for (int i = lowerIndex; i <= upperIndex; ++i) {
2627 View v = getChildAt(i);
2628 // dragViewIndex < pageUnderPointIndex, so after we remove the
2629 // drag view all subsequent views to pageUnderPointIndex will
2630 // shift down.
2631 int oldX = 0;
2632 int newX = 0;
2633 if (slideFromLeft) {
2634 if (i == 0) {
2635 // Simulate the page being offscreen with the page spacing
2636 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002637 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002638 } else {
2639 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2640 }
2641 newX = getViewportOffsetX() + getChildOffset(i);
2642 } else {
2643 oldX = getChildOffset(i) - getChildOffset(i - 1);
2644 newX = 0;
2645 }
2646
2647 // Animate the view translation from its old position to its new
2648 // position
2649 AnimatorSet anim = (AnimatorSet) v.getTag();
2650 if (anim != null) {
2651 anim.cancel();
2652 }
2653
2654 // Note: Hacky, but we want to skip any optimizations to not draw completely
2655 // hidden views
2656 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2657 v.setTranslationX(oldX - newX);
2658 anim = new AnimatorSet();
2659 anim.playTogether(
2660 ObjectAnimator.ofFloat(v, "translationX", 0f),
2661 ObjectAnimator.ofFloat(v, "alpha", 1f));
2662 animations.add(anim);
2663 v.setTag(ANIM_TAG_KEY, anim);
2664 }
2665
2666 AnimatorSet slideAnimations = new AnimatorSet();
2667 slideAnimations.playTogether(animations);
2668 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2669 slideAnimations.addListener(new AnimatorListenerAdapter() {
2670 @Override
2671 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002672 mDeferringForDelete = false;
2673 onEndReordering();
2674 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002675 }
2676 });
2677 slideAnimations.start();
2678
2679 removeView(dragView);
2680 onRemoveView(dragView, true);
2681 }
2682 };
2683 }
2684
2685 public void onFlingToDelete(PointF vel) {
2686 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2687
2688 // NOTE: Because it takes time for the first frame of animation to actually be
2689 // called and we expect the animation to be a continuation of the fling, we have
2690 // to account for the time that has elapsed since the fling finished. And since
2691 // we don't have a startDelay, we will always get call to update when we call
2692 // start() (which we want to ignore).
2693 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2694 private int mCount = -1;
2695 private long mStartTime;
2696 private float mOffset;
2697 /* Anonymous inner class ctor */ {
2698 mStartTime = startTime;
2699 }
2700
2701 @Override
2702 public float getInterpolation(float t) {
2703 if (mCount < 0) {
2704 mCount++;
2705 } else if (mCount == 0) {
2706 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2707 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2708 mCount++;
2709 }
2710 return Math.min(1f, mOffset + t);
2711 }
2712 };
2713
2714 final Rect from = new Rect();
2715 final View dragView = mDragView;
2716 from.left = (int) dragView.getTranslationX();
2717 from.top = (int) dragView.getTranslationY();
2718 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2719 from, startTime, FLING_TO_DELETE_FRICTION);
2720
2721 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2722
2723 // Create and start the animation
2724 ValueAnimator mDropAnim = new ValueAnimator();
2725 mDropAnim.setInterpolator(tInterpolator);
2726 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2727 mDropAnim.setFloatValues(0f, 1f);
2728 mDropAnim.addUpdateListener(updateCb);
2729 mDropAnim.addListener(new AnimatorListenerAdapter() {
2730 public void onAnimationEnd(Animator animation) {
2731 onAnimationEndRunnable.run();
2732 }
2733 });
2734 mDropAnim.start();
2735 mDeferringForDelete = true;
2736 }
2737
2738 /* Drag to delete */
2739 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2740 if (mDeleteDropTarget != null) {
2741 mAltTmpRect.set(0, 0, 0, 0);
2742 View parent = (View) mDeleteDropTarget.getParent();
2743 if (parent != null) {
2744 parent.getGlobalVisibleRect(mAltTmpRect);
2745 }
2746 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2747 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2748 return mTmpRect.contains(x, y);
2749 }
2750 return false;
2751 }
2752
2753 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2754
2755 private void onDropToDelete() {
2756 final View dragView = mDragView;
2757
2758 final float toScale = 0f;
2759 final float toAlpha = 0f;
2760
2761 // Create and start the complex animation
2762 ArrayList<Animator> animations = new ArrayList<Animator>();
2763 AnimatorSet motionAnim = new AnimatorSet();
2764 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2765 motionAnim.playTogether(
2766 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2767 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2768 animations.add(motionAnim);
2769
2770 AnimatorSet alphaAnim = new AnimatorSet();
2771 alphaAnim.setInterpolator(new LinearInterpolator());
2772 alphaAnim.playTogether(
2773 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2774 animations.add(alphaAnim);
2775
2776 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2777
2778 AnimatorSet anim = new AnimatorSet();
2779 anim.playTogether(animations);
2780 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2781 anim.addListener(new AnimatorListenerAdapter() {
2782 public void onAnimationEnd(Animator animation) {
2783 onAnimationEndRunnable.run();
2784 }
2785 });
2786 anim.start();
2787
2788 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002789 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002790
2791 /* Accessibility */
2792 @Override
2793 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2794 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002795 info.setScrollable(getPageCount() > 1);
2796 if (getCurrentPage() < getPageCount() - 1) {
2797 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2798 }
2799 if (getCurrentPage() > 0) {
2800 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2801 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002802 }
2803
2804 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002805 public void sendAccessibilityEvent(int eventType) {
2806 // Don't let the view send real scroll events.
2807 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2808 super.sendAccessibilityEvent(eventType);
2809 }
2810 }
2811
2812 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002813 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2814 super.onInitializeAccessibilityEvent(event);
2815 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002816 }
2817
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002818 @Override
2819 public boolean performAccessibilityAction(int action, Bundle arguments) {
2820 if (super.performAccessibilityAction(action, arguments)) {
2821 return true;
2822 }
2823 switch (action) {
2824 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2825 if (getCurrentPage() < getPageCount() - 1) {
2826 scrollRight();
2827 return true;
2828 }
2829 } break;
2830 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2831 if (getCurrentPage() > 0) {
2832 scrollLeft();
2833 return true;
2834 }
2835 } break;
2836 }
2837 return false;
2838 }
2839
Adam Cohen0ffac432013-07-10 11:19:26 -07002840 protected String getCurrentPageDescription() {
2841 return String.format(getContext().getString(R.string.default_scroll_format),
2842 getNextPage() + 1, getChildCount());
2843 }
2844
Winson Chungd11265e2011-08-30 13:37:23 -07002845 @Override
2846 public boolean onHoverEvent(android.view.MotionEvent event) {
2847 return true;
2848 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002849}