blob: c2fcd9f215b960ec5889d977705fe2f062e6e4bc [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 Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080039import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Winson Chung04998342011-01-05 13:54:43 -080043import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070044
Winson Chung321e9ee2010-08-09 13:37:56 -070045/**
46 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070047 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070048 */
49public abstract class PagedView extends ViewGroup {
50 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070051 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070052
Winson Chung86f77532010-08-24 11:08:22 -070053 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070054 private static final int MIN_LENGTH_FOR_FLING = 25;
55 // The min drag distance to trigger a page shift (regardless of velocity)
56 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Adam Cohene0f66b52010-11-23 15:06:07 -080058 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Adam Cohene0f66b52010-11-23 15:06:07 -080061 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
62 private static final int MINIMUM_SNAP_VELOCITY = 2200;
63 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080064 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080065
Michael Jurka0142d492010-08-25 17:46:15 -070066 // the velocity at which a fling gesture will cause us to snap to the next page
67 protected int mSnapVelocity = 500;
68
69 protected float mSmoothingTime;
70 protected float mTouchX;
71
72 protected boolean mFirstLayout = true;
73
74 protected int mCurrentPage;
75 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080076 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080077 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070079 private VelocityTracker mVelocityTracker;
80
81 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080082 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080083 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080084 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080085 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070086 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070087
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected final static int TOUCH_STATE_REST = 0;
89 protected final static int TOUCH_STATE_SCROLLING = 1;
90 protected final static int TOUCH_STATE_PREV_PAGE = 2;
91 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070092 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101 private int mPagingTouchSlop;
102 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800103 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700104 protected int mPageSpacing;
105 protected int mPageLayoutPaddingTop;
106 protected int mPageLayoutPaddingBottom;
107 protected int mPageLayoutPaddingLeft;
108 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700109 protected int mPageLayoutWidthGap;
110 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700111 protected int mCellCountX;
112 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700113 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800114 protected boolean mAllowOverScroll = true;
115 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka8c920dd2011-01-20 14:16:56 -0800117 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800118 protected float mLayoutScale = 1.0f;
119
Michael Jurka5f1c5092010-09-03 14:15:02 -0700120 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka5f1c5092010-09-03 14:15:02 -0700122 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Winson Chung86f77532010-08-24 11:08:22 -0700124 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Winson Chung86f77532010-08-24 11:08:22 -0700126 private ArrayList<Boolean> mDirtyPageContent;
127 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700129 // choice modes
130 protected static final int CHOICE_MODE_NONE = 0;
131 protected static final int CHOICE_MODE_SINGLE = 1;
132 // Multiple selection mode is not supported by all Launcher actions atm
133 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700134
Michael Jurkae17e19c2010-09-28 11:01:39 -0700135 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700136 private ActionMode mActionMode;
137
Winson Chung04998342011-01-05 13:54:43 -0800138 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
139 // AllApps and Customize, and allows them to share holographic icons for the application view
140 // (which is in both).
141 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700142
Michael Jurka0142d492010-08-25 17:46:15 -0700143 // If true, syncPages and syncPageItems will be called to refresh pages
144 protected boolean mContentIsRefreshable = true;
145
146 // If true, modify alpha of neighboring pages as user scrolls left/right
147 protected boolean mFadeInAdjacentScreens = true;
148
149 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
150 // to switch to a new page
151 protected boolean mUsePagingTouchSlop = true;
152
153 // If true, the subclass should directly update mScrollX itself in its computeScroll method
154 // (SmoothPagedView does this)
155 protected boolean mDeferScrollUpdate = false;
156
Patrick Dubroy1262e362010-10-06 15:49:50 -0700157 protected boolean mIsPageMoving = false;
158
Winson Chung86f77532010-08-24 11:08:22 -0700159 public interface PageSwitchListener {
160 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 }
162
Winson Chung321e9ee2010-08-09 13:37:56 -0700163 public PagedView(Context context) {
164 this(context, null);
165 }
166
167 public PagedView(Context context, AttributeSet attrs) {
168 this(context, attrs, 0);
169 }
170
171 public PagedView(Context context, AttributeSet attrs, int defStyle) {
172 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700173 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700174
Adam Cohen9c4949e2010-10-05 12:27:22 -0700175 TypedArray a = context.obtainStyledAttributes(attrs,
176 R.styleable.PagedView, defStyle, 0);
177 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
178 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800179 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700180 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800181 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700182 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800183 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700184 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800185 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700186 mPageLayoutWidthGap = a.getDimensionPixelSize(
187 R.styleable.PagedView_pageLayoutWidthGap, -1);
188 mPageLayoutHeightGap = a.getDimensionPixelSize(
189 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700190 a.recycle();
191
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700193 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 }
195
196 /**
197 * Initializes various states for this workspace.
198 */
Michael Jurka0142d492010-08-25 17:46:15 -0700199 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700200 mDirtyPageContent = new ArrayList<Boolean>();
201 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800202 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700203 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700204 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700205
206 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
207 mTouchSlop = configuration.getScaledTouchSlop();
208 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
209 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
210 }
211
Winson Chung86f77532010-08-24 11:08:22 -0700212 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
213 mPageSwitchListener = pageSwitchListener;
214 if (mPageSwitchListener != null) {
215 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 }
217 }
218
219 /**
Winson Chung86f77532010-08-24 11:08:22 -0700220 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 *
Winson Chung86f77532010-08-24 11:08:22 -0700222 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 */
Winson Chung86f77532010-08-24 11:08:22 -0700224 int getCurrentPage() {
225 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227
Winson Chung86f77532010-08-24 11:08:22 -0700228 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 return getChildCount();
230 }
231
Winson Chung86f77532010-08-24 11:08:22 -0700232 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 return getChildAt(index);
234 }
235
236 int getScrollWidth() {
237 return getWidth();
238 }
239
Adam Cohenbe909342011-01-28 17:02:58 -0800240 public int getTouchState() {
241 return mTouchState;
242 }
243
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800245 * Updates the scroll of the current page immediately to its final scroll position. We use this
246 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
247 * the previous tab page.
248 */
249 protected void updateCurrentPageScroll() {
250 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
251 scrollTo(newX, 0);
252 mScroller.setFinalX(newX);
253 }
254
255 /**
Winson Chung86f77532010-08-24 11:08:22 -0700256 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 */
Winson Chung86f77532010-08-24 11:08:22 -0700258 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800259 if (!mScroller.isFinished()) {
260 mScroller.abortAnimation();
261 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800262 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
263 // the default
264 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800265 return;
266 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700267
Winson Chung86f77532010-08-24 11:08:22 -0700268 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800269 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700270 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800271 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 }
273
Michael Jurka0142d492010-08-25 17:46:15 -0700274 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700275 if (mPageSwitchListener != null) {
276 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 }
278 }
279
Michael Jurkace7e05f2011-02-01 22:02:35 -0800280 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700281 mIsPageMoving = true;
282 onPageBeginMoving();
283 }
284
Michael Jurkace7e05f2011-02-01 22:02:35 -0800285 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700286 onPageEndMoving();
287 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700288 }
289
290 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700291 protected void onPageBeginMoving() {
292 }
293
294 // a method that subclasses can override to add behavior
295 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700296 }
297
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 /**
Winson Chung86f77532010-08-24 11:08:22 -0700299 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 *
301 * @param l The listener used to respond to long clicks.
302 */
303 @Override
304 public void setOnLongClickListener(OnLongClickListener l) {
305 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700306 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700308 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 }
310 }
311
312 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800313 public void scrollBy(int x, int y) {
314 scrollTo(mUnboundedScrollX + x, mScrollY + y);
315 }
316
317 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700318 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800319 mUnboundedScrollX = x;
320
321 if (x < 0) {
322 super.scrollTo(0, y);
323 if (mAllowOverScroll) {
324 overScroll(x);
325 }
326 } else if (x > mMaxScrollX) {
327 super.scrollTo(mMaxScrollX, y);
328 if (mAllowOverScroll) {
329 overScroll(x - mMaxScrollX);
330 }
331 } else {
332 super.scrollTo(x, y);
333 }
334
Michael Jurka0142d492010-08-25 17:46:15 -0700335 mTouchX = x;
336 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
337 }
338
339 // we moved this functionality to a helper function so SmoothPagedView can reuse it
340 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700342 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700344 invalidate();
345 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700346 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700347 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700348 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700349 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700350 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800351 // We don't want to trigger a page end moving unless the page has settled
352 // and the user has stopped scrolling
353 if (mTouchState == TOUCH_STATE_REST) {
354 pageEndMoving();
355 }
Michael Jurka0142d492010-08-25 17:46:15 -0700356 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 }
Michael Jurka0142d492010-08-25 17:46:15 -0700358 return false;
359 }
360
361 @Override
362 public void computeScroll() {
363 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 }
365
366 @Override
367 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
368 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
369 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
370 if (widthMode != MeasureSpec.EXACTLY) {
371 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
372 }
373
Adam Lesinski6b879f02010-11-04 16:15:23 -0700374 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
375 * of the All apps view on XLarge displays to not take up more space then it needs. Width
376 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
377 * each page to have the same width.
378 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700380 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
381 int maxChildHeight = 0;
382
383 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700384
385 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700386 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 final int childCount = getChildCount();
388 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700389 // disallowing padding in paged view (just pass 0)
390 final View child = getChildAt(i);
391 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
392
393 int childWidthMode;
394 if (lp.width == LayoutParams.WRAP_CONTENT) {
395 childWidthMode = MeasureSpec.AT_MOST;
396 } else {
397 childWidthMode = MeasureSpec.EXACTLY;
398 }
399
400 int childHeightMode;
401 if (lp.height == LayoutParams.WRAP_CONTENT) {
402 childHeightMode = MeasureSpec.AT_MOST;
403 } else {
404 childHeightMode = MeasureSpec.EXACTLY;
405 }
406
407 final int childWidthMeasureSpec =
408 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
409 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700410 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700411
412 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700413 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
414 }
415
416 if (heightMode == MeasureSpec.AT_MOST) {
417 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800419 if (childCount > 0) {
420 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
421 } else {
422 mMaxScrollX = 0;
423 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700424
425 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 }
427
Michael Jurka8c920dd2011-01-20 14:16:56 -0800428 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800429 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
430 int delta = newX - mScrollX;
431
Michael Jurka8c920dd2011-01-20 14:16:56 -0800432 final int pageCount = getChildCount();
433 for (int i = 0; i < pageCount; i++) {
434 View page = (View) getChildAt(i);
435 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800436 }
437 setCurrentPage(newCurrentPage);
438 }
439
Michael Jurka8c920dd2011-01-20 14:16:56 -0800440 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
441 // 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 -0800442 // tightens the layout accordingly
443 public void setLayoutScale(float childrenScale) {
444 mLayoutScale = childrenScale;
445
446 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
447 int childCount = getChildCount();
448 float childrenX[] = new float[childCount];
449 float childrenY[] = new float[childCount];
450 for (int i = 0; i < childCount; i++) {
451 final View child = getChildAt(i);
452 childrenX[i] = child.getX();
453 childrenY[i] = child.getY();
454 }
455 onLayout(false, mLeft, mTop, mRight, mBottom);
456 for (int i = 0; i < childCount; i++) {
457 final View child = getChildAt(i);
458 child.setX(childrenX[i]);
459 child.setY(childrenY[i]);
460 }
461 // Also, the page offset has changed (since the pages are now smaller);
462 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800463 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800464 }
465
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700467 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700468 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
469 setHorizontalScrollBarEnabled(false);
470 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
471 scrollTo(newX, 0);
472 mScroller.setFinalX(newX);
473 setHorizontalScrollBarEnabled(true);
474 mFirstLayout = false;
475 }
476
Adam Lesinski6b879f02010-11-04 16:15:23 -0700477 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 final int childCount = getChildCount();
479 int childLeft = 0;
480 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700481 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 }
483
484 for (int i = 0; i < childCount; i++) {
485 final View child = getChildAt(i);
486 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800487 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700488 final int childHeight = child.getMeasuredHeight();
489 int childTop = mPaddingTop;
490 if (mCenterPagesVertically) {
491 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
492 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800493
Adam Lesinski6b879f02010-11-04 16:15:23 -0700494 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800495 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700496 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 }
498 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800499 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
500 mFirstLayout = false;
501 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 }
503
Winson Chung03929772011-02-23 17:07:10 -0800504 protected void forceUpdateAdjacentPagesAlpha() {
505 mDirtyPageAlpha = true;
506 updateAdjacentPagesAlpha();
507 }
508
Winson Chunge3193b92010-09-10 11:44:42 -0700509 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700510 if (mFadeInAdjacentScreens) {
511 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700512 int halfScreenSize = getMeasuredWidth() / 2;
513 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700514 final int childCount = getChildCount();
515 for (int i = 0; i < childCount; ++i) {
516 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800517 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700518 int halfChildWidth = (childWidth / 2);
519 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700520
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700521 // On the first layout, we may not have a width nor a proper offset, so for now
522 // we should just assume full page width (and calculate the offset according to
523 // that).
524 if (childWidth <= 0) {
525 childWidth = getMeasuredWidth();
526 childCenter = (i * childWidth) + (childWidth / 2);
527 }
528
Winson Chunge8878e32010-09-15 20:37:09 -0700529 int d = halfChildWidth;
530 int distanceFromScreenCenter = childCenter - screenCenter;
531 if (distanceFromScreenCenter > 0) {
532 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700534 }
Michael Jurka0142d492010-08-25 17:46:15 -0700535 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700536 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800537 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700538 }
Michael Jurka0142d492010-08-25 17:46:15 -0700539 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700540 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700541
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700542 // Preventing potential divide-by-zero
543 d = Math.max(1, d);
544
Winson Chunge8878e32010-09-15 20:37:09 -0700545 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
546 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
547 float alpha = 1.0f - dimAlpha;
548
Adam Cohenf34bab52010-09-30 14:11:56 -0700549 if (alpha < ALPHA_QUANTIZE_LEVEL) {
550 alpha = 0.0f;
551 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
552 alpha = 1.0f;
553 }
554
Michael Jurkaa40829a2011-02-16 18:02:45 -0800555 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
556 // this optimization causes alpha to not be properly updated sometimes (repro
557 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
558 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
559 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800560 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700561 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800562 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
Michael Jurka0142d492010-08-25 17:46:15 -0700564 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 }
566 }
Winson Chunge3193b92010-09-10 11:44:42 -0700567 }
568
Adam Cohenf34bab52010-09-30 14:11:56 -0700569 protected void screenScrolled(int screenCenter) {
570 }
571
Winson Chunge3193b92010-09-10 11:44:42 -0700572 @Override
573 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700574 int halfScreenSize = getMeasuredWidth() / 2;
575 int screenCenter = mScrollX + halfScreenSize;
576
577 if (screenCenter != mLastScreenCenter) {
578 screenScrolled(screenCenter);
579 updateAdjacentPagesAlpha();
580 mLastScreenCenter = screenCenter;
581 }
Michael Jurka0142d492010-08-25 17:46:15 -0700582
583 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700584 // As an optimization, this code assumes that all pages have the same width as the 0th
585 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700586 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700587 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800588 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700589 final int screenWidth = getMeasuredWidth();
590 int x = getRelativeChildOffset(0) + pageWidth;
591 int leftScreen = 0;
592 int rightScreen = 0;
593 while (x <= mScrollX) {
594 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800595 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700596 }
597 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800598 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700599 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800600 if (rightScreen < pageCount) {
601 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
602 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700603 }
604 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700605
Michael Jurkac4fb9172010-09-02 17:19:20 -0700606 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800607 // Clip to the bounds
608 canvas.save();
609 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
610 mScrollY + mBottom - mTop);
611
Michael Jurkac4fb9172010-09-02 17:19:20 -0700612 for (int i = leftScreen; i <= rightScreen; i++) {
613 drawChild(canvas, getChildAt(i), drawingTime);
614 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800615 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700616 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 }
618
619 @Override
620 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700621 int page = indexOfChild(child);
622 if (page != mCurrentPage || !mScroller.isFinished()) {
623 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 return true;
625 }
626 return false;
627 }
628
629 @Override
630 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700631 int focusablePage;
632 if (mNextPage != INVALID_PAGE) {
633 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700635 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 }
Winson Chung86f77532010-08-24 11:08:22 -0700637 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 if (v != null) {
639 v.requestFocus(direction, previouslyFocusedRect);
640 }
641 return false;
642 }
643
644 @Override
645 public boolean dispatchUnhandledMove(View focused, int direction) {
646 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700647 if (getCurrentPage() > 0) {
648 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 return true;
650 }
651 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700652 if (getCurrentPage() < getPageCount() - 1) {
653 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700654 return true;
655 }
656 }
657 return super.dispatchUnhandledMove(focused, direction);
658 }
659
660 @Override
661 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700662 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
663 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700664 }
665 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700666 if (mCurrentPage > 0) {
667 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700668 }
669 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700670 if (mCurrentPage < getPageCount() - 1) {
671 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 }
673 }
674 }
675
676 /**
677 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700678 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 *
Winson Chung86f77532010-08-24 11:08:22 -0700680 * This happens when live folders requery, and if they're off page, they
681 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 */
683 @Override
684 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700685 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 View v = focused;
687 while (true) {
688 if (v == current) {
689 super.focusableViewAvailable(focused);
690 return;
691 }
692 if (v == this) {
693 return;
694 }
695 ViewParent parent = v.getParent();
696 if (parent instanceof View) {
697 v = (View)v.getParent();
698 } else {
699 return;
700 }
701 }
702 }
703
704 /**
705 * {@inheritDoc}
706 */
707 @Override
708 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
709 if (disallowIntercept) {
710 // We need to make sure to cancel our long press if
711 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700712 final View currentPage = getChildAt(mCurrentPage);
713 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 }
715 super.requestDisallowInterceptTouchEvent(disallowIntercept);
716 }
717
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800718 /**
719 * Return true if a tap at (x, y) should trigger a flip to the previous page.
720 */
721 protected boolean hitsPreviousPage(float x, float y) {
722 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
723 }
724
725 /**
726 * Return true if a tap at (x, y) should trigger a flip to the next page.
727 */
728 protected boolean hitsNextPage(float x, float y) {
729 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
730 }
731
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 @Override
733 public boolean onInterceptTouchEvent(MotionEvent ev) {
734 /*
735 * This method JUST determines whether we want to intercept the motion.
736 * If we return true, onTouchEvent will be called and we do the actual
737 * scrolling there.
738 */
739
Winson Chung45e1d6e2010-11-09 17:19:49 -0800740 // Skip touch handling if there are no pages to swipe
741 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
742
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 /*
744 * Shortcut the most recurring case: the user is in the dragging
745 * state and he is moving his finger. We want to intercept this
746 * motion.
747 */
748 final int action = ev.getAction();
749 if ((action == MotionEvent.ACTION_MOVE) &&
750 (mTouchState == TOUCH_STATE_SCROLLING)) {
751 return true;
752 }
753
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 switch (action & MotionEvent.ACTION_MASK) {
755 case MotionEvent.ACTION_MOVE: {
756 /*
757 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
758 * whether the user has moved far enough from his original down touch.
759 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700760 if (mActivePointerId != INVALID_POINTER) {
761 determineScrollingStart(ev);
762 break;
763 }
764 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
765 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
766 // i.e. fall through to the next case (don't break)
767 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
768 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 }
770
771 case MotionEvent.ACTION_DOWN: {
772 final float x = ev.getX();
773 final float y = ev.getY();
774 // Remember location of down touch
775 mDownMotionX = x;
776 mLastMotionX = x;
777 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800778 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800779 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 mActivePointerId = ev.getPointerId(0);
781 mAllowLongPress = true;
782
783 /*
784 * If being flinged and user touches the screen, initiate drag;
785 * otherwise don't. mScroller.isFinished should be false when
786 * being flinged.
787 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700788 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700789 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
790 if (finishedScrolling) {
791 mTouchState = TOUCH_STATE_REST;
792 mScroller.abortAnimation();
793 } else {
794 mTouchState = TOUCH_STATE_SCROLLING;
795 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700796
Winson Chung86f77532010-08-24 11:08:22 -0700797 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800799 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800801 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800803 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 mTouchState = TOUCH_STATE_NEXT_PAGE;
805 }
806 }
807 }
808 break;
809 }
810
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800812 onWallpaperTap(ev);
813 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 mTouchState = TOUCH_STATE_REST;
815 mAllowLongPress = false;
816 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 break;
818
819 case MotionEvent.ACTION_POINTER_UP:
820 onSecondaryPointerUp(ev);
821 break;
822 }
823
824 /*
825 * The only time we want to intercept motion events is if we are in the
826 * drag mode.
827 */
828 return mTouchState != TOUCH_STATE_REST;
829 }
830
Winson Chung80baf5a2010-08-09 16:03:15 -0700831 protected void animateClickFeedback(View v, final Runnable r) {
832 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800833 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
834 loadAnimator(mContext, R.anim.paged_view_click_feedback);
835 anim.setTarget(v);
836 anim.addListener(new AnimatorListenerAdapter() {
837 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700838 r.run();
839 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700840 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800841 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700842 }
843
Adam Cohenf8d28232011-02-01 21:47:00 -0800844 protected void determineScrollingStart(MotionEvent ev) {
845 determineScrollingStart(ev, 1.0f);
846 }
847
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 /*
849 * Determines if we should change the touch state to start scrolling after the
850 * user moves their touch point too far.
851 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800852 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 /*
854 * Locally do absolute value. mLastMotionX is set to the y value
855 * of the down event.
856 */
857 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
858 final float x = ev.getX(pointerIndex);
859 final float y = ev.getY(pointerIndex);
860 final int xDiff = (int) Math.abs(x - mLastMotionX);
861 final int yDiff = (int) Math.abs(y - mLastMotionY);
862
Adam Cohenf8d28232011-02-01 21:47:00 -0800863 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 boolean xPaged = xDiff > mPagingTouchSlop;
865 boolean xMoved = xDiff > touchSlop;
866 boolean yMoved = yDiff > touchSlop;
867
Adam Cohenf8d28232011-02-01 21:47:00 -0800868 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700869 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 // Scroll if the user moved far enough along the X axis
871 mTouchState = TOUCH_STATE_SCROLLING;
872 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800873 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700874 mTouchX = mScrollX;
875 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
876 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 }
878 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800879 cancelCurrentPageLongPress();
880 }
881 }
882
883 protected void cancelCurrentPageLongPress() {
884 if (mAllowLongPress) {
885 mAllowLongPress = false;
886 // Try canceling the long press. It could also have been scheduled
887 // by a distant descendant, so use the mAllowLongPress flag to block
888 // everything
889 final View currentPage = getPageAt(mCurrentPage);
890 if (currentPage != null) {
891 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 }
893 }
894 }
895
Adam Cohene0f66b52010-11-23 15:06:07 -0800896 // This curve determines how the effect of scrolling over the limits of the page dimishes
897 // as the user pulls further and further from the bounds
898 private float overScrollInfluenceCurve(float f) {
899 f -= 1.0f;
900 return f * f * f + 1.0f;
901 }
902
Adam Cohen68d73932010-11-15 10:50:58 -0800903 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800904 int screenSize = getMeasuredWidth();
905
906 float f = (amount / screenSize);
907
908 if (f == 0) return;
909 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
910
Adam Cohen7bfc9792011-01-28 13:52:37 -0800911 // Clamp this factor, f, to -1 < f < 1
912 if (Math.abs(f) >= 1) {
913 f /= Math.abs(f);
914 }
915
Adam Cohene0f66b52010-11-23 15:06:07 -0800916 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800917 if (amount < 0) {
918 mScrollX = overScrollAmount;
919 } else {
920 mScrollX = mMaxScrollX + overScrollAmount;
921 }
922 invalidate();
923 }
924
Michael Jurkac5b262c2011-01-12 20:24:50 -0800925 protected float maxOverScroll() {
926 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
927 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
928 float f = 1.0f;
929 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
930 return OVERSCROLL_DAMP_FACTOR * f;
931 }
932
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 @Override
934 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800935 // Skip touch handling if there are no pages to swipe
936 if (getChildCount() <= 0) return super.onTouchEvent(ev);
937
Michael Jurkab8f06722010-10-10 15:58:46 -0700938 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700939
940 final int action = ev.getAction();
941
942 switch (action & MotionEvent.ACTION_MASK) {
943 case MotionEvent.ACTION_DOWN:
944 /*
945 * If being flinged and user touches, stop the fling. isFinished
946 * will be false if being flinged.
947 */
948 if (!mScroller.isFinished()) {
949 mScroller.abortAnimation();
950 }
951
952 // Remember where the motion event started
953 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800954 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800955 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700957 if (mTouchState == TOUCH_STATE_SCROLLING) {
958 pageBeginMoving();
959 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 break;
961
962 case MotionEvent.ACTION_MOVE:
963 if (mTouchState == TOUCH_STATE_SCROLLING) {
964 // Scroll to follow the motion event
965 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
966 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800967 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700968
Adam Cohenaefd4e12011-02-14 16:39:38 -0800969 mTotalMotionX += Math.abs(deltaX);
970
Winson Chungc0844aa2011-02-02 15:25:58 -0800971 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
972 // keep the remainder because we are actually testing if we've moved from the last
973 // scrolled position (which is discrete).
974 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800975 mTouchX += deltaX;
976 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
977 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800978 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800979 } else {
980 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800982 mLastMotionX = x;
983 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 } else {
985 awakenScrollBars();
986 }
Adam Cohen564976a2010-10-13 18:52:07 -0700987 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 determineScrollingStart(ev);
989 }
990 break;
991
992 case MotionEvent.ACTION_UP:
993 if (mTouchState == TOUCH_STATE_SCROLLING) {
994 final int activePointerId = mActivePointerId;
995 final int pointerIndex = ev.findPointerIndex(activePointerId);
996 final float x = ev.getX(pointerIndex);
997 final VelocityTracker velocityTracker = mVelocityTracker;
998 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
999 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001000 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001001 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001002 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001003
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001004 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1005
Adam Cohenaefd4e12011-02-14 16:39:38 -08001006 // In the case that the page is moved far to one direction and then is flung
1007 // in the opposite direction, we use a threshold to determine whether we should
1008 // just return to the starting page, or if we should skip one further.
1009 boolean returnToOriginalPage = false;
1010 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001011 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001012 Math.signum(velocityX) != Math.signum(deltaX)) {
1013 returnToOriginalPage = true;
1014 }
1015
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001016 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001017 Math.abs(velocityX) > snapVelocity;
1018
1019 int finalPage;
1020 // We give flings precedence over large moves, which is why we short-circuit our
1021 // test for a large move if a fling has been registered. That is, a large
1022 // move to the left and fling to the right will register as a fling to the right.
1023 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1024 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1025 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1026 snapToPageWithVelocity(finalPage, velocityX);
1027 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1028 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001029 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001030 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1031 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 } else {
1033 snapToDestination();
1034 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001035 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 // at this point we have not moved beyond the touch slop
1037 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1038 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001039 int nextPage = Math.max(0, mCurrentPage - 1);
1040 if (nextPage != mCurrentPage) {
1041 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 } else {
1043 snapToDestination();
1044 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001045 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 // at this point we have not moved beyond the touch slop
1047 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1048 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001049 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1050 if (nextPage != mCurrentPage) {
1051 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 } else {
1053 snapToDestination();
1054 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001055 } else {
1056 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 }
1058 mTouchState = TOUCH_STATE_REST;
1059 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001060 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 break;
1062
1063 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001064 if (mTouchState == TOUCH_STATE_SCROLLING) {
1065 snapToDestination();
1066 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 mTouchState = TOUCH_STATE_REST;
1068 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001069 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 break;
1071
1072 case MotionEvent.ACTION_POINTER_UP:
1073 onSecondaryPointerUp(ev);
1074 break;
1075 }
1076
1077 return true;
1078 }
1079
Michael Jurkab8f06722010-10-10 15:58:46 -07001080 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1081 if (mVelocityTracker == null) {
1082 mVelocityTracker = VelocityTracker.obtain();
1083 }
1084 mVelocityTracker.addMovement(ev);
1085 }
1086
1087 private void releaseVelocityTracker() {
1088 if (mVelocityTracker != null) {
1089 mVelocityTracker.recycle();
1090 mVelocityTracker = null;
1091 }
1092 }
1093
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 private void onSecondaryPointerUp(MotionEvent ev) {
1095 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1096 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1097 final int pointerId = ev.getPointerId(pointerIndex);
1098 if (pointerId == mActivePointerId) {
1099 // This was our active pointer going up. Choose a new
1100 // active pointer and adjust accordingly.
1101 // TODO: Make this decision more intelligent.
1102 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1103 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1104 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001105 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 mActivePointerId = ev.getPointerId(newPointerIndex);
1107 if (mVelocityTracker != null) {
1108 mVelocityTracker.clear();
1109 }
1110 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001111 if (mTouchState == TOUCH_STATE_REST) {
1112 onWallpaperTap(ev);
1113 }
1114 }
1115
1116 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 }
1118
1119 @Override
1120 public void requestChildFocus(View child, View focused) {
1121 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001122 int page = indexOfChild(child);
1123 if (page >= 0 && !isInTouchMode()) {
1124 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 }
1126 }
1127
Winson Chunge3193b92010-09-10 11:44:42 -07001128 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1129 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001130 int left;
1131 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001132 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001133 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001134 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001135 if (left <= relativeOffset && relativeOffset <= right) {
1136 return i;
1137 }
Winson Chunge3193b92010-09-10 11:44:42 -07001138 }
1139 return -1;
1140 }
1141
Winson Chung1908d072011-02-24 18:09:44 -08001142 protected void setMinimumWidthOverride(int minimumWidth) {
1143 mMinimumWidth = minimumWidth;
1144 }
1145
1146 protected int getChildWidth(int index) {
1147 return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth());
1148 }
1149
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001151 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 }
1153
1154 protected int getChildOffset(int index) {
1155 if (getChildCount() == 0)
1156 return 0;
1157
1158 int offset = getRelativeChildOffset(0);
1159 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001160 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 }
1162 return offset;
1163 }
1164
Michael Jurkad3ef3062010-11-23 16:23:58 -08001165 protected int getScaledMeasuredWidth(View child) {
Winson Chung1908d072011-02-24 18:09:44 -08001166 return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001167 }
1168
Adam Cohend19d3ca2010-09-15 14:43:42 -07001169 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 int minDistanceFromScreenCenter = getMeasuredWidth();
1171 int minDistanceFromScreenCenterIndex = -1;
1172 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1173 final int childCount = getChildCount();
1174 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001175 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001176 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 int halfChildWidth = (childWidth / 2);
1178 int childCenter = getChildOffset(i) + halfChildWidth;
1179 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1180 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1181 minDistanceFromScreenCenter = distanceFromScreenCenter;
1182 minDistanceFromScreenCenterIndex = i;
1183 }
1184 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001185 return minDistanceFromScreenCenterIndex;
1186 }
1187
1188 protected void snapToDestination() {
1189 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 }
1191
Adam Cohene0f66b52010-11-23 15:06:07 -08001192 private static class ScrollInterpolator implements Interpolator {
1193 public ScrollInterpolator() {
1194 }
1195
1196 public float getInterpolation(float t) {
1197 t -= 1.0f;
1198 return t*t*t*t*t + 1;
1199 }
1200 }
1201
1202 // We want the duration of the page snap animation to be influenced by the distance that
1203 // the screen has to travel, however, we don't want this duration to be effected in a
1204 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1205 // of travel has on the overall snap duration.
1206 float distanceInfluenceForSnapDuration(float f) {
1207 f -= 0.5f; // center the values about 0.
1208 f *= 0.3f * Math.PI / 2.0f;
1209 return (float) Math.sin(f);
1210 }
1211
Michael Jurka0142d492010-08-25 17:46:15 -07001212 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001213 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1214 int halfScreenSize = getMeasuredWidth() / 2;
1215
1216 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1217 int delta = newX - mUnboundedScrollX;
1218 int duration = 0;
1219
1220 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1221 // If the velocity is low enough, then treat this more as an automatic page advance
1222 // as opposed to an apparent physical response to flinging
1223 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1224 return;
1225 }
1226
1227 // Here we compute a "distance" that will be used in the computation of the overall
1228 // snap duration. This is a function of the actual distance that needs to be traveled;
1229 // we keep this value close to half screen size in order to reduce the variance in snap
1230 // duration as a function of the distance the page needs to travel.
1231 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1232 float distance = halfScreenSize + halfScreenSize *
1233 distanceInfluenceForSnapDuration(distanceRatio);
1234
1235 velocity = Math.abs(velocity);
1236 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1237
1238 // we want the page's snap velocity to approximately match the velocity at which the
1239 // user flings, so we scale the duration by a value near to the derivative of the scroll
1240 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1241 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1242
1243 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001244 }
1245
1246 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001247 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 }
1249
Michael Jurka0142d492010-08-25 17:46:15 -07001250 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001251 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001252
Winson Chung86f77532010-08-24 11:08:22 -07001253 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001254 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001256 snapToPage(whichPage, delta, duration);
1257 }
1258
1259 protected void snapToPage(int whichPage, int delta, int duration) {
1260 mNextPage = whichPage;
1261
1262 View focusedChild = getFocusedChild();
1263 if (focusedChild != null && whichPage != mCurrentPage &&
1264 focusedChild == getChildAt(mCurrentPage)) {
1265 focusedChild.clearFocus();
1266 }
1267
1268 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 awakenScrollBars(duration);
1270 if (duration == 0) {
1271 duration = Math.abs(delta);
1272 }
1273
1274 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001275 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001276
1277 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001278 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001279 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 invalidate();
1281 }
1282
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 public void scrollLeft() {
1284 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001285 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001287 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 }
1289 }
1290
1291 public void scrollRight() {
1292 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001293 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001295 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 }
1297 }
1298
Winson Chung86f77532010-08-24 11:08:22 -07001299 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 int result = -1;
1301 if (v != null) {
1302 ViewParent vp = v.getParent();
1303 int count = getChildCount();
1304 for (int i = 0; i < count; i++) {
1305 if (vp == getChildAt(i)) {
1306 return i;
1307 }
1308 }
1309 }
1310 return result;
1311 }
1312
1313 /**
1314 * @return True is long presses are still allowed for the current touch
1315 */
1316 public boolean allowLongPress() {
1317 return mAllowLongPress;
1318 }
1319
Michael Jurka0142d492010-08-25 17:46:15 -07001320 /**
1321 * Set true to allow long-press events to be triggered, usually checked by
1322 * {@link Launcher} to accept or block dpad-initiated long-presses.
1323 */
1324 public void setAllowLongPress(boolean allowLongPress) {
1325 mAllowLongPress = allowLongPress;
1326 }
1327
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001329 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001330
1331 SavedState(Parcelable superState) {
1332 super(superState);
1333 }
1334
1335 private SavedState(Parcel in) {
1336 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001337 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 }
1339
1340 @Override
1341 public void writeToParcel(Parcel out, int flags) {
1342 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001343 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 }
1345
1346 public static final Parcelable.Creator<SavedState> CREATOR =
1347 new Parcelable.Creator<SavedState>() {
1348 public SavedState createFromParcel(Parcel in) {
1349 return new SavedState(in);
1350 }
1351
1352 public SavedState[] newArray(int size) {
1353 return new SavedState[size];
1354 }
1355 };
1356 }
1357
Winson Chung86f77532010-08-24 11:08:22 -07001358 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001359 if (mContentIsRefreshable) {
1360 final int count = getChildCount();
1361 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001362 int lowerPageBound = getAssociatedLowerPageBound(page);
1363 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001364 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001365 Page layout = (Page) getChildAt(i);
1366 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001367 if (lowerPageBound <= i && i <= upperPageBound) {
1368 if (mDirtyPageContent.get(i)) {
1369 syncPageItems(i);
1370 mDirtyPageContent.set(i, false);
1371 }
1372 } else {
1373 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001374 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001375 }
1376 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001377 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001378 }
1379 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001380 }
1381 }
1382
Winson Chunge3193b92010-09-10 11:44:42 -07001383 protected int getAssociatedLowerPageBound(int page) {
1384 return Math.max(0, page - 1);
1385 }
1386 protected int getAssociatedUpperPageBound(int page) {
1387 final int count = getChildCount();
1388 return Math.min(page + 1, count - 1);
1389 }
1390
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001391 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001392 if (isChoiceMode(CHOICE_MODE_NONE)) {
1393 mChoiceMode = mode;
1394 mActionMode = startActionMode(callback);
1395 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001396 }
1397
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001398 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001399 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001400 mChoiceMode = CHOICE_MODE_NONE;
1401 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001402 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001403 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001404 }
1405 }
1406
1407 protected boolean isChoiceMode(int mode) {
1408 return mChoiceMode == mode;
1409 }
1410
1411 protected ArrayList<Checkable> getCheckedGrandchildren() {
1412 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1413 final int childCount = getChildCount();
1414 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001415 Page layout = (Page) getChildAt(i);
1416 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001417 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001418 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001419 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001420 checked.add((Checkable) v);
1421 }
1422 }
1423 }
1424 return checked;
1425 }
1426
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001427 /**
1428 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1429 * Otherwise, returns null.
1430 */
1431 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001432 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001433 final int childCount = getChildCount();
1434 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001435 Page layout = (Page) getChildAt(i);
1436 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001437 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001438 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001439 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1440 return (Checkable) v;
1441 }
1442 }
1443 }
1444 }
1445 return null;
1446 }
1447
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001448 protected void resetCheckedGrandchildren() {
1449 // loop through children, and set all of their children to _not_ be checked
1450 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1451 for (int i = 0; i < checked.size(); ++i) {
1452 final Checkable c = checked.get(i);
1453 c.setChecked(false);
1454 }
1455 }
1456
Winson Chunga12a2502010-12-20 14:41:35 -08001457 public void setRestorePage(int restorePage) {
1458 mRestorePage = restorePage;
1459 }
1460
Winson Chung86f77532010-08-24 11:08:22 -07001461 /**
1462 * This method is called ONLY to synchronize the number of pages that the paged view has.
1463 * To actually fill the pages with information, implement syncPageItems() below. It is
1464 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1465 * and therefore, individual page items do not need to be updated in this method.
1466 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001468
1469 /**
1470 * This method is called to synchronize the items that are on a particular page. If views on
1471 * the page can be reused, then they should be updated within this method.
1472 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001474
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001476 if (mContentIsRefreshable) {
1477 // Update all the pages
1478 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001479
Michael Jurka0142d492010-08-25 17:46:15 -07001480 // Mark each of the pages as dirty
1481 final int count = getChildCount();
1482 mDirtyPageContent.clear();
1483 for (int i = 0; i < count; ++i) {
1484 mDirtyPageContent.add(true);
1485 }
1486
Winson Chunga12a2502010-12-20 14:41:35 -08001487 // Use the restore page if necessary
1488 if (mRestorePage > -1) {
1489 mCurrentPage = mRestorePage;
1490 mRestorePage = -1;
1491 }
1492
Michael Jurka0142d492010-08-25 17:46:15 -07001493 // Load any pages that are necessary for the current window of views
1494 loadAssociatedPages(mCurrentPage);
1495 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001496 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001497 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001498 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 }
1500}