blob: 121f22d3ce9a18ed3083d17ccc2d31f7ea16d0b7 [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;
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126 private VelocityTracker mVelocityTracker;
127
Adam Cohen7d30a372013-07-01 17:03:59 -0700128 private float mParentDownMotionX;
129 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700131 private float mDownMotionY;
132 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700133 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800134 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800135 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800137 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700138 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700139
Adam Cohendbdff6b2013-09-18 19:09:15 -0700140 private boolean mCancelTap;
141
Adam Cohenedb40762013-07-18 16:45:45 -0700142 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Michael Jurka0142d492010-08-25 17:46:15 -0700144 protected final static int TOUCH_STATE_REST = 0;
145 protected final static int TOUCH_STATE_SCROLLING = 1;
146 protected final static int TOUCH_STATE_PREV_PAGE = 2;
147 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700148 protected final static int TOUCH_STATE_REORDERING = 4;
149
Adam Cohene45440e2010-10-14 18:33:38 -0700150 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700153 protected boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800154 private boolean mScrollAbortedFromIntercept = false;
155
Winson Chung321e9ee2010-08-09 13:37:56 -0700156
Michael Jurka0142d492010-08-25 17:46:15 -0700157 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700158
Michael Jurka7426c422010-11-11 15:23:47 -0800159 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 private int mPagingTouchSlop;
161 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700162 protected int mPageLayoutPaddingTop;
163 protected int mPageLayoutPaddingBottom;
164 protected int mPageLayoutPaddingLeft;
165 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700166 protected int mPageLayoutWidthGap;
167 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700168 protected int mCellCountX = 0;
169 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700170 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800171 protected boolean mAllowOverScroll = true;
172 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800173 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700174 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700175
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800177 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
178 // the screens from continuing to translate beyond the normal bounds.
179 protected int mOverScrollX;
180
Michael Jurka5f1c5092010-09-03 14:15:02 -0700181 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
Michael Jurka5f1c5092010-09-03 14:15:02 -0700183 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
Winson Chung86f77532010-08-24 11:08:22 -0700185 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Michael Jurkae326f182011-11-21 14:05:46 -0800187 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700188
Michael Jurka0142d492010-08-25 17:46:15 -0700189 // If true, syncPages and syncPageItems will be called to refresh pages
190 protected boolean mContentIsRefreshable = true;
191
192 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700193 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700194
195 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
196 // to switch to a new page
197 protected boolean mUsePagingTouchSlop = true;
198
Michael Jurka8b805b12012-04-18 14:23:14 -0700199 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700200 // (SmoothPagedView does this)
201 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700202 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700203
Patrick Dubroy1262e362010-10-06 15:49:50 -0700204 protected boolean mIsPageMoving = false;
205
Winson Chungf0ea4d32011-06-06 14:27:16 -0700206 // All syncs and layout passes are deferred until data is ready.
207 protected boolean mIsDataReady = false;
208
Adam Cohen7d30a372013-07-01 17:03:59 -0700209 protected boolean mAllowLongPress = true;
210
Winson Chungd2be3812013-07-16 11:11:32 -0700211 // Page Indicator
212 private int mPageIndicatorViewId;
213 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700214 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700215
Adam Cohen7d30a372013-07-01 17:03:59 -0700216 // The viewport whether the pages are to be contained (the actual view may be larger than the
217 // viewport)
218 private Rect mViewport = new Rect();
219
220 // Reordering
221 // We use the min scale to determine how much to expand the actually PagedView measured
222 // dimensions such that when we are zoomed out, the view is not clipped
223 private int REORDERING_DROP_REPOSITION_DURATION = 200;
224 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
225 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700226 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700227 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700228 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700229 protected View mDragView;
230 protected AnimatorSet mZoomInOutAnim;
231 private Runnable mSidePageHoverRunnable;
232 private int mSidePageHoverIndex = -1;
233 // This variable's scope is only for the duration of startReordering() and endReordering()
234 private boolean mReorderingStarted = false;
235 // This variable's scope is for the duration of startReordering() and after the zoomIn()
236 // animation after endReordering()
237 private boolean mIsReordering;
238 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
239 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
240 private int mPostReorderingPreZoomInRemainingAnimationCount;
241 private Runnable mPostReorderingPreZoomInRunnable;
242
Adam Cohen7d30a372013-07-01 17:03:59 -0700243 // Convenience/caching
244 private Matrix mTmpInvMatrix = new Matrix();
245 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700246 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700247 private Rect mTmpRect = new Rect();
248 private Rect mAltTmpRect = new Rect();
249
250 // Fling to delete
251 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
252 private float FLING_TO_DELETE_FRICTION = 0.035f;
253 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
254 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
255 protected int mFlingToDeleteThresholdVelocity = -1400;
256 // Drag to delete
257 private boolean mDeferringForDelete = false;
258 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
259 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
260
261 // Drop to delete
262 private View mDeleteDropTarget;
263
Adam Cohen7d30a372013-07-01 17:03:59 -0700264 // Bouncer
265 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700266
John Spurlock77e1f472013-09-11 10:09:51 -0400267 protected final Rect mInsets = new Rect();
268
Winson Chung86f77532010-08-24 11:08:22 -0700269 public interface PageSwitchListener {
270 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 }
272
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 public PagedView(Context context) {
274 this(context, null);
275 }
276
277 public PagedView(Context context, AttributeSet attrs) {
278 this(context, attrs, 0);
279 }
280
281 public PagedView(Context context, AttributeSet attrs, int defStyle) {
282 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700283
Adam Cohen9c4949e2010-10-05 12:27:22 -0700284 TypedArray a = context.obtainStyledAttributes(attrs,
285 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700286
Adam Cohen9c4949e2010-10-05 12:27:22 -0700287 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800288 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700289 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800290 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700291 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800292 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700293 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800294 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700295 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700296 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700297 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700298 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700299 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700300 a.recycle();
301
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700303 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700304 }
305
306 /**
307 * Initializes various states for this workspace.
308 */
Michael Jurka0142d492010-08-25 17:46:15 -0700309 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700310 mDirtyPageContent = new ArrayList<Boolean>();
311 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800312 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700313 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700314 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700315
316 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700317 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
319 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700320 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800321
Adam Cohen7d30a372013-07-01 17:03:59 -0700322 // Scale the fling-to-delete threshold by the density
323 mFlingToDeleteThresholdVelocity =
324 (int) (mFlingToDeleteThresholdVelocity * mDensity);
325
Adam Cohen265b9a62011-12-07 14:37:18 -0800326 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
327 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
328 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 }
331
Winson Chungd2be3812013-07-16 11:11:32 -0700332 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700333 super.onAttachedToWindow();
334
Winson Chungd2be3812013-07-16 11:11:32 -0700335 // Hook up the page indicator
336 ViewGroup parent = (ViewGroup) getParent();
337 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
338 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700339 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700340
Winson Chung7819a562013-09-19 15:55:45 -0700341 ArrayList<PageIndicator.PageMarkerResources> markers =
342 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700343 for (int i = 0; i < getChildCount(); ++i) {
344 markers.add(getPageIndicatorMarker(i));
345 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700346
Winson Chungc58497e2013-09-03 17:48:37 -0700347 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700348
349 OnClickListener listener = getPageIndicatorClickListener();
350 if (listener != null) {
351 mPageIndicator.setOnClickListener(listener);
352 }
Adam Cohen53805212013-10-01 10:39:23 -0700353 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700354 }
355 }
356
Adam Cohen53805212013-10-01 10:39:23 -0700357 protected String getPageIndicatorDescription() {
358 return getCurrentPageDescription();
359 }
360
361 protected OnClickListener getPageIndicatorClickListener() {
362 return null;
363 }
364
Winson Chungd2be3812013-07-16 11:11:32 -0700365 protected void onDetachedFromWindow() {
366 // Unhook the page indicator
367 mPageIndicator = null;
368 }
369
Adam Cohen7d30a372013-07-01 17:03:59 -0700370 void setDeleteDropTarget(View v) {
371 mDeleteDropTarget = v;
372 }
373
374 // Convenience methods to map points from self to parent and vice versa
375 float[] mapPointFromViewToParent(View v, float x, float y) {
376 mTmpPoint[0] = x;
377 mTmpPoint[1] = y;
378 v.getMatrix().mapPoints(mTmpPoint);
379 mTmpPoint[0] += v.getLeft();
380 mTmpPoint[1] += v.getTop();
381 return mTmpPoint;
382 }
383 float[] mapPointFromParentToView(View v, float x, float y) {
384 mTmpPoint[0] = x - v.getLeft();
385 mTmpPoint[1] = y - v.getTop();
386 v.getMatrix().invert(mTmpInvMatrix);
387 mTmpInvMatrix.mapPoints(mTmpPoint);
388 return mTmpPoint;
389 }
390
391 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700392 if (mDragView != null) {
393 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
394 (mDragViewBaselineLeft - mDragView.getLeft());
395 float y = mLastMotionY - mDownMotionY;
396 mDragView.setTranslationX(x);
397 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700398
Adam Cohenf358a4b2013-07-23 16:47:31 -0700399 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
400 + x + ", " + y);
401 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700402 }
403
404 public void setMinScale(float f) {
405 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700406 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700407 requestLayout();
408 }
409
410 @Override
411 public void setScaleX(float scaleX) {
412 super.setScaleX(scaleX);
413 if (isReordering(true)) {
414 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
415 mLastMotionX = p[0];
416 mLastMotionY = p[1];
417 updateDragViewTranslationDuringDrag();
418 }
419 }
420
421 // Convenience methods to get the actual width/height of the PagedView (since it is measured
422 // to be larger to account for the minimum possible scale)
423 int getViewportWidth() {
424 return mViewport.width();
425 }
426 int getViewportHeight() {
427 return mViewport.height();
428 }
429
430 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
431 // PagedView both horizontally and vertically
432 int getViewportOffsetX() {
433 return (getMeasuredWidth() - getViewportWidth()) / 2;
434 }
435
436 int getViewportOffsetY() {
437 return (getMeasuredHeight() - getViewportHeight()) / 2;
438 }
439
Adam Cohenedb40762013-07-18 16:45:45 -0700440 PageIndicator getPageIndicator() {
441 return mPageIndicator;
442 }
Winson Chung7819a562013-09-19 15:55:45 -0700443 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
444 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700445 }
Adam Cohenedb40762013-07-18 16:45:45 -0700446
Winson Chung86f77532010-08-24 11:08:22 -0700447 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
448 mPageSwitchListener = pageSwitchListener;
449 if (mPageSwitchListener != null) {
450 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700451 }
452 }
453
454 /**
Winson Chung52aee602013-01-30 12:01:02 -0800455 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
456 */
457 public boolean isLayoutRtl() {
458 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
459 }
460
461 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700462 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
463 * out pages.
464 */
465 protected void setDataIsReady() {
466 mIsDataReady = true;
467 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700468
Winson Chungf0ea4d32011-06-06 14:27:16 -0700469 protected boolean isDataReady() {
470 return mIsDataReady;
471 }
472
473 /**
Winson Chung86f77532010-08-24 11:08:22 -0700474 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 *
Winson Chung86f77532010-08-24 11:08:22 -0700476 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 */
Winson Chung86f77532010-08-24 11:08:22 -0700478 int getCurrentPage() {
479 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700481
Winson Chung360e63f2012-04-27 13:48:05 -0700482 int getNextPage() {
483 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
484 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700485
Winson Chung86f77532010-08-24 11:08:22 -0700486 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 return getChildCount();
488 }
489
Winson Chung86f77532010-08-24 11:08:22 -0700490 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 return getChildAt(index);
492 }
493
Adam Cohenae4f1552011-10-20 00:15:42 -0700494 protected int indexToPage(int index) {
495 return index;
496 }
497
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800499 * Updates the scroll of the current page immediately to its final scroll position. We use this
500 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
501 * the previous tab page.
502 */
503 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700504 // If the current page is invalid, just reset the scroll position to zero
505 int newX = 0;
506 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700507 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700508 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800509 scrollTo(newX, 0);
510 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700511 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800512 }
513
514 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700515 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700516 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
517 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700518 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700519 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700520 mCurrentPage = getNextPage();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700521 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700522 }
523
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700524 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700525 mScroller.abortAnimation();
526 // We need to clean up the next page here to avoid computeScrollHelper from
527 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700528 if (resetNextPage) {
529 mNextPage = INVALID_PAGE;
530 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700531 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700532
533 private void forceFinishScroller() {
534 mScroller.forceFinished(true);
535 // We need to clean up the next page here to avoid computeScrollHelper from
536 // updating current page on the pass.
537 mNextPage = INVALID_PAGE;
538 }
539
Chet Haasebc2f0822012-10-26 17:59:53 -0700540 /**
Winson Chung86f77532010-08-24 11:08:22 -0700541 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700542 */
Winson Chung86f77532010-08-24 11:08:22 -0700543 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800544 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700545 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800546 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800547 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
548 // the default
549 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800550 return;
551 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700552 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700553 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chung181c3dc2013-07-17 15:36:20 -0700554 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700555 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800556 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
558
Winson Chung8c87cd82013-07-23 16:20:10 -0700559 /**
560 * The restore page will be set in place of the current page at the next (likely first)
561 * layout.
562 */
563 void setRestorePage(int restorePage) {
564 mRestorePage = restorePage;
565 }
566
Michael Jurka0142d492010-08-25 17:46:15 -0700567 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700568 if (mPageSwitchListener != null) {
569 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
Winson Chungd2be3812013-07-16 11:11:32 -0700571
572 // Update the page indicator (when we aren't reordering)
573 if (mPageIndicator != null && !isReordering(false)) {
574 mPageIndicator.setActiveMarker(getNextPage());
575 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700576 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800577 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700578 if (!mIsPageMoving) {
579 mIsPageMoving = true;
580 onPageBeginMoving();
581 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700582 }
583
Michael Jurkace7e05f2011-02-01 22:02:35 -0800584 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700585 if (mIsPageMoving) {
586 mIsPageMoving = false;
587 onPageEndMoving();
588 }
Michael Jurka0142d492010-08-25 17:46:15 -0700589 }
590
Adam Cohen26976d92011-03-22 15:33:33 -0700591 protected boolean isPageMoving() {
592 return mIsPageMoving;
593 }
594
Michael Jurka0142d492010-08-25 17:46:15 -0700595 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700596 protected void onPageBeginMoving() {
597 }
598
599 // a method that subclasses can override to add behavior
600 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700601 }
602
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 /**
Winson Chung86f77532010-08-24 11:08:22 -0700604 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 *
606 * @param l The listener used to respond to long clicks.
607 */
608 @Override
609 public void setOnLongClickListener(OnLongClickListener l) {
610 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700611 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700612 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700613 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700614 }
Adam Cohen1697b792013-09-17 19:08:21 -0700615 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 }
617
618 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800619 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800621 }
622
623 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700624 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700625 // In free scroll mode, we clamp the scrollX
626 if (mFreeScroll) {
627 x = Math.min(x, mFreeScrollMaxScrollX);
628 x = Math.max(x, mFreeScrollMinScrollX);
629 }
630
Adam Cohen0ffac432013-07-10 11:19:26 -0700631 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800632 mUnboundedScrollX = x;
633
Adam Cohen0ffac432013-07-10 11:19:26 -0700634 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
635 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
636 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800637 super.scrollTo(0, y);
638 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700639 if (isRtl) {
640 overScroll(x - mMaxScrollX);
641 } else {
642 overScroll(x);
643 }
Adam Cohen68d73932010-11-15 10:50:58 -0800644 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700645 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800646 super.scrollTo(mMaxScrollX, y);
647 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700648 if (isRtl) {
649 overScroll(x);
650 } else {
651 overScroll(x - mMaxScrollX);
652 }
Adam Cohen68d73932010-11-15 10:50:58 -0800653 }
654 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800655 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800656 super.scrollTo(x, y);
657 }
658
Michael Jurka0142d492010-08-25 17:46:15 -0700659 mTouchX = x;
660 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700661
662 // Update the last motion events when scrolling
663 if (isReordering(true)) {
664 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
665 mLastMotionX = p[0];
666 mLastMotionY = p[1];
667 updateDragViewTranslationDuringDrag();
668 }
Michael Jurka0142d492010-08-25 17:46:15 -0700669 }
670
Adam Cohen53805212013-10-01 10:39:23 -0700671 private void sendScrollAccessibilityEvent() {
672 AccessibilityManager am =
673 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
674 if (am.isEnabled()) {
675 AccessibilityEvent ev =
676 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700677 ev.setItemCount(getChildCount());
678 ev.setFromIndex(mCurrentPage);
Adam Cohen53805212013-10-01 10:39:23 -0700679
Alan Viverette254139a2013-10-08 13:13:46 -0700680 final int action;
Adam Cohen53805212013-10-01 10:39:23 -0700681 if (getNextPage() >= mCurrentPage) {
682 action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
683 } else {
684 action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
685 }
686
687 ev.setAction(action);
688 sendAccessibilityEventUnchecked(ev);
689 }
690 }
691
Michael Jurka0142d492010-08-25 17:46:15 -0700692 // we moved this functionality to a helper function so SmoothPagedView can reuse it
693 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700695 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700696 if (getScrollX() != mScroller.getCurrX()
697 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700698 || mOverScrollX != mScroller.getCurrX()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700699 float scaleX = mFreeScroll ? getScaleX() : 1f;
700 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
701 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700702 }
Michael Jurka0142d492010-08-25 17:46:15 -0700703 invalidate();
704 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700705 } else if (mNextPage != INVALID_PAGE) {
Adam Cohen53805212013-10-01 10:39:23 -0700706 sendScrollAccessibilityEvent();
707
Winson Chung86f77532010-08-24 11:08:22 -0700708 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700709 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700710 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700711
Winson Chung9c0565f2013-07-19 13:49:06 -0700712 // Load the associated pages if necessary
713 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
714 loadAssociatedPages(mCurrentPage);
715 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
716 }
717
Adam Cohen73aa9752010-11-24 16:26:58 -0800718 // We don't want to trigger a page end moving unless the page has settled
719 // and the user has stopped scrolling
720 if (mTouchState == TOUCH_STATE_REST) {
721 pageEndMoving();
722 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700723
Adam Cohen7d30a372013-07-01 17:03:59 -0700724 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700725 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700726 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700727 if (am.isEnabled()) {
728 // Notify the user when the page changes
729 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700730 }
Michael Jurka0142d492010-08-25 17:46:15 -0700731 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 }
Michael Jurka0142d492010-08-25 17:46:15 -0700733 return false;
734 }
735
736 @Override
737 public void computeScroll() {
738 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700739 }
740
Adam Cohen7d30a372013-07-01 17:03:59 -0700741 protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) {
742 return mTopAlignPageWhenShrinkingForBouncer;
743 }
744
Adam Cohen96d30a12013-07-16 18:13:21 -0700745 public static class LayoutParams extends ViewGroup.LayoutParams {
746 public boolean isFullScreenPage = false;
747
748 /**
749 * {@inheritDoc}
750 */
751 public LayoutParams(int width, int height) {
752 super(width, height);
753 }
754
755 public LayoutParams(ViewGroup.LayoutParams source) {
756 super(source);
757 }
758 }
759
760 protected LayoutParams generateDefaultLayoutParams() {
761 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
762 }
763
Adam Cohenedb40762013-07-18 16:45:45 -0700764 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700765 LayoutParams lp = generateDefaultLayoutParams();
766 lp.isFullScreenPage = true;
767 super.addView(page, 0, lp);
768 }
769
Adam Cohen410f3cd2013-09-22 12:09:32 -0700770 public int getNormalChildHeight() {
771 return mNormalChildHeight;
772 }
773
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 @Override
775 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700777 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
778 return;
779 }
780
Adam Cohen7d30a372013-07-01 17:03:59 -0700781 // We measure the dimensions of the PagedView to be larger than the pages so that when we
782 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700783 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
784 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
785 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700786 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Adam Cohen7d30a372013-07-01 17:03:59 -0700787 // NOTE: We multiply by 1.5f to account for the fact that depending on the offset of the
788 // viewport, we can be at most one and a half screens offset once we scale down
789 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohendbdff6b2013-09-18 19:09:15 -0700790 int maxSize = Math.max(dm.widthPixels, dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700791
Winson Chungc58497e2013-09-03 17:48:37 -0700792 int parentWidthSize, parentHeightSize;
793 int scaledWidthSize, scaledHeightSize;
794 if (mUseMinScale) {
795 parentWidthSize = (int) (1.5f * maxSize);
796 parentHeightSize = maxSize;
797 scaledWidthSize = (int) (parentWidthSize / mMinScale);
798 scaledHeightSize = (int) (parentHeightSize / mMinScale);
799 } else {
800 scaledWidthSize = widthSize;
801 scaledHeightSize = heightSize;
802 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700803 mViewport.set(0, 0, widthSize, heightSize);
804
805 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
806 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
807 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 }
809
Winson Chung8aad6102012-05-11 16:27:49 -0700810 // Return early if we aren't given a proper dimension
811 if (widthSize <= 0 || heightSize <= 0) {
812 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
813 return;
814 }
815
Adam Lesinski6b879f02010-11-04 16:15:23 -0700816 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
817 * of the All apps view on XLarge displays to not take up more space then it needs. Width
818 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
819 * each page to have the same width.
820 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700821 final int verticalPadding = getPaddingTop() + getPaddingBottom();
822 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700823
824 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700825 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700826 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700827 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
828 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
829 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
830 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 final int childCount = getChildCount();
832 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700833 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700834 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100835 if (child.getVisibility() != GONE) {
836 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700837
Vladimir Marko2824b072013-10-04 16:42:17 +0100838 int childWidthMode;
839 int childHeightMode;
840 int childWidth;
841 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700842
Vladimir Marko2824b072013-10-04 16:42:17 +0100843 if (!lp.isFullScreenPage) {
844 if (lp.width == LayoutParams.WRAP_CONTENT) {
845 childWidthMode = MeasureSpec.AT_MOST;
846 } else {
847 childWidthMode = MeasureSpec.EXACTLY;
848 }
849
850 if (lp.height == LayoutParams.WRAP_CONTENT) {
851 childHeightMode = MeasureSpec.AT_MOST;
852 } else {
853 childHeightMode = MeasureSpec.EXACTLY;
854 }
855
856 childWidth = widthSize - horizontalPadding;
857 childHeight = heightSize - verticalPadding - mInsets.top - mInsets.bottom;
858 mNormalChildHeight = childHeight;
859
Adam Cohen96d30a12013-07-16 18:13:21 -0700860 } else {
861 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700862 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100863
864 if (mUseMinScale) {
865 childWidth = getViewportWidth();
866 childHeight = getViewportHeight();
867 } else {
868 childWidth = widthSize - getPaddingLeft() - getPaddingRight();
869 childHeight = heightSize - getPaddingTop() - getPaddingBottom();
870 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700871 }
872
Vladimir Marko2824b072013-10-04 16:42:17 +0100873 final int childWidthMeasureSpec =
874 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
875 final int childHeightMeasureSpec =
876 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
877 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700879 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700880 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800881 }
882
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700884 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700885 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700886 return;
887 }
888
Winson Chung785d2eb2011-04-14 16:08:02 -0700889 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700891
Adam Cohenedb40762013-07-18 16:45:45 -0700892 int screenWidth = getViewportWidth();
893
Adam Cohen7d30a372013-07-01 17:03:59 -0700894 int offsetX = getViewportOffsetX();
895 int offsetY = getViewportOffsetY();
896
897 // Update the viewport offsets
898 mViewport.offset(offsetX, offsetY);
899
Adam Cohen0ffac432013-07-10 11:19:26 -0700900 final boolean isRtl = isLayoutRtl();
901
902 final int startIndex = isRtl ? childCount - 1 : 0;
903 final int endIndex = isRtl ? -1 : childCount;
904 final int delta = isRtl ? -1 : 1;
905
Adam Cohen7d30a372013-07-01 17:03:59 -0700906 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700907
Michael Jurka8fd3adc2013-10-16 13:50:24 -0700908 int childLeft = offsetX + (screenWidth - getChildWidth(startIndex)) / 2;
Adam Cohenedb40762013-07-18 16:45:45 -0700909 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
910 mPageScrolls = new int[getChildCount()];
911 }
912
Adam Cohen0ffac432013-07-10 11:19:26 -0700913 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700914 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 if (child.getVisibility() != View.GONE) {
Vladimir Marko2824b072013-10-04 16:42:17 +0100916 LayoutParams lp = (LayoutParams) child.getLayoutParams();
917 int childTop;
918 if (lp.isFullScreenPage) {
919 childTop = offsetY;
920 } else {
921 childTop = offsetY + getPaddingTop() + mInsets.top;
922 if (mCenterPagesVertically) {
923 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
924 }
925 }
926
Adam Cohen96d30a12013-07-16 18:13:21 -0700927 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700928 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800929
Winson Chung785d2eb2011-04-14 16:08:02 -0700930 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700931 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800932 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700933
934 // We assume the left and right padding are equal, and hence center the pages
935 // horizontally
Adam Cohen6ad0e7d2013-07-24 13:55:41 -0700936 int scrollOffset = (getViewportWidth() - childWidth) / 2;
Adam Cohenedb40762013-07-18 16:45:45 -0700937 mPageScrolls[i] = childLeft - scrollOffset - offsetX;
938
Adam Cohen6ad0e7d2013-07-24 13:55:41 -0700939 if (i != endIndex - delta) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -0700940 childLeft += childWidth + scrollOffset;
Adam Cohen6ad0e7d2013-07-24 13:55:41 -0700941 int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
Adam Cohenaf9b0e52013-09-23 19:27:38 -0700942 childLeft += nextScrollOffset;
Adam Cohen6ad0e7d2013-07-24 13:55:41 -0700943 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 }
945 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700946
947 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
948 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800949 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700950 setHorizontalScrollBarEnabled(true);
951 mFirstLayout = false;
952 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700953
954 if (childCount > 0) {
955 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700956 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700957 } else {
958 mMaxScrollX = 0;
959 }
960
961 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
962 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700963 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700964 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700965 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700966 } else {
967 setCurrentPage(getNextPage());
968 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700969 }
970 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -0700971
972 if (isReordering(true)) {
973 updateDragViewTranslationDuringDrag();
974 }
Winson Chunge3193b92010-09-10 11:44:42 -0700975 }
976
Adam Cohenf34bab52010-09-30 14:11:56 -0700977 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700978 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
979
980 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700981 for (int i = 0; i < getChildCount(); i++) {
982 View child = getChildAt(i);
983 if (child != null) {
984 float scrollProgress = getScrollProgress(screenCenter, child, i);
985 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800986 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700987 }
988 }
989 invalidate();
990 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700991 }
992
Winson Chungc58497e2013-09-03 17:48:37 -0700993 protected void enablePagedViewAnimations() {
994 mAllowPagedViewAnimations = true;
995
996 }
997 protected void disablePagedViewAnimations() {
998 mAllowPagedViewAnimations = false;
999 }
1000
Winson Chunge3193b92010-09-10 11:44:42 -07001001 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -07001002 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -07001003 // Update the page indicator, we don't update the page indicator as we
1004 // add/remove pages
1005 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001006 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001007 mPageIndicator.addMarker(pageIndex,
1008 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001009 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001010 }
1011
Adam Cohen2591f6a2011-10-25 14:36:40 -07001012 // This ensures that when children are added, they get the correct transforms / alphas
1013 // in accordance with any scroll effects.
1014 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001015 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001016 invalidate();
1017 }
1018
Michael Jurka8b805b12012-04-18 14:23:14 -07001019 @Override
1020 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001021 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001022 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001023 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001024 }
1025
Winson Chungd2be3812013-07-16 11:11:32 -07001026 private void removeMarkerForView(int index) {
1027 // Update the page indicator, we don't update the page indicator as we
1028 // add/remove pages
1029 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001030 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001031 }
1032 }
1033
1034 @Override
1035 public void removeView(View v) {
1036 // XXX: We should find a better way to hook into this before the view
1037 // gets removed form its parent...
1038 removeMarkerForView(indexOfChild(v));
1039 super.removeView(v);
1040 }
1041 @Override
1042 public void removeViewInLayout(View v) {
1043 // XXX: We should find a better way to hook into this before the view
1044 // gets removed form its parent...
1045 removeMarkerForView(indexOfChild(v));
1046 super.removeViewInLayout(v);
1047 }
1048 @Override
1049 public void removeViewAt(int index) {
1050 // XXX: We should find a better way to hook into this before the view
1051 // gets removed form its parent...
1052 removeViewAt(index);
1053 super.removeViewAt(index);
1054 }
1055 @Override
1056 public void removeAllViewsInLayout() {
1057 // Update the page indicator, we don't update the page indicator as we
1058 // add/remove pages
1059 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001060 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001061 }
1062
1063 super.removeAllViewsInLayout();
1064 }
1065
Adam Cohen73894962011-10-31 13:17:17 -07001066 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001067 if (index < 0 || index > getChildCount() - 1) return 0;
1068
Adam Cohenf698c6e2013-07-17 18:44:25 -07001069 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001070
Adam Cohenf698c6e2013-07-17 18:44:25 -07001071 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001072 }
1073
Adam Cohenf358a4b2013-07-23 16:47:31 -07001074 protected void getOverviewModePages(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001075 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001076 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001077 }
1078
Michael Jurkadde558b2011-11-09 22:09:06 -08001079 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001080 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001081 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001082
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001083 range[0] = -1;
1084 range[1] = -1;
1085
Michael Jurkac4fb9172010-09-02 17:19:20 -07001086 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001087 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001088 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001089
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001090 int count = getChildCount();
1091 for (int i = 0; i < count; i++) {
1092 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001093
Winson Chungc9ca2982013-07-19 12:07:38 -07001094 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001095 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001096 if (mTmpIntPoint[0] > viewportWidth) {
1097 if (range[0] == -1) {
1098 continue;
1099 } else {
1100 break;
1101 }
1102 }
1103
Adam Cohen6a678da2013-09-24 10:58:01 -07001104 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001105 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1106 if (mTmpIntPoint[0] < 0) {
1107 if (range[0] == -1) {
1108 continue;
1109 } else {
1110 break;
1111 }
1112 }
1113 curScreen = i;
1114 if (range[0] < 0) {
1115 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001116 }
1117 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001118
1119 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001120 } else {
1121 range[0] = -1;
1122 range[1] = -1;
1123 }
1124 }
1125
Michael Jurka920d7f42012-05-14 16:29:55 -07001126 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001127 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001128 }
1129
Michael Jurkadde558b2011-11-09 22:09:06 -08001130 @Override
1131 protected void dispatchDraw(Canvas canvas) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001132 int halfScreenSize = getViewportWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -07001133 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1134 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -08001135 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -08001136
1137 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -07001138 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1139 // set it for the next frame
1140 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -08001141 screenScrolled(screenCenter);
1142 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -08001143 }
1144
1145 // Find out which screens are visible; as an optimization we only call draw on them
1146 final int pageCount = getChildCount();
1147 if (pageCount > 0) {
1148 getVisiblePages(mTempVisiblePagesRange);
1149 final int leftScreen = mTempVisiblePagesRange[0];
1150 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001151 if (leftScreen != -1 && rightScreen != -1) {
1152 final long drawingTime = getDrawingTime();
1153 // Clip to the bounds
1154 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001155 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1156 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001157
Adam Cohen7d30a372013-07-01 17:03:59 -07001158 // Draw all the children, leaving the drag view for last
1159 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001160 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001161 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001162 if (mForceDrawAllChildrenNextFrame ||
1163 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001164 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001165 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001166 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001167 // Draw the drag view on top (if there is one)
1168 if (mDragView != null) {
1169 drawChild(canvas, mDragView, drawingTime);
1170 }
1171
Michael Jurka5e368ff2012-05-14 23:13:15 -07001172 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001173 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001174 }
Michael Jurka0142d492010-08-25 17:46:15 -07001175 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 }
1177
1178 @Override
1179 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001180 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001181 if (page != mCurrentPage || !mScroller.isFinished()) {
1182 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 return true;
1184 }
1185 return false;
1186 }
1187
1188 @Override
1189 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001190 int focusablePage;
1191 if (mNextPage != INVALID_PAGE) {
1192 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001194 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 }
Winson Chung86f77532010-08-24 11:08:22 -07001196 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001198 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 }
1200 return false;
1201 }
1202
1203 @Override
1204 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001205 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001207 if (getCurrentPage() > 0) {
1208 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001209 return true;
1210 }
1211 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001212 if (getCurrentPage() < getPageCount() - 1) {
1213 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 return true;
1215 }
1216 }
1217 return super.dispatchUnhandledMove(focused, direction);
1218 }
1219
1220 @Override
1221 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001222 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001223 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001224 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 }
1226 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001227 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001228 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 }
1230 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001231 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001232 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 }
1234 }
1235 }
1236
1237 /**
1238 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001239 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 *
Winson Chung86f77532010-08-24 11:08:22 -07001241 * This happens when live folders requery, and if they're off page, they
1242 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 */
1244 @Override
1245 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001246 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 View v = focused;
1248 while (true) {
1249 if (v == current) {
1250 super.focusableViewAvailable(focused);
1251 return;
1252 }
1253 if (v == this) {
1254 return;
1255 }
1256 ViewParent parent = v.getParent();
1257 if (parent instanceof View) {
1258 v = (View)v.getParent();
1259 } else {
1260 return;
1261 }
1262 }
1263 }
1264
1265 /**
1266 * {@inheritDoc}
1267 */
1268 @Override
1269 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1270 if (disallowIntercept) {
1271 // We need to make sure to cancel our long press if
1272 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001273 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001274 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 }
1276 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1277 }
1278
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001279 /**
1280 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1281 */
1282 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohenedb40762013-07-18 16:45:45 -07001283 int offset = (getViewportWidth() - getChildWidth(mCurrentPage)) / 2;
Adam Cohen0ffac432013-07-10 11:19:26 -07001284 if (isLayoutRtl()) {
1285 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen0cd0eba2013-10-28 14:22:25 -07001286 offset));
Adam Cohen0ffac432013-07-10 11:19:26 -07001287 }
Adam Cohen0cd0eba2013-10-28 14:22:25 -07001288 return (x < getViewportOffsetX() + offset);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001289 }
1290
1291 /**
1292 * Return true if a tap at (x, y) should trigger a flip to the next page.
1293 */
1294 protected boolean hitsNextPage(float x, float y) {
Adam Cohenedb40762013-07-18 16:45:45 -07001295 int offset = (getViewportWidth() - getChildWidth(mCurrentPage)) / 2;
Adam Cohen0ffac432013-07-10 11:19:26 -07001296 if (isLayoutRtl()) {
Adam Cohen0cd0eba2013-10-28 14:22:25 -07001297 return (x < getViewportOffsetX() + offset);
Adam Cohen0ffac432013-07-10 11:19:26 -07001298 }
1299 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen0cd0eba2013-10-28 14:22:25 -07001300 offset));
Adam Cohen7d30a372013-07-01 17:03:59 -07001301 }
Winson Chung52aee602013-01-30 12:01:02 -08001302
Adam Cohen7d30a372013-07-01 17:03:59 -07001303 /** Returns whether x and y originated within the buffered viewport */
1304 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1305 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1306 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1307 return mTmpRect.contains(x, y);
1308 }
1309
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 @Override
1311 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001312 if (DISABLE_TOUCH_INTERACTION) {
1313 return false;
1314 }
1315
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 /*
1317 * This method JUST determines whether we want to intercept the motion.
1318 * If we return true, onTouchEvent will be called and we do the actual
1319 * scrolling there.
1320 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001321 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001322
Winson Chung45e1d6e2010-11-09 17:19:49 -08001323 // Skip touch handling if there are no pages to swipe
1324 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1325
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 /*
1327 * Shortcut the most recurring case: the user is in the dragging
1328 * state and he is moving his finger. We want to intercept this
1329 * motion.
1330 */
1331 final int action = ev.getAction();
1332 if ((action == MotionEvent.ACTION_MOVE) &&
1333 (mTouchState == TOUCH_STATE_SCROLLING)) {
1334 return true;
1335 }
1336
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 switch (action & MotionEvent.ACTION_MASK) {
1338 case MotionEvent.ACTION_MOVE: {
1339 /*
1340 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1341 * whether the user has moved far enough from his original down touch.
1342 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001343 if (mActivePointerId != INVALID_POINTER) {
1344 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001345 }
1346 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1347 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1348 // i.e. fall through to the next case (don't break)
1349 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1350 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001351 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 }
1353
1354 case MotionEvent.ACTION_DOWN: {
1355 final float x = ev.getX();
1356 final float y = ev.getY();
1357 // Remember location of down touch
1358 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001359 mDownMotionY = y;
1360 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 mLastMotionX = x;
1362 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001363 float[] p = mapPointFromViewToParent(this, x, y);
1364 mParentDownMotionX = p[0];
1365 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001366 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001367 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001368 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001369
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 /*
1371 * If being flinged and user touches the screen, initiate drag;
1372 * otherwise don't. mScroller.isFinished should be false when
1373 * being flinged.
1374 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001375 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001376 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
1377 if (finishedScrolling) {
1378 mTouchState = TOUCH_STATE_REST;
Adam Cohencae7f572013-11-04 14:42:52 -08001379 mScrollAbortedFromIntercept = true;
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001380 abortScrollerAnimation(false);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001381 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001382 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1383 mTouchState = TOUCH_STATE_SCROLLING;
1384 } else {
1385 mTouchState = TOUCH_STATE_REST;
1386 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001387 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001388
Winson Chung86f77532010-08-24 11:08:22 -07001389 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001391 if (!DISABLE_TOUCH_SIDE_PAGES) {
1392 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1393 if (getChildCount() > 0) {
1394 if (hitsPreviousPage(x, y)) {
1395 mTouchState = TOUCH_STATE_PREV_PAGE;
1396 } else if (hitsNextPage(x, y)) {
1397 mTouchState = TOUCH_STATE_NEXT_PAGE;
1398 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 }
1400 }
1401 }
1402 break;
1403 }
1404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001406 case MotionEvent.ACTION_CANCEL:
Adam Cohencae7f572013-11-04 14:42:52 -08001407 if (mScrollAbortedFromIntercept) {
1408 snapToDestination();
1409 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001410 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 break;
1412
1413 case MotionEvent.ACTION_POINTER_UP:
1414 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001415 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 break;
1417 }
1418
1419 /*
1420 * The only time we want to intercept motion events is if we are in the
1421 * drag mode.
1422 */
1423 return mTouchState != TOUCH_STATE_REST;
1424 }
1425
Adam Cohenf8d28232011-02-01 21:47:00 -08001426 protected void determineScrollingStart(MotionEvent ev) {
1427 determineScrollingStart(ev, 1.0f);
1428 }
1429
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 /*
1431 * Determines if we should change the touch state to start scrolling after the
1432 * user moves their touch point too far.
1433 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001434 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001435 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001437 if (pointerIndex == -1) return;
1438
1439 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 final float x = ev.getX(pointerIndex);
1441 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001442 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1443
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 final int xDiff = (int) Math.abs(x - mLastMotionX);
1445 final int yDiff = (int) Math.abs(y - mLastMotionY);
1446
Adam Cohenf8d28232011-02-01 21:47:00 -08001447 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001448 boolean xPaged = xDiff > mPagingTouchSlop;
1449 boolean xMoved = xDiff > touchSlop;
1450 boolean yMoved = yDiff > touchSlop;
1451
Adam Cohenf8d28232011-02-01 21:47:00 -08001452 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001453 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 // Scroll if the user moved far enough along the X axis
1455 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001456 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001458 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001459 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001460 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1461 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001463 }
1464 }
1465
Adam Cohen7d30a372013-07-01 17:03:59 -07001466 protected float getMaxScrollProgress() {
1467 return 1.0f;
1468 }
1469
Adam Cohenf8d28232011-02-01 21:47:00 -08001470 protected void cancelCurrentPageLongPress() {
1471 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001472 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001473 // Try canceling the long press. It could also have been scheduled
1474 // by a distant descendant, so use the mAllowLongPress flag to block
1475 // everything
1476 final View currentPage = getPageAt(mCurrentPage);
1477 if (currentPage != null) {
1478 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001479 }
1480 }
1481 }
1482
Adam Cohen7d30a372013-07-01 17:03:59 -07001483 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1484 final int halfScreenSize = getViewportWidth() / 2;
1485
1486 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1487 screenCenter = Math.max(halfScreenSize, screenCenter);
1488
1489 return getScrollProgress(screenCenter, v, page);
1490 }
1491
Adam Cohenb5ba0972011-09-07 18:02:31 -07001492 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001493 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001494
Adam Cohen0cd0eba2013-10-28 14:22:25 -07001495 int offset = (getViewportWidth() - getChildWidth(page)) / 2;
1496 int totalDistance = v.getMeasuredWidth() + offset;
Adam Cohenedb40762013-07-18 16:45:45 -07001497 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001498
1499 float scrollProgress = delta / (totalDistance * 1.0f);
Adam Cohen7d30a372013-07-01 17:03:59 -07001500 scrollProgress = Math.min(scrollProgress, getMaxScrollProgress());
1501 scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001502 return scrollProgress;
1503 }
1504
Adam Cohenedb40762013-07-18 16:45:45 -07001505 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001506 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001507 return 0;
1508 } else {
1509 return mPageScrolls[index];
1510 }
1511 }
1512
Adam Cohen564a2e72013-10-09 14:47:32 -07001513 // While layout transitions are occurring, a child's position may stray from its baseline
1514 // position. This method returns the magnitude of this stray at any given time.
1515 public int getLayoutTransitionOffsetForPage(int index) {
1516 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1517 return 0;
1518 } else {
1519 View child = getChildAt(index);
1520 int scrollOffset = (getViewportWidth() - child.getMeasuredWidth()) / 2;
1521 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1522 return (int) (child.getX() - baselineX);
1523 }
1524 }
1525
Adam Cohene0f66b52010-11-23 15:06:07 -08001526 // This curve determines how the effect of scrolling over the limits of the page dimishes
1527 // as the user pulls further and further from the bounds
1528 private float overScrollInfluenceCurve(float f) {
1529 f -= 1.0f;
1530 return f * f * f + 1.0f;
1531 }
1532
Adam Cohenb5ba0972011-09-07 18:02:31 -07001533 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001534 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001535
1536 // We want to reach the max over scroll effect when the user has
1537 // over scrolled half the size of the screen
1538 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1539
1540 if (f == 0) return;
1541
1542 // Clamp this factor, f, to -1 < f < 1
1543 if (Math.abs(f) >= 1) {
1544 f /= Math.abs(f);
1545 }
1546
1547 int overScrollAmount = (int) Math.round(f * screenSize);
1548 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001549 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001550 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001551 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001552 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001553 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001554 }
1555 invalidate();
1556 }
1557
1558 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001559 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001560
1561 float f = (amount / screenSize);
1562
1563 if (f == 0) return;
1564 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1565
Adam Cohen7bfc9792011-01-28 13:52:37 -08001566 // Clamp this factor, f, to -1 < f < 1
1567 if (Math.abs(f) >= 1) {
1568 f /= Math.abs(f);
1569 }
1570
Adam Cohene0f66b52010-11-23 15:06:07 -08001571 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001572 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001573 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001574 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001575 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001576 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001577 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001578 }
1579 invalidate();
1580 }
1581
Adam Cohenb5ba0972011-09-07 18:02:31 -07001582 protected void overScroll(float amount) {
1583 dampedOverScroll(amount);
1584 }
1585
Michael Jurkac5b262c2011-01-12 20:24:50 -08001586 protected float maxOverScroll() {
1587 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001588 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001589 float f = 1.0f;
1590 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1591 return OVERSCROLL_DAMP_FACTOR * f;
1592 }
1593
Adam Cohenf358a4b2013-07-23 16:47:31 -07001594 protected void enableFreeScroll() {
1595 setEnableFreeScroll(true, -1);
1596 }
1597
1598 protected void disableFreeScroll(int snapPage) {
1599 setEnableFreeScroll(false, snapPage);
1600 }
1601
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001602 void updateFreescrollBounds() {
1603 getOverviewModePages(mTempVisiblePagesRange);
1604 if (isLayoutRtl()) {
1605 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1606 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1607 } else {
1608 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1609 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1610 }
1611 }
1612
Adam Cohenf358a4b2013-07-23 16:47:31 -07001613 private void setEnableFreeScroll(boolean freeScroll, int snapPage) {
1614 mFreeScroll = freeScroll;
1615
1616 if (snapPage == -1) {
1617 snapPage = getPageNearestToCenterOfScreen();
1618 }
1619
Adam Cohenf358a4b2013-07-23 16:47:31 -07001620 if (!mFreeScroll) {
1621 snapToPage(snapPage);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001622 } else {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001623 updateFreescrollBounds();
1624 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001625 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1626 setCurrentPage(mTempVisiblePagesRange[0]);
1627 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1628 setCurrentPage(mTempVisiblePagesRange[1]);
1629 }
1630 }
1631
1632 setEnableOverscroll(!freeScroll);
1633 }
1634
1635 private void setEnableOverscroll(boolean enable) {
1636 mAllowOverScroll = enable;
1637 }
1638
1639 int getNearestHoverOverPageIndex() {
1640 if (mDragView != null) {
1641 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1642 + mDragView.getTranslationX());
1643 getOverviewModePages(mTempVisiblePagesRange);
1644 int minDistance = Integer.MAX_VALUE;
1645 int minIndex = indexOfChild(mDragView);
1646 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1647 View page = getPageAt(i);
1648 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1649 int d = Math.abs(dragX - pageX);
1650 if (d < minDistance) {
1651 minIndex = i;
1652 minDistance = d;
1653 }
1654 }
1655 return minIndex;
1656 }
1657 return -1;
1658 }
1659
Winson Chung321e9ee2010-08-09 13:37:56 -07001660 @Override
1661 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001662 if (DISABLE_TOUCH_INTERACTION) {
1663 return false;
1664 }
1665
Adam Cohen1697b792013-09-17 19:08:21 -07001666 super.onTouchEvent(ev);
1667
Winson Chung45e1d6e2010-11-09 17:19:49 -08001668 // Skip touch handling if there are no pages to swipe
1669 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1670
Michael Jurkab8f06722010-10-10 15:58:46 -07001671 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001672
1673 final int action = ev.getAction();
1674
1675 switch (action & MotionEvent.ACTION_MASK) {
1676 case MotionEvent.ACTION_DOWN:
1677 /*
1678 * If being flinged and user touches, stop the fling. isFinished
1679 * will be false if being flinged.
1680 */
1681 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001682 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001683 }
1684
1685 // Remember where the motion event started
1686 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001687 mDownMotionY = mLastMotionY = ev.getY();
1688 mDownScrollX = getScrollX();
1689 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1690 mParentDownMotionX = p[0];
1691 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001692 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001693 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001694 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001695
Michael Jurka0142d492010-08-25 17:46:15 -07001696 if (mTouchState == TOUCH_STATE_SCROLLING) {
1697 pageBeginMoving();
1698 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001699 break;
1700
1701 case MotionEvent.ACTION_MOVE:
1702 if (mTouchState == TOUCH_STATE_SCROLLING) {
1703 // Scroll to follow the motion event
1704 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001705
1706 if (pointerIndex == -1) return true;
1707
Winson Chung321e9ee2010-08-09 13:37:56 -07001708 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001709 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001710
Adam Cohenaefd4e12011-02-14 16:39:38 -08001711 mTotalMotionX += Math.abs(deltaX);
1712
Winson Chungc0844aa2011-02-02 15:25:58 -08001713 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1714 // keep the remainder because we are actually testing if we've moved from the last
1715 // scrolled position (which is discrete).
1716 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001717 mTouchX += deltaX;
1718 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1719 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001720 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001721 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001722 } else {
1723 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001724 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001725 mLastMotionX = x;
1726 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001727 } else {
1728 awakenScrollBars();
1729 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001730 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1731 // Update the last motion position
1732 mLastMotionX = ev.getX();
1733 mLastMotionY = ev.getY();
1734
1735 // Update the parent down so that our zoom animations take this new movement into
1736 // account
1737 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1738 mParentDownMotionX = pt[0];
1739 mParentDownMotionY = pt[1];
1740 updateDragViewTranslationDuringDrag();
1741
1742 // Find the closest page to the touch point
1743 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001744
1745 // Change the drag view if we are hovering over the drop target
1746 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1747 (int) mParentDownMotionX, (int) mParentDownMotionY);
1748 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1749
Adam Cohen7d30a372013-07-01 17:03:59 -07001750 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1751 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1752 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1753 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1754
Adam Cohenf358a4b2013-07-23 16:47:31 -07001755 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1756 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1757 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001758 mTempVisiblePagesRange[0] = 0;
1759 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001760 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001761 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1762 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1763 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1764 mSidePageHoverIndex = pageUnderPointIndex;
1765 mSidePageHoverRunnable = new Runnable() {
1766 @Override
1767 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001768 // Setup the scroll to the correct page before we swap the views
1769 snapToPage(pageUnderPointIndex);
1770
1771 // For each of the pages between the paged view and the drag view,
1772 // animate them from the previous position to the new position in
1773 // the layout (as a result of the drag view moving in the layout)
1774 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1775 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1776 dragViewIndex + 1 : pageUnderPointIndex;
1777 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1778 dragViewIndex - 1 : pageUnderPointIndex;
1779 for (int i = lowerIndex; i <= upperIndex; ++i) {
1780 View v = getChildAt(i);
1781 // dragViewIndex < pageUnderPointIndex, so after we remove the
1782 // drag view all subsequent views to pageUnderPointIndex will
1783 // shift down.
1784 int oldX = getViewportOffsetX() + getChildOffset(i);
1785 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1786
1787 // Animate the view translation from its old position to its new
1788 // position
1789 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1790 if (anim != null) {
1791 anim.cancel();
1792 }
1793
1794 v.setTranslationX(oldX - newX);
1795 anim = new AnimatorSet();
1796 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1797 anim.playTogether(
1798 ObjectAnimator.ofFloat(v, "translationX", 0f));
1799 anim.start();
1800 v.setTag(anim);
1801 }
1802
1803 removeView(mDragView);
1804 onRemoveView(mDragView, false);
1805 addView(mDragView, pageUnderPointIndex);
1806 onAddView(mDragView, pageUnderPointIndex);
1807 mSidePageHoverIndex = -1;
Winson Chungd2be3812013-07-16 11:11:32 -07001808 mPageIndicator.setActiveMarker(getNextPage());
Adam Cohen7d30a372013-07-01 17:03:59 -07001809 }
1810 };
1811 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1812 }
1813 } else {
1814 removeCallbacks(mSidePageHoverRunnable);
1815 mSidePageHoverIndex = -1;
1816 }
Adam Cohen564976a2010-10-13 18:52:07 -07001817 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001818 determineScrollingStart(ev);
1819 }
1820 break;
1821
1822 case MotionEvent.ACTION_UP:
1823 if (mTouchState == TOUCH_STATE_SCROLLING) {
1824 final int activePointerId = mActivePointerId;
1825 final int pointerIndex = ev.findPointerIndex(activePointerId);
1826 final float x = ev.getX(pointerIndex);
1827 final VelocityTracker velocityTracker = mVelocityTracker;
1828 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1829 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001830 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001831 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001832 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1833 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001834
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001835 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1836
Adam Cohen00481b32011-11-18 12:03:48 -08001837 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001838 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001839
Adam Cohenf358a4b2013-07-23 16:47:31 -07001840 if (!mFreeScroll) {
1841 // In the case that the page is moved far to one direction and then is flung
1842 // in the opposite direction, we use a threshold to determine whether we should
1843 // just return to the starting page, or if we should skip one further.
1844 boolean returnToOriginalPage = false;
1845 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1846 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1847 returnToOriginalPage = true;
1848 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001849
Adam Cohenf358a4b2013-07-23 16:47:31 -07001850 int finalPage;
1851 // We give flings precedence over large moves, which is why we short-circuit our
1852 // test for a large move if a fling has been registered. That is, a large
1853 // move to the left and fling to the right will register as a fling to the right.
1854 final boolean isRtl = isLayoutRtl();
1855 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1856 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1857 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1858 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1859 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1860 snapToPageWithVelocity(finalPage, velocityX);
1861 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1862 (isFling && isVelocityXLeft)) &&
1863 mCurrentPage < getChildCount() - 1) {
1864 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1865 snapToPageWithVelocity(finalPage, velocityX);
1866 } else {
1867 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001868 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001869 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001870 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001871 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001872 }
1873
1874 float scaleX = getScaleX();
1875 int vX = (int) (-velocityX * scaleX);
1876 int initialScrollX = (int) (getScrollX() * scaleX);
1877
1878 mScroller.fling(initialScrollX,
1879 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1880 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001881 }
Adam Cohencae7f572013-11-04 14:42:52 -08001882 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1883 // at this point we have not moved beyond the touch slop
1884 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1885 // we can just page
1886 int nextPage = Math.max(0, mCurrentPage - 1);
1887 if (nextPage != mCurrentPage) {
1888 snapToPage(nextPage);
1889 } else {
1890 snapToDestination();
1891 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001892 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001893 // at this point we have not moved beyond the touch slop
1894 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1895 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001896 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1897 if (nextPage != mCurrentPage) {
1898 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001899 } else {
1900 snapToDestination();
1901 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001902 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1903 // Update the last motion position
1904 mLastMotionX = ev.getX();
1905 mLastMotionY = ev.getY();
1906
1907 // Update the parent down so that our zoom animations take this new movement into
1908 // account
1909 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1910 mParentDownMotionX = pt[0];
1911 mParentDownMotionY = pt[1];
1912 updateDragViewTranslationDuringDrag();
1913 boolean handledFling = false;
1914 if (!DISABLE_FLING_TO_DELETE) {
1915 // Check the velocity and see if we are flinging-to-delete
1916 PointF flingToDeleteVector = isFlingingToDelete();
1917 if (flingToDeleteVector != null) {
1918 onFlingToDelete(flingToDeleteVector);
1919 handledFling = true;
1920 }
1921 }
1922 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1923 (int) mParentDownMotionY)) {
1924 onDropToDelete();
1925 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001926 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001927 if (!mCancelTap) {
1928 onUnhandledTap(ev);
1929 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001930 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001931
1932 // Remove the callback to wait for the side page hover timeout
1933 removeCallbacks(mSidePageHoverRunnable);
1934 // End any intermediate reordering states
1935 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001936 break;
1937
1938 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001939 if (mTouchState == TOUCH_STATE_SCROLLING) {
1940 snapToDestination();
1941 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001942 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001943 break;
1944
1945 case MotionEvent.ACTION_POINTER_UP:
1946 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001947 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 break;
1949 }
1950
1951 return true;
1952 }
1953
Adam Cohen7d30a372013-07-01 17:03:59 -07001954 public void onFlingToDelete(View v) {}
1955 public void onRemoveView(View v, boolean deletePermanently) {}
1956 public void onRemoveViewAnimationCompleted() {}
1957 public void onAddView(View v, int index) {}
1958
1959 private void resetTouchState() {
1960 releaseVelocityTracker();
1961 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001962 mCancelTap = false;
Adam Cohencae7f572013-11-04 14:42:52 -08001963 mScrollAbortedFromIntercept = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001964 mTouchState = TOUCH_STATE_REST;
1965 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001966 }
1967
Adam Cohen1697b792013-09-17 19:08:21 -07001968 protected void onUnhandledTap(MotionEvent ev) {
1969 ((Launcher) getContext()).onClick(this);
1970 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001971
Winson Chung185d7162011-02-28 13:47:29 -08001972 @Override
1973 public boolean onGenericMotionEvent(MotionEvent event) {
1974 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1975 switch (event.getAction()) {
1976 case MotionEvent.ACTION_SCROLL: {
1977 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1978 final float vscroll;
1979 final float hscroll;
1980 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1981 vscroll = 0;
1982 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1983 } else {
1984 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1985 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1986 }
1987 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001988 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
1989 : (hscroll > 0 || vscroll > 0);
1990 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001991 scrollRight();
1992 } else {
1993 scrollLeft();
1994 }
1995 return true;
1996 }
1997 }
1998 }
1999 }
2000 return super.onGenericMotionEvent(event);
2001 }
2002
Michael Jurkab8f06722010-10-10 15:58:46 -07002003 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2004 if (mVelocityTracker == null) {
2005 mVelocityTracker = VelocityTracker.obtain();
2006 }
2007 mVelocityTracker.addMovement(ev);
2008 }
2009
2010 private void releaseVelocityTracker() {
2011 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002012 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002013 mVelocityTracker.recycle();
2014 mVelocityTracker = null;
2015 }
2016 }
2017
Winson Chung321e9ee2010-08-09 13:37:56 -07002018 private void onSecondaryPointerUp(MotionEvent ev) {
2019 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2020 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2021 final int pointerId = ev.getPointerId(pointerIndex);
2022 if (pointerId == mActivePointerId) {
2023 // This was our active pointer going up. Choose a new
2024 // active pointer and adjust accordingly.
2025 // TODO: Make this decision more intelligent.
2026 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2027 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2028 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002029 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002030 mActivePointerId = ev.getPointerId(newPointerIndex);
2031 if (mVelocityTracker != null) {
2032 mVelocityTracker.clear();
2033 }
2034 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002035 }
2036
Winson Chung321e9ee2010-08-09 13:37:56 -07002037 @Override
2038 public void requestChildFocus(View child, View focused) {
2039 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002040 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002041 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002042 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002043 }
2044 }
2045
Winson Chung1908d072011-02-24 18:09:44 -08002046 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002047 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002048 }
2049
Adam Cohen7d30a372013-07-01 17:03:59 -07002050 int getPageNearestToPoint(float x) {
2051 int index = 0;
2052 for (int i = 0; i < getChildCount(); ++i) {
2053 if (x < getChildAt(i).getRight() - getScrollX()) {
2054 return index;
2055 } else {
2056 index++;
2057 }
2058 }
2059 return Math.min(index, getChildCount() - 1);
2060 }
2061
Adam Cohend19d3ca2010-09-15 14:43:42 -07002062 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002063 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002064 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002065 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002066 final int childCount = getChildCount();
2067 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002068 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002069 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002070 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002071 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002072 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2073 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2074 minDistanceFromScreenCenter = distanceFromScreenCenter;
2075 minDistanceFromScreenCenterIndex = i;
2076 }
2077 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002078 return minDistanceFromScreenCenterIndex;
2079 }
2080
2081 protected void snapToDestination() {
2082 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002083 }
2084
Adam Cohene0f66b52010-11-23 15:06:07 -08002085 private static class ScrollInterpolator implements Interpolator {
2086 public ScrollInterpolator() {
2087 }
2088
2089 public float getInterpolation(float t) {
2090 t -= 1.0f;
2091 return t*t*t*t*t + 1;
2092 }
2093 }
2094
2095 // We want the duration of the page snap animation to be influenced by the distance that
2096 // the screen has to travel, however, we don't want this duration to be effected in a
2097 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2098 // of travel has on the overall snap duration.
2099 float distanceInfluenceForSnapDuration(float f) {
2100 f -= 0.5f; // center the values about 0.
2101 f *= 0.3f * Math.PI / 2.0f;
2102 return (float) Math.sin(f);
2103 }
2104
Michael Jurka0142d492010-08-25 17:46:15 -07002105 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002106 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
Adam Cohen7d30a372013-07-01 17:03:59 -07002107 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002108
Adam Cohenedb40762013-07-18 16:45:45 -07002109 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002110 int delta = newX - mUnboundedScrollX;
2111 int duration = 0;
2112
Adam Cohen265b9a62011-12-07 14:37:18 -08002113 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002114 // If the velocity is low enough, then treat this more as an automatic page advance
2115 // as opposed to an apparent physical response to flinging
2116 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2117 return;
2118 }
2119
2120 // Here we compute a "distance" that will be used in the computation of the overall
2121 // snap duration. This is a function of the actual distance that needs to be traveled;
2122 // we keep this value close to half screen size in order to reduce the variance in snap
2123 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002124 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002125 float distance = halfScreenSize + halfScreenSize *
2126 distanceInfluenceForSnapDuration(distanceRatio);
2127
2128 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002129 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002130
2131 // we want the page's snap velocity to approximately match the velocity at which the
2132 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002133 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2134 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002135
2136 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002137 }
2138
2139 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002140 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002141 }
2142
Adam Cohen7d30a372013-07-01 17:03:59 -07002143 protected void snapToPageImmediately(int whichPage) {
2144 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true);
2145 }
2146
Michael Jurka0142d492010-08-25 17:46:15 -07002147 protected void snapToPage(int whichPage, int duration) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002148 snapToPage(whichPage, duration, false);
2149 }
2150
2151 protected void snapToPage(int whichPage, int duration, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -07002152 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07002153
Adam Cohenedb40762013-07-18 16:45:45 -07002154 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002155 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002156 final int delta = newX - sX;
Adam Cohen7d30a372013-07-01 17:03:59 -07002157 snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07002158 }
2159
2160 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002161 snapToPage(whichPage, delta, duration, false);
2162 }
Michael Jurka0142d492010-08-25 17:46:15 -07002163
Adam Cohen7d30a372013-07-01 17:03:59 -07002164 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate) {
2165 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002166 View focusedChild = getFocusedChild();
2167 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002168 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002169 focusedChild.clearFocus();
2170 }
2171
Adam Cohen53805212013-10-01 10:39:23 -07002172 sendScrollAccessibilityEvent();
2173
Michael Jurka0142d492010-08-25 17:46:15 -07002174 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002175 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002176 if (immediate) {
2177 duration = 0;
2178 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002179 duration = Math.abs(delta);
2180 }
2181
Adam Cohenf358a4b2013-07-23 16:47:31 -07002182 if (!mScroller.isFinished()) {
2183 mScroller.abortAnimation();
2184 }
Adam Cohen68d73932010-11-15 10:50:58 -08002185 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002186
Michael Jurka0142d492010-08-25 17:46:15 -07002187 notifyPageSwitchListener();
Adam Cohen7d30a372013-07-01 17:03:59 -07002188
2189 // Trigger a compute() to finish switching pages if necessary
2190 if (immediate) {
2191 computeScroll();
2192 }
2193
Winson Chung9c0565f2013-07-19 13:49:06 -07002194 // Defer loading associated pages until the scroll settles
2195 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2196
Adam Cohen7d30a372013-07-01 17:03:59 -07002197 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002198 invalidate();
2199 }
2200
Winson Chung321e9ee2010-08-09 13:37:56 -07002201 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002202 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002203 }
2204
2205 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002206 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002207 }
2208
Winson Chung86f77532010-08-24 11:08:22 -07002209 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002210 int result = -1;
2211 if (v != null) {
2212 ViewParent vp = v.getParent();
2213 int count = getChildCount();
2214 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002215 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002216 return i;
2217 }
2218 }
2219 }
2220 return result;
2221 }
2222
2223 /**
2224 * @return True is long presses are still allowed for the current touch
2225 */
2226 public boolean allowLongPress() {
2227 return mAllowLongPress;
2228 }
2229
Adam Cohendbdff6b2013-09-18 19:09:15 -07002230 @Override
2231 public boolean performLongClick() {
2232 mCancelTap = true;
2233 return super.performLongClick();
2234 }
2235
Michael Jurka0142d492010-08-25 17:46:15 -07002236 /**
2237 * Set true to allow long-press events to be triggered, usually checked by
2238 * {@link Launcher} to accept or block dpad-initiated long-presses.
2239 */
2240 public void setAllowLongPress(boolean allowLongPress) {
2241 mAllowLongPress = allowLongPress;
2242 }
2243
Winson Chung321e9ee2010-08-09 13:37:56 -07002244 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002245 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002246
2247 SavedState(Parcelable superState) {
2248 super(superState);
2249 }
2250
2251 private SavedState(Parcel in) {
2252 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002253 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002254 }
2255
2256 @Override
2257 public void writeToParcel(Parcel out, int flags) {
2258 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002259 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002260 }
2261
2262 public static final Parcelable.Creator<SavedState> CREATOR =
2263 new Parcelable.Creator<SavedState>() {
2264 public SavedState createFromParcel(Parcel in) {
2265 return new SavedState(in);
2266 }
2267
2268 public SavedState[] newArray(int size) {
2269 return new SavedState[size];
2270 }
2271 };
2272 }
2273
Winson Chungf314b0e2011-08-16 11:54:27 -07002274 protected void loadAssociatedPages(int page) {
2275 loadAssociatedPages(page, false);
2276 }
2277 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002278 if (mContentIsRefreshable) {
2279 final int count = getChildCount();
2280 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002281 int lowerPageBound = getAssociatedLowerPageBound(page);
2282 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002283 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2284 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002285 // First, clear any pages that should no longer be loaded
2286 for (int i = 0; i < count; ++i) {
2287 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002288 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002289 if (layout.getPageChildCount() > 0) {
2290 layout.removeAllViewsOnPage();
2291 }
2292 mDirtyPageContent.set(i, true);
2293 }
2294 }
2295 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002296 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002297 if ((i != page) && immediateAndOnly) {
2298 continue;
2299 }
Michael Jurka0142d492010-08-25 17:46:15 -07002300 if (lowerPageBound <= i && i <= upperPageBound) {
2301 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002302 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002303 mDirtyPageContent.set(i, false);
2304 }
Winson Chung86f77532010-08-24 11:08:22 -07002305 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002306 }
2307 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002308 }
2309 }
2310
Winson Chunge3193b92010-09-10 11:44:42 -07002311 protected int getAssociatedLowerPageBound(int page) {
2312 return Math.max(0, page - 1);
2313 }
2314 protected int getAssociatedUpperPageBound(int page) {
2315 final int count = getChildCount();
2316 return Math.min(page + 1, count - 1);
2317 }
2318
Winson Chung86f77532010-08-24 11:08:22 -07002319 /**
2320 * This method is called ONLY to synchronize the number of pages that the paged view has.
2321 * To actually fill the pages with information, implement syncPageItems() below. It is
2322 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2323 * and therefore, individual page items do not need to be updated in this method.
2324 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002325 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002326
2327 /**
2328 * This method is called to synchronize the items that are on a particular page. If views on
2329 * the page can be reused, then they should be updated within this method.
2330 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002331 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002332
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002333 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002334 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002335 }
2336 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002337 invalidatePageData(currentPage, false);
2338 }
2339 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002340 if (!mIsDataReady) {
2341 return;
2342 }
2343
Michael Jurka0142d492010-08-25 17:46:15 -07002344 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002345 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002346 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002347
Michael Jurka0142d492010-08-25 17:46:15 -07002348 // Update all the pages
2349 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002350
Winson Chung5a808352011-06-27 19:08:49 -07002351 // We must force a measure after we've loaded the pages to update the content width and
2352 // to determine the full scroll width
2353 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2354 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2355
2356 // Set a new page as the current page if necessary
2357 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002358 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002359 }
2360
Michael Jurka0142d492010-08-25 17:46:15 -07002361 // Mark each of the pages as dirty
2362 final int count = getChildCount();
2363 mDirtyPageContent.clear();
2364 for (int i = 0; i < count; ++i) {
2365 mDirtyPageContent.add(true);
2366 }
2367
2368 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002369 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002370 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002371 }
Adam Cohen06559042013-09-29 18:30:56 -07002372 if (isPageMoving()) {
2373 // If the page is moving, then snap it to the final position to ensure we don't get
2374 // stuck between pages
2375 snapToDestination();
2376 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002377 }
Winson Chung007c6982011-06-14 13:27:53 -07002378
Adam Cohen7d30a372013-07-01 17:03:59 -07002379 // Animate the drag view back to the original position
2380 void animateDragViewToOriginalPosition() {
2381 if (mDragView != null) {
2382 AnimatorSet anim = new AnimatorSet();
2383 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2384 anim.playTogether(
2385 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002386 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2387 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2388 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002389 anim.addListener(new AnimatorListenerAdapter() {
2390 @Override
2391 public void onAnimationEnd(Animator animation) {
2392 onPostReorderingAnimationCompleted();
2393 }
2394 });
2395 anim.start();
2396 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002397 }
2398
Adam Cohen7d30a372013-07-01 17:03:59 -07002399 protected void onStartReordering() {
2400 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2401 mTouchState = TOUCH_STATE_REORDERING;
2402 mIsReordering = true;
2403
Adam Cohen7d30a372013-07-01 17:03:59 -07002404 // We must invalidate to trigger a redraw to update the layers such that the drag view
2405 // is always drawn on top
2406 invalidate();
2407 }
2408
2409 private void onPostReorderingAnimationCompleted() {
2410 // Trigger the callback when reordering has settled
2411 --mPostReorderingPreZoomInRemainingAnimationCount;
2412 if (mPostReorderingPreZoomInRunnable != null &&
2413 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2414 mPostReorderingPreZoomInRunnable.run();
2415 mPostReorderingPreZoomInRunnable = null;
2416 }
2417 }
2418
2419 protected void onEndReordering() {
2420 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002421 }
2422
Adam Cohenf358a4b2013-07-23 16:47:31 -07002423 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002424 int dragViewIndex = indexOfChild(v);
2425
2426 if (mTouchState != TOUCH_STATE_REST) return false;
2427
Adam Cohen7d30a372013-07-01 17:03:59 -07002428 mTempVisiblePagesRange[0] = 0;
2429 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002430 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002431 mReorderingStarted = true;
2432
2433 // Check if we are within the reordering range
2434 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002435 dragViewIndex <= mTempVisiblePagesRange[1]) {
2436 // Find the drag view under the pointer
2437 mDragView = getChildAt(dragViewIndex);
2438 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2439 mDragViewBaselineLeft = mDragView.getLeft();
2440 disableFreeScroll(-1);
2441 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002442 return true;
2443 }
2444 return false;
2445 }
2446
2447 boolean isReordering(boolean testTouchState) {
2448 boolean state = mIsReordering;
2449 if (testTouchState) {
2450 state &= (mTouchState == TOUCH_STATE_REORDERING);
2451 }
2452 return state;
2453 }
2454 void endReordering() {
2455 // For simplicity, we call endReordering sometimes even if reordering was never started.
2456 // In that case, we don't want to do anything.
2457 if (!mReorderingStarted) return;
2458 mReorderingStarted = false;
2459
2460 // If we haven't flung-to-delete the current child, then we just animate the drag view
2461 // back into position
2462 final Runnable onCompleteRunnable = new Runnable() {
2463 @Override
2464 public void run() {
2465 onEndReordering();
2466 }
2467 };
2468 if (!mDeferringForDelete) {
2469 mPostReorderingPreZoomInRunnable = new Runnable() {
2470 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002471 onCompleteRunnable.run();
2472 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002473 };
2474 };
2475
2476 mPostReorderingPreZoomInRemainingAnimationCount =
2477 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2478 // Snap to the current page
2479 snapToPage(indexOfChild(mDragView), 0);
2480 // Animate the drag view back to the front position
2481 animateDragViewToOriginalPosition();
2482 } else {
2483 // Handled in post-delete-animation-callbacks
2484 }
2485 }
2486
Adam Cohen7d30a372013-07-01 17:03:59 -07002487 /*
2488 * Flinging to delete - IN PROGRESS
2489 */
2490 private PointF isFlingingToDelete() {
2491 ViewConfiguration config = ViewConfiguration.get(getContext());
2492 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2493
2494 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2495 // Do a quick dot product test to ensure that we are flinging upwards
2496 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2497 mVelocityTracker.getYVelocity());
2498 PointF upVec = new PointF(0f, -1f);
2499 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2500 (vel.length() * upVec.length()));
2501 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2502 return vel;
2503 }
2504 }
2505 return null;
2506 }
2507
2508 /**
2509 * Creates an animation from the current drag view along its current velocity vector.
2510 * For this animation, the alpha runs for a fixed duration and we update the position
2511 * progressively.
2512 */
2513 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2514 private View mDragView;
2515 private PointF mVelocity;
2516 private Rect mFrom;
2517 private long mPrevTime;
2518 private float mFriction;
2519
2520 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2521
2522 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2523 long startTime, float friction) {
2524 mDragView = dragView;
2525 mVelocity = vel;
2526 mFrom = from;
2527 mPrevTime = startTime;
2528 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2529 }
2530
2531 @Override
2532 public void onAnimationUpdate(ValueAnimator animation) {
2533 float t = ((Float) animation.getAnimatedValue()).floatValue();
2534 long curTime = AnimationUtils.currentAnimationTimeMillis();
2535
2536 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2537 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2538
2539 mDragView.setTranslationX(mFrom.left);
2540 mDragView.setTranslationY(mFrom.top);
2541 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2542
2543 mVelocity.x *= mFriction;
2544 mVelocity.y *= mFriction;
2545 mPrevTime = curTime;
2546 }
2547 };
2548
2549 private static final int ANIM_TAG_KEY = 100;
2550
2551 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2552 return new Runnable() {
2553 @Override
2554 public void run() {
2555 int dragViewIndex = indexOfChild(dragView);
2556
2557 // For each of the pages around the drag view, animate them from the previous
2558 // position to the new position in the layout (as a result of the drag view moving
2559 // in the layout)
2560 // NOTE: We can make an assumption here because we have side-bound pages that we
2561 // will always have pages to animate in from the left
Adam Cohenf358a4b2013-07-23 16:47:31 -07002562 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002563 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2564 boolean slideFromLeft = (isLastWidgetPage ||
2565 dragViewIndex > mTempVisiblePagesRange[0]);
2566
2567 // Setup the scroll to the correct page before we swap the views
2568 if (slideFromLeft) {
2569 snapToPageImmediately(dragViewIndex - 1);
2570 }
2571
2572 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2573 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2574 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2575 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2576 ArrayList<Animator> animations = new ArrayList<Animator>();
2577 for (int i = lowerIndex; i <= upperIndex; ++i) {
2578 View v = getChildAt(i);
2579 // dragViewIndex < pageUnderPointIndex, so after we remove the
2580 // drag view all subsequent views to pageUnderPointIndex will
2581 // shift down.
2582 int oldX = 0;
2583 int newX = 0;
2584 if (slideFromLeft) {
2585 if (i == 0) {
Adam Cohen0cd0eba2013-10-28 14:22:25 -07002586 int pageSpace = (getViewportWidth() - getChildWidth(i)) / 2;
Adam Cohen7d30a372013-07-01 17:03:59 -07002587 // Simulate the page being offscreen with the page spacing
2588 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen0cd0eba2013-10-28 14:22:25 -07002589 - pageSpace;
Adam Cohen7d30a372013-07-01 17:03:59 -07002590 } else {
2591 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2592 }
2593 newX = getViewportOffsetX() + getChildOffset(i);
2594 } else {
2595 oldX = getChildOffset(i) - getChildOffset(i - 1);
2596 newX = 0;
2597 }
2598
2599 // Animate the view translation from its old position to its new
2600 // position
2601 AnimatorSet anim = (AnimatorSet) v.getTag();
2602 if (anim != null) {
2603 anim.cancel();
2604 }
2605
2606 // Note: Hacky, but we want to skip any optimizations to not draw completely
2607 // hidden views
2608 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2609 v.setTranslationX(oldX - newX);
2610 anim = new AnimatorSet();
2611 anim.playTogether(
2612 ObjectAnimator.ofFloat(v, "translationX", 0f),
2613 ObjectAnimator.ofFloat(v, "alpha", 1f));
2614 animations.add(anim);
2615 v.setTag(ANIM_TAG_KEY, anim);
2616 }
2617
2618 AnimatorSet slideAnimations = new AnimatorSet();
2619 slideAnimations.playTogether(animations);
2620 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2621 slideAnimations.addListener(new AnimatorListenerAdapter() {
2622 @Override
2623 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002624 mDeferringForDelete = false;
2625 onEndReordering();
2626 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002627 }
2628 });
2629 slideAnimations.start();
2630
2631 removeView(dragView);
2632 onRemoveView(dragView, true);
2633 }
2634 };
2635 }
2636
2637 public void onFlingToDelete(PointF vel) {
2638 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2639
2640 // NOTE: Because it takes time for the first frame of animation to actually be
2641 // called and we expect the animation to be a continuation of the fling, we have
2642 // to account for the time that has elapsed since the fling finished. And since
2643 // we don't have a startDelay, we will always get call to update when we call
2644 // start() (which we want to ignore).
2645 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2646 private int mCount = -1;
2647 private long mStartTime;
2648 private float mOffset;
2649 /* Anonymous inner class ctor */ {
2650 mStartTime = startTime;
2651 }
2652
2653 @Override
2654 public float getInterpolation(float t) {
2655 if (mCount < 0) {
2656 mCount++;
2657 } else if (mCount == 0) {
2658 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2659 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2660 mCount++;
2661 }
2662 return Math.min(1f, mOffset + t);
2663 }
2664 };
2665
2666 final Rect from = new Rect();
2667 final View dragView = mDragView;
2668 from.left = (int) dragView.getTranslationX();
2669 from.top = (int) dragView.getTranslationY();
2670 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2671 from, startTime, FLING_TO_DELETE_FRICTION);
2672
2673 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2674
2675 // Create and start the animation
2676 ValueAnimator mDropAnim = new ValueAnimator();
2677 mDropAnim.setInterpolator(tInterpolator);
2678 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2679 mDropAnim.setFloatValues(0f, 1f);
2680 mDropAnim.addUpdateListener(updateCb);
2681 mDropAnim.addListener(new AnimatorListenerAdapter() {
2682 public void onAnimationEnd(Animator animation) {
2683 onAnimationEndRunnable.run();
2684 }
2685 });
2686 mDropAnim.start();
2687 mDeferringForDelete = true;
2688 }
2689
2690 /* Drag to delete */
2691 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2692 if (mDeleteDropTarget != null) {
2693 mAltTmpRect.set(0, 0, 0, 0);
2694 View parent = (View) mDeleteDropTarget.getParent();
2695 if (parent != null) {
2696 parent.getGlobalVisibleRect(mAltTmpRect);
2697 }
2698 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2699 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2700 return mTmpRect.contains(x, y);
2701 }
2702 return false;
2703 }
2704
2705 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2706
2707 private void onDropToDelete() {
2708 final View dragView = mDragView;
2709
2710 final float toScale = 0f;
2711 final float toAlpha = 0f;
2712
2713 // Create and start the complex animation
2714 ArrayList<Animator> animations = new ArrayList<Animator>();
2715 AnimatorSet motionAnim = new AnimatorSet();
2716 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2717 motionAnim.playTogether(
2718 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2719 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2720 animations.add(motionAnim);
2721
2722 AnimatorSet alphaAnim = new AnimatorSet();
2723 alphaAnim.setInterpolator(new LinearInterpolator());
2724 alphaAnim.playTogether(
2725 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2726 animations.add(alphaAnim);
2727
2728 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2729
2730 AnimatorSet anim = new AnimatorSet();
2731 anim.playTogether(animations);
2732 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2733 anim.addListener(new AnimatorListenerAdapter() {
2734 public void onAnimationEnd(Animator animation) {
2735 onAnimationEndRunnable.run();
2736 }
2737 });
2738 anim.start();
2739
2740 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002741 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002742
2743 /* Accessibility */
2744 @Override
2745 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2746 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002747 info.setScrollable(getPageCount() > 1);
2748 if (getCurrentPage() < getPageCount() - 1) {
2749 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2750 }
2751 if (getCurrentPage() > 0) {
2752 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2753 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002754 }
2755
2756 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002757 public void sendAccessibilityEvent(int eventType) {
2758 // Don't let the view send real scroll events.
2759 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2760 super.sendAccessibilityEvent(eventType);
2761 }
2762 }
2763
2764 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002765 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2766 super.onInitializeAccessibilityEvent(event);
2767 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002768 }
2769
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002770 @Override
2771 public boolean performAccessibilityAction(int action, Bundle arguments) {
2772 if (super.performAccessibilityAction(action, arguments)) {
2773 return true;
2774 }
2775 switch (action) {
2776 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2777 if (getCurrentPage() < getPageCount() - 1) {
2778 scrollRight();
2779 return true;
2780 }
2781 } break;
2782 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2783 if (getCurrentPage() > 0) {
2784 scrollLeft();
2785 return true;
2786 }
2787 } break;
2788 }
2789 return false;
2790 }
2791
Adam Cohen0ffac432013-07-10 11:19:26 -07002792 protected String getCurrentPageDescription() {
2793 return String.format(getContext().getString(R.string.default_scroll_format),
2794 getNextPage() + 1, getChildCount());
2795 }
2796
Winson Chungd11265e2011-08-30 13:37:23 -07002797 @Override
2798 public boolean onHoverEvent(android.view.MotionEvent event) {
2799 return true;
2800 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002801}