blob: ede029b05bacc424fb37126e853a7bf1e79d2400 [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
236 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800237 * Updates the scroll of the current page immediately to its final scroll position. We use this
238 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
239 * the previous tab page.
240 */
241 protected void updateCurrentPageScroll() {
242 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
243 scrollTo(newX, 0);
244 mScroller.setFinalX(newX);
245 }
246
247 /**
Winson Chung86f77532010-08-24 11:08:22 -0700248 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 */
Winson Chung86f77532010-08-24 11:08:22 -0700250 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800251 if (!mScroller.isFinished()) {
252 mScroller.abortAnimation();
253 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800254 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
255 // the default
256 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800257 return;
258 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700259
Winson Chung86f77532010-08-24 11:08:22 -0700260 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800261 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700262 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800263 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 }
265
Michael Jurka0142d492010-08-25 17:46:15 -0700266 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700267 if (mPageSwitchListener != null) {
268 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 }
270 }
271
Patrick Dubroy1262e362010-10-06 15:49:50 -0700272 private void pageBeginMoving() {
273 mIsPageMoving = true;
274 onPageBeginMoving();
275 }
276
277 private void pageEndMoving() {
278 onPageEndMoving();
279 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700280 }
281
282 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700283 protected void onPageBeginMoving() {
284 }
285
286 // a method that subclasses can override to add behavior
287 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700288 }
289
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 /**
Winson Chung86f77532010-08-24 11:08:22 -0700291 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 *
293 * @param l The listener used to respond to long clicks.
294 */
295 @Override
296 public void setOnLongClickListener(OnLongClickListener l) {
297 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700298 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700300 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700301 }
302 }
303
304 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800305 public void scrollBy(int x, int y) {
306 scrollTo(mUnboundedScrollX + x, mScrollY + y);
307 }
308
309 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700310 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800311 mUnboundedScrollX = x;
312
313 if (x < 0) {
314 super.scrollTo(0, y);
315 if (mAllowOverScroll) {
316 overScroll(x);
317 }
318 } else if (x > mMaxScrollX) {
319 super.scrollTo(mMaxScrollX, y);
320 if (mAllowOverScroll) {
321 overScroll(x - mMaxScrollX);
322 }
323 } else {
324 super.scrollTo(x, y);
325 }
326
Michael Jurka0142d492010-08-25 17:46:15 -0700327 mTouchX = x;
328 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
329 }
330
331 // we moved this functionality to a helper function so SmoothPagedView can reuse it
332 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700334 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700336 invalidate();
337 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700338 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700339 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700340 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700341 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700342 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800343 // We don't want to trigger a page end moving unless the page has settled
344 // and the user has stopped scrolling
345 if (mTouchState == TOUCH_STATE_REST) {
346 pageEndMoving();
347 }
Michael Jurka0142d492010-08-25 17:46:15 -0700348 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 }
Michael Jurka0142d492010-08-25 17:46:15 -0700350 return false;
351 }
352
353 @Override
354 public void computeScroll() {
355 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
357
358 @Override
359 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
360 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
361 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
362 if (widthMode != MeasureSpec.EXACTLY) {
363 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
364 }
365
Adam Lesinski6b879f02010-11-04 16:15:23 -0700366 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
367 * of the All apps view on XLarge displays to not take up more space then it needs. Width
368 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
369 * each page to have the same width.
370 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700372 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
373 int maxChildHeight = 0;
374
375 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700376
377 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700378 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 final int childCount = getChildCount();
380 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700381 // disallowing padding in paged view (just pass 0)
382 final View child = getChildAt(i);
383 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
384
385 int childWidthMode;
386 if (lp.width == LayoutParams.WRAP_CONTENT) {
387 childWidthMode = MeasureSpec.AT_MOST;
388 } else {
389 childWidthMode = MeasureSpec.EXACTLY;
390 }
391
392 int childHeightMode;
393 if (lp.height == LayoutParams.WRAP_CONTENT) {
394 childHeightMode = MeasureSpec.AT_MOST;
395 } else {
396 childHeightMode = MeasureSpec.EXACTLY;
397 }
398
399 final int childWidthMeasureSpec =
400 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
401 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700402 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700403
404 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700405 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
406 }
407
408 if (heightMode == MeasureSpec.AT_MOST) {
409 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800411 if (childCount > 0) {
412 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
413 } else {
414 mMaxScrollX = 0;
415 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700416
417 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 }
419
Michael Jurka8c920dd2011-01-20 14:16:56 -0800420 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800421 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
422 int delta = newX - mScrollX;
423
Michael Jurka8c920dd2011-01-20 14:16:56 -0800424 final int pageCount = getChildCount();
425 for (int i = 0; i < pageCount; i++) {
426 View page = (View) getChildAt(i);
427 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800428 }
429 setCurrentPage(newCurrentPage);
430 }
431
Michael Jurka8c920dd2011-01-20 14:16:56 -0800432 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
433 // 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 -0800434 // tightens the layout accordingly
435 public void setLayoutScale(float childrenScale) {
436 mLayoutScale = childrenScale;
437
438 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
439 int childCount = getChildCount();
440 float childrenX[] = new float[childCount];
441 float childrenY[] = new float[childCount];
442 for (int i = 0; i < childCount; i++) {
443 final View child = getChildAt(i);
444 childrenX[i] = child.getX();
445 childrenY[i] = child.getY();
446 }
447 onLayout(false, mLeft, mTop, mRight, mBottom);
448 for (int i = 0; i < childCount; i++) {
449 final View child = getChildAt(i);
450 child.setX(childrenX[i]);
451 child.setY(childrenY[i]);
452 }
453 // Also, the page offset has changed (since the pages are now smaller);
454 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800455 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800456 }
457
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700459 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700460 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
461 setHorizontalScrollBarEnabled(false);
462 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
463 scrollTo(newX, 0);
464 mScroller.setFinalX(newX);
465 setHorizontalScrollBarEnabled(true);
466 mFirstLayout = false;
467 }
468
Adam Lesinski6b879f02010-11-04 16:15:23 -0700469 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 final int childCount = getChildCount();
471 int childLeft = 0;
472 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700473 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
475
476 for (int i = 0; i < childCount; i++) {
477 final View child = getChildAt(i);
478 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800479 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700480 final int childHeight = child.getMeasuredHeight();
481 int childTop = mPaddingTop;
482 if (mCenterPagesVertically) {
483 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
484 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800485
Adam Lesinski6b879f02010-11-04 16:15:23 -0700486 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800487 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700488 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
490 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800491 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
492 mFirstLayout = false;
493 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 }
495
Winson Chunge3193b92010-09-10 11:44:42 -0700496 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700497 if (mFadeInAdjacentScreens) {
498 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700499 int halfScreenSize = getMeasuredWidth() / 2;
500 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700501 final int childCount = getChildCount();
502 for (int i = 0; i < childCount; ++i) {
503 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800504 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700505 int halfChildWidth = (childWidth / 2);
506 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700507
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700508 // On the first layout, we may not have a width nor a proper offset, so for now
509 // we should just assume full page width (and calculate the offset according to
510 // that).
511 if (childWidth <= 0) {
512 childWidth = getMeasuredWidth();
513 childCenter = (i * childWidth) + (childWidth / 2);
514 }
515
Winson Chunge8878e32010-09-15 20:37:09 -0700516 int d = halfChildWidth;
517 int distanceFromScreenCenter = childCenter - screenCenter;
518 if (distanceFromScreenCenter > 0) {
519 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800520 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700521 }
Michael Jurka0142d492010-08-25 17:46:15 -0700522 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700523 if (i < childCount - 1) {
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 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700527 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700528
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700529 // Preventing potential divide-by-zero
530 d = Math.max(1, d);
531
Winson Chunge8878e32010-09-15 20:37:09 -0700532 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
533 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
534 float alpha = 1.0f - dimAlpha;
535
Adam Cohenf34bab52010-09-30 14:11:56 -0700536 if (alpha < ALPHA_QUANTIZE_LEVEL) {
537 alpha = 0.0f;
538 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
539 alpha = 1.0f;
540 }
541
Michael Jurka0142d492010-08-25 17:46:15 -0700542 if (Float.compare(alpha, layout.getAlpha()) != 0) {
543 layout.setAlpha(alpha);
544 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 }
Michael Jurka0142d492010-08-25 17:46:15 -0700546 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 }
548 }
Winson Chunge3193b92010-09-10 11:44:42 -0700549 }
550
Adam Cohenf34bab52010-09-30 14:11:56 -0700551 protected void screenScrolled(int screenCenter) {
552 }
553
Winson Chunge3193b92010-09-10 11:44:42 -0700554 @Override
555 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700556 int halfScreenSize = getMeasuredWidth() / 2;
557 int screenCenter = mScrollX + halfScreenSize;
558
559 if (screenCenter != mLastScreenCenter) {
560 screenScrolled(screenCenter);
561 updateAdjacentPagesAlpha();
562 mLastScreenCenter = screenCenter;
563 }
Michael Jurka0142d492010-08-25 17:46:15 -0700564
565 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700566 // As an optimization, this code assumes that all pages have the same width as the 0th
567 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700568 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700569 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800570 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700571 final int screenWidth = getMeasuredWidth();
572 int x = getRelativeChildOffset(0) + pageWidth;
573 int leftScreen = 0;
574 int rightScreen = 0;
575 while (x <= mScrollX) {
576 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800577 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700578 }
579 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800580 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700581 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800582 if (rightScreen < pageCount) {
583 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
584 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700585 }
586 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700587
Michael Jurkac4fb9172010-09-02 17:19:20 -0700588 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800589 // Clip to the bounds
590 canvas.save();
591 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
592 mScrollY + mBottom - mTop);
593
Michael Jurkac4fb9172010-09-02 17:19:20 -0700594 for (int i = leftScreen; i <= rightScreen; i++) {
595 drawChild(canvas, getChildAt(i), drawingTime);
596 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800597 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700598 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
600
601 @Override
602 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700603 int page = indexOfChild(child);
604 if (page != mCurrentPage || !mScroller.isFinished()) {
605 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700606 return true;
607 }
608 return false;
609 }
610
611 @Override
612 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700613 int focusablePage;
614 if (mNextPage != INVALID_PAGE) {
615 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700617 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 }
Winson Chung86f77532010-08-24 11:08:22 -0700619 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 if (v != null) {
621 v.requestFocus(direction, previouslyFocusedRect);
622 }
623 return false;
624 }
625
626 @Override
627 public boolean dispatchUnhandledMove(View focused, int direction) {
628 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700629 if (getCurrentPage() > 0) {
630 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700631 return true;
632 }
633 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 if (getCurrentPage() < getPageCount() - 1) {
635 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 return true;
637 }
638 }
639 return super.dispatchUnhandledMove(focused, direction);
640 }
641
642 @Override
643 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700644 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
645 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
647 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700648 if (mCurrentPage > 0) {
649 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 }
651 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700652 if (mCurrentPage < getPageCount() - 1) {
653 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700654 }
655 }
656 }
657
658 /**
659 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700660 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700661 *
Winson Chung86f77532010-08-24 11:08:22 -0700662 * This happens when live folders requery, and if they're off page, they
663 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700664 */
665 @Override
666 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700667 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700668 View v = focused;
669 while (true) {
670 if (v == current) {
671 super.focusableViewAvailable(focused);
672 return;
673 }
674 if (v == this) {
675 return;
676 }
677 ViewParent parent = v.getParent();
678 if (parent instanceof View) {
679 v = (View)v.getParent();
680 } else {
681 return;
682 }
683 }
684 }
685
686 /**
687 * {@inheritDoc}
688 */
689 @Override
690 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
691 if (disallowIntercept) {
692 // We need to make sure to cancel our long press if
693 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700694 final View currentPage = getChildAt(mCurrentPage);
695 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700696 }
697 super.requestDisallowInterceptTouchEvent(disallowIntercept);
698 }
699
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800700 /**
701 * Return true if a tap at (x, y) should trigger a flip to the previous page.
702 */
703 protected boolean hitsPreviousPage(float x, float y) {
704 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
705 }
706
707 /**
708 * Return true if a tap at (x, y) should trigger a flip to the next page.
709 */
710 protected boolean hitsNextPage(float x, float y) {
711 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
712 }
713
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 @Override
715 public boolean onInterceptTouchEvent(MotionEvent ev) {
716 /*
717 * This method JUST determines whether we want to intercept the motion.
718 * If we return true, onTouchEvent will be called and we do the actual
719 * scrolling there.
720 */
721
Winson Chung45e1d6e2010-11-09 17:19:49 -0800722 // Skip touch handling if there are no pages to swipe
723 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
724
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 /*
726 * Shortcut the most recurring case: the user is in the dragging
727 * state and he is moving his finger. We want to intercept this
728 * motion.
729 */
730 final int action = ev.getAction();
731 if ((action == MotionEvent.ACTION_MOVE) &&
732 (mTouchState == TOUCH_STATE_SCROLLING)) {
733 return true;
734 }
735
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 switch (action & MotionEvent.ACTION_MASK) {
737 case MotionEvent.ACTION_MOVE: {
738 /*
739 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
740 * whether the user has moved far enough from his original down touch.
741 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700742 if (mActivePointerId != INVALID_POINTER) {
743 determineScrollingStart(ev);
744 break;
745 }
746 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
747 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
748 // i.e. fall through to the next case (don't break)
749 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
750 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752
753 case MotionEvent.ACTION_DOWN: {
754 final float x = ev.getX();
755 final float y = ev.getY();
756 // Remember location of down touch
757 mDownMotionX = x;
758 mLastMotionX = x;
759 mLastMotionY = y;
760 mActivePointerId = ev.getPointerId(0);
761 mAllowLongPress = true;
762
763 /*
764 * If being flinged and user touches the screen, initiate drag;
765 * otherwise don't. mScroller.isFinished should be false when
766 * being flinged.
767 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700768 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700769 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
770 if (finishedScrolling) {
771 mTouchState = TOUCH_STATE_REST;
772 mScroller.abortAnimation();
773 } else {
774 mTouchState = TOUCH_STATE_SCROLLING;
775 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700776
Winson Chung86f77532010-08-24 11:08:22 -0700777 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800779 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800781 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800783 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 mTouchState = TOUCH_STATE_NEXT_PAGE;
785 }
786 }
787 }
788 break;
789 }
790
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800792 onWallpaperTap(ev);
793 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 mTouchState = TOUCH_STATE_REST;
795 mAllowLongPress = false;
796 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 break;
798
799 case MotionEvent.ACTION_POINTER_UP:
800 onSecondaryPointerUp(ev);
801 break;
802 }
803
804 /*
805 * The only time we want to intercept motion events is if we are in the
806 * drag mode.
807 */
808 return mTouchState != TOUCH_STATE_REST;
809 }
810
Winson Chung80baf5a2010-08-09 16:03:15 -0700811 protected void animateClickFeedback(View v, final Runnable r) {
812 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800813 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
814 loadAnimator(mContext, R.anim.paged_view_click_feedback);
815 anim.setTarget(v);
816 anim.addListener(new AnimatorListenerAdapter() {
817 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700818 r.run();
819 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700820 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800821 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700822 }
823
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 /*
825 * Determines if we should change the touch state to start scrolling after the
826 * user moves their touch point too far.
827 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700828 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 /*
830 * Locally do absolute value. mLastMotionX is set to the y value
831 * of the down event.
832 */
833 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
834 final float x = ev.getX(pointerIndex);
835 final float y = ev.getY(pointerIndex);
836 final int xDiff = (int) Math.abs(x - mLastMotionX);
837 final int yDiff = (int) Math.abs(y - mLastMotionY);
838
839 final int touchSlop = mTouchSlop;
840 boolean xPaged = xDiff > mPagingTouchSlop;
841 boolean xMoved = xDiff > touchSlop;
842 boolean yMoved = yDiff > touchSlop;
843
844 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700845 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 // Scroll if the user moved far enough along the X axis
847 mTouchState = TOUCH_STATE_SCROLLING;
848 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700849 mTouchX = mScrollX;
850 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
851 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 }
853 // Either way, cancel any pending longpress
854 if (mAllowLongPress) {
855 mAllowLongPress = false;
856 // Try canceling the long press. It could also have been scheduled
857 // by a distant descendant, so use the mAllowLongPress flag to block
858 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700859 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700860 if (currentPage != null) {
861 currentPage.cancelLongPress();
862 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 }
864 }
865 }
866
Adam Cohene0f66b52010-11-23 15:06:07 -0800867 // This curve determines how the effect of scrolling over the limits of the page dimishes
868 // as the user pulls further and further from the bounds
869 private float overScrollInfluenceCurve(float f) {
870 f -= 1.0f;
871 return f * f * f + 1.0f;
872 }
873
Adam Cohen68d73932010-11-15 10:50:58 -0800874 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800875 int screenSize = getMeasuredWidth();
876
877 float f = (amount / screenSize);
878
879 if (f == 0) return;
880 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
881
Adam Cohen7bfc9792011-01-28 13:52:37 -0800882 // Clamp this factor, f, to -1 < f < 1
883 if (Math.abs(f) >= 1) {
884 f /= Math.abs(f);
885 }
886
Adam Cohene0f66b52010-11-23 15:06:07 -0800887 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800888 if (amount < 0) {
889 mScrollX = overScrollAmount;
890 } else {
891 mScrollX = mMaxScrollX + overScrollAmount;
892 }
893 invalidate();
894 }
895
Michael Jurkac5b262c2011-01-12 20:24:50 -0800896 protected float maxOverScroll() {
897 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
898 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
899 float f = 1.0f;
900 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
901 return OVERSCROLL_DAMP_FACTOR * f;
902 }
903
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 @Override
905 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800906 // Skip touch handling if there are no pages to swipe
907 if (getChildCount() <= 0) return super.onTouchEvent(ev);
908
Michael Jurkab8f06722010-10-10 15:58:46 -0700909 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700910
911 final int action = ev.getAction();
912
913 switch (action & MotionEvent.ACTION_MASK) {
914 case MotionEvent.ACTION_DOWN:
915 /*
916 * If being flinged and user touches, stop the fling. isFinished
917 * will be false if being flinged.
918 */
919 if (!mScroller.isFinished()) {
920 mScroller.abortAnimation();
921 }
922
923 // Remember where the motion event started
924 mDownMotionX = mLastMotionX = ev.getX();
925 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700926 if (mTouchState == TOUCH_STATE_SCROLLING) {
927 pageBeginMoving();
928 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 break;
930
931 case MotionEvent.ACTION_MOVE:
932 if (mTouchState == TOUCH_STATE_SCROLLING) {
933 // Scroll to follow the motion event
934 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
935 final float x = ev.getX(pointerIndex);
936 final int deltaX = (int) (mLastMotionX - x);
937 mLastMotionX = x;
938
Adam Cohen68d73932010-11-15 10:50:58 -0800939 if (deltaX != 0) {
940 mTouchX += deltaX;
941 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
942 if (!mDeferScrollUpdate) {
943 scrollBy(deltaX, 0);
944 } else {
945 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 }
947 } else {
948 awakenScrollBars();
949 }
Adam Cohen564976a2010-10-13 18:52:07 -0700950 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 determineScrollingStart(ev);
952 }
953 break;
954
955 case MotionEvent.ACTION_UP:
956 if (mTouchState == TOUCH_STATE_SCROLLING) {
957 final int activePointerId = mActivePointerId;
958 final int pointerIndex = ev.findPointerIndex(activePointerId);
959 final float x = ev.getX(pointerIndex);
960 final VelocityTracker velocityTracker = mVelocityTracker;
961 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
962 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700963 final int deltaX = (int) (x - mDownMotionX);
964 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
965 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700966
Michael Jurka0142d492010-08-25 17:46:15 -0700967 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700968 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800969 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700970 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700971 } else if ((isSignificantMove && deltaX < 0 ||
972 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700973 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700974 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 } else {
976 snapToDestination();
977 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800978 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 // at this point we have not moved beyond the touch slop
980 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
981 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700982 int nextPage = Math.max(0, mCurrentPage - 1);
983 if (nextPage != mCurrentPage) {
984 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 } else {
986 snapToDestination();
987 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800988 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 // at this point we have not moved beyond the touch slop
990 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
991 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700992 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
993 if (nextPage != mCurrentPage) {
994 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 } else {
996 snapToDestination();
997 }
Jeff Brown1d0867c2010-12-02 18:27:39 -0800998 } else {
999 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 }
1001 mTouchState = TOUCH_STATE_REST;
1002 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001003 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 break;
1005
1006 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001007 if (mTouchState == TOUCH_STATE_SCROLLING) {
1008 snapToDestination();
1009 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 mTouchState = TOUCH_STATE_REST;
1011 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001012 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 break;
1014
1015 case MotionEvent.ACTION_POINTER_UP:
1016 onSecondaryPointerUp(ev);
1017 break;
1018 }
1019
1020 return true;
1021 }
1022
Michael Jurkab8f06722010-10-10 15:58:46 -07001023 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1024 if (mVelocityTracker == null) {
1025 mVelocityTracker = VelocityTracker.obtain();
1026 }
1027 mVelocityTracker.addMovement(ev);
1028 }
1029
1030 private void releaseVelocityTracker() {
1031 if (mVelocityTracker != null) {
1032 mVelocityTracker.recycle();
1033 mVelocityTracker = null;
1034 }
1035 }
1036
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 private void onSecondaryPointerUp(MotionEvent ev) {
1038 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1039 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1040 final int pointerId = ev.getPointerId(pointerIndex);
1041 if (pointerId == mActivePointerId) {
1042 // This was our active pointer going up. Choose a new
1043 // active pointer and adjust accordingly.
1044 // TODO: Make this decision more intelligent.
1045 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1046 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1047 mLastMotionY = ev.getY(newPointerIndex);
1048 mActivePointerId = ev.getPointerId(newPointerIndex);
1049 if (mVelocityTracker != null) {
1050 mVelocityTracker.clear();
1051 }
1052 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001053 if (mTouchState == TOUCH_STATE_REST) {
1054 onWallpaperTap(ev);
1055 }
1056 }
1057
1058 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 }
1060
1061 @Override
1062 public void requestChildFocus(View child, View focused) {
1063 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001064 int page = indexOfChild(child);
1065 if (page >= 0 && !isInTouchMode()) {
1066 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068 }
1069
Winson Chunge3193b92010-09-10 11:44:42 -07001070 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1071 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001072 int left;
1073 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001074 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001075 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001076 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001077 if (left <= relativeOffset && relativeOffset <= right) {
1078 return i;
1079 }
Winson Chunge3193b92010-09-10 11:44:42 -07001080 }
1081 return -1;
1082 }
1083
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 protected int getRelativeChildOffset(int index) {
1085 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1086 }
1087
1088 protected int getChildOffset(int index) {
1089 if (getChildCount() == 0)
1090 return 0;
1091
1092 int offset = getRelativeChildOffset(0);
1093 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001094 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 }
1096 return offset;
1097 }
1098
Michael Jurkad3ef3062010-11-23 16:23:58 -08001099 protected int getScaledMeasuredWidth(View child) {
1100 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1101 }
1102
Adam Cohend19d3ca2010-09-15 14:43:42 -07001103 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001104 int minDistanceFromScreenCenter = getMeasuredWidth();
1105 int minDistanceFromScreenCenterIndex = -1;
1106 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1107 final int childCount = getChildCount();
1108 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001109 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001110 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 int halfChildWidth = (childWidth / 2);
1112 int childCenter = getChildOffset(i) + halfChildWidth;
1113 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1114 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1115 minDistanceFromScreenCenter = distanceFromScreenCenter;
1116 minDistanceFromScreenCenterIndex = i;
1117 }
1118 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001119 return minDistanceFromScreenCenterIndex;
1120 }
1121
1122 protected void snapToDestination() {
1123 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 }
1125
Adam Cohene0f66b52010-11-23 15:06:07 -08001126 private static class ScrollInterpolator implements Interpolator {
1127 public ScrollInterpolator() {
1128 }
1129
1130 public float getInterpolation(float t) {
1131 t -= 1.0f;
1132 return t*t*t*t*t + 1;
1133 }
1134 }
1135
1136 // We want the duration of the page snap animation to be influenced by the distance that
1137 // the screen has to travel, however, we don't want this duration to be effected in a
1138 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1139 // of travel has on the overall snap duration.
1140 float distanceInfluenceForSnapDuration(float f) {
1141 f -= 0.5f; // center the values about 0.
1142 f *= 0.3f * Math.PI / 2.0f;
1143 return (float) Math.sin(f);
1144 }
1145
Michael Jurka0142d492010-08-25 17:46:15 -07001146 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001147 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1148 int halfScreenSize = getMeasuredWidth() / 2;
1149
1150 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1151 int delta = newX - mUnboundedScrollX;
1152 int duration = 0;
1153
1154 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1155 // If the velocity is low enough, then treat this more as an automatic page advance
1156 // as opposed to an apparent physical response to flinging
1157 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1158 return;
1159 }
1160
1161 // Here we compute a "distance" that will be used in the computation of the overall
1162 // snap duration. This is a function of the actual distance that needs to be traveled;
1163 // we keep this value close to half screen size in order to reduce the variance in snap
1164 // duration as a function of the distance the page needs to travel.
1165 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1166 float distance = halfScreenSize + halfScreenSize *
1167 distanceInfluenceForSnapDuration(distanceRatio);
1168
1169 velocity = Math.abs(velocity);
1170 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1171
1172 // we want the page's snap velocity to approximately match the velocity at which the
1173 // user flings, so we scale the duration by a value near to the derivative of the scroll
1174 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1175 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1176
1177 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001178 }
1179
1180 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001181 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 }
1183
Michael Jurka0142d492010-08-25 17:46:15 -07001184 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001185 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001186
Winson Chung86f77532010-08-24 11:08:22 -07001187 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001188 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001190 snapToPage(whichPage, delta, duration);
1191 }
1192
1193 protected void snapToPage(int whichPage, int delta, int duration) {
1194 mNextPage = whichPage;
1195
1196 View focusedChild = getFocusedChild();
1197 if (focusedChild != null && whichPage != mCurrentPage &&
1198 focusedChild == getChildAt(mCurrentPage)) {
1199 focusedChild.clearFocus();
1200 }
1201
1202 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 awakenScrollBars(duration);
1204 if (duration == 0) {
1205 duration = Math.abs(delta);
1206 }
1207
1208 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001209 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001210
1211 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001212 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001213 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 invalidate();
1215 }
1216
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 public void scrollLeft() {
1218 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001219 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001221 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 }
1223 }
1224
1225 public void scrollRight() {
1226 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001227 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001229 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 }
1231 }
1232
Winson Chung86f77532010-08-24 11:08:22 -07001233 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 int result = -1;
1235 if (v != null) {
1236 ViewParent vp = v.getParent();
1237 int count = getChildCount();
1238 for (int i = 0; i < count; i++) {
1239 if (vp == getChildAt(i)) {
1240 return i;
1241 }
1242 }
1243 }
1244 return result;
1245 }
1246
1247 /**
1248 * @return True is long presses are still allowed for the current touch
1249 */
1250 public boolean allowLongPress() {
1251 return mAllowLongPress;
1252 }
1253
Michael Jurka0142d492010-08-25 17:46:15 -07001254 /**
1255 * Set true to allow long-press events to be triggered, usually checked by
1256 * {@link Launcher} to accept or block dpad-initiated long-presses.
1257 */
1258 public void setAllowLongPress(boolean allowLongPress) {
1259 mAllowLongPress = allowLongPress;
1260 }
1261
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001263 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001264
1265 SavedState(Parcelable superState) {
1266 super(superState);
1267 }
1268
1269 private SavedState(Parcel in) {
1270 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001271 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273
1274 @Override
1275 public void writeToParcel(Parcel out, int flags) {
1276 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001277 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 }
1279
1280 public static final Parcelable.Creator<SavedState> CREATOR =
1281 new Parcelable.Creator<SavedState>() {
1282 public SavedState createFromParcel(Parcel in) {
1283 return new SavedState(in);
1284 }
1285
1286 public SavedState[] newArray(int size) {
1287 return new SavedState[size];
1288 }
1289 };
1290 }
1291
Winson Chung86f77532010-08-24 11:08:22 -07001292 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001293 if (mContentIsRefreshable) {
1294 final int count = getChildCount();
1295 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001296 int lowerPageBound = getAssociatedLowerPageBound(page);
1297 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001298 for (int i = 0; i < count; ++i) {
1299 final ViewGroup layout = (ViewGroup) getChildAt(i);
1300 final int childCount = layout.getChildCount();
1301 if (lowerPageBound <= i && i <= upperPageBound) {
1302 if (mDirtyPageContent.get(i)) {
1303 syncPageItems(i);
1304 mDirtyPageContent.set(i, false);
1305 }
1306 } else {
1307 if (childCount > 0) {
1308 layout.removeAllViews();
1309 }
1310 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001311 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001312 }
1313 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001314 }
1315 }
1316
Winson Chunge3193b92010-09-10 11:44:42 -07001317 protected int getAssociatedLowerPageBound(int page) {
1318 return Math.max(0, page - 1);
1319 }
1320 protected int getAssociatedUpperPageBound(int page) {
1321 final int count = getChildCount();
1322 return Math.min(page + 1, count - 1);
1323 }
1324
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001325 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001326 if (isChoiceMode(CHOICE_MODE_NONE)) {
1327 mChoiceMode = mode;
1328 mActionMode = startActionMode(callback);
1329 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001330 }
1331
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001332 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001333 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001334 mChoiceMode = CHOICE_MODE_NONE;
1335 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001336 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001337 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001338 }
1339 }
1340
1341 protected boolean isChoiceMode(int mode) {
1342 return mChoiceMode == mode;
1343 }
1344
1345 protected ArrayList<Checkable> getCheckedGrandchildren() {
1346 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1347 final int childCount = getChildCount();
1348 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001349 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001350 final int grandChildCount = layout.getChildCount();
1351 for (int j = 0; j < grandChildCount; ++j) {
1352 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001353 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001354 checked.add((Checkable) v);
1355 }
1356 }
1357 }
1358 return checked;
1359 }
1360
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001361 /**
1362 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1363 * Otherwise, returns null.
1364 */
1365 protected Checkable getSingleCheckedGrandchild() {
1366 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1367 final int childCount = getChildCount();
1368 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001369 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001370 final int grandChildCount = layout.getChildCount();
1371 for (int j = 0; j < grandChildCount; ++j) {
1372 final View v = layout.getChildAt(j);
1373 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1374 return (Checkable) v;
1375 }
1376 }
1377 }
1378 }
1379 return null;
1380 }
1381
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001382 public Object getChosenItem() {
1383 View checkedView = (View) getSingleCheckedGrandchild();
1384 if (checkedView != null) {
1385 return checkedView.getTag();
1386 }
1387 return null;
1388 }
1389
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001390 protected void resetCheckedGrandchildren() {
1391 // loop through children, and set all of their children to _not_ be checked
1392 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1393 for (int i = 0; i < checked.size(); ++i) {
1394 final Checkable c = checked.get(i);
1395 c.setChecked(false);
1396 }
1397 }
1398
Winson Chunga12a2502010-12-20 14:41:35 -08001399 public void setRestorePage(int restorePage) {
1400 mRestorePage = restorePage;
1401 }
1402
Winson Chung86f77532010-08-24 11:08:22 -07001403 /**
1404 * This method is called ONLY to synchronize the number of pages that the paged view has.
1405 * To actually fill the pages with information, implement syncPageItems() below. It is
1406 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1407 * and therefore, individual page items do not need to be updated in this method.
1408 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001410
1411 /**
1412 * This method is called to synchronize the items that are on a particular page. If views on
1413 * the page can be reused, then they should be updated within this method.
1414 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001416
Winson Chung321e9ee2010-08-09 13:37:56 -07001417 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001418 if (mContentIsRefreshable) {
1419 // Update all the pages
1420 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001421
Michael Jurka0142d492010-08-25 17:46:15 -07001422 // Mark each of the pages as dirty
1423 final int count = getChildCount();
1424 mDirtyPageContent.clear();
1425 for (int i = 0; i < count; ++i) {
1426 mDirtyPageContent.add(true);
1427 }
1428
Winson Chunga12a2502010-12-20 14:41:35 -08001429 // Use the restore page if necessary
1430 if (mRestorePage > -1) {
1431 mCurrentPage = mRestorePage;
1432 mRestorePage = -1;
1433 }
1434
Michael Jurka0142d492010-08-25 17:46:15 -07001435 // Load any pages that are necessary for the current window of views
1436 loadAssociatedPages(mCurrentPage);
1437 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001438 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001439 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001440 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 }
1442}