blob: 490417aca1870c531d4aeefdf5a3c4da7adc8fea [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;
82 protected float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070083 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070084
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected final static int TOUCH_STATE_REST = 0;
86 protected final static int TOUCH_STATE_SCROLLING = 1;
87 protected final static int TOUCH_STATE_PREV_PAGE = 2;
88 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070089 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070090
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070094
Michael Jurka7426c422010-11-11 15:23:47 -080095 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070096
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070098 private int mPagingTouchSlop;
99 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700100 protected int mPageSpacing;
101 protected int mPageLayoutPaddingTop;
102 protected int mPageLayoutPaddingBottom;
103 protected int mPageLayoutPaddingLeft;
104 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700105 protected int mPageLayoutWidthGap;
106 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700107 protected int mCellCountX;
108 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700109 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800110 protected boolean mAllowOverScroll = true;
111 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka8c920dd2011-01-20 14:16:56 -0800113 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800114 protected float mLayoutScale = 1.0f;
115
Michael Jurka5f1c5092010-09-03 14:15:02 -0700116 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka5f1c5092010-09-03 14:15:02 -0700118 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Winson Chung86f77532010-08-24 11:08:22 -0700120 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Winson Chung86f77532010-08-24 11:08:22 -0700122 private ArrayList<Boolean> mDirtyPageContent;
123 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700125 // choice modes
126 protected static final int CHOICE_MODE_NONE = 0;
127 protected static final int CHOICE_MODE_SINGLE = 1;
128 // Multiple selection mode is not supported by all Launcher actions atm
129 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700130
Michael Jurkae17e19c2010-09-28 11:01:39 -0700131 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700132 private ActionMode mActionMode;
133
Winson Chung04998342011-01-05 13:54:43 -0800134 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
135 // AllApps and Customize, and allows them to share holographic icons for the application view
136 // (which is in both).
137 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700138
Michael Jurka0142d492010-08-25 17:46:15 -0700139 // If true, syncPages and syncPageItems will be called to refresh pages
140 protected boolean mContentIsRefreshable = true;
141
142 // If true, modify alpha of neighboring pages as user scrolls left/right
143 protected boolean mFadeInAdjacentScreens = true;
144
145 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
146 // to switch to a new page
147 protected boolean mUsePagingTouchSlop = true;
148
149 // If true, the subclass should directly update mScrollX itself in its computeScroll method
150 // (SmoothPagedView does this)
151 protected boolean mDeferScrollUpdate = false;
152
Patrick Dubroy1262e362010-10-06 15:49:50 -0700153 protected boolean mIsPageMoving = false;
154
Winson Chung86f77532010-08-24 11:08:22 -0700155 public interface PageSwitchListener {
156 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 }
158
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 public PagedView(Context context) {
160 this(context, null);
161 }
162
163 public PagedView(Context context, AttributeSet attrs) {
164 this(context, attrs, 0);
165 }
166
167 public PagedView(Context context, AttributeSet attrs, int defStyle) {
168 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700169 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700170
Adam Cohen9c4949e2010-10-05 12:27:22 -0700171 TypedArray a = context.obtainStyledAttributes(attrs,
172 R.styleable.PagedView, defStyle, 0);
173 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
174 mPageLayoutPaddingTop = a.getDimensionPixelSize(
175 R.styleable.PagedView_pageLayoutPaddingTop, 10);
176 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
177 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
178 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
179 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
180 mPageLayoutPaddingRight = a.getDimensionPixelSize(
181 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700182 mPageLayoutWidthGap = a.getDimensionPixelSize(
183 R.styleable.PagedView_pageLayoutWidthGap, -1);
184 mPageLayoutHeightGap = a.getDimensionPixelSize(
185 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700186 a.recycle();
187
Winson Chung321e9ee2010-08-09 13:37:56 -0700188 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700189 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 }
191
192 /**
193 * Initializes various states for this workspace.
194 */
Michael Jurka0142d492010-08-25 17:46:15 -0700195 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700196 mDirtyPageContent = new ArrayList<Boolean>();
197 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800198 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700199 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700200 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700201
202 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
203 mTouchSlop = configuration.getScaledTouchSlop();
204 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
205 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
206 }
207
Winson Chung86f77532010-08-24 11:08:22 -0700208 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
209 mPageSwitchListener = pageSwitchListener;
210 if (mPageSwitchListener != null) {
211 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 }
213 }
214
215 /**
Winson Chung86f77532010-08-24 11:08:22 -0700216 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 *
Winson Chung86f77532010-08-24 11:08:22 -0700218 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 */
Winson Chung86f77532010-08-24 11:08:22 -0700220 int getCurrentPage() {
221 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 }
223
Winson Chung86f77532010-08-24 11:08:22 -0700224 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 return getChildCount();
226 }
227
Winson Chung86f77532010-08-24 11:08:22 -0700228 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 return getChildAt(index);
230 }
231
232 int getScrollWidth() {
233 return getWidth();
234 }
235
Adam Cohenbe909342011-01-28 17:02:58 -0800236 public int getTouchState() {
237 return mTouchState;
238 }
239
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800241 * Updates the scroll of the current page immediately to its final scroll position. We use this
242 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
243 * the previous tab page.
244 */
245 protected void updateCurrentPageScroll() {
246 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
247 scrollTo(newX, 0);
248 mScroller.setFinalX(newX);
249 }
250
251 /**
Winson Chung86f77532010-08-24 11:08:22 -0700252 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700253 */
Winson Chung86f77532010-08-24 11:08:22 -0700254 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800255 if (!mScroller.isFinished()) {
256 mScroller.abortAnimation();
257 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800258 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
259 // the default
260 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800261 return;
262 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700263
Winson Chung86f77532010-08-24 11:08:22 -0700264 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800265 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700266 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800267 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 }
269
Michael Jurka0142d492010-08-25 17:46:15 -0700270 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700271 if (mPageSwitchListener != null) {
272 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 }
274 }
275
Patrick Dubroy1262e362010-10-06 15:49:50 -0700276 private void pageBeginMoving() {
277 mIsPageMoving = true;
278 onPageBeginMoving();
279 }
280
281 private void pageEndMoving() {
282 onPageEndMoving();
283 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700284 }
285
286 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700287 protected void onPageBeginMoving() {
288 }
289
290 // a method that subclasses can override to add behavior
291 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700292 }
293
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 /**
Winson Chung86f77532010-08-24 11:08:22 -0700295 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 *
297 * @param l The listener used to respond to long clicks.
298 */
299 @Override
300 public void setOnLongClickListener(OnLongClickListener l) {
301 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700302 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700304 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 }
306 }
307
308 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800309 public void scrollBy(int x, int y) {
310 scrollTo(mUnboundedScrollX + x, mScrollY + y);
311 }
312
313 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700314 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800315 mUnboundedScrollX = x;
316
317 if (x < 0) {
318 super.scrollTo(0, y);
319 if (mAllowOverScroll) {
320 overScroll(x);
321 }
322 } else if (x > mMaxScrollX) {
323 super.scrollTo(mMaxScrollX, y);
324 if (mAllowOverScroll) {
325 overScroll(x - mMaxScrollX);
326 }
327 } else {
328 super.scrollTo(x, y);
329 }
330
Michael Jurka0142d492010-08-25 17:46:15 -0700331 mTouchX = x;
332 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
333 }
334
335 // we moved this functionality to a helper function so SmoothPagedView can reuse it
336 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700338 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700340 invalidate();
341 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700342 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700343 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700344 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700345 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700346 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800347 // We don't want to trigger a page end moving unless the page has settled
348 // and the user has stopped scrolling
349 if (mTouchState == TOUCH_STATE_REST) {
350 pageEndMoving();
351 }
Michael Jurka0142d492010-08-25 17:46:15 -0700352 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 }
Michael Jurka0142d492010-08-25 17:46:15 -0700354 return false;
355 }
356
357 @Override
358 public void computeScroll() {
359 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 }
361
362 @Override
363 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
364 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
365 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
366 if (widthMode != MeasureSpec.EXACTLY) {
367 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
368 }
369
Adam Lesinski6b879f02010-11-04 16:15:23 -0700370 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
371 * of the All apps view on XLarge displays to not take up more space then it needs. Width
372 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
373 * each page to have the same width.
374 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700376 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
377 int maxChildHeight = 0;
378
379 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700380
381 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700382 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700383 final int childCount = getChildCount();
384 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700385 // disallowing padding in paged view (just pass 0)
386 final View child = getChildAt(i);
387 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
388
389 int childWidthMode;
390 if (lp.width == LayoutParams.WRAP_CONTENT) {
391 childWidthMode = MeasureSpec.AT_MOST;
392 } else {
393 childWidthMode = MeasureSpec.EXACTLY;
394 }
395
396 int childHeightMode;
397 if (lp.height == LayoutParams.WRAP_CONTENT) {
398 childHeightMode = MeasureSpec.AT_MOST;
399 } else {
400 childHeightMode = MeasureSpec.EXACTLY;
401 }
402
403 final int childWidthMeasureSpec =
404 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
405 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700406 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700407
408 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700409 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
410 }
411
412 if (heightMode == MeasureSpec.AT_MOST) {
413 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700414 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800415 if (childCount > 0) {
416 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
417 } else {
418 mMaxScrollX = 0;
419 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700420
421 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 }
423
Michael Jurka8c920dd2011-01-20 14:16:56 -0800424 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800425 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
426 int delta = newX - mScrollX;
427
Michael Jurka8c920dd2011-01-20 14:16:56 -0800428 final int pageCount = getChildCount();
429 for (int i = 0; i < pageCount; i++) {
430 View page = (View) getChildAt(i);
431 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800432 }
433 setCurrentPage(newCurrentPage);
434 }
435
Michael Jurka8c920dd2011-01-20 14:16:56 -0800436 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
437 // 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 -0800438 // tightens the layout accordingly
439 public void setLayoutScale(float childrenScale) {
440 mLayoutScale = childrenScale;
441
442 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
443 int childCount = getChildCount();
444 float childrenX[] = new float[childCount];
445 float childrenY[] = new float[childCount];
446 for (int i = 0; i < childCount; i++) {
447 final View child = getChildAt(i);
448 childrenX[i] = child.getX();
449 childrenY[i] = child.getY();
450 }
451 onLayout(false, mLeft, mTop, mRight, mBottom);
452 for (int i = 0; i < childCount; i++) {
453 final View child = getChildAt(i);
454 child.setX(childrenX[i]);
455 child.setY(childrenY[i]);
456 }
457 // Also, the page offset has changed (since the pages are now smaller);
458 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800459 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800460 }
461
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700463 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700464 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
465 setHorizontalScrollBarEnabled(false);
466 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
467 scrollTo(newX, 0);
468 mScroller.setFinalX(newX);
469 setHorizontalScrollBarEnabled(true);
470 mFirstLayout = false;
471 }
472
Adam Lesinski6b879f02010-11-04 16:15:23 -0700473 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 final int childCount = getChildCount();
475 int childLeft = 0;
476 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700477 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
479
480 for (int i = 0; i < childCount; i++) {
481 final View child = getChildAt(i);
482 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800483 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700484 final int childHeight = child.getMeasuredHeight();
485 int childTop = mPaddingTop;
486 if (mCenterPagesVertically) {
487 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
488 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800489
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800491 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700492 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700493 }
494 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800495 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
496 mFirstLayout = false;
497 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
499
Winson Chunge3193b92010-09-10 11:44:42 -0700500 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700501 if (mFadeInAdjacentScreens) {
502 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700503 int halfScreenSize = getMeasuredWidth() / 2;
504 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700505 final int childCount = getChildCount();
506 for (int i = 0; i < childCount; ++i) {
507 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700509 int halfChildWidth = (childWidth / 2);
510 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700511
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700512 // On the first layout, we may not have a width nor a proper offset, so for now
513 // we should just assume full page width (and calculate the offset according to
514 // that).
515 if (childWidth <= 0) {
516 childWidth = getMeasuredWidth();
517 childCenter = (i * childWidth) + (childWidth / 2);
518 }
519
Winson Chunge8878e32010-09-15 20:37:09 -0700520 int d = halfChildWidth;
521 int distanceFromScreenCenter = childCenter - screenCenter;
522 if (distanceFromScreenCenter > 0) {
523 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700525 }
Michael Jurka0142d492010-08-25 17:46:15 -0700526 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700527 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800528 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700529 }
Michael Jurka0142d492010-08-25 17:46:15 -0700530 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700531 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700532
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700533 // Preventing potential divide-by-zero
534 d = Math.max(1, d);
535
Winson Chunge8878e32010-09-15 20:37:09 -0700536 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
537 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
538 float alpha = 1.0f - dimAlpha;
539
Adam Cohenf34bab52010-09-30 14:11:56 -0700540 if (alpha < ALPHA_QUANTIZE_LEVEL) {
541 alpha = 0.0f;
542 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
543 alpha = 1.0f;
544 }
545
Michael Jurka0142d492010-08-25 17:46:15 -0700546 if (Float.compare(alpha, layout.getAlpha()) != 0) {
547 layout.setAlpha(alpha);
548 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 }
Michael Jurka0142d492010-08-25 17:46:15 -0700550 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700551 }
552 }
Winson Chunge3193b92010-09-10 11:44:42 -0700553 }
554
Adam Cohenf34bab52010-09-30 14:11:56 -0700555 protected void screenScrolled(int screenCenter) {
556 }
557
Winson Chunge3193b92010-09-10 11:44:42 -0700558 @Override
559 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700560 int halfScreenSize = getMeasuredWidth() / 2;
561 int screenCenter = mScrollX + halfScreenSize;
562
563 if (screenCenter != mLastScreenCenter) {
564 screenScrolled(screenCenter);
565 updateAdjacentPagesAlpha();
566 mLastScreenCenter = screenCenter;
567 }
Michael Jurka0142d492010-08-25 17:46:15 -0700568
569 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700570 // As an optimization, this code assumes that all pages have the same width as the 0th
571 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700572 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700573 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800574 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700575 final int screenWidth = getMeasuredWidth();
576 int x = getRelativeChildOffset(0) + pageWidth;
577 int leftScreen = 0;
578 int rightScreen = 0;
579 while (x <= mScrollX) {
580 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800581 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700582 }
583 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800584 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700585 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800586 if (rightScreen < pageCount) {
587 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
588 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700589 }
590 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700591
Michael Jurkac4fb9172010-09-02 17:19:20 -0700592 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800593 // Clip to the bounds
594 canvas.save();
595 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
596 mScrollY + mBottom - mTop);
597
Michael Jurkac4fb9172010-09-02 17:19:20 -0700598 for (int i = leftScreen; i <= rightScreen; i++) {
599 drawChild(canvas, getChildAt(i), drawingTime);
600 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800601 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700602 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 }
604
605 @Override
606 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700607 int page = indexOfChild(child);
608 if (page != mCurrentPage || !mScroller.isFinished()) {
609 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 return true;
611 }
612 return false;
613 }
614
615 @Override
616 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700617 int focusablePage;
618 if (mNextPage != INVALID_PAGE) {
619 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700621 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700622 }
Winson Chung86f77532010-08-24 11:08:22 -0700623 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 if (v != null) {
625 v.requestFocus(direction, previouslyFocusedRect);
626 }
627 return false;
628 }
629
630 @Override
631 public boolean dispatchUnhandledMove(View focused, int direction) {
632 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700633 if (getCurrentPage() > 0) {
634 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 return true;
636 }
637 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700638 if (getCurrentPage() < getPageCount() - 1) {
639 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 return true;
641 }
642 }
643 return super.dispatchUnhandledMove(focused, direction);
644 }
645
646 @Override
647 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700648 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
649 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 }
651 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700652 if (mCurrentPage > 0) {
653 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700654 }
655 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700656 if (mCurrentPage < getPageCount() - 1) {
657 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 }
659 }
660 }
661
662 /**
663 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700664 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 *
Winson Chung86f77532010-08-24 11:08:22 -0700666 * This happens when live folders requery, and if they're off page, they
667 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700668 */
669 @Override
670 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700671 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 View v = focused;
673 while (true) {
674 if (v == current) {
675 super.focusableViewAvailable(focused);
676 return;
677 }
678 if (v == this) {
679 return;
680 }
681 ViewParent parent = v.getParent();
682 if (parent instanceof View) {
683 v = (View)v.getParent();
684 } else {
685 return;
686 }
687 }
688 }
689
690 /**
691 * {@inheritDoc}
692 */
693 @Override
694 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
695 if (disallowIntercept) {
696 // We need to make sure to cancel our long press if
697 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700698 final View currentPage = getChildAt(mCurrentPage);
699 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700700 }
701 super.requestDisallowInterceptTouchEvent(disallowIntercept);
702 }
703
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800704 /**
705 * Return true if a tap at (x, y) should trigger a flip to the previous page.
706 */
707 protected boolean hitsPreviousPage(float x, float y) {
708 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
709 }
710
711 /**
712 * Return true if a tap at (x, y) should trigger a flip to the next page.
713 */
714 protected boolean hitsNextPage(float x, float y) {
715 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
716 }
717
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 @Override
719 public boolean onInterceptTouchEvent(MotionEvent ev) {
720 /*
721 * This method JUST determines whether we want to intercept the motion.
722 * If we return true, onTouchEvent will be called and we do the actual
723 * scrolling there.
724 */
725
Winson Chung45e1d6e2010-11-09 17:19:49 -0800726 // Skip touch handling if there are no pages to swipe
727 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
728
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 /*
730 * Shortcut the most recurring case: the user is in the dragging
731 * state and he is moving his finger. We want to intercept this
732 * motion.
733 */
734 final int action = ev.getAction();
735 if ((action == MotionEvent.ACTION_MOVE) &&
736 (mTouchState == TOUCH_STATE_SCROLLING)) {
737 return true;
738 }
739
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 switch (action & MotionEvent.ACTION_MASK) {
741 case MotionEvent.ACTION_MOVE: {
742 /*
743 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
744 * whether the user has moved far enough from his original down touch.
745 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700746 if (mActivePointerId != INVALID_POINTER) {
747 determineScrollingStart(ev);
748 break;
749 }
750 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
751 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
752 // i.e. fall through to the next case (don't break)
753 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
754 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 }
756
757 case MotionEvent.ACTION_DOWN: {
758 final float x = ev.getX();
759 final float y = ev.getY();
760 // Remember location of down touch
761 mDownMotionX = x;
762 mLastMotionX = x;
763 mLastMotionY = y;
764 mActivePointerId = ev.getPointerId(0);
765 mAllowLongPress = true;
766
767 /*
768 * If being flinged and user touches the screen, initiate drag;
769 * otherwise don't. mScroller.isFinished should be false when
770 * being flinged.
771 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700772 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700773 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
774 if (finishedScrolling) {
775 mTouchState = TOUCH_STATE_REST;
776 mScroller.abortAnimation();
777 } else {
778 mTouchState = TOUCH_STATE_SCROLLING;
779 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700780
Winson Chung86f77532010-08-24 11:08:22 -0700781 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800783 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800785 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800787 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 mTouchState = TOUCH_STATE_NEXT_PAGE;
789 }
790 }
791 }
792 break;
793 }
794
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800796 onWallpaperTap(ev);
797 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 mTouchState = TOUCH_STATE_REST;
799 mAllowLongPress = false;
800 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 break;
802
803 case MotionEvent.ACTION_POINTER_UP:
804 onSecondaryPointerUp(ev);
805 break;
806 }
807
808 /*
809 * The only time we want to intercept motion events is if we are in the
810 * drag mode.
811 */
812 return mTouchState != TOUCH_STATE_REST;
813 }
814
Winson Chung80baf5a2010-08-09 16:03:15 -0700815 protected void animateClickFeedback(View v, final Runnable r) {
816 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800817 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
818 loadAnimator(mContext, R.anim.paged_view_click_feedback);
819 anim.setTarget(v);
820 anim.addListener(new AnimatorListenerAdapter() {
821 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700822 r.run();
823 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700824 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800825 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700826 }
827
Adam Cohenf8d28232011-02-01 21:47:00 -0800828 protected void determineScrollingStart(MotionEvent ev) {
829 determineScrollingStart(ev, 1.0f);
830 }
831
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 /*
833 * Determines if we should change the touch state to start scrolling after the
834 * user moves their touch point too far.
835 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800836 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 /*
838 * Locally do absolute value. mLastMotionX is set to the y value
839 * of the down event.
840 */
841 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
842 final float x = ev.getX(pointerIndex);
843 final float y = ev.getY(pointerIndex);
844 final int xDiff = (int) Math.abs(x - mLastMotionX);
845 final int yDiff = (int) Math.abs(y - mLastMotionY);
846
Adam Cohenf8d28232011-02-01 21:47:00 -0800847 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 boolean xPaged = xDiff > mPagingTouchSlop;
849 boolean xMoved = xDiff > touchSlop;
850 boolean yMoved = yDiff > touchSlop;
851
Adam Cohenf8d28232011-02-01 21:47:00 -0800852 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700853 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 // Scroll if the user moved far enough along the X axis
855 mTouchState = TOUCH_STATE_SCROLLING;
856 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700857 mTouchX = mScrollX;
858 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
859 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 }
861 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800862 cancelCurrentPageLongPress();
863 }
864 }
865
866 protected void cancelCurrentPageLongPress() {
867 if (mAllowLongPress) {
868 mAllowLongPress = false;
869 // Try canceling the long press. It could also have been scheduled
870 // by a distant descendant, so use the mAllowLongPress flag to block
871 // everything
872 final View currentPage = getPageAt(mCurrentPage);
873 if (currentPage != null) {
874 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 }
876 }
877 }
878
Adam Cohene0f66b52010-11-23 15:06:07 -0800879 // This curve determines how the effect of scrolling over the limits of the page dimishes
880 // as the user pulls further and further from the bounds
881 private float overScrollInfluenceCurve(float f) {
882 f -= 1.0f;
883 return f * f * f + 1.0f;
884 }
885
Adam Cohen68d73932010-11-15 10:50:58 -0800886 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800887 int screenSize = getMeasuredWidth();
888
889 float f = (amount / screenSize);
890
891 if (f == 0) return;
892 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
893
Adam Cohen7bfc9792011-01-28 13:52:37 -0800894 // Clamp this factor, f, to -1 < f < 1
895 if (Math.abs(f) >= 1) {
896 f /= Math.abs(f);
897 }
898
Adam Cohene0f66b52010-11-23 15:06:07 -0800899 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800900 if (amount < 0) {
901 mScrollX = overScrollAmount;
902 } else {
903 mScrollX = mMaxScrollX + overScrollAmount;
904 }
905 invalidate();
906 }
907
Michael Jurkac5b262c2011-01-12 20:24:50 -0800908 protected float maxOverScroll() {
909 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
910 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
911 float f = 1.0f;
912 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
913 return OVERSCROLL_DAMP_FACTOR * f;
914 }
915
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 @Override
917 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800918 // Skip touch handling if there are no pages to swipe
919 if (getChildCount() <= 0) return super.onTouchEvent(ev);
920
Michael Jurkab8f06722010-10-10 15:58:46 -0700921 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700922
923 final int action = ev.getAction();
924
925 switch (action & MotionEvent.ACTION_MASK) {
926 case MotionEvent.ACTION_DOWN:
927 /*
928 * If being flinged and user touches, stop the fling. isFinished
929 * will be false if being flinged.
930 */
931 if (!mScroller.isFinished()) {
932 mScroller.abortAnimation();
933 }
934
935 // Remember where the motion event started
936 mDownMotionX = mLastMotionX = ev.getX();
937 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700938 if (mTouchState == TOUCH_STATE_SCROLLING) {
939 pageBeginMoving();
940 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 break;
942
943 case MotionEvent.ACTION_MOVE:
944 if (mTouchState == TOUCH_STATE_SCROLLING) {
945 // Scroll to follow the motion event
946 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
947 final float x = ev.getX(pointerIndex);
948 final int deltaX = (int) (mLastMotionX - x);
949 mLastMotionX = x;
950
Adam Cohen68d73932010-11-15 10:50:58 -0800951 if (deltaX != 0) {
952 mTouchX += deltaX;
953 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
954 if (!mDeferScrollUpdate) {
955 scrollBy(deltaX, 0);
956 } else {
957 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 }
959 } else {
960 awakenScrollBars();
961 }
Adam Cohen564976a2010-10-13 18:52:07 -0700962 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 determineScrollingStart(ev);
964 }
965 break;
966
967 case MotionEvent.ACTION_UP:
968 if (mTouchState == TOUCH_STATE_SCROLLING) {
969 final int activePointerId = mActivePointerId;
970 final int pointerIndex = ev.findPointerIndex(activePointerId);
971 final float x = ev.getX(pointerIndex);
972 final VelocityTracker velocityTracker = mVelocityTracker;
973 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
974 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700975 final int deltaX = (int) (x - mDownMotionX);
976 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
977 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700978
Michael Jurka0142d492010-08-25 17:46:15 -0700979 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700980 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800981 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700982 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700983 } else if ((isSignificantMove && deltaX < 0 ||
984 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700985 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700986 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 } else {
988 snapToDestination();
989 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800990 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 // at this point we have not moved beyond the touch slop
992 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
993 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700994 int nextPage = Math.max(0, mCurrentPage - 1);
995 if (nextPage != mCurrentPage) {
996 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 } else {
998 snapToDestination();
999 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001000 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 // at this point we have not moved beyond the touch slop
1002 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1003 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001004 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1005 if (nextPage != mCurrentPage) {
1006 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 } else {
1008 snapToDestination();
1009 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001010 } else {
1011 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 }
1013 mTouchState = TOUCH_STATE_REST;
1014 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001015 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 break;
1017
1018 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001019 if (mTouchState == TOUCH_STATE_SCROLLING) {
1020 snapToDestination();
1021 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 mTouchState = TOUCH_STATE_REST;
1023 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001024 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 break;
1026
1027 case MotionEvent.ACTION_POINTER_UP:
1028 onSecondaryPointerUp(ev);
1029 break;
1030 }
1031
1032 return true;
1033 }
1034
Michael Jurkab8f06722010-10-10 15:58:46 -07001035 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1036 if (mVelocityTracker == null) {
1037 mVelocityTracker = VelocityTracker.obtain();
1038 }
1039 mVelocityTracker.addMovement(ev);
1040 }
1041
1042 private void releaseVelocityTracker() {
1043 if (mVelocityTracker != null) {
1044 mVelocityTracker.recycle();
1045 mVelocityTracker = null;
1046 }
1047 }
1048
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 private void onSecondaryPointerUp(MotionEvent ev) {
1050 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1051 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1052 final int pointerId = ev.getPointerId(pointerIndex);
1053 if (pointerId == mActivePointerId) {
1054 // This was our active pointer going up. Choose a new
1055 // active pointer and adjust accordingly.
1056 // TODO: Make this decision more intelligent.
1057 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1058 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1059 mLastMotionY = ev.getY(newPointerIndex);
1060 mActivePointerId = ev.getPointerId(newPointerIndex);
1061 if (mVelocityTracker != null) {
1062 mVelocityTracker.clear();
1063 }
1064 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001065 if (mTouchState == TOUCH_STATE_REST) {
1066 onWallpaperTap(ev);
1067 }
1068 }
1069
1070 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 }
1072
1073 @Override
1074 public void requestChildFocus(View child, View focused) {
1075 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001076 int page = indexOfChild(child);
1077 if (page >= 0 && !isInTouchMode()) {
1078 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 }
1080 }
1081
Winson Chunge3193b92010-09-10 11:44:42 -07001082 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1083 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001084 int left;
1085 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001086 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001087 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001088 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001089 if (left <= relativeOffset && relativeOffset <= right) {
1090 return i;
1091 }
Winson Chunge3193b92010-09-10 11:44:42 -07001092 }
1093 return -1;
1094 }
1095
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 protected int getRelativeChildOffset(int index) {
1097 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1098 }
1099
1100 protected int getChildOffset(int index) {
1101 if (getChildCount() == 0)
1102 return 0;
1103
1104 int offset = getRelativeChildOffset(0);
1105 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001106 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 }
1108 return offset;
1109 }
1110
Michael Jurkad3ef3062010-11-23 16:23:58 -08001111 protected int getScaledMeasuredWidth(View child) {
1112 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1113 }
1114
Adam Cohend19d3ca2010-09-15 14:43:42 -07001115 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001116 int minDistanceFromScreenCenter = getMeasuredWidth();
1117 int minDistanceFromScreenCenterIndex = -1;
1118 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1119 final int childCount = getChildCount();
1120 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001121 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001122 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001123 int halfChildWidth = (childWidth / 2);
1124 int childCenter = getChildOffset(i) + halfChildWidth;
1125 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1126 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1127 minDistanceFromScreenCenter = distanceFromScreenCenter;
1128 minDistanceFromScreenCenterIndex = i;
1129 }
1130 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001131 return minDistanceFromScreenCenterIndex;
1132 }
1133
1134 protected void snapToDestination() {
1135 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001136 }
1137
Adam Cohene0f66b52010-11-23 15:06:07 -08001138 private static class ScrollInterpolator implements Interpolator {
1139 public ScrollInterpolator() {
1140 }
1141
1142 public float getInterpolation(float t) {
1143 t -= 1.0f;
1144 return t*t*t*t*t + 1;
1145 }
1146 }
1147
1148 // We want the duration of the page snap animation to be influenced by the distance that
1149 // the screen has to travel, however, we don't want this duration to be effected in a
1150 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1151 // of travel has on the overall snap duration.
1152 float distanceInfluenceForSnapDuration(float f) {
1153 f -= 0.5f; // center the values about 0.
1154 f *= 0.3f * Math.PI / 2.0f;
1155 return (float) Math.sin(f);
1156 }
1157
Michael Jurka0142d492010-08-25 17:46:15 -07001158 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001159 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1160 int halfScreenSize = getMeasuredWidth() / 2;
1161
1162 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1163 int delta = newX - mUnboundedScrollX;
1164 int duration = 0;
1165
1166 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1167 // If the velocity is low enough, then treat this more as an automatic page advance
1168 // as opposed to an apparent physical response to flinging
1169 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1170 return;
1171 }
1172
1173 // Here we compute a "distance" that will be used in the computation of the overall
1174 // snap duration. This is a function of the actual distance that needs to be traveled;
1175 // we keep this value close to half screen size in order to reduce the variance in snap
1176 // duration as a function of the distance the page needs to travel.
1177 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1178 float distance = halfScreenSize + halfScreenSize *
1179 distanceInfluenceForSnapDuration(distanceRatio);
1180
1181 velocity = Math.abs(velocity);
1182 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1183
1184 // we want the page's snap velocity to approximately match the velocity at which the
1185 // user flings, so we scale the duration by a value near to the derivative of the scroll
1186 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1187 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1188
1189 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001190 }
1191
1192 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001193 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 }
1195
Michael Jurka0142d492010-08-25 17:46:15 -07001196 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001197 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001198
Winson Chung86f77532010-08-24 11:08:22 -07001199 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001200 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001202 snapToPage(whichPage, delta, duration);
1203 }
1204
1205 protected void snapToPage(int whichPage, int delta, int duration) {
1206 mNextPage = whichPage;
1207
1208 View focusedChild = getFocusedChild();
1209 if (focusedChild != null && whichPage != mCurrentPage &&
1210 focusedChild == getChildAt(mCurrentPage)) {
1211 focusedChild.clearFocus();
1212 }
1213
1214 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 awakenScrollBars(duration);
1216 if (duration == 0) {
1217 duration = Math.abs(delta);
1218 }
1219
1220 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001221 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001222
1223 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001224 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001225 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 invalidate();
1227 }
1228
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 public void scrollLeft() {
1230 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001231 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001233 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235 }
1236
1237 public void scrollRight() {
1238 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001239 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001241 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 }
1243 }
1244
Winson Chung86f77532010-08-24 11:08:22 -07001245 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 int result = -1;
1247 if (v != null) {
1248 ViewParent vp = v.getParent();
1249 int count = getChildCount();
1250 for (int i = 0; i < count; i++) {
1251 if (vp == getChildAt(i)) {
1252 return i;
1253 }
1254 }
1255 }
1256 return result;
1257 }
1258
1259 /**
1260 * @return True is long presses are still allowed for the current touch
1261 */
1262 public boolean allowLongPress() {
1263 return mAllowLongPress;
1264 }
1265
Michael Jurka0142d492010-08-25 17:46:15 -07001266 /**
1267 * Set true to allow long-press events to be triggered, usually checked by
1268 * {@link Launcher} to accept or block dpad-initiated long-presses.
1269 */
1270 public void setAllowLongPress(boolean allowLongPress) {
1271 mAllowLongPress = allowLongPress;
1272 }
1273
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001275 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001276
1277 SavedState(Parcelable superState) {
1278 super(superState);
1279 }
1280
1281 private SavedState(Parcel in) {
1282 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001283 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001284 }
1285
1286 @Override
1287 public void writeToParcel(Parcel out, int flags) {
1288 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001289 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 }
1291
1292 public static final Parcelable.Creator<SavedState> CREATOR =
1293 new Parcelable.Creator<SavedState>() {
1294 public SavedState createFromParcel(Parcel in) {
1295 return new SavedState(in);
1296 }
1297
1298 public SavedState[] newArray(int size) {
1299 return new SavedState[size];
1300 }
1301 };
1302 }
1303
Winson Chung86f77532010-08-24 11:08:22 -07001304 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001305 if (mContentIsRefreshable) {
1306 final int count = getChildCount();
1307 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001308 int lowerPageBound = getAssociatedLowerPageBound(page);
1309 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001310 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001311 Page layout = (Page) getChildAt(i);
1312 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001313 if (lowerPageBound <= i && i <= upperPageBound) {
1314 if (mDirtyPageContent.get(i)) {
1315 syncPageItems(i);
1316 mDirtyPageContent.set(i, false);
1317 }
1318 } else {
1319 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001320 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001321 }
1322 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001323 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001324 }
1325 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001326 }
1327 }
1328
Winson Chunge3193b92010-09-10 11:44:42 -07001329 protected int getAssociatedLowerPageBound(int page) {
1330 return Math.max(0, page - 1);
1331 }
1332 protected int getAssociatedUpperPageBound(int page) {
1333 final int count = getChildCount();
1334 return Math.min(page + 1, count - 1);
1335 }
1336
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001337 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001338 if (isChoiceMode(CHOICE_MODE_NONE)) {
1339 mChoiceMode = mode;
1340 mActionMode = startActionMode(callback);
1341 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001342 }
1343
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001344 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001345 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001346 mChoiceMode = CHOICE_MODE_NONE;
1347 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001348 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001349 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001350 }
1351 }
1352
1353 protected boolean isChoiceMode(int mode) {
1354 return mChoiceMode == mode;
1355 }
1356
1357 protected ArrayList<Checkable> getCheckedGrandchildren() {
1358 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1359 final int childCount = getChildCount();
1360 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001361 Page layout = (Page) getChildAt(i);
1362 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001363 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001364 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001365 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001366 checked.add((Checkable) v);
1367 }
1368 }
1369 }
1370 return checked;
1371 }
1372
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001373 /**
1374 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1375 * Otherwise, returns null.
1376 */
1377 protected Checkable getSingleCheckedGrandchild() {
1378 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1379 final int childCount = getChildCount();
1380 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001381 Page layout = (Page) getChildAt(i);
1382 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001383 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001384 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001385 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1386 return (Checkable) v;
1387 }
1388 }
1389 }
1390 }
1391 return null;
1392 }
1393
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001394 public Object getChosenItem() {
1395 View checkedView = (View) getSingleCheckedGrandchild();
1396 if (checkedView != null) {
1397 return checkedView.getTag();
1398 }
1399 return null;
1400 }
1401
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001402 protected void resetCheckedGrandchildren() {
1403 // loop through children, and set all of their children to _not_ be checked
1404 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1405 for (int i = 0; i < checked.size(); ++i) {
1406 final Checkable c = checked.get(i);
1407 c.setChecked(false);
1408 }
1409 }
1410
Winson Chunga12a2502010-12-20 14:41:35 -08001411 public void setRestorePage(int restorePage) {
1412 mRestorePage = restorePage;
1413 }
1414
Winson Chung86f77532010-08-24 11:08:22 -07001415 /**
1416 * This method is called ONLY to synchronize the number of pages that the paged view has.
1417 * To actually fill the pages with information, implement syncPageItems() below. It is
1418 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1419 * and therefore, individual page items do not need to be updated in this method.
1420 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001422
1423 /**
1424 * This method is called to synchronize the items that are on a particular page. If views on
1425 * the page can be reused, then they should be updated within this method.
1426 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001428
Winson Chung321e9ee2010-08-09 13:37:56 -07001429 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001430 if (mContentIsRefreshable) {
1431 // Update all the pages
1432 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001433
Michael Jurka0142d492010-08-25 17:46:15 -07001434 // Mark each of the pages as dirty
1435 final int count = getChildCount();
1436 mDirtyPageContent.clear();
1437 for (int i = 0; i < count; ++i) {
1438 mDirtyPageContent.add(true);
1439 }
1440
Winson Chunga12a2502010-12-20 14:41:35 -08001441 // Use the restore page if necessary
1442 if (mRestorePage > -1) {
1443 mCurrentPage = mRestorePage;
1444 mRestorePage = -1;
1445 }
1446
Michael Jurka0142d492010-08-25 17:46:15 -07001447 // Load any pages that are necessary for the current window of views
1448 loadAssociatedPages(mCurrentPage);
1449 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001450 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001451 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001452 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001453 }
1454}