blob: 46c861b3fbf29736777925932a402d0110928268 [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;
Adam Cohen3b185e22013-10-29 14:45:58 -0700127 private int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 private float mParentDownMotionX;
130 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700132 private float mDownMotionY;
133 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700134 private float mDragViewBaselineLeft;
Michael Jurka7426c422010-11-11 15:23:47 -0800135 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800136 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800137 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800138 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700139 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700140
Adam Cohendbdff6b2013-09-18 19:09:15 -0700141 private boolean mCancelTap;
142
Adam Cohenedb40762013-07-18 16:45:45 -0700143 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 protected final static int TOUCH_STATE_REST = 0;
146 protected final static int TOUCH_STATE_SCROLLING = 1;
147 protected final static int TOUCH_STATE_PREV_PAGE = 2;
148 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700149 protected final static int TOUCH_STATE_REORDERING = 4;
150
Adam Cohene45440e2010-10-14 18:33:38 -0700151 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurka0142d492010-08-25 17:46:15 -0700153 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700154 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700155
Michael Jurka0142d492010-08-25 17:46:15 -0700156 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700157
Michael Jurka7426c422010-11-11 15:23:47 -0800158 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 private int mPagingTouchSlop;
160 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700161 protected int mPageLayoutPaddingTop;
162 protected int mPageLayoutPaddingBottom;
163 protected int mPageLayoutPaddingLeft;
164 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700165 protected int mPageLayoutWidthGap;
166 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700167 protected int mCellCountX = 0;
168 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700169 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800170 protected boolean mAllowOverScroll = true;
171 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800172 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700173 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700174
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800176 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
177 // the screens from continuing to translate beyond the normal bounds.
178 protected int mOverScrollX;
179
Michael Jurka5f1c5092010-09-03 14:15:02 -0700180 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700181
Michael Jurka5f1c5092010-09-03 14:15:02 -0700182 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Winson Chung86f77532010-08-24 11:08:22 -0700184 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700185
Michael Jurkae326f182011-11-21 14:05:46 -0800186 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700187
Michael Jurka0142d492010-08-25 17:46:15 -0700188 // If true, syncPages and syncPageItems will be called to refresh pages
189 protected boolean mContentIsRefreshable = true;
190
191 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700192 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700193
194 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
195 // to switch to a new page
196 protected boolean mUsePagingTouchSlop = true;
197
Michael Jurka8b805b12012-04-18 14:23:14 -0700198 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700199 // (SmoothPagedView does this)
200 protected boolean mDeferScrollUpdate = false;
Winson Chung9c0565f2013-07-19 13:49:06 -0700201 protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700202
Patrick Dubroy1262e362010-10-06 15:49:50 -0700203 protected boolean mIsPageMoving = false;
204
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 // All syncs and layout passes are deferred until data is ready.
206 protected boolean mIsDataReady = false;
207
Adam Cohen7d30a372013-07-01 17:03:59 -0700208 protected boolean mAllowLongPress = true;
209
Winson Chungd2be3812013-07-16 11:11:32 -0700210 // Page Indicator
211 private int mPageIndicatorViewId;
212 private PageIndicator mPageIndicator;
Winson Chungc58497e2013-09-03 17:48:37 -0700213 private boolean mAllowPagedViewAnimations = true;
Winson Chung007c6982011-06-14 13:27:53 -0700214
Adam Cohen7d30a372013-07-01 17:03:59 -0700215 // The viewport whether the pages are to be contained (the actual view may be larger than the
216 // viewport)
217 private Rect mViewport = new Rect();
218
219 // Reordering
220 // We use the min scale to determine how much to expand the actually PagedView measured
221 // dimensions such that when we are zoomed out, the view is not clipped
222 private int REORDERING_DROP_REPOSITION_DURATION = 200;
223 protected int REORDERING_REORDER_REPOSITION_DURATION = 300;
224 protected int REORDERING_ZOOM_IN_OUT_DURATION = 250;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700225 private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
Adam Cohen7d30a372013-07-01 17:03:59 -0700226 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700227 private boolean mUseMinScale = false;
Adam Cohen7d30a372013-07-01 17:03:59 -0700228 protected View mDragView;
229 protected AnimatorSet mZoomInOutAnim;
230 private Runnable mSidePageHoverRunnable;
231 private int mSidePageHoverIndex = -1;
232 // This variable's scope is only for the duration of startReordering() and endReordering()
233 private boolean mReorderingStarted = false;
234 // This variable's scope is for the duration of startReordering() and after the zoomIn()
235 // animation after endReordering()
236 private boolean mIsReordering;
237 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
238 private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
239 private int mPostReorderingPreZoomInRemainingAnimationCount;
240 private Runnable mPostReorderingPreZoomInRunnable;
241
Adam Cohen7d30a372013-07-01 17:03:59 -0700242 // Convenience/caching
243 private Matrix mTmpInvMatrix = new Matrix();
244 private float[] mTmpPoint = new float[2];
Winson Chungc9ca2982013-07-19 12:07:38 -0700245 private int[] mTmpIntPoint = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700246 private Rect mTmpRect = new Rect();
247 private Rect mAltTmpRect = new Rect();
248
249 // Fling to delete
250 private int FLING_TO_DELETE_FADE_OUT_DURATION = 350;
251 private float FLING_TO_DELETE_FRICTION = 0.035f;
252 // The degrees specifies how much deviation from the up vector to still consider a fling "up"
253 private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f;
254 protected int mFlingToDeleteThresholdVelocity = -1400;
255 // Drag to delete
256 private boolean mDeferringForDelete = false;
257 private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250;
258 private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350;
259
260 // Drop to delete
261 private View mDeleteDropTarget;
262
Adam Cohen7d30a372013-07-01 17:03:59 -0700263 // Bouncer
264 private boolean mTopAlignPageWhenShrinkingForBouncer = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700265
John Spurlock77e1f472013-09-11 10:09:51 -0400266 protected final Rect mInsets = new Rect();
267
Winson Chung86f77532010-08-24 11:08:22 -0700268 public interface PageSwitchListener {
269 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 }
271
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 public PagedView(Context context) {
273 this(context, null);
274 }
275
276 public PagedView(Context context, AttributeSet attrs) {
277 this(context, attrs, 0);
278 }
279
280 public PagedView(Context context, AttributeSet attrs, int defStyle) {
281 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700282
Adam Cohen9c4949e2010-10-05 12:27:22 -0700283 TypedArray a = context.obtainStyledAttributes(attrs,
284 R.styleable.PagedView, defStyle, 0);
Adam Cohen0cd0eba2013-10-28 14:22:25 -0700285
Adam Cohen9c4949e2010-10-05 12:27:22 -0700286 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800287 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700288 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800289 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700290 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800291 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700292 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800293 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700294 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700295 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700296 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700297 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700298 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700299 a.recycle();
300
Winson Chung321e9ee2010-08-09 13:37:56 -0700301 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700302 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
304
305 /**
306 * Initializes various states for this workspace.
307 */
Michael Jurka0142d492010-08-25 17:46:15 -0700308 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700309 mDirtyPageContent = new ArrayList<Boolean>();
310 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800311 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700312 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700313 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700314
315 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700316 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
318 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700319 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800320
Adam Cohen7d30a372013-07-01 17:03:59 -0700321 // Scale the fling-to-delete threshold by the density
322 mFlingToDeleteThresholdVelocity =
323 (int) (mFlingToDeleteThresholdVelocity * mDensity);
324
Adam Cohen265b9a62011-12-07 14:37:18 -0800325 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
326 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
327 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700328 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 }
330
Winson Chungd2be3812013-07-16 11:11:32 -0700331 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700332 super.onAttachedToWindow();
333
Winson Chungd2be3812013-07-16 11:11:32 -0700334 // Hook up the page indicator
335 ViewGroup parent = (ViewGroup) getParent();
336 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
337 mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
Winson Chungc58497e2013-09-03 17:48:37 -0700338 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chung82dfe582013-07-26 15:07:49 -0700339
Winson Chung7819a562013-09-19 15:55:45 -0700340 ArrayList<PageIndicator.PageMarkerResources> markers =
341 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700342 for (int i = 0; i < getChildCount(); ++i) {
343 markers.add(getPageIndicatorMarker(i));
344 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700345
Winson Chungc58497e2013-09-03 17:48:37 -0700346 mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700347
348 OnClickListener listener = getPageIndicatorClickListener();
349 if (listener != null) {
350 mPageIndicator.setOnClickListener(listener);
351 }
Adam Cohen53805212013-10-01 10:39:23 -0700352 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700353 }
354 }
355
Adam Cohen53805212013-10-01 10:39:23 -0700356 protected String getPageIndicatorDescription() {
357 return getCurrentPageDescription();
358 }
359
360 protected OnClickListener getPageIndicatorClickListener() {
361 return null;
362 }
363
Winson Chungd2be3812013-07-16 11:11:32 -0700364 protected void onDetachedFromWindow() {
365 // Unhook the page indicator
366 mPageIndicator = null;
367 }
368
Adam Cohen7d30a372013-07-01 17:03:59 -0700369 void setDeleteDropTarget(View v) {
370 mDeleteDropTarget = v;
371 }
372
373 // Convenience methods to map points from self to parent and vice versa
374 float[] mapPointFromViewToParent(View v, float x, float y) {
375 mTmpPoint[0] = x;
376 mTmpPoint[1] = y;
377 v.getMatrix().mapPoints(mTmpPoint);
378 mTmpPoint[0] += v.getLeft();
379 mTmpPoint[1] += v.getTop();
380 return mTmpPoint;
381 }
382 float[] mapPointFromParentToView(View v, float x, float y) {
383 mTmpPoint[0] = x - v.getLeft();
384 mTmpPoint[1] = y - v.getTop();
385 v.getMatrix().invert(mTmpInvMatrix);
386 mTmpInvMatrix.mapPoints(mTmpPoint);
387 return mTmpPoint;
388 }
389
390 void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700391 if (mDragView != null) {
392 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
393 (mDragViewBaselineLeft - mDragView.getLeft());
394 float y = mLastMotionY - mDownMotionY;
395 mDragView.setTranslationX(x);
396 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700397
Adam Cohenf358a4b2013-07-23 16:47:31 -0700398 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
399 + x + ", " + y);
400 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700401 }
402
403 public void setMinScale(float f) {
404 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700405 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700406 requestLayout();
407 }
408
409 @Override
410 public void setScaleX(float scaleX) {
411 super.setScaleX(scaleX);
412 if (isReordering(true)) {
413 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
414 mLastMotionX = p[0];
415 mLastMotionY = p[1];
416 updateDragViewTranslationDuringDrag();
417 }
418 }
419
420 // Convenience methods to get the actual width/height of the PagedView (since it is measured
421 // to be larger to account for the minimum possible scale)
422 int getViewportWidth() {
423 return mViewport.width();
424 }
425 int getViewportHeight() {
426 return mViewport.height();
427 }
428
429 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
430 // PagedView both horizontally and vertically
431 int getViewportOffsetX() {
432 return (getMeasuredWidth() - getViewportWidth()) / 2;
433 }
434
435 int getViewportOffsetY() {
436 return (getMeasuredHeight() - getViewportHeight()) / 2;
437 }
438
Adam Cohenedb40762013-07-18 16:45:45 -0700439 PageIndicator getPageIndicator() {
440 return mPageIndicator;
441 }
Winson Chung7819a562013-09-19 15:55:45 -0700442 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
443 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700444 }
Adam Cohenedb40762013-07-18 16:45:45 -0700445
Winson Chung86f77532010-08-24 11:08:22 -0700446 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
447 mPageSwitchListener = pageSwitchListener;
448 if (mPageSwitchListener != null) {
449 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 }
451 }
452
453 /**
Winson Chung52aee602013-01-30 12:01:02 -0800454 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
455 */
456 public boolean isLayoutRtl() {
457 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
458 }
459
460 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
462 * out pages.
463 */
464 protected void setDataIsReady() {
465 mIsDataReady = true;
466 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700467
Winson Chungf0ea4d32011-06-06 14:27:16 -0700468 protected boolean isDataReady() {
469 return mIsDataReady;
470 }
471
472 /**
Winson Chung86f77532010-08-24 11:08:22 -0700473 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 *
Winson Chung86f77532010-08-24 11:08:22 -0700475 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 */
Winson Chung86f77532010-08-24 11:08:22 -0700477 int getCurrentPage() {
478 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700480
Winson Chung360e63f2012-04-27 13:48:05 -0700481 int getNextPage() {
482 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
483 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700484
Winson Chung86f77532010-08-24 11:08:22 -0700485 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 return getChildCount();
487 }
488
Winson Chung86f77532010-08-24 11:08:22 -0700489 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 return getChildAt(index);
491 }
492
Adam Cohenae4f1552011-10-20 00:15:42 -0700493 protected int indexToPage(int index) {
494 return index;
495 }
496
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800498 * Updates the scroll of the current page immediately to its final scroll position. We use this
499 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
500 * the previous tab page.
501 */
502 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700503 // If the current page is invalid, just reset the scroll position to zero
504 int newX = 0;
505 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700506 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700507 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800508 scrollTo(newX, 0);
509 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700510 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800511 }
512
513 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700514 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
Adam Cohen4fe4c932013-10-28 16:02:34 -0700515 * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and
516 * re-enable scrolling.
Chet Haasebc2f0822012-10-26 17:59:53 -0700517 */
Adam Cohen4fe4c932013-10-28 16:02:34 -0700518 void stopScrolling() {
Winson Chungb88ae412013-10-29 17:21:56 -0700519 mCurrentPage = getNextPage();
Adam Cohen4fe4c932013-10-28 16:02:34 -0700520 forceFinishScroller();
Chet Haasebc2f0822012-10-26 17:59:53 -0700521 }
522
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700523 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700524 mScroller.abortAnimation();
525 // We need to clean up the next page here to avoid computeScrollHelper from
526 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700527 if (resetNextPage) {
528 mNextPage = INVALID_PAGE;
529 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700530 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700531
532 private void forceFinishScroller() {
533 mScroller.forceFinished(true);
534 // We need to clean up the next page here to avoid computeScrollHelper from
535 // updating current page on the pass.
536 mNextPage = INVALID_PAGE;
537 }
538
Chet Haasebc2f0822012-10-26 17:59:53 -0700539 /**
Winson Chung86f77532010-08-24 11:08:22 -0700540 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 */
Winson Chung86f77532010-08-24 11:08:22 -0700542 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800543 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700544 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800545 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800546 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
547 // the default
548 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800549 return;
550 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700551 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700552 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chung181c3dc2013-07-17 15:36:20 -0700553 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700554 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800555 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 }
557
Winson Chung8c87cd82013-07-23 16:20:10 -0700558 /**
559 * The restore page will be set in place of the current page at the next (likely first)
560 * layout.
561 */
562 void setRestorePage(int restorePage) {
563 mRestorePage = restorePage;
564 }
565
Michael Jurka0142d492010-08-25 17:46:15 -0700566 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700567 if (mPageSwitchListener != null) {
568 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 }
Winson Chungd2be3812013-07-16 11:11:32 -0700570
571 // Update the page indicator (when we aren't reordering)
572 if (mPageIndicator != null && !isReordering(false)) {
573 mPageIndicator.setActiveMarker(getNextPage());
574 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800576 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700577 if (!mIsPageMoving) {
578 mIsPageMoving = true;
579 onPageBeginMoving();
580 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700581 }
582
Michael Jurkace7e05f2011-02-01 22:02:35 -0800583 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700584 if (mIsPageMoving) {
585 mIsPageMoving = false;
586 onPageEndMoving();
587 }
Michael Jurka0142d492010-08-25 17:46:15 -0700588 }
589
Adam Cohen26976d92011-03-22 15:33:33 -0700590 protected boolean isPageMoving() {
591 return mIsPageMoving;
592 }
593
Michael Jurka0142d492010-08-25 17:46:15 -0700594 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700595 protected void onPageBeginMoving() {
596 }
597
598 // a method that subclasses can override to add behavior
599 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700600 }
601
Winson Chung321e9ee2010-08-09 13:37:56 -0700602 /**
Winson Chung86f77532010-08-24 11:08:22 -0700603 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 *
605 * @param l The listener used to respond to long clicks.
606 */
607 @Override
608 public void setOnLongClickListener(OnLongClickListener l) {
609 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700610 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700612 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
Adam Cohen1697b792013-09-17 19:08:21 -0700614 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 }
616
617 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800618 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800620 }
621
622 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700623 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700624 // In free scroll mode, we clamp the scrollX
625 if (mFreeScroll) {
626 x = Math.min(x, mFreeScrollMaxScrollX);
627 x = Math.max(x, mFreeScrollMinScrollX);
628 }
629
Adam Cohen0ffac432013-07-10 11:19:26 -0700630 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800631 mUnboundedScrollX = x;
632
Adam Cohen0ffac432013-07-10 11:19:26 -0700633 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
634 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
635 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800636 super.scrollTo(0, y);
637 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700638 if (isRtl) {
639 overScroll(x - mMaxScrollX);
640 } else {
641 overScroll(x);
642 }
Adam Cohen68d73932010-11-15 10:50:58 -0800643 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700644 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800645 super.scrollTo(mMaxScrollX, y);
646 if (mAllowOverScroll) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700647 if (isRtl) {
648 overScroll(x);
649 } else {
650 overScroll(x - mMaxScrollX);
651 }
Adam Cohen68d73932010-11-15 10:50:58 -0800652 }
653 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800654 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800655 super.scrollTo(x, y);
656 }
657
Michael Jurka0142d492010-08-25 17:46:15 -0700658 mTouchX = x;
659 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
Adam Cohen7d30a372013-07-01 17:03:59 -0700660
661 // Update the last motion events when scrolling
662 if (isReordering(true)) {
663 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
664 mLastMotionX = p[0];
665 mLastMotionY = p[1];
666 updateDragViewTranslationDuringDrag();
667 }
Michael Jurka0142d492010-08-25 17:46:15 -0700668 }
669
Adam Cohen53805212013-10-01 10:39:23 -0700670 private void sendScrollAccessibilityEvent() {
671 AccessibilityManager am =
672 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
673 if (am.isEnabled()) {
674 AccessibilityEvent ev =
675 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Adam Cohen53805212013-10-01 10:39:23 -0700676 ev.setItemCount(getChildCount());
677 ev.setFromIndex(mCurrentPage);
Adam Cohene4602ae2013-10-31 15:46:45 -0700678 ev.setToIndex(getNextPage());
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 Cohen3b185e22013-10-29 14:45:58 -0700790 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
791 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700792
Adam Cohen3b185e22013-10-29 14:45:58 -0700793 int parentWidthSize = (int) (1.5f * maxSize);
794 int parentHeightSize = maxSize;
Winson Chungc58497e2013-09-03 17:48:37 -0700795 int scaledWidthSize, scaledHeightSize;
796 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700797 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
Adam Cohen3b185e22013-10-29 14:45:58 -0700856 childWidth = getViewportWidth() - horizontalPadding
857 - mInsets.left - mInsets.right;
858 childHeight = getViewportHeight() - verticalPadding
859 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100860 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700861 } else {
862 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700863 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100864
Adam Cohen3b185e22013-10-29 14:45:58 -0700865 childWidth = getViewportWidth() - mInsets.left - mInsets.right;
866 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700867 }
868
Vladimir Marko2824b072013-10-04 16:42:17 +0100869 final int childWidthMeasureSpec =
870 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
871 final int childHeightMeasureSpec =
872 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
873 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700874 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700875 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700876 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800877 }
878
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700880 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700881 if (!mIsDataReady || getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700882 return;
883 }
884
Winson Chung785d2eb2011-04-14 16:08:02 -0700885 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700887
Adam Cohen7d30a372013-07-01 17:03:59 -0700888 int offsetX = getViewportOffsetX();
889 int offsetY = getViewportOffsetY();
890
891 // Update the viewport offsets
892 mViewport.offset(offsetX, offsetY);
893
Adam Cohen0ffac432013-07-10 11:19:26 -0700894 final boolean isRtl = isLayoutRtl();
895
896 final int startIndex = isRtl ? childCount - 1 : 0;
897 final int endIndex = isRtl ? -1 : childCount;
898 final int delta = isRtl ? -1 : 1;
899
Adam Cohen7d30a372013-07-01 17:03:59 -0700900 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700901
Adam Cohen3b185e22013-10-29 14:45:58 -0700902 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
903
904 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Adam Cohenedb40762013-07-18 16:45:45 -0700905 if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
906 mPageScrolls = new int[getChildCount()];
907 }
908
Adam Cohen0ffac432013-07-10 11:19:26 -0700909 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700910 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700912 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100913 int childTop;
914 if (lp.isFullScreenPage) {
915 childTop = offsetY;
916 } else {
917 childTop = offsetY + getPaddingTop() + mInsets.top;
918 if (mCenterPagesVertically) {
919 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
920 }
921 }
922
Adam Cohen96d30a12013-07-16 18:13:21 -0700923 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700924 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800925
Winson Chung785d2eb2011-04-14 16:08:02 -0700926 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700927 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800928 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700929
Adam Cohen3b185e22013-10-29 14:45:58 -0700930 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
931 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
932 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 }
934 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700935
936 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
937 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800938 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700939 setHorizontalScrollBarEnabled(true);
940 mFirstLayout = false;
941 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700942
943 if (childCount > 0) {
944 final int index = isLayoutRtl() ? 0 : childCount - 1;
Adam Cohenedb40762013-07-18 16:45:45 -0700945 mMaxScrollX = getScrollForPage(index);
Adam Cohenf698c6e2013-07-17 18:44:25 -0700946 } else {
947 mMaxScrollX = 0;
948 }
949
950 if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() &&
951 !mDeferringForDelete) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700952 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700953 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700954 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700955 } else {
956 setCurrentPage(getNextPage());
957 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700958 }
959 mChildCountOnLastLayout = getChildCount();
Adam Cohenf358a4b2013-07-23 16:47:31 -0700960
961 if (isReordering(true)) {
962 updateDragViewTranslationDuringDrag();
963 }
Winson Chunge3193b92010-09-10 11:44:42 -0700964 }
965
Adam Cohen3b185e22013-10-29 14:45:58 -0700966 public void setPageSpacing(int pageSpacing) {
967 mPageSpacing = pageSpacing;
968 requestLayout();
969 }
970
Adam Cohenf34bab52010-09-30 14:11:56 -0700971 protected void screenScrolled(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -0700972 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
973
974 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700975 for (int i = 0; i < getChildCount(); i++) {
976 View child = getChildAt(i);
977 if (child != null) {
978 float scrollProgress = getScrollProgress(screenCenter, child, i);
979 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800980 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700981 }
982 }
983 invalidate();
984 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700985 }
986
Winson Chungc58497e2013-09-03 17:48:37 -0700987 protected void enablePagedViewAnimations() {
988 mAllowPagedViewAnimations = true;
989
990 }
991 protected void disablePagedViewAnimations() {
992 mAllowPagedViewAnimations = false;
993 }
994
Winson Chunge3193b92010-09-10 11:44:42 -0700995 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700996 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700997 // Update the page indicator, we don't update the page indicator as we
998 // add/remove pages
999 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -07001000 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -07001001 mPageIndicator.addMarker(pageIndex,
1002 getPageIndicatorMarker(pageIndex),
Winson Chungc58497e2013-09-03 17:48:37 -07001003 mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001004 }
1005
Adam Cohen2591f6a2011-10-25 14:36:40 -07001006 // This ensures that when children are added, they get the correct transforms / alphas
1007 // in accordance with any scroll effects.
1008 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001009 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -07001010 invalidate();
1011 }
1012
Michael Jurka8b805b12012-04-18 14:23:14 -07001013 @Override
1014 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001015 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001016 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -07001017 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -07001018 }
1019
Winson Chungd2be3812013-07-16 11:11:32 -07001020 private void removeMarkerForView(int index) {
1021 // Update the page indicator, we don't update the page indicator as we
1022 // add/remove pages
1023 if (mPageIndicator != null && !isReordering(false)) {
Winson Chungc58497e2013-09-03 17:48:37 -07001024 mPageIndicator.removeMarker(index, mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001025 }
1026 }
1027
1028 @Override
1029 public void removeView(View v) {
1030 // XXX: We should find a better way to hook into this before the view
1031 // gets removed form its parent...
1032 removeMarkerForView(indexOfChild(v));
1033 super.removeView(v);
1034 }
1035 @Override
1036 public void removeViewInLayout(View v) {
1037 // XXX: We should find a better way to hook into this before the view
1038 // gets removed form its parent...
1039 removeMarkerForView(indexOfChild(v));
1040 super.removeViewInLayout(v);
1041 }
1042 @Override
1043 public void removeViewAt(int index) {
1044 // XXX: We should find a better way to hook into this before the view
1045 // gets removed form its parent...
1046 removeViewAt(index);
1047 super.removeViewAt(index);
1048 }
1049 @Override
1050 public void removeAllViewsInLayout() {
1051 // Update the page indicator, we don't update the page indicator as we
1052 // add/remove pages
1053 if (mPageIndicator != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07001054 mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations);
Winson Chungd2be3812013-07-16 11:11:32 -07001055 }
1056
1057 super.removeAllViewsInLayout();
1058 }
1059
Adam Cohen73894962011-10-31 13:17:17 -07001060 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001061 if (index < 0 || index > getChildCount() - 1) return 0;
1062
Adam Cohenf698c6e2013-07-17 18:44:25 -07001063 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001064
Adam Cohenf698c6e2013-07-17 18:44:25 -07001065 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001066 }
1067
Adam Cohenf358a4b2013-07-23 16:47:31 -07001068 protected void getOverviewModePages(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001069 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001070 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001071 }
1072
Michael Jurkadde558b2011-11-09 22:09:06 -08001073 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -07001074 final int pageCount = getChildCount();
Winson Chungc9ca2982013-07-19 12:07:38 -07001075 mTmpIntPoint[0] = mTmpIntPoint[1] = 0;
Michael Jurka4ff7d792012-04-02 03:46:50 -07001076
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001077 range[0] = -1;
1078 range[1] = -1;
1079
Michael Jurkac4fb9172010-09-02 17:19:20 -07001080 if (pageCount > 0) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001081 int viewportWidth = getViewportWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001082 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001083
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001084 int count = getChildCount();
1085 for (int i = 0; i < count; i++) {
1086 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001087
Winson Chungc9ca2982013-07-19 12:07:38 -07001088 mTmpIntPoint[0] = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -07001089 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001090 if (mTmpIntPoint[0] > viewportWidth) {
1091 if (range[0] == -1) {
1092 continue;
1093 } else {
1094 break;
1095 }
1096 }
1097
Adam Cohen6a678da2013-09-24 10:58:01 -07001098 mTmpIntPoint[0] = currPage.getMeasuredWidth();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001099 Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false);
1100 if (mTmpIntPoint[0] < 0) {
1101 if (range[0] == -1) {
1102 continue;
1103 } else {
1104 break;
1105 }
1106 }
1107 curScreen = i;
1108 if (range[0] < 0) {
1109 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001110 }
1111 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001112
1113 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001114 } else {
1115 range[0] = -1;
1116 range[1] = -1;
1117 }
1118 }
1119
Michael Jurka920d7f42012-05-14 16:29:55 -07001120 protected boolean shouldDrawChild(View child) {
Adam Cohenf3434992013-09-16 16:52:59 -07001121 return child.getAlpha() > 0 && child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001122 }
1123
Michael Jurkadde558b2011-11-09 22:09:06 -08001124 @Override
1125 protected void dispatchDraw(Canvas canvas) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001126 int halfScreenSize = getViewportWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -07001127 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
1128 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -08001129 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -08001130
1131 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -07001132 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1133 // set it for the next frame
1134 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -08001135 screenScrolled(screenCenter);
1136 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -08001137 }
1138
1139 // Find out which screens are visible; as an optimization we only call draw on them
1140 final int pageCount = getChildCount();
1141 if (pageCount > 0) {
1142 getVisiblePages(mTempVisiblePagesRange);
1143 final int leftScreen = mTempVisiblePagesRange[0];
1144 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001145 if (leftScreen != -1 && rightScreen != -1) {
1146 final long drawingTime = getDrawingTime();
1147 // Clip to the bounds
1148 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001149 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1150 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001151
Adam Cohen7d30a372013-07-01 17:03:59 -07001152 // Draw all the children, leaving the drag view for last
1153 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001154 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001155 if (v == mDragView) continue;
Michael Jurka5e368ff2012-05-14 23:13:15 -07001156 if (mForceDrawAllChildrenNextFrame ||
1157 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -08001158 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001159 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001160 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001161 // Draw the drag view on top (if there is one)
1162 if (mDragView != null) {
1163 drawChild(canvas, mDragView, drawingTime);
1164 }
1165
Michael Jurka5e368ff2012-05-14 23:13:15 -07001166 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -08001167 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001168 }
Michael Jurka0142d492010-08-25 17:46:15 -07001169 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 }
1171
1172 @Override
1173 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001174 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001175 if (page != mCurrentPage || !mScroller.isFinished()) {
1176 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 return true;
1178 }
1179 return false;
1180 }
1181
1182 @Override
1183 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001184 int focusablePage;
1185 if (mNextPage != INVALID_PAGE) {
1186 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001188 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 }
Winson Chung86f77532010-08-24 11:08:22 -07001190 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001192 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 }
1194 return false;
1195 }
1196
1197 @Override
1198 public boolean dispatchUnhandledMove(View focused, int direction) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001199 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001201 if (getCurrentPage() > 0) {
1202 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 return true;
1204 }
1205 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001206 if (getCurrentPage() < getPageCount() - 1) {
1207 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 return true;
1209 }
1210 }
1211 return super.dispatchUnhandledMove(focused, direction);
1212 }
1213
1214 @Override
1215 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001216 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001218 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 }
1220 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001221 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001222 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 }
1224 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001225 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001226 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 }
1228 }
1229 }
1230
1231 /**
1232 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001233 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 *
Winson Chung86f77532010-08-24 11:08:22 -07001235 * This happens when live folders requery, and if they're off page, they
1236 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 */
1238 @Override
1239 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001240 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 View v = focused;
1242 while (true) {
1243 if (v == current) {
1244 super.focusableViewAvailable(focused);
1245 return;
1246 }
1247 if (v == this) {
1248 return;
1249 }
1250 ViewParent parent = v.getParent();
1251 if (parent instanceof View) {
1252 v = (View)v.getParent();
1253 } else {
1254 return;
1255 }
1256 }
1257 }
1258
1259 /**
1260 * {@inheritDoc}
1261 */
1262 @Override
1263 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1264 if (disallowIntercept) {
1265 // We need to make sure to cancel our long press if
1266 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001267 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001268 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 }
1270 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1271 }
1272
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001273 /**
1274 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1275 */
1276 protected boolean hitsPreviousPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001277 if (isLayoutRtl()) {
1278 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001279 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001280 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001281 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001282 }
1283
1284 /**
1285 * Return true if a tap at (x, y) should trigger a flip to the next page.
1286 */
1287 protected boolean hitsNextPage(float x, float y) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001288 if (isLayoutRtl()) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001289 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001290 }
1291 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001292 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001293 }
Winson Chung52aee602013-01-30 12:01:02 -08001294
Adam Cohen7d30a372013-07-01 17:03:59 -07001295 /** Returns whether x and y originated within the buffered viewport */
1296 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
1297 mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
1298 mViewport.right + mViewport.width() / 2, mViewport.bottom);
1299 return mTmpRect.contains(x, y);
1300 }
1301
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 @Override
1303 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001304 if (DISABLE_TOUCH_INTERACTION) {
1305 return false;
1306 }
1307
Winson Chung321e9ee2010-08-09 13:37:56 -07001308 /*
1309 * This method JUST determines whether we want to intercept the motion.
1310 * If we return true, onTouchEvent will be called and we do the actual
1311 * scrolling there.
1312 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001313 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001314
Winson Chung45e1d6e2010-11-09 17:19:49 -08001315 // Skip touch handling if there are no pages to swipe
1316 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1317
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 /*
1319 * Shortcut the most recurring case: the user is in the dragging
1320 * state and he is moving his finger. We want to intercept this
1321 * motion.
1322 */
1323 final int action = ev.getAction();
1324 if ((action == MotionEvent.ACTION_MOVE) &&
1325 (mTouchState == TOUCH_STATE_SCROLLING)) {
1326 return true;
1327 }
1328
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 switch (action & MotionEvent.ACTION_MASK) {
1330 case MotionEvent.ACTION_MOVE: {
1331 /*
1332 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1333 * whether the user has moved far enough from his original down touch.
1334 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001335 if (mActivePointerId != INVALID_POINTER) {
1336 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001337 }
1338 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1339 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1340 // i.e. fall through to the next case (don't break)
1341 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1342 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001343 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 }
1345
1346 case MotionEvent.ACTION_DOWN: {
1347 final float x = ev.getX();
1348 final float y = ev.getY();
1349 // Remember location of down touch
1350 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001351 mDownMotionY = y;
1352 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 mLastMotionX = x;
1354 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001355 float[] p = mapPointFromViewToParent(this, x, y);
1356 mParentDownMotionX = p[0];
1357 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001358 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001359 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001361
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 /*
1363 * If being flinged and user touches the screen, initiate drag;
1364 * otherwise don't. mScroller.isFinished should be false when
1365 * being flinged.
1366 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001367 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001368 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
1369 if (finishedScrolling) {
1370 mTouchState = TOUCH_STATE_REST;
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001371 abortScrollerAnimation(false);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001372 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001373 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1374 mTouchState = TOUCH_STATE_SCROLLING;
1375 } else {
1376 mTouchState = TOUCH_STATE_REST;
1377 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001378 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001379
Winson Chung86f77532010-08-24 11:08:22 -07001380 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001381 // to scroll the current page
Adam Cohen7d30a372013-07-01 17:03:59 -07001382 if (!DISABLE_TOUCH_SIDE_PAGES) {
1383 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
1384 if (getChildCount() > 0) {
1385 if (hitsPreviousPage(x, y)) {
1386 mTouchState = TOUCH_STATE_PREV_PAGE;
1387 } else if (hitsNextPage(x, y)) {
1388 mTouchState = TOUCH_STATE_NEXT_PAGE;
1389 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 }
1391 }
1392 }
1393 break;
1394 }
1395
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001397 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001398 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 break;
1400
1401 case MotionEvent.ACTION_POINTER_UP:
1402 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001403 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 break;
1405 }
1406
1407 /*
1408 * The only time we want to intercept motion events is if we are in the
1409 * drag mode.
1410 */
1411 return mTouchState != TOUCH_STATE_REST;
1412 }
1413
Adam Cohenf8d28232011-02-01 21:47:00 -08001414 protected void determineScrollingStart(MotionEvent ev) {
1415 determineScrollingStart(ev, 1.0f);
1416 }
1417
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 /*
1419 * Determines if we should change the touch state to start scrolling after the
1420 * user moves their touch point too far.
1421 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001422 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001423 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001425 if (pointerIndex == -1) return;
1426
1427 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 final float x = ev.getX(pointerIndex);
1429 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001430 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1431
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 final int xDiff = (int) Math.abs(x - mLastMotionX);
1433 final int yDiff = (int) Math.abs(y - mLastMotionY);
1434
Adam Cohenf8d28232011-02-01 21:47:00 -08001435 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 boolean xPaged = xDiff > mPagingTouchSlop;
1437 boolean xMoved = xDiff > touchSlop;
1438 boolean yMoved = yDiff > touchSlop;
1439
Adam Cohenf8d28232011-02-01 21:47:00 -08001440 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001441 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001442 // Scroll if the user moved far enough along the X axis
1443 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001444 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001446 mLastMotionXRemainder = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001447 mTouchX = getViewportOffsetX() + getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001448 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1449 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001450 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001451 }
1452 }
1453
Adam Cohen7d30a372013-07-01 17:03:59 -07001454 protected float getMaxScrollProgress() {
1455 return 1.0f;
1456 }
1457
Adam Cohenf8d28232011-02-01 21:47:00 -08001458 protected void cancelCurrentPageLongPress() {
1459 if (mAllowLongPress) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001460 //mAllowLongPress = false;
Adam Cohenf8d28232011-02-01 21:47:00 -08001461 // Try canceling the long press. It could also have been scheduled
1462 // by a distant descendant, so use the mAllowLongPress flag to block
1463 // everything
1464 final View currentPage = getPageAt(mCurrentPage);
1465 if (currentPage != null) {
1466 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 }
1468 }
1469 }
1470
Adam Cohen7d30a372013-07-01 17:03:59 -07001471 protected float getBoundedScrollProgress(int screenCenter, View v, int page) {
1472 final int halfScreenSize = getViewportWidth() / 2;
1473
1474 screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter);
1475 screenCenter = Math.max(halfScreenSize, screenCenter);
1476
1477 return getScrollProgress(screenCenter, v, page);
1478 }
1479
Adam Cohenb5ba0972011-09-07 18:02:31 -07001480 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001481 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001482
Adam Cohenedb40762013-07-18 16:45:45 -07001483 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001484 int count = getChildCount();
1485
1486 final int totalDistance;
1487
1488 int adjacentPage = page + 1;
1489 if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) {
1490 adjacentPage = page - 1;
1491 }
1492
1493 if (adjacentPage < 0 || adjacentPage > count - 1) {
1494 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1495 } else {
1496 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1497 }
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);
Adam Cohen3b185e22013-10-29 14:45:58 -07001520
1521 int scrollOffset = 0;
1522 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1523 if (!lp.isFullScreenPage) {
1524 scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft();
1525 }
1526
Adam Cohen564a2e72013-10-09 14:47:32 -07001527 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1528 return (int) (child.getX() - baselineX);
1529 }
1530 }
1531
Adam Cohene0f66b52010-11-23 15:06:07 -08001532 // This curve determines how the effect of scrolling over the limits of the page dimishes
1533 // as the user pulls further and further from the bounds
1534 private float overScrollInfluenceCurve(float f) {
1535 f -= 1.0f;
1536 return f * f * f + 1.0f;
1537 }
1538
Adam Cohenb5ba0972011-09-07 18:02:31 -07001539 protected void acceleratedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001540 int screenSize = getViewportWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001541
1542 // We want to reach the max over scroll effect when the user has
1543 // over scrolled half the size of the screen
1544 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1545
1546 if (f == 0) return;
1547
1548 // Clamp this factor, f, to -1 < f < 1
1549 if (Math.abs(f) >= 1) {
1550 f /= Math.abs(f);
1551 }
1552
1553 int overScrollAmount = (int) Math.round(f * screenSize);
1554 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001555 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001556 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001557 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001558 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001559 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001560 }
1561 invalidate();
1562 }
1563
1564 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001565 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001566
1567 float f = (amount / screenSize);
1568
1569 if (f == 0) return;
1570 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1571
Adam Cohen7bfc9792011-01-28 13:52:37 -08001572 // Clamp this factor, f, to -1 < f < 1
1573 if (Math.abs(f) >= 1) {
1574 f /= Math.abs(f);
1575 }
1576
Adam Cohene0f66b52010-11-23 15:06:07 -08001577 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001578 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001579 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001580 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001581 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001582 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001583 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001584 }
1585 invalidate();
1586 }
1587
Adam Cohenb5ba0972011-09-07 18:02:31 -07001588 protected void overScroll(float amount) {
1589 dampedOverScroll(amount);
1590 }
1591
Michael Jurkac5b262c2011-01-12 20:24:50 -08001592 protected float maxOverScroll() {
1593 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001594 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001595 float f = 1.0f;
1596 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1597 return OVERSCROLL_DAMP_FACTOR * f;
1598 }
1599
Adam Cohenf358a4b2013-07-23 16:47:31 -07001600 protected void enableFreeScroll() {
1601 setEnableFreeScroll(true, -1);
1602 }
1603
1604 protected void disableFreeScroll(int snapPage) {
1605 setEnableFreeScroll(false, snapPage);
1606 }
1607
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001608 void updateFreescrollBounds() {
1609 getOverviewModePages(mTempVisiblePagesRange);
1610 if (isLayoutRtl()) {
1611 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1612 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1613 } else {
1614 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1615 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1616 }
1617 }
1618
Adam Cohenf358a4b2013-07-23 16:47:31 -07001619 private void setEnableFreeScroll(boolean freeScroll, int snapPage) {
1620 mFreeScroll = freeScroll;
1621
1622 if (snapPage == -1) {
1623 snapPage = getPageNearestToCenterOfScreen();
1624 }
1625
Adam Cohenf358a4b2013-07-23 16:47:31 -07001626 if (!mFreeScroll) {
1627 snapToPage(snapPage);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001628 } else {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001629 updateFreescrollBounds();
1630 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001631 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1632 setCurrentPage(mTempVisiblePagesRange[0]);
1633 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1634 setCurrentPage(mTempVisiblePagesRange[1]);
1635 }
1636 }
1637
1638 setEnableOverscroll(!freeScroll);
1639 }
1640
1641 private void setEnableOverscroll(boolean enable) {
1642 mAllowOverScroll = enable;
1643 }
1644
1645 int getNearestHoverOverPageIndex() {
1646 if (mDragView != null) {
1647 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1648 + mDragView.getTranslationX());
1649 getOverviewModePages(mTempVisiblePagesRange);
1650 int minDistance = Integer.MAX_VALUE;
1651 int minIndex = indexOfChild(mDragView);
1652 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1653 View page = getPageAt(i);
1654 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1655 int d = Math.abs(dragX - pageX);
1656 if (d < minDistance) {
1657 minIndex = i;
1658 minDistance = d;
1659 }
1660 }
1661 return minIndex;
1662 }
1663 return -1;
1664 }
1665
Winson Chung321e9ee2010-08-09 13:37:56 -07001666 @Override
1667 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001668 if (DISABLE_TOUCH_INTERACTION) {
1669 return false;
1670 }
1671
Adam Cohen1697b792013-09-17 19:08:21 -07001672 super.onTouchEvent(ev);
1673
Winson Chung45e1d6e2010-11-09 17:19:49 -08001674 // Skip touch handling if there are no pages to swipe
1675 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1676
Michael Jurkab8f06722010-10-10 15:58:46 -07001677 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001678
1679 final int action = ev.getAction();
1680
1681 switch (action & MotionEvent.ACTION_MASK) {
1682 case MotionEvent.ACTION_DOWN:
1683 /*
1684 * If being flinged and user touches, stop the fling. isFinished
1685 * will be false if being flinged.
1686 */
1687 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001688 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001689 }
1690
1691 // Remember where the motion event started
1692 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001693 mDownMotionY = mLastMotionY = ev.getY();
1694 mDownScrollX = getScrollX();
1695 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1696 mParentDownMotionX = p[0];
1697 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001698 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001699 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001700 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001701
Michael Jurka0142d492010-08-25 17:46:15 -07001702 if (mTouchState == TOUCH_STATE_SCROLLING) {
1703 pageBeginMoving();
1704 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001705 break;
1706
1707 case MotionEvent.ACTION_MOVE:
1708 if (mTouchState == TOUCH_STATE_SCROLLING) {
1709 // Scroll to follow the motion event
1710 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001711
1712 if (pointerIndex == -1) return true;
1713
Winson Chung321e9ee2010-08-09 13:37:56 -07001714 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001715 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001716
Adam Cohenaefd4e12011-02-14 16:39:38 -08001717 mTotalMotionX += Math.abs(deltaX);
1718
Winson Chungc0844aa2011-02-02 15:25:58 -08001719 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1720 // keep the remainder because we are actually testing if we've moved from the last
1721 // scrolled position (which is discrete).
1722 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001723 mTouchX += deltaX;
1724 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1725 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001726 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001727 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001728 } else {
1729 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001730 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001731 mLastMotionX = x;
1732 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001733 } else {
1734 awakenScrollBars();
1735 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001736 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1737 // Update the last motion position
1738 mLastMotionX = ev.getX();
1739 mLastMotionY = ev.getY();
1740
1741 // Update the parent down so that our zoom animations take this new movement into
1742 // account
1743 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1744 mParentDownMotionX = pt[0];
1745 mParentDownMotionY = pt[1];
1746 updateDragViewTranslationDuringDrag();
1747
1748 // Find the closest page to the touch point
1749 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001750
1751 // Change the drag view if we are hovering over the drop target
1752 boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget(
1753 (int) mParentDownMotionX, (int) mParentDownMotionY);
1754 setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);
1755
Adam Cohen7d30a372013-07-01 17:03:59 -07001756 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1757 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1758 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1759 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1760
Adam Cohenf358a4b2013-07-23 16:47:31 -07001761 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
1762 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) &&
1763 !isHoveringOverDelete) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001764 mTempVisiblePagesRange[0] = 0;
1765 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001766 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001767 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1768 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1769 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1770 mSidePageHoverIndex = pageUnderPointIndex;
1771 mSidePageHoverRunnable = new Runnable() {
1772 @Override
1773 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001774 // Setup the scroll to the correct page before we swap the views
1775 snapToPage(pageUnderPointIndex);
1776
1777 // For each of the pages between the paged view and the drag view,
1778 // animate them from the previous position to the new position in
1779 // the layout (as a result of the drag view moving in the layout)
1780 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1781 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1782 dragViewIndex + 1 : pageUnderPointIndex;
1783 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1784 dragViewIndex - 1 : pageUnderPointIndex;
1785 for (int i = lowerIndex; i <= upperIndex; ++i) {
1786 View v = getChildAt(i);
1787 // dragViewIndex < pageUnderPointIndex, so after we remove the
1788 // drag view all subsequent views to pageUnderPointIndex will
1789 // shift down.
1790 int oldX = getViewportOffsetX() + getChildOffset(i);
1791 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1792
1793 // Animate the view translation from its old position to its new
1794 // position
1795 AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
1796 if (anim != null) {
1797 anim.cancel();
1798 }
1799
1800 v.setTranslationX(oldX - newX);
1801 anim = new AnimatorSet();
1802 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
1803 anim.playTogether(
1804 ObjectAnimator.ofFloat(v, "translationX", 0f));
1805 anim.start();
1806 v.setTag(anim);
1807 }
1808
1809 removeView(mDragView);
1810 onRemoveView(mDragView, false);
1811 addView(mDragView, pageUnderPointIndex);
1812 onAddView(mDragView, pageUnderPointIndex);
1813 mSidePageHoverIndex = -1;
Winson Chungd2be3812013-07-16 11:11:32 -07001814 mPageIndicator.setActiveMarker(getNextPage());
Adam Cohen7d30a372013-07-01 17:03:59 -07001815 }
1816 };
1817 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1818 }
1819 } else {
1820 removeCallbacks(mSidePageHoverRunnable);
1821 mSidePageHoverIndex = -1;
1822 }
Adam Cohen564976a2010-10-13 18:52:07 -07001823 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001824 determineScrollingStart(ev);
1825 }
1826 break;
1827
1828 case MotionEvent.ACTION_UP:
1829 if (mTouchState == TOUCH_STATE_SCROLLING) {
1830 final int activePointerId = mActivePointerId;
1831 final int pointerIndex = ev.findPointerIndex(activePointerId);
1832 final float x = ev.getX(pointerIndex);
1833 final VelocityTracker velocityTracker = mVelocityTracker;
1834 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1835 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001836 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001837 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001838 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1839 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001840
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001841 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1842
Adam Cohen00481b32011-11-18 12:03:48 -08001843 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001844 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001845
Adam Cohenf358a4b2013-07-23 16:47:31 -07001846 if (!mFreeScroll) {
1847 // In the case that the page is moved far to one direction and then is flung
1848 // in the opposite direction, we use a threshold to determine whether we should
1849 // just return to the starting page, or if we should skip one further.
1850 boolean returnToOriginalPage = false;
1851 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1852 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1853 returnToOriginalPage = true;
1854 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001855
Adam Cohenf358a4b2013-07-23 16:47:31 -07001856 int finalPage;
1857 // We give flings precedence over large moves, which is why we short-circuit our
1858 // test for a large move if a fling has been registered. That is, a large
1859 // move to the left and fling to the right will register as a fling to the right.
1860 final boolean isRtl = isLayoutRtl();
1861 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1862 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1863 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1864 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1865 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1866 snapToPageWithVelocity(finalPage, velocityX);
1867 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1868 (isFling && isVelocityXLeft)) &&
1869 mCurrentPage < getChildCount() - 1) {
1870 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1871 snapToPageWithVelocity(finalPage, velocityX);
1872 } else {
1873 snapToDestination();
1874 } } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1875 // at this point we have not moved beyond the touch slop
1876 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1877 // we can just page
1878 int nextPage = Math.max(0, mCurrentPage - 1);
1879 if (nextPage != mCurrentPage) {
1880 snapToPage(nextPage);
1881 } else {
1882 snapToDestination();
1883 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001884 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001885 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001886 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001887 }
1888
1889 float scaleX = getScaleX();
1890 int vX = (int) (-velocityX * scaleX);
1891 int initialScrollX = (int) (getScrollX() * scaleX);
1892
1893 mScroller.fling(initialScrollX,
1894 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1895 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001896 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001897 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001898 // at this point we have not moved beyond the touch slop
1899 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1900 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001901 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1902 if (nextPage != mCurrentPage) {
1903 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001904 } else {
1905 snapToDestination();
1906 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001907 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1908 // Update the last motion position
1909 mLastMotionX = ev.getX();
1910 mLastMotionY = ev.getY();
1911
1912 // Update the parent down so that our zoom animations take this new movement into
1913 // account
1914 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1915 mParentDownMotionX = pt[0];
1916 mParentDownMotionY = pt[1];
1917 updateDragViewTranslationDuringDrag();
1918 boolean handledFling = false;
1919 if (!DISABLE_FLING_TO_DELETE) {
1920 // Check the velocity and see if we are flinging-to-delete
1921 PointF flingToDeleteVector = isFlingingToDelete();
1922 if (flingToDeleteVector != null) {
1923 onFlingToDelete(flingToDeleteVector);
1924 handledFling = true;
1925 }
1926 }
1927 if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
1928 (int) mParentDownMotionY)) {
1929 onDropToDelete();
1930 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001931 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001932 if (!mCancelTap) {
1933 onUnhandledTap(ev);
1934 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001935 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001936
1937 // Remove the callback to wait for the side page hover timeout
1938 removeCallbacks(mSidePageHoverRunnable);
1939 // End any intermediate reordering states
1940 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001941 break;
1942
1943 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001944 if (mTouchState == TOUCH_STATE_SCROLLING) {
1945 snapToDestination();
1946 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001947 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001948 break;
1949
1950 case MotionEvent.ACTION_POINTER_UP:
1951 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001952 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001953 break;
1954 }
1955
1956 return true;
1957 }
1958
Adam Cohen7d30a372013-07-01 17:03:59 -07001959 public void onFlingToDelete(View v) {}
1960 public void onRemoveView(View v, boolean deletePermanently) {}
1961 public void onRemoveViewAnimationCompleted() {}
1962 public void onAddView(View v, int index) {}
1963
1964 private void resetTouchState() {
1965 releaseVelocityTracker();
1966 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001967 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001968 mTouchState = TOUCH_STATE_REST;
1969 mActivePointerId = INVALID_POINTER;
Adam Cohen7d30a372013-07-01 17:03:59 -07001970 }
1971
Adam Cohen1697b792013-09-17 19:08:21 -07001972 protected void onUnhandledTap(MotionEvent ev) {
1973 ((Launcher) getContext()).onClick(this);
1974 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001975
Winson Chung185d7162011-02-28 13:47:29 -08001976 @Override
1977 public boolean onGenericMotionEvent(MotionEvent event) {
1978 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1979 switch (event.getAction()) {
1980 case MotionEvent.ACTION_SCROLL: {
1981 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1982 final float vscroll;
1983 final float hscroll;
1984 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1985 vscroll = 0;
1986 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1987 } else {
1988 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1989 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1990 }
1991 if (hscroll != 0 || vscroll != 0) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001992 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
1993 : (hscroll > 0 || vscroll > 0);
1994 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001995 scrollRight();
1996 } else {
1997 scrollLeft();
1998 }
1999 return true;
2000 }
2001 }
2002 }
2003 }
2004 return super.onGenericMotionEvent(event);
2005 }
2006
Michael Jurkab8f06722010-10-10 15:58:46 -07002007 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
2008 if (mVelocityTracker == null) {
2009 mVelocityTracker = VelocityTracker.obtain();
2010 }
2011 mVelocityTracker.addMovement(ev);
2012 }
2013
2014 private void releaseVelocityTracker() {
2015 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002016 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07002017 mVelocityTracker.recycle();
2018 mVelocityTracker = null;
2019 }
2020 }
2021
Winson Chung321e9ee2010-08-09 13:37:56 -07002022 private void onSecondaryPointerUp(MotionEvent ev) {
2023 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
2024 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
2025 final int pointerId = ev.getPointerId(pointerIndex);
2026 if (pointerId == mActivePointerId) {
2027 // This was our active pointer going up. Choose a new
2028 // active pointer and adjust accordingly.
2029 // TODO: Make this decision more intelligent.
2030 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
2031 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
2032 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08002033 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07002034 mActivePointerId = ev.getPointerId(newPointerIndex);
2035 if (mVelocityTracker != null) {
2036 mVelocityTracker.clear();
2037 }
2038 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08002039 }
2040
Winson Chung321e9ee2010-08-09 13:37:56 -07002041 @Override
2042 public void requestChildFocus(View child, View focused) {
2043 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07002044 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07002045 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07002046 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07002047 }
2048 }
2049
Winson Chung1908d072011-02-24 18:09:44 -08002050 protected int getChildWidth(int index) {
Adam Cohen96d30a12013-07-16 18:13:21 -07002051 return getPageAt(index).getMeasuredWidth();
Winson Chung1908d072011-02-24 18:09:44 -08002052 }
2053
Adam Cohen7d30a372013-07-01 17:03:59 -07002054 int getPageNearestToPoint(float x) {
2055 int index = 0;
2056 for (int i = 0; i < getChildCount(); ++i) {
2057 if (x < getChildAt(i).getRight() - getScrollX()) {
2058 return index;
2059 } else {
2060 index++;
2061 }
2062 }
2063 return Math.min(index, getChildCount() - 1);
2064 }
2065
Adam Cohend19d3ca2010-09-15 14:43:42 -07002066 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07002067 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07002068 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07002069 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07002070 final int childCount = getChildCount();
2071 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002072 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07002073 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07002074 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07002075 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07002076 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
2077 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
2078 minDistanceFromScreenCenter = distanceFromScreenCenter;
2079 minDistanceFromScreenCenterIndex = i;
2080 }
2081 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07002082 return minDistanceFromScreenCenterIndex;
2083 }
2084
2085 protected void snapToDestination() {
2086 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002087 }
2088
Adam Cohene0f66b52010-11-23 15:06:07 -08002089 private static class ScrollInterpolator implements Interpolator {
2090 public ScrollInterpolator() {
2091 }
2092
2093 public float getInterpolation(float t) {
2094 t -= 1.0f;
2095 return t*t*t*t*t + 1;
2096 }
2097 }
2098
2099 // We want the duration of the page snap animation to be influenced by the distance that
2100 // the screen has to travel, however, we don't want this duration to be effected in a
2101 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
2102 // of travel has on the overall snap duration.
2103 float distanceInfluenceForSnapDuration(float f) {
2104 f -= 0.5f; // center the values about 0.
2105 f *= 0.3f * Math.PI / 2.0f;
2106 return (float) Math.sin(f);
2107 }
2108
Michael Jurka0142d492010-08-25 17:46:15 -07002109 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002110 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
Adam Cohen7d30a372013-07-01 17:03:59 -07002111 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002112
Adam Cohenedb40762013-07-18 16:45:45 -07002113 final int newX = getScrollForPage(whichPage);
Adam Cohene0f66b52010-11-23 15:06:07 -08002114 int delta = newX - mUnboundedScrollX;
2115 int duration = 0;
2116
Adam Cohen265b9a62011-12-07 14:37:18 -08002117 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002118 // If the velocity is low enough, then treat this more as an automatic page advance
2119 // as opposed to an apparent physical response to flinging
2120 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
2121 return;
2122 }
2123
2124 // Here we compute a "distance" that will be used in the computation of the overall
2125 // snap duration. This is a function of the actual distance that needs to be traveled;
2126 // we keep this value close to half screen size in order to reduce the variance in snap
2127 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002128 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002129 float distance = halfScreenSize + halfScreenSize *
2130 distanceInfluenceForSnapDuration(distanceRatio);
2131
2132 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002133 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002134
2135 // we want the page's snap velocity to approximately match the velocity at which the
2136 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002137 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2138 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002139
2140 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002141 }
2142
2143 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07002144 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002145 }
2146
Adam Cohen7d30a372013-07-01 17:03:59 -07002147 protected void snapToPageImmediately(int whichPage) {
2148 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true);
2149 }
2150
Michael Jurka0142d492010-08-25 17:46:15 -07002151 protected void snapToPage(int whichPage, int duration) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002152 snapToPage(whichPage, duration, false);
2153 }
2154
2155 protected void snapToPage(int whichPage, int duration, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -07002156 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07002157
Adam Cohenedb40762013-07-18 16:45:45 -07002158 int newX = getScrollForPage(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08002159 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07002160 final int delta = newX - sX;
Adam Cohen7d30a372013-07-01 17:03:59 -07002161 snapToPage(whichPage, delta, duration, immediate);
Michael Jurka0142d492010-08-25 17:46:15 -07002162 }
2163
2164 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohen7d30a372013-07-01 17:03:59 -07002165 snapToPage(whichPage, delta, duration, false);
2166 }
Michael Jurka0142d492010-08-25 17:46:15 -07002167
Adam Cohen7d30a372013-07-01 17:03:59 -07002168 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate) {
2169 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002170 View focusedChild = getFocusedChild();
2171 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07002172 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07002173 focusedChild.clearFocus();
2174 }
2175
Adam Cohen53805212013-10-01 10:39:23 -07002176 sendScrollAccessibilityEvent();
2177
Michael Jurka0142d492010-08-25 17:46:15 -07002178 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002179 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002180 if (immediate) {
2181 duration = 0;
2182 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002183 duration = Math.abs(delta);
2184 }
2185
Adam Cohenf358a4b2013-07-23 16:47:31 -07002186 if (!mScroller.isFinished()) {
2187 mScroller.abortAnimation();
2188 }
Adam Cohen68d73932010-11-15 10:50:58 -08002189 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002190
Michael Jurka0142d492010-08-25 17:46:15 -07002191 notifyPageSwitchListener();
Adam Cohen7d30a372013-07-01 17:03:59 -07002192
2193 // Trigger a compute() to finish switching pages if necessary
2194 if (immediate) {
2195 computeScroll();
2196 }
2197
Winson Chung9c0565f2013-07-19 13:49:06 -07002198 // Defer loading associated pages until the scroll settles
2199 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
2200
Adam Cohen7d30a372013-07-01 17:03:59 -07002201 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002202 invalidate();
2203 }
2204
Winson Chung321e9ee2010-08-09 13:37:56 -07002205 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002206 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002207 }
2208
2209 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002210 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002211 }
2212
Winson Chung86f77532010-08-24 11:08:22 -07002213 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002214 int result = -1;
2215 if (v != null) {
2216 ViewParent vp = v.getParent();
2217 int count = getChildCount();
2218 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07002219 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002220 return i;
2221 }
2222 }
2223 }
2224 return result;
2225 }
2226
2227 /**
2228 * @return True is long presses are still allowed for the current touch
2229 */
2230 public boolean allowLongPress() {
2231 return mAllowLongPress;
2232 }
2233
Adam Cohendbdff6b2013-09-18 19:09:15 -07002234 @Override
2235 public boolean performLongClick() {
2236 mCancelTap = true;
2237 return super.performLongClick();
2238 }
2239
Michael Jurka0142d492010-08-25 17:46:15 -07002240 /**
2241 * Set true to allow long-press events to be triggered, usually checked by
2242 * {@link Launcher} to accept or block dpad-initiated long-presses.
2243 */
2244 public void setAllowLongPress(boolean allowLongPress) {
2245 mAllowLongPress = allowLongPress;
2246 }
2247
Winson Chung321e9ee2010-08-09 13:37:56 -07002248 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002249 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002250
2251 SavedState(Parcelable superState) {
2252 super(superState);
2253 }
2254
2255 private SavedState(Parcel in) {
2256 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002257 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002258 }
2259
2260 @Override
2261 public void writeToParcel(Parcel out, int flags) {
2262 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002263 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002264 }
2265
2266 public static final Parcelable.Creator<SavedState> CREATOR =
2267 new Parcelable.Creator<SavedState>() {
2268 public SavedState createFromParcel(Parcel in) {
2269 return new SavedState(in);
2270 }
2271
2272 public SavedState[] newArray(int size) {
2273 return new SavedState[size];
2274 }
2275 };
2276 }
2277
Winson Chungf314b0e2011-08-16 11:54:27 -07002278 protected void loadAssociatedPages(int page) {
2279 loadAssociatedPages(page, false);
2280 }
2281 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07002282 if (mContentIsRefreshable) {
2283 final int count = getChildCount();
2284 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07002285 int lowerPageBound = getAssociatedLowerPageBound(page);
2286 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07002287 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
2288 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08002289 // First, clear any pages that should no longer be loaded
2290 for (int i = 0; i < count; ++i) {
2291 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002292 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08002293 if (layout.getPageChildCount() > 0) {
2294 layout.removeAllViewsOnPage();
2295 }
2296 mDirtyPageContent.set(i, true);
2297 }
2298 }
2299 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07002300 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002301 if ((i != page) && immediateAndOnly) {
2302 continue;
2303 }
Michael Jurka0142d492010-08-25 17:46:15 -07002304 if (lowerPageBound <= i && i <= upperPageBound) {
2305 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002306 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002307 mDirtyPageContent.set(i, false);
2308 }
Winson Chung86f77532010-08-24 11:08:22 -07002309 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002310 }
2311 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002312 }
2313 }
2314
Winson Chunge3193b92010-09-10 11:44:42 -07002315 protected int getAssociatedLowerPageBound(int page) {
2316 return Math.max(0, page - 1);
2317 }
2318 protected int getAssociatedUpperPageBound(int page) {
2319 final int count = getChildCount();
2320 return Math.min(page + 1, count - 1);
2321 }
2322
Winson Chung86f77532010-08-24 11:08:22 -07002323 /**
2324 * This method is called ONLY to synchronize the number of pages that the paged view has.
2325 * To actually fill the pages with information, implement syncPageItems() below. It is
2326 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
2327 * and therefore, individual page items do not need to be updated in this method.
2328 */
Winson Chung321e9ee2010-08-09 13:37:56 -07002329 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002330
2331 /**
2332 * This method is called to synchronize the items that are on a particular page. If views on
2333 * the page can be reused, then they should be updated within this method.
2334 */
Winson Chungf314b0e2011-08-16 11:54:27 -07002335 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07002336
Patrick Dubroy244d74c2011-05-19 16:48:48 -07002337 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07002338 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07002339 }
2340 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07002341 invalidatePageData(currentPage, false);
2342 }
2343 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002344 if (!mIsDataReady) {
2345 return;
2346 }
2347
Michael Jurka0142d492010-08-25 17:46:15 -07002348 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07002349 // Force all scrolling-related behavior to end
Adam Cohen4fe4c932013-10-28 16:02:34 -07002350 forceFinishScroller();
Adam Cohen0cd3b642011-10-14 14:58:00 -07002351
Michael Jurka0142d492010-08-25 17:46:15 -07002352 // Update all the pages
2353 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07002354
Winson Chung5a808352011-06-27 19:08:49 -07002355 // We must force a measure after we've loaded the pages to update the content width and
2356 // to determine the full scroll width
2357 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
2358 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
2359
2360 // Set a new page as the current page if necessary
2361 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07002362 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07002363 }
2364
Michael Jurka0142d492010-08-25 17:46:15 -07002365 // Mark each of the pages as dirty
2366 final int count = getChildCount();
2367 mDirtyPageContent.clear();
2368 for (int i = 0; i < count; ++i) {
2369 mDirtyPageContent.add(true);
2370 }
2371
2372 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07002373 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07002374 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07002375 }
Adam Cohen06559042013-09-29 18:30:56 -07002376 if (isPageMoving()) {
2377 // If the page is moving, then snap it to the final position to ensure we don't get
2378 // stuck between pages
2379 snapToDestination();
2380 }
Winson Chung321e9ee2010-08-09 13:37:56 -07002381 }
Winson Chung007c6982011-06-14 13:27:53 -07002382
Adam Cohen7d30a372013-07-01 17:03:59 -07002383 // Animate the drag view back to the original position
2384 void animateDragViewToOriginalPosition() {
2385 if (mDragView != null) {
2386 AnimatorSet anim = new AnimatorSet();
2387 anim.setDuration(REORDERING_DROP_REPOSITION_DURATION);
2388 anim.playTogether(
2389 ObjectAnimator.ofFloat(mDragView, "translationX", 0f),
Adam Cohenf358a4b2013-07-23 16:47:31 -07002390 ObjectAnimator.ofFloat(mDragView, "translationY", 0f),
2391 ObjectAnimator.ofFloat(mDragView, "scaleX", 1f),
2392 ObjectAnimator.ofFloat(mDragView, "scaleY", 1f));
Adam Cohen7d30a372013-07-01 17:03:59 -07002393 anim.addListener(new AnimatorListenerAdapter() {
2394 @Override
2395 public void onAnimationEnd(Animator animation) {
2396 onPostReorderingAnimationCompleted();
2397 }
2398 });
2399 anim.start();
2400 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002401 }
2402
Adam Cohen7d30a372013-07-01 17:03:59 -07002403 protected void onStartReordering() {
2404 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2405 mTouchState = TOUCH_STATE_REORDERING;
2406 mIsReordering = true;
2407
Adam Cohen7d30a372013-07-01 17:03:59 -07002408 // We must invalidate to trigger a redraw to update the layers such that the drag view
2409 // is always drawn on top
2410 invalidate();
2411 }
2412
2413 private void onPostReorderingAnimationCompleted() {
2414 // Trigger the callback when reordering has settled
2415 --mPostReorderingPreZoomInRemainingAnimationCount;
2416 if (mPostReorderingPreZoomInRunnable != null &&
2417 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2418 mPostReorderingPreZoomInRunnable.run();
2419 mPostReorderingPreZoomInRunnable = null;
2420 }
2421 }
2422
2423 protected void onEndReordering() {
2424 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002425 }
2426
Adam Cohenf358a4b2013-07-23 16:47:31 -07002427 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002428 int dragViewIndex = indexOfChild(v);
2429
2430 if (mTouchState != TOUCH_STATE_REST) return false;
2431
Adam Cohen7d30a372013-07-01 17:03:59 -07002432 mTempVisiblePagesRange[0] = 0;
2433 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002434 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002435 mReorderingStarted = true;
2436
2437 // Check if we are within the reordering range
2438 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 dragViewIndex <= mTempVisiblePagesRange[1]) {
2440 // Find the drag view under the pointer
2441 mDragView = getChildAt(dragViewIndex);
2442 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2443 mDragViewBaselineLeft = mDragView.getLeft();
2444 disableFreeScroll(-1);
2445 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002446 return true;
2447 }
2448 return false;
2449 }
2450
2451 boolean isReordering(boolean testTouchState) {
2452 boolean state = mIsReordering;
2453 if (testTouchState) {
2454 state &= (mTouchState == TOUCH_STATE_REORDERING);
2455 }
2456 return state;
2457 }
2458 void endReordering() {
2459 // For simplicity, we call endReordering sometimes even if reordering was never started.
2460 // In that case, we don't want to do anything.
2461 if (!mReorderingStarted) return;
2462 mReorderingStarted = false;
2463
2464 // If we haven't flung-to-delete the current child, then we just animate the drag view
2465 // back into position
2466 final Runnable onCompleteRunnable = new Runnable() {
2467 @Override
2468 public void run() {
2469 onEndReordering();
2470 }
2471 };
2472 if (!mDeferringForDelete) {
2473 mPostReorderingPreZoomInRunnable = new Runnable() {
2474 public void run() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002475 onCompleteRunnable.run();
2476 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002477 };
2478 };
2479
2480 mPostReorderingPreZoomInRemainingAnimationCount =
2481 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2482 // Snap to the current page
2483 snapToPage(indexOfChild(mDragView), 0);
2484 // Animate the drag view back to the front position
2485 animateDragViewToOriginalPosition();
2486 } else {
2487 // Handled in post-delete-animation-callbacks
2488 }
2489 }
2490
Adam Cohen7d30a372013-07-01 17:03:59 -07002491 /*
2492 * Flinging to delete - IN PROGRESS
2493 */
2494 private PointF isFlingingToDelete() {
2495 ViewConfiguration config = ViewConfiguration.get(getContext());
2496 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
2497
2498 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
2499 // Do a quick dot product test to ensure that we are flinging upwards
2500 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
2501 mVelocityTracker.getYVelocity());
2502 PointF upVec = new PointF(0f, -1f);
2503 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
2504 (vel.length() * upVec.length()));
2505 if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
2506 return vel;
2507 }
2508 }
2509 return null;
2510 }
2511
2512 /**
2513 * Creates an animation from the current drag view along its current velocity vector.
2514 * For this animation, the alpha runs for a fixed duration and we update the position
2515 * progressively.
2516 */
2517 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
2518 private View mDragView;
2519 private PointF mVelocity;
2520 private Rect mFrom;
2521 private long mPrevTime;
2522 private float mFriction;
2523
2524 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
2525
2526 public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from,
2527 long startTime, float friction) {
2528 mDragView = dragView;
2529 mVelocity = vel;
2530 mFrom = from;
2531 mPrevTime = startTime;
2532 mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction);
2533 }
2534
2535 @Override
2536 public void onAnimationUpdate(ValueAnimator animation) {
2537 float t = ((Float) animation.getAnimatedValue()).floatValue();
2538 long curTime = AnimationUtils.currentAnimationTimeMillis();
2539
2540 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
2541 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
2542
2543 mDragView.setTranslationX(mFrom.left);
2544 mDragView.setTranslationY(mFrom.top);
2545 mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
2546
2547 mVelocity.x *= mFriction;
2548 mVelocity.y *= mFriction;
2549 mPrevTime = curTime;
2550 }
2551 };
2552
2553 private static final int ANIM_TAG_KEY = 100;
2554
2555 private Runnable createPostDeleteAnimationRunnable(final View dragView) {
2556 return new Runnable() {
2557 @Override
2558 public void run() {
2559 int dragViewIndex = indexOfChild(dragView);
2560
2561 // For each of the pages around the drag view, animate them from the previous
2562 // position to the new position in the layout (as a result of the drag view moving
2563 // in the layout)
2564 // NOTE: We can make an assumption here because we have side-bound pages that we
2565 // will always have pages to animate in from the left
Adam Cohenf358a4b2013-07-23 16:47:31 -07002566 getOverviewModePages(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002567 boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]);
2568 boolean slideFromLeft = (isLastWidgetPage ||
2569 dragViewIndex > mTempVisiblePagesRange[0]);
2570
2571 // Setup the scroll to the correct page before we swap the views
2572 if (slideFromLeft) {
2573 snapToPageImmediately(dragViewIndex - 1);
2574 }
2575
2576 int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]);
2577 int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1);
2578 int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 );
2579 int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex);
2580 ArrayList<Animator> animations = new ArrayList<Animator>();
2581 for (int i = lowerIndex; i <= upperIndex; ++i) {
2582 View v = getChildAt(i);
2583 // dragViewIndex < pageUnderPointIndex, so after we remove the
2584 // drag view all subsequent views to pageUnderPointIndex will
2585 // shift down.
2586 int oldX = 0;
2587 int newX = 0;
2588 if (slideFromLeft) {
2589 if (i == 0) {
2590 // Simulate the page being offscreen with the page spacing
2591 oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i)
Adam Cohen3b185e22013-10-29 14:45:58 -07002592 - mPageSpacing;
Adam Cohen7d30a372013-07-01 17:03:59 -07002593 } else {
2594 oldX = getViewportOffsetX() + getChildOffset(i - 1);
2595 }
2596 newX = getViewportOffsetX() + getChildOffset(i);
2597 } else {
2598 oldX = getChildOffset(i) - getChildOffset(i - 1);
2599 newX = 0;
2600 }
2601
2602 // Animate the view translation from its old position to its new
2603 // position
2604 AnimatorSet anim = (AnimatorSet) v.getTag();
2605 if (anim != null) {
2606 anim.cancel();
2607 }
2608
2609 // Note: Hacky, but we want to skip any optimizations to not draw completely
2610 // hidden views
2611 v.setAlpha(Math.max(v.getAlpha(), 0.01f));
2612 v.setTranslationX(oldX - newX);
2613 anim = new AnimatorSet();
2614 anim.playTogether(
2615 ObjectAnimator.ofFloat(v, "translationX", 0f),
2616 ObjectAnimator.ofFloat(v, "alpha", 1f));
2617 animations.add(anim);
2618 v.setTag(ANIM_TAG_KEY, anim);
2619 }
2620
2621 AnimatorSet slideAnimations = new AnimatorSet();
2622 slideAnimations.playTogether(animations);
2623 slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION);
2624 slideAnimations.addListener(new AnimatorListenerAdapter() {
2625 @Override
2626 public void onAnimationEnd(Animator animation) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002627 mDeferringForDelete = false;
2628 onEndReordering();
2629 onRemoveViewAnimationCompleted();
Adam Cohen7d30a372013-07-01 17:03:59 -07002630 }
2631 });
2632 slideAnimations.start();
2633
2634 removeView(dragView);
2635 onRemoveView(dragView, true);
2636 }
2637 };
2638 }
2639
2640 public void onFlingToDelete(PointF vel) {
2641 final long startTime = AnimationUtils.currentAnimationTimeMillis();
2642
2643 // NOTE: Because it takes time for the first frame of animation to actually be
2644 // called and we expect the animation to be a continuation of the fling, we have
2645 // to account for the time that has elapsed since the fling finished. And since
2646 // we don't have a startDelay, we will always get call to update when we call
2647 // start() (which we want to ignore).
2648 final TimeInterpolator tInterpolator = new TimeInterpolator() {
2649 private int mCount = -1;
2650 private long mStartTime;
2651 private float mOffset;
2652 /* Anonymous inner class ctor */ {
2653 mStartTime = startTime;
2654 }
2655
2656 @Override
2657 public float getInterpolation(float t) {
2658 if (mCount < 0) {
2659 mCount++;
2660 } else if (mCount == 0) {
2661 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
2662 mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION);
2663 mCount++;
2664 }
2665 return Math.min(1f, mOffset + t);
2666 }
2667 };
2668
2669 final Rect from = new Rect();
2670 final View dragView = mDragView;
2671 from.left = (int) dragView.getTranslationX();
2672 from.top = (int) dragView.getTranslationY();
2673 AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel,
2674 from, startTime, FLING_TO_DELETE_FRICTION);
2675
2676 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2677
2678 // Create and start the animation
2679 ValueAnimator mDropAnim = new ValueAnimator();
2680 mDropAnim.setInterpolator(tInterpolator);
2681 mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION);
2682 mDropAnim.setFloatValues(0f, 1f);
2683 mDropAnim.addUpdateListener(updateCb);
2684 mDropAnim.addListener(new AnimatorListenerAdapter() {
2685 public void onAnimationEnd(Animator animation) {
2686 onAnimationEndRunnable.run();
2687 }
2688 });
2689 mDropAnim.start();
2690 mDeferringForDelete = true;
2691 }
2692
2693 /* Drag to delete */
2694 private boolean isHoveringOverDeleteDropTarget(int x, int y) {
2695 if (mDeleteDropTarget != null) {
2696 mAltTmpRect.set(0, 0, 0, 0);
2697 View parent = (View) mDeleteDropTarget.getParent();
2698 if (parent != null) {
2699 parent.getGlobalVisibleRect(mAltTmpRect);
2700 }
2701 mDeleteDropTarget.getGlobalVisibleRect(mTmpRect);
2702 mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top);
2703 return mTmpRect.contains(x, y);
2704 }
2705 return false;
2706 }
2707
2708 protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {}
2709
2710 private void onDropToDelete() {
2711 final View dragView = mDragView;
2712
2713 final float toScale = 0f;
2714 final float toAlpha = 0f;
2715
2716 // Create and start the complex animation
2717 ArrayList<Animator> animations = new ArrayList<Animator>();
2718 AnimatorSet motionAnim = new AnimatorSet();
2719 motionAnim.setInterpolator(new DecelerateInterpolator(2));
2720 motionAnim.playTogether(
2721 ObjectAnimator.ofFloat(dragView, "scaleX", toScale),
2722 ObjectAnimator.ofFloat(dragView, "scaleY", toScale));
2723 animations.add(motionAnim);
2724
2725 AnimatorSet alphaAnim = new AnimatorSet();
2726 alphaAnim.setInterpolator(new LinearInterpolator());
2727 alphaAnim.playTogether(
2728 ObjectAnimator.ofFloat(dragView, "alpha", toAlpha));
2729 animations.add(alphaAnim);
2730
2731 final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView);
2732
2733 AnimatorSet anim = new AnimatorSet();
2734 anim.playTogether(animations);
2735 anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION);
2736 anim.addListener(new AnimatorListenerAdapter() {
2737 public void onAnimationEnd(Animator animation) {
2738 onAnimationEndRunnable.run();
2739 }
2740 });
2741 anim.start();
2742
2743 mDeferringForDelete = true;
Winson Chung007c6982011-06-14 13:27:53 -07002744 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002745
2746 /* Accessibility */
2747 @Override
2748 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2749 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002750 info.setScrollable(getPageCount() > 1);
2751 if (getCurrentPage() < getPageCount() - 1) {
2752 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2753 }
2754 if (getCurrentPage() > 0) {
2755 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2756 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002757 }
2758
2759 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002760 public void sendAccessibilityEvent(int eventType) {
2761 // Don't let the view send real scroll events.
2762 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2763 super.sendAccessibilityEvent(eventType);
2764 }
2765 }
2766
2767 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002768 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2769 super.onInitializeAccessibilityEvent(event);
2770 event.setScrollable(true);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002771 }
2772
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002773 @Override
2774 public boolean performAccessibilityAction(int action, Bundle arguments) {
2775 if (super.performAccessibilityAction(action, arguments)) {
2776 return true;
2777 }
2778 switch (action) {
2779 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2780 if (getCurrentPage() < getPageCount() - 1) {
2781 scrollRight();
2782 return true;
2783 }
2784 } break;
2785 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2786 if (getCurrentPage() > 0) {
2787 scrollLeft();
2788 return true;
2789 }
2790 } break;
2791 }
2792 return false;
2793 }
2794
Adam Cohen0ffac432013-07-10 11:19:26 -07002795 protected String getCurrentPageDescription() {
2796 return String.format(getContext().getString(R.string.default_scroll_format),
2797 getNextPage() + 1, getChildCount());
2798 }
2799
Winson Chungd11265e2011-08-30 13:37:23 -07002800 @Override
2801 public boolean onHoverEvent(android.view.MotionEvent event) {
2802 return true;
2803 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002804}