blob: 26ea4a812c2e168fdfa56944ad2ac5cb6c9298f1 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
17package com.android.launcher2;
18
Winson Chung04998342011-01-05 13:54:43 -080019import java.util.ArrayList;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070020
Winson Chung228a0fa2011-01-26 22:14:13 -080021import android.animation.Animator;
22import android.animation.AnimatorInflater;
23import android.animation.AnimatorListenerAdapter;
24import android.animation.ObjectAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
28import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.os.Parcel;
30import android.os.Parcelable;
31import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080041import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070042import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung321e9ee2010-08-09 13:37:56 -070047/**
48 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070049 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070050 */
51public abstract class PagedView extends ViewGroup {
52 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070053 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Winson Chung86f77532010-08-24 11:08:22 -070055 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070056 private static final int MIN_LENGTH_FOR_FLING = 25;
57 // The min drag distance to trigger a page shift (regardless of velocity)
58 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Adam Cohene0f66b52010-11-23 15:06:07 -080060 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Adam Cohene0f66b52010-11-23 15:06:07 -080063 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
64 private static final int MINIMUM_SNAP_VELOCITY = 2200;
65 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080066 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080067
Michael Jurka0142d492010-08-25 17:46:15 -070068 // the velocity at which a fling gesture will cause us to snap to the next page
69 protected int mSnapVelocity = 500;
70
71 protected float mSmoothingTime;
72 protected float mTouchX;
73
74 protected boolean mFirstLayout = true;
75
76 protected int mCurrentPage;
77 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080078 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080079 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070081 private VelocityTracker mVelocityTracker;
82
83 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080084 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080085 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080086 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080087 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070088 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070089
Michael Jurka0142d492010-08-25 17:46:15 -070090 protected final static int TOUCH_STATE_REST = 0;
91 protected final static int TOUCH_STATE_SCROLLING = 1;
92 protected final static int TOUCH_STATE_PREV_PAGE = 2;
93 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070094 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka7426c422010-11-11 15:23:47 -0800102 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103 private int mPagingTouchSlop;
104 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800105 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700106 protected int mPageSpacing;
107 protected int mPageLayoutPaddingTop;
108 protected int mPageLayoutPaddingBottom;
109 protected int mPageLayoutPaddingLeft;
110 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700111 protected int mPageLayoutWidthGap;
112 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700113 protected int mCellCountX;
114 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700115 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800116 protected boolean mAllowOverScroll = true;
117 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Michael Jurka8c920dd2011-01-20 14:16:56 -0800119 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800120 protected float mLayoutScale = 1.0f;
121
Michael Jurka5f1c5092010-09-03 14:15:02 -0700122 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Michael Jurka5f1c5092010-09-03 14:15:02 -0700124 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Winson Chung86f77532010-08-24 11:08:22 -0700126 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Winson Chung86f77532010-08-24 11:08:22 -0700128 private ArrayList<Boolean> mDirtyPageContent;
129 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700131 // choice modes
132 protected static final int CHOICE_MODE_NONE = 0;
133 protected static final int CHOICE_MODE_SINGLE = 1;
134 // Multiple selection mode is not supported by all Launcher actions atm
135 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700136
Michael Jurkae17e19c2010-09-28 11:01:39 -0700137 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700138 private ActionMode mActionMode;
139
Winson Chung04998342011-01-05 13:54:43 -0800140 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
141 // AllApps and Customize, and allows them to share holographic icons for the application view
142 // (which is in both).
143 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 // If true, syncPages and syncPageItems will be called to refresh pages
146 protected boolean mContentIsRefreshable = true;
147
148 // If true, modify alpha of neighboring pages as user scrolls left/right
149 protected boolean mFadeInAdjacentScreens = true;
150
151 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
152 // to switch to a new page
153 protected boolean mUsePagingTouchSlop = true;
154
155 // If true, the subclass should directly update mScrollX itself in its computeScroll method
156 // (SmoothPagedView does this)
157 protected boolean mDeferScrollUpdate = false;
158
Patrick Dubroy1262e362010-10-06 15:49:50 -0700159 protected boolean mIsPageMoving = false;
160
Winson Chung86f77532010-08-24 11:08:22 -0700161 public interface PageSwitchListener {
162 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700163 }
164
Winson Chung321e9ee2010-08-09 13:37:56 -0700165 public PagedView(Context context) {
166 this(context, null);
167 }
168
169 public PagedView(Context context, AttributeSet attrs) {
170 this(context, attrs, 0);
171 }
172
173 public PagedView(Context context, AttributeSet attrs, int defStyle) {
174 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700175 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700176
Adam Cohen9c4949e2010-10-05 12:27:22 -0700177 TypedArray a = context.obtainStyledAttributes(attrs,
178 R.styleable.PagedView, defStyle, 0);
179 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
180 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800181 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700182 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800183 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700184 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800185 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700186 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800187 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700188 mPageLayoutWidthGap = a.getDimensionPixelSize(
189 R.styleable.PagedView_pageLayoutWidthGap, -1);
190 mPageLayoutHeightGap = a.getDimensionPixelSize(
191 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700192 a.recycle();
193
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700195 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 }
197
198 /**
199 * Initializes various states for this workspace.
200 */
Michael Jurka0142d492010-08-25 17:46:15 -0700201 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700202 mDirtyPageContent = new ArrayList<Boolean>();
203 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800204 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700205 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700206 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700207
208 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
209 mTouchSlop = configuration.getScaledTouchSlop();
210 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
211 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
212 }
213
Winson Chung86f77532010-08-24 11:08:22 -0700214 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
215 mPageSwitchListener = pageSwitchListener;
216 if (mPageSwitchListener != null) {
217 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
219 }
220
221 /**
Winson Chung86f77532010-08-24 11:08:22 -0700222 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 *
Winson Chung86f77532010-08-24 11:08:22 -0700224 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 */
Winson Chung86f77532010-08-24 11:08:22 -0700226 int getCurrentPage() {
227 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
Winson Chung86f77532010-08-24 11:08:22 -0700230 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 return getChildCount();
232 }
233
Winson Chung86f77532010-08-24 11:08:22 -0700234 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 return getChildAt(index);
236 }
237
238 int getScrollWidth() {
239 return getWidth();
240 }
241
Adam Cohenbe909342011-01-28 17:02:58 -0800242 public int getTouchState() {
243 return mTouchState;
244 }
245
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800247 * Updates the scroll of the current page immediately to its final scroll position. We use this
248 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
249 * the previous tab page.
250 */
251 protected void updateCurrentPageScroll() {
252 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
253 scrollTo(newX, 0);
254 mScroller.setFinalX(newX);
255 }
256
257 /**
Winson Chung86f77532010-08-24 11:08:22 -0700258 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 */
Winson Chung86f77532010-08-24 11:08:22 -0700260 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800261 if (!mScroller.isFinished()) {
262 mScroller.abortAnimation();
263 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800264 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
265 // the default
266 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800267 return;
268 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700269
Winson Chung86f77532010-08-24 11:08:22 -0700270 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800271 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700272 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800273 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 }
275
Michael Jurka0142d492010-08-25 17:46:15 -0700276 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700277 if (mPageSwitchListener != null) {
278 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 }
280 }
281
Michael Jurkace7e05f2011-02-01 22:02:35 -0800282 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700283 mIsPageMoving = true;
284 onPageBeginMoving();
285 }
286
Michael Jurkace7e05f2011-02-01 22:02:35 -0800287 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700288 onPageEndMoving();
289 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700290 }
291
292 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 protected void onPageBeginMoving() {
294 }
295
296 // a method that subclasses can override to add behavior
297 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700298 }
299
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 /**
Winson Chung86f77532010-08-24 11:08:22 -0700301 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 *
303 * @param l The listener used to respond to long clicks.
304 */
305 @Override
306 public void setOnLongClickListener(OnLongClickListener l) {
307 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700308 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700310 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 }
312 }
313
314 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800315 public void scrollBy(int x, int y) {
316 scrollTo(mUnboundedScrollX + x, mScrollY + y);
317 }
318
319 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700320 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800321 mUnboundedScrollX = x;
322
323 if (x < 0) {
324 super.scrollTo(0, y);
325 if (mAllowOverScroll) {
326 overScroll(x);
327 }
328 } else if (x > mMaxScrollX) {
329 super.scrollTo(mMaxScrollX, y);
330 if (mAllowOverScroll) {
331 overScroll(x - mMaxScrollX);
332 }
333 } else {
334 super.scrollTo(x, y);
335 }
336
Michael Jurka0142d492010-08-25 17:46:15 -0700337 mTouchX = x;
338 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
339 }
340
341 // we moved this functionality to a helper function so SmoothPagedView can reuse it
342 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700344 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700346 invalidate();
347 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700348 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700349 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700350 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700351 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700352 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800353 // We don't want to trigger a page end moving unless the page has settled
354 // and the user has stopped scrolling
355 if (mTouchState == TOUCH_STATE_REST) {
356 pageEndMoving();
357 }
Michael Jurka0142d492010-08-25 17:46:15 -0700358 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700359 }
Michael Jurka0142d492010-08-25 17:46:15 -0700360 return false;
361 }
362
363 @Override
364 public void computeScroll() {
365 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 }
367
368 @Override
369 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
370 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
371 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
372 if (widthMode != MeasureSpec.EXACTLY) {
373 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
374 }
375
Adam Lesinski6b879f02010-11-04 16:15:23 -0700376 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
377 * of the All apps view on XLarge displays to not take up more space then it needs. Width
378 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
379 * each page to have the same width.
380 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700382 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
383 int maxChildHeight = 0;
384
385 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700386
387 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700388 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 final int childCount = getChildCount();
390 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700391 // disallowing padding in paged view (just pass 0)
392 final View child = getChildAt(i);
393 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
394
395 int childWidthMode;
396 if (lp.width == LayoutParams.WRAP_CONTENT) {
397 childWidthMode = MeasureSpec.AT_MOST;
398 } else {
399 childWidthMode = MeasureSpec.EXACTLY;
400 }
401
402 int childHeightMode;
403 if (lp.height == LayoutParams.WRAP_CONTENT) {
404 childHeightMode = MeasureSpec.AT_MOST;
405 } else {
406 childHeightMode = MeasureSpec.EXACTLY;
407 }
408
409 final int childWidthMeasureSpec =
410 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
411 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700412 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700413
414 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700415 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
416 }
417
418 if (heightMode == MeasureSpec.AT_MOST) {
419 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700420 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800421 if (childCount > 0) {
422 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
423 } else {
424 mMaxScrollX = 0;
425 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700426
427 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
429
Michael Jurka8c920dd2011-01-20 14:16:56 -0800430 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800431 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
432 int delta = newX - mScrollX;
433
Michael Jurka8c920dd2011-01-20 14:16:56 -0800434 final int pageCount = getChildCount();
435 for (int i = 0; i < pageCount; i++) {
436 View page = (View) getChildAt(i);
437 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800438 }
439 setCurrentPage(newCurrentPage);
440 }
441
Michael Jurka8c920dd2011-01-20 14:16:56 -0800442 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
443 // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and
Michael Jurkad3ef3062010-11-23 16:23:58 -0800444 // tightens the layout accordingly
445 public void setLayoutScale(float childrenScale) {
446 mLayoutScale = childrenScale;
447
448 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
449 int childCount = getChildCount();
450 float childrenX[] = new float[childCount];
451 float childrenY[] = new float[childCount];
452 for (int i = 0; i < childCount; i++) {
453 final View child = getChildAt(i);
454 childrenX[i] = child.getX();
455 childrenY[i] = child.getY();
456 }
457 onLayout(false, mLeft, mTop, mRight, mBottom);
458 for (int i = 0; i < childCount; i++) {
459 final View child = getChildAt(i);
460 child.setX(childrenX[i]);
461 child.setY(childrenY[i]);
462 }
463 // Also, the page offset has changed (since the pages are now smaller);
464 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800465 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800466 }
467
Winson Chung321e9ee2010-08-09 13:37:56 -0700468 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700469 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700470 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
471 setHorizontalScrollBarEnabled(false);
472 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
473 scrollTo(newX, 0);
474 mScroller.setFinalX(newX);
475 setHorizontalScrollBarEnabled(true);
476 mFirstLayout = false;
477 }
478
Adam Lesinski6b879f02010-11-04 16:15:23 -0700479 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 final int childCount = getChildCount();
481 int childLeft = 0;
482 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700483 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 }
485
486 for (int i = 0; i < childCount; i++) {
487 final View child = getChildAt(i);
488 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800489 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 final int childHeight = child.getMeasuredHeight();
491 int childTop = mPaddingTop;
492 if (mCenterPagesVertically) {
493 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
494 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800495
Adam Lesinski6b879f02010-11-04 16:15:23 -0700496 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800497 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700498 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 }
500 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800501 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
502 mFirstLayout = false;
503 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700504 }
505
Winson Chung03929772011-02-23 17:07:10 -0800506 protected void forceUpdateAdjacentPagesAlpha() {
507 mDirtyPageAlpha = true;
508 updateAdjacentPagesAlpha();
509 }
510
Winson Chunge3193b92010-09-10 11:44:42 -0700511 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700512 if (mFadeInAdjacentScreens) {
513 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700514 int halfScreenSize = getMeasuredWidth() / 2;
515 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700516 final int childCount = getChildCount();
517 for (int i = 0; i < childCount; ++i) {
518 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800519 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700520 int halfChildWidth = (childWidth / 2);
521 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700522
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700523 // On the first layout, we may not have a width nor a proper offset, so for now
524 // we should just assume full page width (and calculate the offset according to
525 // that).
526 if (childWidth <= 0) {
527 childWidth = getMeasuredWidth();
528 childCenter = (i * childWidth) + (childWidth / 2);
529 }
530
Winson Chunge8878e32010-09-15 20:37:09 -0700531 int d = halfChildWidth;
532 int distanceFromScreenCenter = childCenter - screenCenter;
533 if (distanceFromScreenCenter > 0) {
534 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800535 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700536 }
Michael Jurka0142d492010-08-25 17:46:15 -0700537 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700538 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700540 }
Michael Jurka0142d492010-08-25 17:46:15 -0700541 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700542 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700543
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700544 // Preventing potential divide-by-zero
545 d = Math.max(1, d);
546
Winson Chunge8878e32010-09-15 20:37:09 -0700547 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
548 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
549 float alpha = 1.0f - dimAlpha;
550
Adam Cohenf34bab52010-09-30 14:11:56 -0700551 if (alpha < ALPHA_QUANTIZE_LEVEL) {
552 alpha = 0.0f;
553 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
554 alpha = 1.0f;
555 }
556
Michael Jurkaa40829a2011-02-16 18:02:45 -0800557 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
558 // this optimization causes alpha to not be properly updated sometimes (repro
559 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
560 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
561 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800562 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700563 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800564 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 }
Michael Jurka0142d492010-08-25 17:46:15 -0700566 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 }
568 }
Winson Chunge3193b92010-09-10 11:44:42 -0700569 }
570
Adam Cohenf34bab52010-09-30 14:11:56 -0700571 protected void screenScrolled(int screenCenter) {
572 }
573
Winson Chunge3193b92010-09-10 11:44:42 -0700574 @Override
575 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700576 int halfScreenSize = getMeasuredWidth() / 2;
577 int screenCenter = mScrollX + halfScreenSize;
578
579 if (screenCenter != mLastScreenCenter) {
580 screenScrolled(screenCenter);
581 updateAdjacentPagesAlpha();
582 mLastScreenCenter = screenCenter;
583 }
Michael Jurka0142d492010-08-25 17:46:15 -0700584
585 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700586 // As an optimization, this code assumes that all pages have the same width as the 0th
587 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700588 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700589 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700591 final int screenWidth = getMeasuredWidth();
592 int x = getRelativeChildOffset(0) + pageWidth;
593 int leftScreen = 0;
594 int rightScreen = 0;
595 while (x <= mScrollX) {
596 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800597 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700598 }
599 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800600 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700601 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800602 if (rightScreen < pageCount) {
603 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
604 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700605 }
606 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700607
Michael Jurkac4fb9172010-09-02 17:19:20 -0700608 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800609 // Clip to the bounds
610 canvas.save();
611 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
612 mScrollY + mBottom - mTop);
613
Michael Jurkac4fb9172010-09-02 17:19:20 -0700614 for (int i = leftScreen; i <= rightScreen; i++) {
615 drawChild(canvas, getChildAt(i), drawingTime);
616 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800617 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700618 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700619 }
620
621 @Override
622 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700623 int page = indexOfChild(child);
624 if (page != mCurrentPage || !mScroller.isFinished()) {
625 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 return true;
627 }
628 return false;
629 }
630
631 @Override
632 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700633 int focusablePage;
634 if (mNextPage != INVALID_PAGE) {
635 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700637 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
Winson Chung86f77532010-08-24 11:08:22 -0700639 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 if (v != null) {
641 v.requestFocus(direction, previouslyFocusedRect);
642 }
643 return false;
644 }
645
646 @Override
647 public boolean dispatchUnhandledMove(View focused, int direction) {
648 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700649 if (getCurrentPage() > 0) {
650 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 return true;
652 }
653 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700654 if (getCurrentPage() < getPageCount() - 1) {
655 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 return true;
657 }
658 }
659 return super.dispatchUnhandledMove(focused, direction);
660 }
661
662 @Override
663 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700664 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
665 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700666 }
667 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700668 if (mCurrentPage > 0) {
669 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 }
671 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700672 if (mCurrentPage < getPageCount() - 1) {
673 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700674 }
675 }
676 }
677
678 /**
679 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700680 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 *
Winson Chung86f77532010-08-24 11:08:22 -0700682 * This happens when live folders requery, and if they're off page, they
683 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700684 */
685 @Override
686 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700687 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 View v = focused;
689 while (true) {
690 if (v == current) {
691 super.focusableViewAvailable(focused);
692 return;
693 }
694 if (v == this) {
695 return;
696 }
697 ViewParent parent = v.getParent();
698 if (parent instanceof View) {
699 v = (View)v.getParent();
700 } else {
701 return;
702 }
703 }
704 }
705
706 /**
707 * {@inheritDoc}
708 */
709 @Override
710 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
711 if (disallowIntercept) {
712 // We need to make sure to cancel our long press if
713 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700714 final View currentPage = getChildAt(mCurrentPage);
715 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 }
717 super.requestDisallowInterceptTouchEvent(disallowIntercept);
718 }
719
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800720 /**
721 * Return true if a tap at (x, y) should trigger a flip to the previous page.
722 */
723 protected boolean hitsPreviousPage(float x, float y) {
724 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
725 }
726
727 /**
728 * Return true if a tap at (x, y) should trigger a flip to the next page.
729 */
730 protected boolean hitsNextPage(float x, float y) {
731 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
732 }
733
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 @Override
735 public boolean onInterceptTouchEvent(MotionEvent ev) {
736 /*
737 * This method JUST determines whether we want to intercept the motion.
738 * If we return true, onTouchEvent will be called and we do the actual
739 * scrolling there.
740 */
741
Winson Chung45e1d6e2010-11-09 17:19:49 -0800742 // Skip touch handling if there are no pages to swipe
743 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
744
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 /*
746 * Shortcut the most recurring case: the user is in the dragging
747 * state and he is moving his finger. We want to intercept this
748 * motion.
749 */
750 final int action = ev.getAction();
751 if ((action == MotionEvent.ACTION_MOVE) &&
752 (mTouchState == TOUCH_STATE_SCROLLING)) {
753 return true;
754 }
755
Winson Chung321e9ee2010-08-09 13:37:56 -0700756 switch (action & MotionEvent.ACTION_MASK) {
757 case MotionEvent.ACTION_MOVE: {
758 /*
759 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
760 * whether the user has moved far enough from his original down touch.
761 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700762 if (mActivePointerId != INVALID_POINTER) {
763 determineScrollingStart(ev);
764 break;
765 }
766 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
767 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
768 // i.e. fall through to the next case (don't break)
769 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
770 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 }
772
773 case MotionEvent.ACTION_DOWN: {
774 final float x = ev.getX();
775 final float y = ev.getY();
776 // Remember location of down touch
777 mDownMotionX = x;
778 mLastMotionX = x;
779 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800780 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800781 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 mActivePointerId = ev.getPointerId(0);
783 mAllowLongPress = true;
784
785 /*
786 * If being flinged and user touches the screen, initiate drag;
787 * otherwise don't. mScroller.isFinished should be false when
788 * being flinged.
789 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700790 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700791 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
792 if (finishedScrolling) {
793 mTouchState = TOUCH_STATE_REST;
794 mScroller.abortAnimation();
795 } else {
796 mTouchState = TOUCH_STATE_SCROLLING;
797 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700798
Winson Chung86f77532010-08-24 11:08:22 -0700799 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800801 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800803 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800805 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 mTouchState = TOUCH_STATE_NEXT_PAGE;
807 }
808 }
809 }
810 break;
811 }
812
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800814 onWallpaperTap(ev);
815 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 mTouchState = TOUCH_STATE_REST;
817 mAllowLongPress = false;
818 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 break;
820
821 case MotionEvent.ACTION_POINTER_UP:
822 onSecondaryPointerUp(ev);
823 break;
824 }
825
826 /*
827 * The only time we want to intercept motion events is if we are in the
828 * drag mode.
829 */
830 return mTouchState != TOUCH_STATE_REST;
831 }
832
Winson Chung80baf5a2010-08-09 16:03:15 -0700833 protected void animateClickFeedback(View v, final Runnable r) {
834 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800835 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
836 loadAnimator(mContext, R.anim.paged_view_click_feedback);
837 anim.setTarget(v);
838 anim.addListener(new AnimatorListenerAdapter() {
839 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700840 r.run();
841 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700842 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800843 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700844 }
845
Adam Cohenf8d28232011-02-01 21:47:00 -0800846 protected void determineScrollingStart(MotionEvent ev) {
847 determineScrollingStart(ev, 1.0f);
848 }
849
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 /*
851 * Determines if we should change the touch state to start scrolling after the
852 * user moves their touch point too far.
853 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800854 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 /*
856 * Locally do absolute value. mLastMotionX is set to the y value
857 * of the down event.
858 */
859 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
860 final float x = ev.getX(pointerIndex);
861 final float y = ev.getY(pointerIndex);
862 final int xDiff = (int) Math.abs(x - mLastMotionX);
863 final int yDiff = (int) Math.abs(y - mLastMotionY);
864
Adam Cohenf8d28232011-02-01 21:47:00 -0800865 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 boolean xPaged = xDiff > mPagingTouchSlop;
867 boolean xMoved = xDiff > touchSlop;
868 boolean yMoved = yDiff > touchSlop;
869
Adam Cohenf8d28232011-02-01 21:47:00 -0800870 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700871 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 // Scroll if the user moved far enough along the X axis
873 mTouchState = TOUCH_STATE_SCROLLING;
874 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800875 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700876 mTouchX = mScrollX;
877 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
878 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 }
880 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800881 cancelCurrentPageLongPress();
882 }
883 }
884
885 protected void cancelCurrentPageLongPress() {
886 if (mAllowLongPress) {
887 mAllowLongPress = false;
888 // Try canceling the long press. It could also have been scheduled
889 // by a distant descendant, so use the mAllowLongPress flag to block
890 // everything
891 final View currentPage = getPageAt(mCurrentPage);
892 if (currentPage != null) {
893 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 }
895 }
896 }
897
Adam Cohene0f66b52010-11-23 15:06:07 -0800898 // This curve determines how the effect of scrolling over the limits of the page dimishes
899 // as the user pulls further and further from the bounds
900 private float overScrollInfluenceCurve(float f) {
901 f -= 1.0f;
902 return f * f * f + 1.0f;
903 }
904
Adam Cohen68d73932010-11-15 10:50:58 -0800905 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800906 int screenSize = getMeasuredWidth();
907
908 float f = (amount / screenSize);
909
910 if (f == 0) return;
911 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
912
Adam Cohen7bfc9792011-01-28 13:52:37 -0800913 // Clamp this factor, f, to -1 < f < 1
914 if (Math.abs(f) >= 1) {
915 f /= Math.abs(f);
916 }
917
Adam Cohene0f66b52010-11-23 15:06:07 -0800918 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800919 if (amount < 0) {
920 mScrollX = overScrollAmount;
921 } else {
922 mScrollX = mMaxScrollX + overScrollAmount;
923 }
924 invalidate();
925 }
926
Michael Jurkac5b262c2011-01-12 20:24:50 -0800927 protected float maxOverScroll() {
928 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
929 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
930 float f = 1.0f;
931 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
932 return OVERSCROLL_DAMP_FACTOR * f;
933 }
934
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 @Override
936 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800937 // Skip touch handling if there are no pages to swipe
938 if (getChildCount() <= 0) return super.onTouchEvent(ev);
939
Michael Jurkab8f06722010-10-10 15:58:46 -0700940 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700941
942 final int action = ev.getAction();
943
944 switch (action & MotionEvent.ACTION_MASK) {
945 case MotionEvent.ACTION_DOWN:
946 /*
947 * If being flinged and user touches, stop the fling. isFinished
948 * will be false if being flinged.
949 */
950 if (!mScroller.isFinished()) {
951 mScroller.abortAnimation();
952 }
953
954 // Remember where the motion event started
955 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800956 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800957 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700959 if (mTouchState == TOUCH_STATE_SCROLLING) {
960 pageBeginMoving();
961 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 break;
963
964 case MotionEvent.ACTION_MOVE:
965 if (mTouchState == TOUCH_STATE_SCROLLING) {
966 // Scroll to follow the motion event
967 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
968 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800969 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700970
Adam Cohenaefd4e12011-02-14 16:39:38 -0800971 mTotalMotionX += Math.abs(deltaX);
972
Winson Chungc0844aa2011-02-02 15:25:58 -0800973 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
974 // keep the remainder because we are actually testing if we've moved from the last
975 // scrolled position (which is discrete).
976 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800977 mTouchX += deltaX;
978 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
979 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800980 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800981 } else {
982 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800984 mLastMotionX = x;
985 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 } else {
987 awakenScrollBars();
988 }
Adam Cohen564976a2010-10-13 18:52:07 -0700989 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 determineScrollingStart(ev);
991 }
992 break;
993
994 case MotionEvent.ACTION_UP:
995 if (mTouchState == TOUCH_STATE_SCROLLING) {
996 final int activePointerId = mActivePointerId;
997 final int pointerIndex = ev.findPointerIndex(activePointerId);
998 final float x = ev.getX(pointerIndex);
999 final VelocityTracker velocityTracker = mVelocityTracker;
1000 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1001 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001002 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001003 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001004 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001005
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001006 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1007
Adam Cohenaefd4e12011-02-14 16:39:38 -08001008 // In the case that the page is moved far to one direction and then is flung
1009 // in the opposite direction, we use a threshold to determine whether we should
1010 // just return to the starting page, or if we should skip one further.
1011 boolean returnToOriginalPage = false;
1012 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001013 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001014 Math.signum(velocityX) != Math.signum(deltaX)) {
1015 returnToOriginalPage = true;
1016 }
1017
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001018 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001019 Math.abs(velocityX) > snapVelocity;
1020
1021 int finalPage;
1022 // We give flings precedence over large moves, which is why we short-circuit our
1023 // test for a large move if a fling has been registered. That is, a large
1024 // move to the left and fling to the right will register as a fling to the right.
1025 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1026 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1027 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1028 snapToPageWithVelocity(finalPage, velocityX);
1029 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1030 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001031 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001032 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1033 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 } else {
1035 snapToDestination();
1036 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001037 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 // at this point we have not moved beyond the touch slop
1039 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1040 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001041 int nextPage = Math.max(0, mCurrentPage - 1);
1042 if (nextPage != mCurrentPage) {
1043 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 } else {
1045 snapToDestination();
1046 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001047 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 // at this point we have not moved beyond the touch slop
1049 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1050 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001051 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1052 if (nextPage != mCurrentPage) {
1053 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001054 } else {
1055 snapToDestination();
1056 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001057 } else {
1058 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 }
1060 mTouchState = TOUCH_STATE_REST;
1061 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001062 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 break;
1064
1065 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001066 if (mTouchState == TOUCH_STATE_SCROLLING) {
1067 snapToDestination();
1068 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 mTouchState = TOUCH_STATE_REST;
1070 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001071 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 break;
1073
1074 case MotionEvent.ACTION_POINTER_UP:
1075 onSecondaryPointerUp(ev);
1076 break;
1077 }
1078
1079 return true;
1080 }
1081
Winson Chung185d7162011-02-28 13:47:29 -08001082 @Override
1083 public boolean onGenericMotionEvent(MotionEvent event) {
1084 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1085 switch (event.getAction()) {
1086 case MotionEvent.ACTION_SCROLL: {
1087 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1088 final float vscroll;
1089 final float hscroll;
1090 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1091 vscroll = 0;
1092 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1093 } else {
1094 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1095 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1096 }
1097 if (hscroll != 0 || vscroll != 0) {
1098 if (hscroll > 0 || vscroll > 0) {
1099 scrollRight();
1100 } else {
1101 scrollLeft();
1102 }
1103 return true;
1104 }
1105 }
1106 }
1107 }
1108 return super.onGenericMotionEvent(event);
1109 }
1110
Michael Jurkab8f06722010-10-10 15:58:46 -07001111 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1112 if (mVelocityTracker == null) {
1113 mVelocityTracker = VelocityTracker.obtain();
1114 }
1115 mVelocityTracker.addMovement(ev);
1116 }
1117
1118 private void releaseVelocityTracker() {
1119 if (mVelocityTracker != null) {
1120 mVelocityTracker.recycle();
1121 mVelocityTracker = null;
1122 }
1123 }
1124
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 private void onSecondaryPointerUp(MotionEvent ev) {
1126 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1127 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1128 final int pointerId = ev.getPointerId(pointerIndex);
1129 if (pointerId == mActivePointerId) {
1130 // This was our active pointer going up. Choose a new
1131 // active pointer and adjust accordingly.
1132 // TODO: Make this decision more intelligent.
1133 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1134 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1135 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001136 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 mActivePointerId = ev.getPointerId(newPointerIndex);
1138 if (mVelocityTracker != null) {
1139 mVelocityTracker.clear();
1140 }
1141 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001142 if (mTouchState == TOUCH_STATE_REST) {
1143 onWallpaperTap(ev);
1144 }
1145 }
1146
1147 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 }
1149
1150 @Override
1151 public void requestChildFocus(View child, View focused) {
1152 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001153 int page = indexOfChild(child);
1154 if (page >= 0 && !isInTouchMode()) {
1155 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 }
1157 }
1158
Winson Chunge3193b92010-09-10 11:44:42 -07001159 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1160 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001161 int left;
1162 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001163 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001164 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001165 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001166 if (left <= relativeOffset && relativeOffset <= right) {
1167 return i;
1168 }
Winson Chunge3193b92010-09-10 11:44:42 -07001169 }
1170 return -1;
1171 }
1172
Winson Chung1908d072011-02-24 18:09:44 -08001173 protected void setMinimumWidthOverride(int minimumWidth) {
1174 mMinimumWidth = minimumWidth;
1175 }
1176
1177 protected int getChildWidth(int index) {
1178 return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth());
1179 }
1180
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001182 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 }
1184
1185 protected int getChildOffset(int index) {
1186 if (getChildCount() == 0)
1187 return 0;
1188
1189 int offset = getRelativeChildOffset(0);
1190 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001191 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 }
1193 return offset;
1194 }
1195
Michael Jurkad3ef3062010-11-23 16:23:58 -08001196 protected int getScaledMeasuredWidth(View child) {
Winson Chung1908d072011-02-24 18:09:44 -08001197 return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001198 }
1199
Adam Cohend19d3ca2010-09-15 14:43:42 -07001200 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 int minDistanceFromScreenCenter = getMeasuredWidth();
1202 int minDistanceFromScreenCenterIndex = -1;
1203 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1204 final int childCount = getChildCount();
1205 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001206 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001207 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 int halfChildWidth = (childWidth / 2);
1209 int childCenter = getChildOffset(i) + halfChildWidth;
1210 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1211 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1212 minDistanceFromScreenCenter = distanceFromScreenCenter;
1213 minDistanceFromScreenCenterIndex = i;
1214 }
1215 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001216 return minDistanceFromScreenCenterIndex;
1217 }
1218
1219 protected void snapToDestination() {
1220 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 }
1222
Adam Cohene0f66b52010-11-23 15:06:07 -08001223 private static class ScrollInterpolator implements Interpolator {
1224 public ScrollInterpolator() {
1225 }
1226
1227 public float getInterpolation(float t) {
1228 t -= 1.0f;
1229 return t*t*t*t*t + 1;
1230 }
1231 }
1232
1233 // We want the duration of the page snap animation to be influenced by the distance that
1234 // the screen has to travel, however, we don't want this duration to be effected in a
1235 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1236 // of travel has on the overall snap duration.
1237 float distanceInfluenceForSnapDuration(float f) {
1238 f -= 0.5f; // center the values about 0.
1239 f *= 0.3f * Math.PI / 2.0f;
1240 return (float) Math.sin(f);
1241 }
1242
Michael Jurka0142d492010-08-25 17:46:15 -07001243 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001244 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1245 int halfScreenSize = getMeasuredWidth() / 2;
1246
1247 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1248 int delta = newX - mUnboundedScrollX;
1249 int duration = 0;
1250
1251 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1252 // If the velocity is low enough, then treat this more as an automatic page advance
1253 // as opposed to an apparent physical response to flinging
1254 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1255 return;
1256 }
1257
1258 // Here we compute a "distance" that will be used in the computation of the overall
1259 // snap duration. This is a function of the actual distance that needs to be traveled;
1260 // we keep this value close to half screen size in order to reduce the variance in snap
1261 // duration as a function of the distance the page needs to travel.
1262 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1263 float distance = halfScreenSize + halfScreenSize *
1264 distanceInfluenceForSnapDuration(distanceRatio);
1265
1266 velocity = Math.abs(velocity);
1267 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1268
1269 // we want the page's snap velocity to approximately match the velocity at which the
1270 // user flings, so we scale the duration by a value near to the derivative of the scroll
1271 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1272 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1273
1274 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001275 }
1276
1277 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001278 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 }
1280
Michael Jurka0142d492010-08-25 17:46:15 -07001281 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001282 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001283
Winson Chung86f77532010-08-24 11:08:22 -07001284 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001285 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001287 snapToPage(whichPage, delta, duration);
1288 }
1289
1290 protected void snapToPage(int whichPage, int delta, int duration) {
1291 mNextPage = whichPage;
1292
1293 View focusedChild = getFocusedChild();
1294 if (focusedChild != null && whichPage != mCurrentPage &&
1295 focusedChild == getChildAt(mCurrentPage)) {
1296 focusedChild.clearFocus();
1297 }
1298
1299 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 awakenScrollBars(duration);
1301 if (duration == 0) {
1302 duration = Math.abs(delta);
1303 }
1304
1305 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001306 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001307
1308 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001309 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001310 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 invalidate();
1312 }
1313
Winson Chung321e9ee2010-08-09 13:37:56 -07001314 public void scrollLeft() {
1315 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001316 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001317 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001318 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001319 }
1320 }
1321
1322 public void scrollRight() {
1323 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001324 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001326 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001327 }
1328 }
1329
Winson Chung86f77532010-08-24 11:08:22 -07001330 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 int result = -1;
1332 if (v != null) {
1333 ViewParent vp = v.getParent();
1334 int count = getChildCount();
1335 for (int i = 0; i < count; i++) {
1336 if (vp == getChildAt(i)) {
1337 return i;
1338 }
1339 }
1340 }
1341 return result;
1342 }
1343
1344 /**
1345 * @return True is long presses are still allowed for the current touch
1346 */
1347 public boolean allowLongPress() {
1348 return mAllowLongPress;
1349 }
1350
Michael Jurka0142d492010-08-25 17:46:15 -07001351 /**
1352 * Set true to allow long-press events to be triggered, usually checked by
1353 * {@link Launcher} to accept or block dpad-initiated long-presses.
1354 */
1355 public void setAllowLongPress(boolean allowLongPress) {
1356 mAllowLongPress = allowLongPress;
1357 }
1358
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001360 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001361
1362 SavedState(Parcelable superState) {
1363 super(superState);
1364 }
1365
1366 private SavedState(Parcel in) {
1367 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001368 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 }
1370
1371 @Override
1372 public void writeToParcel(Parcel out, int flags) {
1373 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001374 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001375 }
1376
1377 public static final Parcelable.Creator<SavedState> CREATOR =
1378 new Parcelable.Creator<SavedState>() {
1379 public SavedState createFromParcel(Parcel in) {
1380 return new SavedState(in);
1381 }
1382
1383 public SavedState[] newArray(int size) {
1384 return new SavedState[size];
1385 }
1386 };
1387 }
1388
Winson Chung86f77532010-08-24 11:08:22 -07001389 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001390 if (mContentIsRefreshable) {
1391 final int count = getChildCount();
1392 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001393 int lowerPageBound = getAssociatedLowerPageBound(page);
1394 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001395 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001396 Page layout = (Page) getChildAt(i);
1397 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001398 if (lowerPageBound <= i && i <= upperPageBound) {
1399 if (mDirtyPageContent.get(i)) {
1400 syncPageItems(i);
1401 mDirtyPageContent.set(i, false);
1402 }
1403 } else {
1404 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001405 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001406 }
1407 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001408 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001409 }
1410 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001411 }
1412 }
1413
Winson Chunge3193b92010-09-10 11:44:42 -07001414 protected int getAssociatedLowerPageBound(int page) {
1415 return Math.max(0, page - 1);
1416 }
1417 protected int getAssociatedUpperPageBound(int page) {
1418 final int count = getChildCount();
1419 return Math.min(page + 1, count - 1);
1420 }
1421
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001422 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001423 if (isChoiceMode(CHOICE_MODE_NONE)) {
1424 mChoiceMode = mode;
1425 mActionMode = startActionMode(callback);
1426 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001427 }
1428
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001429 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001430 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001431 mChoiceMode = CHOICE_MODE_NONE;
1432 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001433 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001434 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001435 }
1436 }
1437
1438 protected boolean isChoiceMode(int mode) {
1439 return mChoiceMode == mode;
1440 }
1441
1442 protected ArrayList<Checkable> getCheckedGrandchildren() {
1443 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1444 final int childCount = getChildCount();
1445 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001446 Page layout = (Page) getChildAt(i);
1447 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001448 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001449 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001450 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001451 checked.add((Checkable) v);
1452 }
1453 }
1454 }
1455 return checked;
1456 }
1457
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001458 /**
1459 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1460 * Otherwise, returns null.
1461 */
1462 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001463 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001464 final int childCount = getChildCount();
1465 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001466 Page layout = (Page) getChildAt(i);
1467 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001468 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001469 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001470 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1471 return (Checkable) v;
1472 }
1473 }
1474 }
1475 }
1476 return null;
1477 }
1478
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001479 protected void resetCheckedGrandchildren() {
1480 // loop through children, and set all of their children to _not_ be checked
1481 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1482 for (int i = 0; i < checked.size(); ++i) {
1483 final Checkable c = checked.get(i);
1484 c.setChecked(false);
1485 }
1486 }
1487
Winson Chunga12a2502010-12-20 14:41:35 -08001488 public void setRestorePage(int restorePage) {
1489 mRestorePage = restorePage;
1490 }
1491
Winson Chung86f77532010-08-24 11:08:22 -07001492 /**
1493 * This method is called ONLY to synchronize the number of pages that the paged view has.
1494 * To actually fill the pages with information, implement syncPageItems() below. It is
1495 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1496 * and therefore, individual page items do not need to be updated in this method.
1497 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001499
1500 /**
1501 * This method is called to synchronize the items that are on a particular page. If views on
1502 * the page can be reused, then they should be updated within this method.
1503 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001505
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001507 if (mContentIsRefreshable) {
1508 // Update all the pages
1509 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001510
Michael Jurka0142d492010-08-25 17:46:15 -07001511 // Mark each of the pages as dirty
1512 final int count = getChildCount();
1513 mDirtyPageContent.clear();
1514 for (int i = 0; i < count; ++i) {
1515 mDirtyPageContent.add(true);
1516 }
1517
Winson Chunga12a2502010-12-20 14:41:35 -08001518 // Use the restore page if necessary
1519 if (mRestorePage > -1) {
1520 mCurrentPage = mRestorePage;
1521 mRestorePage = -1;
1522 }
1523
Michael Jurka0142d492010-08-25 17:46:15 -07001524 // Load any pages that are necessary for the current window of views
1525 loadAssociatedPages(mCurrentPage);
1526 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001527 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001528 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001529 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 }
1531}