blob: 05e4086cf4edcdf9b4eeaf5554177434be44210f [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 Cohen68d73932010-11-15 10:50:58 -080064
Michael Jurka0142d492010-08-25 17:46:15 -070065 // the velocity at which a fling gesture will cause us to snap to the next page
66 protected int mSnapVelocity = 500;
67
68 protected float mSmoothingTime;
69 protected float mTouchX;
70
71 protected boolean mFirstLayout = true;
72
73 protected int mCurrentPage;
74 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080075 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080076 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070077 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070078 private VelocityTracker mVelocityTracker;
79
80 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080081 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080082 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080083 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080084 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070085 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070086
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected final static int TOUCH_STATE_REST = 0;
88 protected final static int TOUCH_STATE_SCROLLING = 1;
89 protected final static int TOUCH_STATE_PREV_PAGE = 2;
90 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070091 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070096
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070098
Michael Jurka7426c422010-11-11 15:23:47 -080099 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700100 private int mPagingTouchSlop;
101 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700102 protected int mPageSpacing;
103 protected int mPageLayoutPaddingTop;
104 protected int mPageLayoutPaddingBottom;
105 protected int mPageLayoutPaddingLeft;
106 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700107 protected int mPageLayoutWidthGap;
108 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700109 protected int mCellCountX;
110 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700111 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800112 protected boolean mAllowOverScroll = true;
113 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka8c920dd2011-01-20 14:16:56 -0800115 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800116 protected float mLayoutScale = 1.0f;
117
Michael Jurka5f1c5092010-09-03 14:15:02 -0700118 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka5f1c5092010-09-03 14:15:02 -0700120 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Winson Chung86f77532010-08-24 11:08:22 -0700122 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Winson Chung86f77532010-08-24 11:08:22 -0700124 private ArrayList<Boolean> mDirtyPageContent;
125 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700127 // choice modes
128 protected static final int CHOICE_MODE_NONE = 0;
129 protected static final int CHOICE_MODE_SINGLE = 1;
130 // Multiple selection mode is not supported by all Launcher actions atm
131 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700132
Michael Jurkae17e19c2010-09-28 11:01:39 -0700133 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700134 private ActionMode mActionMode;
135
Winson Chung04998342011-01-05 13:54:43 -0800136 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
137 // AllApps and Customize, and allows them to share holographic icons for the application view
138 // (which is in both).
139 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700140
Michael Jurka0142d492010-08-25 17:46:15 -0700141 // If true, syncPages and syncPageItems will be called to refresh pages
142 protected boolean mContentIsRefreshable = true;
143
144 // If true, modify alpha of neighboring pages as user scrolls left/right
145 protected boolean mFadeInAdjacentScreens = true;
146
147 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
148 // to switch to a new page
149 protected boolean mUsePagingTouchSlop = true;
150
151 // If true, the subclass should directly update mScrollX itself in its computeScroll method
152 // (SmoothPagedView does this)
153 protected boolean mDeferScrollUpdate = false;
154
Patrick Dubroy1262e362010-10-06 15:49:50 -0700155 protected boolean mIsPageMoving = false;
156
Winson Chung86f77532010-08-24 11:08:22 -0700157 public interface PageSwitchListener {
158 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 }
160
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 public PagedView(Context context) {
162 this(context, null);
163 }
164
165 public PagedView(Context context, AttributeSet attrs) {
166 this(context, attrs, 0);
167 }
168
169 public PagedView(Context context, AttributeSet attrs, int defStyle) {
170 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700171 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700172
Adam Cohen9c4949e2010-10-05 12:27:22 -0700173 TypedArray a = context.obtainStyledAttributes(attrs,
174 R.styleable.PagedView, defStyle, 0);
175 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
176 mPageLayoutPaddingTop = a.getDimensionPixelSize(
177 R.styleable.PagedView_pageLayoutPaddingTop, 10);
178 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
179 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
180 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
181 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
182 mPageLayoutPaddingRight = a.getDimensionPixelSize(
183 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700184 mPageLayoutWidthGap = a.getDimensionPixelSize(
185 R.styleable.PagedView_pageLayoutWidthGap, -1);
186 mPageLayoutHeightGap = a.getDimensionPixelSize(
187 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700188 a.recycle();
189
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700191 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 }
193
194 /**
195 * Initializes various states for this workspace.
196 */
Michael Jurka0142d492010-08-25 17:46:15 -0700197 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700198 mDirtyPageContent = new ArrayList<Boolean>();
199 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800200 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700201 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700202 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700203
204 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
205 mTouchSlop = configuration.getScaledTouchSlop();
206 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
207 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
208 }
209
Winson Chung86f77532010-08-24 11:08:22 -0700210 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
211 mPageSwitchListener = pageSwitchListener;
212 if (mPageSwitchListener != null) {
213 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 }
215 }
216
217 /**
Winson Chung86f77532010-08-24 11:08:22 -0700218 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 *
Winson Chung86f77532010-08-24 11:08:22 -0700220 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 */
Winson Chung86f77532010-08-24 11:08:22 -0700222 int getCurrentPage() {
223 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 }
225
Winson Chung86f77532010-08-24 11:08:22 -0700226 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 return getChildCount();
228 }
229
Winson Chung86f77532010-08-24 11:08:22 -0700230 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 return getChildAt(index);
232 }
233
234 int getScrollWidth() {
235 return getWidth();
236 }
237
Adam Cohenbe909342011-01-28 17:02:58 -0800238 public int getTouchState() {
239 return mTouchState;
240 }
241
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800243 * Updates the scroll of the current page immediately to its final scroll position. We use this
244 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
245 * the previous tab page.
246 */
247 protected void updateCurrentPageScroll() {
248 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
249 scrollTo(newX, 0);
250 mScroller.setFinalX(newX);
251 }
252
253 /**
Winson Chung86f77532010-08-24 11:08:22 -0700254 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700255 */
Winson Chung86f77532010-08-24 11:08:22 -0700256 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800257 if (!mScroller.isFinished()) {
258 mScroller.abortAnimation();
259 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800260 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
261 // the default
262 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800263 return;
264 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700265
Winson Chung86f77532010-08-24 11:08:22 -0700266 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800267 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700268 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800269 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 }
271
Michael Jurka0142d492010-08-25 17:46:15 -0700272 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700273 if (mPageSwitchListener != null) {
274 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 }
276 }
277
Michael Jurkace7e05f2011-02-01 22:02:35 -0800278 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700279 mIsPageMoving = true;
280 onPageBeginMoving();
281 }
282
Michael Jurkace7e05f2011-02-01 22:02:35 -0800283 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700284 onPageEndMoving();
285 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700286 }
287
288 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700289 protected void onPageBeginMoving() {
290 }
291
292 // a method that subclasses can override to add behavior
293 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700294 }
295
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 /**
Winson Chung86f77532010-08-24 11:08:22 -0700297 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 *
299 * @param l The listener used to respond to long clicks.
300 */
301 @Override
302 public void setOnLongClickListener(OnLongClickListener l) {
303 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700304 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700306 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 }
308 }
309
310 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800311 public void scrollBy(int x, int y) {
312 scrollTo(mUnboundedScrollX + x, mScrollY + y);
313 }
314
315 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700316 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800317 mUnboundedScrollX = x;
318
319 if (x < 0) {
320 super.scrollTo(0, y);
321 if (mAllowOverScroll) {
322 overScroll(x);
323 }
324 } else if (x > mMaxScrollX) {
325 super.scrollTo(mMaxScrollX, y);
326 if (mAllowOverScroll) {
327 overScroll(x - mMaxScrollX);
328 }
329 } else {
330 super.scrollTo(x, y);
331 }
332
Michael Jurka0142d492010-08-25 17:46:15 -0700333 mTouchX = x;
334 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
335 }
336
337 // we moved this functionality to a helper function so SmoothPagedView can reuse it
338 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700340 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700342 invalidate();
343 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700344 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700345 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700346 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700347 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700348 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800349 // We don't want to trigger a page end moving unless the page has settled
350 // and the user has stopped scrolling
351 if (mTouchState == TOUCH_STATE_REST) {
352 pageEndMoving();
353 }
Michael Jurka0142d492010-08-25 17:46:15 -0700354 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700355 }
Michael Jurka0142d492010-08-25 17:46:15 -0700356 return false;
357 }
358
359 @Override
360 public void computeScroll() {
361 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 }
363
364 @Override
365 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
366 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
367 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
368 if (widthMode != MeasureSpec.EXACTLY) {
369 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
370 }
371
Adam Lesinski6b879f02010-11-04 16:15:23 -0700372 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
373 * of the All apps view on XLarge displays to not take up more space then it needs. Width
374 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
375 * each page to have the same width.
376 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700378 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
379 int maxChildHeight = 0;
380
381 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700382
383 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700384 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700385 final int childCount = getChildCount();
386 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700387 // disallowing padding in paged view (just pass 0)
388 final View child = getChildAt(i);
389 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
390
391 int childWidthMode;
392 if (lp.width == LayoutParams.WRAP_CONTENT) {
393 childWidthMode = MeasureSpec.AT_MOST;
394 } else {
395 childWidthMode = MeasureSpec.EXACTLY;
396 }
397
398 int childHeightMode;
399 if (lp.height == LayoutParams.WRAP_CONTENT) {
400 childHeightMode = MeasureSpec.AT_MOST;
401 } else {
402 childHeightMode = MeasureSpec.EXACTLY;
403 }
404
405 final int childWidthMeasureSpec =
406 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
407 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700408 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700409
410 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700411 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
412 }
413
414 if (heightMode == MeasureSpec.AT_MOST) {
415 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800417 if (childCount > 0) {
418 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
419 } else {
420 mMaxScrollX = 0;
421 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700422
423 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700424 }
425
Michael Jurka8c920dd2011-01-20 14:16:56 -0800426 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800427 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
428 int delta = newX - mScrollX;
429
Michael Jurka8c920dd2011-01-20 14:16:56 -0800430 final int pageCount = getChildCount();
431 for (int i = 0; i < pageCount; i++) {
432 View page = (View) getChildAt(i);
433 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800434 }
435 setCurrentPage(newCurrentPage);
436 }
437
Michael Jurka8c920dd2011-01-20 14:16:56 -0800438 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
439 // 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 -0800440 // tightens the layout accordingly
441 public void setLayoutScale(float childrenScale) {
442 mLayoutScale = childrenScale;
443
444 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
445 int childCount = getChildCount();
446 float childrenX[] = new float[childCount];
447 float childrenY[] = new float[childCount];
448 for (int i = 0; i < childCount; i++) {
449 final View child = getChildAt(i);
450 childrenX[i] = child.getX();
451 childrenY[i] = child.getY();
452 }
453 onLayout(false, mLeft, mTop, mRight, mBottom);
454 for (int i = 0; i < childCount; i++) {
455 final View child = getChildAt(i);
456 child.setX(childrenX[i]);
457 child.setY(childrenY[i]);
458 }
459 // Also, the page offset has changed (since the pages are now smaller);
460 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800461 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800462 }
463
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700465 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700466 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
467 setHorizontalScrollBarEnabled(false);
468 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
469 scrollTo(newX, 0);
470 mScroller.setFinalX(newX);
471 setHorizontalScrollBarEnabled(true);
472 mFirstLayout = false;
473 }
474
Adam Lesinski6b879f02010-11-04 16:15:23 -0700475 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 final int childCount = getChildCount();
477 int childLeft = 0;
478 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700479 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 }
481
482 for (int i = 0; i < childCount; i++) {
483 final View child = getChildAt(i);
484 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800485 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700486 final int childHeight = child.getMeasuredHeight();
487 int childTop = mPaddingTop;
488 if (mCenterPagesVertically) {
489 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
490 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800491
Adam Lesinski6b879f02010-11-04 16:15:23 -0700492 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800493 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700494 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 }
496 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800497 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
498 mFirstLayout = false;
499 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 }
501
Winson Chunge3193b92010-09-10 11:44:42 -0700502 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700503 if (mFadeInAdjacentScreens) {
504 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700505 int halfScreenSize = getMeasuredWidth() / 2;
506 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700507 final int childCount = getChildCount();
508 for (int i = 0; i < childCount; ++i) {
509 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800510 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700511 int halfChildWidth = (childWidth / 2);
512 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700513
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700514 // On the first layout, we may not have a width nor a proper offset, so for now
515 // we should just assume full page width (and calculate the offset according to
516 // that).
517 if (childWidth <= 0) {
518 childWidth = getMeasuredWidth();
519 childCenter = (i * childWidth) + (childWidth / 2);
520 }
521
Winson Chunge8878e32010-09-15 20:37:09 -0700522 int d = halfChildWidth;
523 int distanceFromScreenCenter = childCenter - screenCenter;
524 if (distanceFromScreenCenter > 0) {
525 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700527 }
Michael Jurka0142d492010-08-25 17:46:15 -0700528 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700529 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800530 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700531 }
Michael Jurka0142d492010-08-25 17:46:15 -0700532 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700533 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700534
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700535 // Preventing potential divide-by-zero
536 d = Math.max(1, d);
537
Winson Chunge8878e32010-09-15 20:37:09 -0700538 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
539 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
540 float alpha = 1.0f - dimAlpha;
541
Adam Cohenf34bab52010-09-30 14:11:56 -0700542 if (alpha < ALPHA_QUANTIZE_LEVEL) {
543 alpha = 0.0f;
544 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
545 alpha = 1.0f;
546 }
547
Michael Jurkacfdb0962011-02-04 01:38:00 -0800548 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700549 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800550 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700551 }
Michael Jurka0142d492010-08-25 17:46:15 -0700552 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 }
554 }
Winson Chunge3193b92010-09-10 11:44:42 -0700555 }
556
Adam Cohenf34bab52010-09-30 14:11:56 -0700557 protected void screenScrolled(int screenCenter) {
558 }
559
Winson Chunge3193b92010-09-10 11:44:42 -0700560 @Override
561 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700562 int halfScreenSize = getMeasuredWidth() / 2;
563 int screenCenter = mScrollX + halfScreenSize;
564
565 if (screenCenter != mLastScreenCenter) {
566 screenScrolled(screenCenter);
567 updateAdjacentPagesAlpha();
568 mLastScreenCenter = screenCenter;
569 }
Michael Jurka0142d492010-08-25 17:46:15 -0700570
571 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700572 // As an optimization, this code assumes that all pages have the same width as the 0th
573 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700574 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700575 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800576 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700577 final int screenWidth = getMeasuredWidth();
578 int x = getRelativeChildOffset(0) + pageWidth;
579 int leftScreen = 0;
580 int rightScreen = 0;
581 while (x <= mScrollX) {
582 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800583 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700584 }
585 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800586 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700587 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800588 if (rightScreen < pageCount) {
589 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
590 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700591 }
592 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700593
Michael Jurkac4fb9172010-09-02 17:19:20 -0700594 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800595 // Clip to the bounds
596 canvas.save();
597 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
598 mScrollY + mBottom - mTop);
599
Michael Jurkac0759f52011-02-03 16:47:14 -0800600 for (int i = 0; i < pageCount; i++) {
601 View child = getChildAt(i);
602 if (child != null && child instanceof PagedViewCellLayout) {
603 boolean willBeDrawn = i >= leftScreen && i <= rightScreen;
604 if (!willBeDrawn) {
605 ((PagedViewCellLayout)child).destroyHardwareLayers();
606 }
607 }
608 }
609
Michael Jurkac4fb9172010-09-02 17:19:20 -0700610 for (int i = leftScreen; i <= rightScreen; i++) {
Michael Jurkac0759f52011-02-03 16:47:14 -0800611 View child = getChildAt(i);
612 if (child != null && child instanceof PagedViewCellLayout) {
613 ((PagedViewCellLayout)child).createHardwareLayers();
614 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700615 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 Cohenaefd4e12011-02-14 16:39:38 -08001003 boolean isSignificantMove = mTotalMotionX > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001004 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001005
1006 // 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));
1011 if (Math.abs(deltaX) > pageWidth / 3 &&
1012 Math.signum(velocityX) != Math.signum(deltaX)) {
1013 returnToOriginalPage = true;
1014 }
1015
1016 boolean isFling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING &&
1017 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 Chung321e9ee2010-08-09 13:37:56 -07001142 protected int getRelativeChildOffset(int index) {
1143 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1144 }
1145
1146 protected int getChildOffset(int index) {
1147 if (getChildCount() == 0)
1148 return 0;
1149
1150 int offset = getRelativeChildOffset(0);
1151 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 }
1154 return offset;
1155 }
1156
Michael Jurkad3ef3062010-11-23 16:23:58 -08001157 protected int getScaledMeasuredWidth(View child) {
1158 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1159 }
1160
Adam Cohend19d3ca2010-09-15 14:43:42 -07001161 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 int minDistanceFromScreenCenter = getMeasuredWidth();
1163 int minDistanceFromScreenCenterIndex = -1;
1164 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1165 final int childCount = getChildCount();
1166 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001167 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001168 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 int halfChildWidth = (childWidth / 2);
1170 int childCenter = getChildOffset(i) + halfChildWidth;
1171 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1172 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1173 minDistanceFromScreenCenter = distanceFromScreenCenter;
1174 minDistanceFromScreenCenterIndex = i;
1175 }
1176 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001177 return minDistanceFromScreenCenterIndex;
1178 }
1179
1180 protected void snapToDestination() {
1181 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 }
1183
Adam Cohene0f66b52010-11-23 15:06:07 -08001184 private static class ScrollInterpolator implements Interpolator {
1185 public ScrollInterpolator() {
1186 }
1187
1188 public float getInterpolation(float t) {
1189 t -= 1.0f;
1190 return t*t*t*t*t + 1;
1191 }
1192 }
1193
1194 // We want the duration of the page snap animation to be influenced by the distance that
1195 // the screen has to travel, however, we don't want this duration to be effected in a
1196 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1197 // of travel has on the overall snap duration.
1198 float distanceInfluenceForSnapDuration(float f) {
1199 f -= 0.5f; // center the values about 0.
1200 f *= 0.3f * Math.PI / 2.0f;
1201 return (float) Math.sin(f);
1202 }
1203
Michael Jurka0142d492010-08-25 17:46:15 -07001204 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001205 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1206 int halfScreenSize = getMeasuredWidth() / 2;
1207
1208 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1209 int delta = newX - mUnboundedScrollX;
1210 int duration = 0;
1211
1212 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1213 // If the velocity is low enough, then treat this more as an automatic page advance
1214 // as opposed to an apparent physical response to flinging
1215 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1216 return;
1217 }
1218
1219 // Here we compute a "distance" that will be used in the computation of the overall
1220 // snap duration. This is a function of the actual distance that needs to be traveled;
1221 // we keep this value close to half screen size in order to reduce the variance in snap
1222 // duration as a function of the distance the page needs to travel.
1223 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1224 float distance = halfScreenSize + halfScreenSize *
1225 distanceInfluenceForSnapDuration(distanceRatio);
1226
1227 velocity = Math.abs(velocity);
1228 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1229
1230 // we want the page's snap velocity to approximately match the velocity at which the
1231 // user flings, so we scale the duration by a value near to the derivative of the scroll
1232 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1233 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1234
1235 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001236 }
1237
1238 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001239 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 }
1241
Michael Jurka0142d492010-08-25 17:46:15 -07001242 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001243 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001244
Winson Chung86f77532010-08-24 11:08:22 -07001245 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001246 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001248 snapToPage(whichPage, delta, duration);
1249 }
1250
1251 protected void snapToPage(int whichPage, int delta, int duration) {
1252 mNextPage = whichPage;
1253
1254 View focusedChild = getFocusedChild();
1255 if (focusedChild != null && whichPage != mCurrentPage &&
1256 focusedChild == getChildAt(mCurrentPage)) {
1257 focusedChild.clearFocus();
1258 }
1259
1260 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 awakenScrollBars(duration);
1262 if (duration == 0) {
1263 duration = Math.abs(delta);
1264 }
1265
1266 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001267 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001268
1269 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001270 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001271 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 invalidate();
1273 }
1274
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 public void scrollLeft() {
1276 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001277 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001279 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 }
1281 }
1282
1283 public void scrollRight() {
1284 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001285 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001287 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 }
1289 }
1290
Winson Chung86f77532010-08-24 11:08:22 -07001291 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001292 int result = -1;
1293 if (v != null) {
1294 ViewParent vp = v.getParent();
1295 int count = getChildCount();
1296 for (int i = 0; i < count; i++) {
1297 if (vp == getChildAt(i)) {
1298 return i;
1299 }
1300 }
1301 }
1302 return result;
1303 }
1304
1305 /**
1306 * @return True is long presses are still allowed for the current touch
1307 */
1308 public boolean allowLongPress() {
1309 return mAllowLongPress;
1310 }
1311
Michael Jurka0142d492010-08-25 17:46:15 -07001312 /**
1313 * Set true to allow long-press events to be triggered, usually checked by
1314 * {@link Launcher} to accept or block dpad-initiated long-presses.
1315 */
1316 public void setAllowLongPress(boolean allowLongPress) {
1317 mAllowLongPress = allowLongPress;
1318 }
1319
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001321 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001322
1323 SavedState(Parcelable superState) {
1324 super(superState);
1325 }
1326
1327 private SavedState(Parcel in) {
1328 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001329 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 }
1331
1332 @Override
1333 public void writeToParcel(Parcel out, int flags) {
1334 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001335 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 }
1337
1338 public static final Parcelable.Creator<SavedState> CREATOR =
1339 new Parcelable.Creator<SavedState>() {
1340 public SavedState createFromParcel(Parcel in) {
1341 return new SavedState(in);
1342 }
1343
1344 public SavedState[] newArray(int size) {
1345 return new SavedState[size];
1346 }
1347 };
1348 }
1349
Winson Chung86f77532010-08-24 11:08:22 -07001350 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001351 if (mContentIsRefreshable) {
1352 final int count = getChildCount();
1353 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001354 int lowerPageBound = getAssociatedLowerPageBound(page);
1355 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001356 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001357 Page layout = (Page) getChildAt(i);
1358 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001359 if (lowerPageBound <= i && i <= upperPageBound) {
1360 if (mDirtyPageContent.get(i)) {
1361 syncPageItems(i);
1362 mDirtyPageContent.set(i, false);
1363 }
1364 } else {
1365 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001366 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001367 }
1368 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001369 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001370 }
1371 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001372 }
1373 }
1374
Winson Chunge3193b92010-09-10 11:44:42 -07001375 protected int getAssociatedLowerPageBound(int page) {
1376 return Math.max(0, page - 1);
1377 }
1378 protected int getAssociatedUpperPageBound(int page) {
1379 final int count = getChildCount();
1380 return Math.min(page + 1, count - 1);
1381 }
1382
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001383 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001384 if (isChoiceMode(CHOICE_MODE_NONE)) {
1385 mChoiceMode = mode;
1386 mActionMode = startActionMode(callback);
1387 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001388 }
1389
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001390 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001391 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001392 mChoiceMode = CHOICE_MODE_NONE;
1393 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001394 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001395 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001396 }
1397 }
1398
1399 protected boolean isChoiceMode(int mode) {
1400 return mChoiceMode == mode;
1401 }
1402
1403 protected ArrayList<Checkable> getCheckedGrandchildren() {
1404 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1405 final int childCount = getChildCount();
1406 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001407 Page layout = (Page) getChildAt(i);
1408 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001409 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001410 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001411 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001412 checked.add((Checkable) v);
1413 }
1414 }
1415 }
1416 return checked;
1417 }
1418
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001419 /**
1420 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1421 * Otherwise, returns null.
1422 */
1423 protected Checkable getSingleCheckedGrandchild() {
1424 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1425 final int childCount = getChildCount();
1426 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001427 Page layout = (Page) getChildAt(i);
1428 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001429 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001430 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001431 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1432 return (Checkable) v;
1433 }
1434 }
1435 }
1436 }
1437 return null;
1438 }
1439
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001440 public Object getChosenItem() {
1441 View checkedView = (View) getSingleCheckedGrandchild();
1442 if (checkedView != null) {
1443 return checkedView.getTag();
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}