blob: 8483f959f8a9bf486baa91e3dbb623c7fefa2771 [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
Michael Jurkaaf91de02010-11-23 16:23:58 -080019import com.android.launcher.R;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070020
Winson Chung321e9ee2010-08-09 13:37:56 -070021import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070022import android.content.res.TypedArray;
Winson Chung241c3b42010-08-25 16:53:03 -070023import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.graphics.Canvas;
25import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.os.Parcel;
27import android.os.Parcelable;
28import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070029import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.view.MotionEvent;
31import android.view.VelocityTracker;
32import android.view.View;
33import android.view.ViewConfiguration;
34import android.view.ViewGroup;
35import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070036import android.view.animation.Animation;
Winson Chunge3193b92010-09-10 11:44:42 -070037import android.view.animation.AnimationUtils;
Adam Cohene0f66b52010-11-23 15:06:07 -080038import android.view.animation.Interpolator;
Adam Cohen68d73932010-11-15 10:50:58 -080039import android.view.animation.Animation.AnimationListener;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Michael Jurkaaf91de02010-11-23 16:23:58 -080043import java.util.ArrayList;
44import java.util.HashMap;
Winson Chung80baf5a2010-08-09 16:03:15 -070045
Winson Chung321e9ee2010-08-09 13:37:56 -070046/**
47 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070048 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070049 */
50public abstract class PagedView extends ViewGroup {
51 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070052 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung86f77532010-08-24 11:08:22 -070054 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070055 private static final int MIN_LENGTH_FOR_FLING = 25;
56 // The min drag distance to trigger a page shift (regardless of velocity)
57 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070058
Adam Cohene0f66b52010-11-23 15:06:07 -080059 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Adam Cohene0f66b52010-11-23 15:06:07 -080062 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
63 private static final int MINIMUM_SNAP_VELOCITY = 2200;
64 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohen68d73932010-11-15 10:50:58 -080065
Michael Jurka0142d492010-08-25 17:46:15 -070066 // the velocity at which a fling gesture will cause us to snap to the next page
67 protected int mSnapVelocity = 500;
68
69 protected float mSmoothingTime;
70 protected float mTouchX;
71
72 protected boolean mFirstLayout = true;
73
74 protected int mCurrentPage;
75 protected int mNextPage = INVALID_PAGE;
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 Jurkad3ef3062010-11-23 16:23:58 -0800113 // parameter that adjusts the layout to be optimized for CellLayouts with that scale factor
114 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 Chung241c3b42010-08-25 16:53:03 -0700134 protected PagedViewIconCache mPageViewIconCache;
135
Michael Jurka0142d492010-08-25 17:46:15 -0700136 // If true, syncPages and syncPageItems will be called to refresh pages
137 protected boolean mContentIsRefreshable = true;
138
139 // If true, modify alpha of neighboring pages as user scrolls left/right
140 protected boolean mFadeInAdjacentScreens = true;
141
142 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
143 // to switch to a new page
144 protected boolean mUsePagingTouchSlop = true;
145
146 // If true, the subclass should directly update mScrollX itself in its computeScroll method
147 // (SmoothPagedView does this)
148 protected boolean mDeferScrollUpdate = false;
149
Patrick Dubroy1262e362010-10-06 15:49:50 -0700150 protected boolean mIsPageMoving = false;
151
Winson Chung241c3b42010-08-25 16:53:03 -0700152 /**
153 * Simple cache mechanism for PagedViewIcon outlines.
154 */
155 class PagedViewIconCache {
156 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
157
158 public void clear() {
159 iconOutlineCache.clear();
160 }
161 public void addOutline(Object key, Bitmap b) {
162 iconOutlineCache.put(key, b);
163 }
164 public void removeOutline(Object key) {
165 if (iconOutlineCache.containsKey(key)) {
166 iconOutlineCache.remove(key);
167 }
168 }
169 public Bitmap getOutline(Object key) {
170 return iconOutlineCache.get(key);
171 }
172 }
173
Winson Chung86f77532010-08-24 11:08:22 -0700174 public interface PageSwitchListener {
175 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700176 }
177
Winson Chung321e9ee2010-08-09 13:37:56 -0700178 public PagedView(Context context) {
179 this(context, null);
180 }
181
182 public PagedView(Context context, AttributeSet attrs) {
183 this(context, attrs, 0);
184 }
185
186 public PagedView(Context context, AttributeSet attrs, int defStyle) {
187 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700188 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700189
Adam Cohen9c4949e2010-10-05 12:27:22 -0700190 TypedArray a = context.obtainStyledAttributes(attrs,
191 R.styleable.PagedView, defStyle, 0);
192 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
193 mPageLayoutPaddingTop = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutPaddingTop, 10);
195 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
196 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
197 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
198 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
199 mPageLayoutPaddingRight = a.getDimensionPixelSize(
200 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700201 mPageLayoutWidthGap = a.getDimensionPixelSize(
202 R.styleable.PagedView_pageLayoutWidthGap, -1);
203 mPageLayoutHeightGap = a.getDimensionPixelSize(
204 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 a.recycle();
206
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700208 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 }
210
211 /**
212 * Initializes various states for this workspace.
213 */
Michael Jurka0142d492010-08-25 17:46:15 -0700214 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700215 mDirtyPageContent = new ArrayList<Boolean>();
216 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700217 mPageViewIconCache = new PagedViewIconCache();
Adam Cohene0f66b52010-11-23 15:06:07 -0800218 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700219 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700220 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700221
222 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
223 mTouchSlop = configuration.getScaledTouchSlop();
224 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
225 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
226 }
227
Winson Chung86f77532010-08-24 11:08:22 -0700228 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
229 mPageSwitchListener = pageSwitchListener;
230 if (mPageSwitchListener != null) {
231 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 }
233 }
234
235 /**
Winson Chung86f77532010-08-24 11:08:22 -0700236 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 *
Winson Chung86f77532010-08-24 11:08:22 -0700238 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 */
Winson Chung86f77532010-08-24 11:08:22 -0700240 int getCurrentPage() {
241 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 }
243
Winson Chung86f77532010-08-24 11:08:22 -0700244 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 return getChildCount();
246 }
247
Winson Chung86f77532010-08-24 11:08:22 -0700248 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 return getChildAt(index);
250 }
251
252 int getScrollWidth() {
253 return getWidth();
254 }
255
256 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800257 * Updates the scroll of the current page immediately to its final scroll position. We use this
258 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
259 * the previous tab page.
260 */
261 protected void updateCurrentPageScroll() {
262 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
263 scrollTo(newX, 0);
264 mScroller.setFinalX(newX);
265 }
266
267 /**
Winson Chung86f77532010-08-24 11:08:22 -0700268 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 */
Winson Chung86f77532010-08-24 11:08:22 -0700270 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800271 if (!mScroller.isFinished()) {
272 mScroller.abortAnimation();
273 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800274 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
275 // the default
276 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800277 return;
278 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700279
Winson Chung86f77532010-08-24 11:08:22 -0700280 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800281 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700282
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700284 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 }
286
Michael Jurka0142d492010-08-25 17:46:15 -0700287 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700288 if (mPageSwitchListener != null) {
289 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 }
291 }
292
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 private void pageBeginMoving() {
294 mIsPageMoving = true;
295 onPageBeginMoving();
296 }
297
298 private void pageEndMoving() {
299 onPageEndMoving();
300 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700301 }
302
303 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700304 protected void onPageBeginMoving() {
305 }
306
307 // a method that subclasses can override to add behavior
308 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700309 }
310
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 /**
Winson Chung86f77532010-08-24 11:08:22 -0700312 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700313 *
314 * @param l The listener used to respond to long clicks.
315 */
316 @Override
317 public void setOnLongClickListener(OnLongClickListener l) {
318 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700319 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700321 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700322 }
323 }
324
325 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800326 public void scrollBy(int x, int y) {
327 scrollTo(mUnboundedScrollX + x, mScrollY + y);
328 }
329
330 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700331 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800332 mUnboundedScrollX = x;
333
334 if (x < 0) {
335 super.scrollTo(0, y);
336 if (mAllowOverScroll) {
337 overScroll(x);
338 }
339 } else if (x > mMaxScrollX) {
340 super.scrollTo(mMaxScrollX, y);
341 if (mAllowOverScroll) {
342 overScroll(x - mMaxScrollX);
343 }
344 } else {
345 super.scrollTo(x, y);
346 }
347
Michael Jurka0142d492010-08-25 17:46:15 -0700348 mTouchX = x;
349 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
350 }
351
352 // we moved this functionality to a helper function so SmoothPagedView can reuse it
353 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700355 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700357 invalidate();
358 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700359 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700360 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700361 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700362 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700363 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800364 // We don't want to trigger a page end moving unless the page has settled
365 // and the user has stopped scrolling
366 if (mTouchState == TOUCH_STATE_REST) {
367 pageEndMoving();
368 }
Michael Jurka0142d492010-08-25 17:46:15 -0700369 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 }
Michael Jurka0142d492010-08-25 17:46:15 -0700371 return false;
372 }
373
374 @Override
375 public void computeScroll() {
376 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 }
378
379 @Override
380 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
381 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
382 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
383 if (widthMode != MeasureSpec.EXACTLY) {
384 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
385 }
386
Adam Lesinski6b879f02010-11-04 16:15:23 -0700387 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
388 * of the All apps view on XLarge displays to not take up more space then it needs. Width
389 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
390 * each page to have the same width.
391 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700393 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
394 int maxChildHeight = 0;
395
396 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700397
398 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700399 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 final int childCount = getChildCount();
401 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700402 // disallowing padding in paged view (just pass 0)
403 final View child = getChildAt(i);
404 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
405
406 int childWidthMode;
407 if (lp.width == LayoutParams.WRAP_CONTENT) {
408 childWidthMode = MeasureSpec.AT_MOST;
409 } else {
410 childWidthMode = MeasureSpec.EXACTLY;
411 }
412
413 int childHeightMode;
414 if (lp.height == LayoutParams.WRAP_CONTENT) {
415 childHeightMode = MeasureSpec.AT_MOST;
416 } else {
417 childHeightMode = MeasureSpec.EXACTLY;
418 }
419
420 final int childWidthMeasureSpec =
421 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
422 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700423 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700424
425 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700426 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
427 }
428
429 if (heightMode == MeasureSpec.AT_MOST) {
430 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800432 if (childCount > 0) {
433 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
434 } else {
435 mMaxScrollX = 0;
436 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700437
438 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 }
440
Michael Jurkaaf91de02010-11-23 16:23:58 -0800441 protected void moveToNewPageWithoutMovingCellLayouts(int newCurrentPage) {
442 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
443 int delta = newX - mScrollX;
444
445 final int screenCount = getChildCount();
446 for (int i = 0; i < screenCount; i++) {
447 CellLayout cl = (CellLayout) getChildAt(i);
448 cl.setX(cl.getX() + delta);
449 }
450 setCurrentPage(newCurrentPage);
451 }
452
Michael Jurkad3ef3062010-11-23 16:23:58 -0800453 // A layout scale of 1.0f assumes that the CellLayouts, in their unshrunken state, have a
454 // scale of 1.0f. A layout scale of 0.8f assumes the CellLayouts have a scale of 0.8f, and
455 // tightens the layout accordingly
456 public void setLayoutScale(float childrenScale) {
457 mLayoutScale = childrenScale;
458
459 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
460 int childCount = getChildCount();
461 float childrenX[] = new float[childCount];
462 float childrenY[] = new float[childCount];
463 for (int i = 0; i < childCount; i++) {
464 final View child = getChildAt(i);
465 childrenX[i] = child.getX();
466 childrenY[i] = child.getY();
467 }
468 onLayout(false, mLeft, mTop, mRight, mBottom);
469 for (int i = 0; i < childCount; i++) {
470 final View child = getChildAt(i);
471 child.setX(childrenX[i]);
472 child.setY(childrenY[i]);
473 }
474 // Also, the page offset has changed (since the pages are now smaller);
475 // update the page offset, but again preserving absolute X and Y coordinates
476 moveToNewPageWithoutMovingCellLayouts(mCurrentPage);
477 }
478
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700480 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700481 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
482 setHorizontalScrollBarEnabled(false);
483 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
484 scrollTo(newX, 0);
485 mScroller.setFinalX(newX);
486 setHorizontalScrollBarEnabled(true);
487 mFirstLayout = false;
488 }
489
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 final int childCount = getChildCount();
492 int childLeft = 0;
493 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700494 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 }
496
497 for (int i = 0; i < childCount; i++) {
498 final View child = getChildAt(i);
499 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800500 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700501 final int childHeight = child.getMeasuredHeight();
502 int childTop = mPaddingTop;
503 if (mCenterPagesVertically) {
504 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
505 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800506
Adam Lesinski6b879f02010-11-04 16:15:23 -0700507 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700509 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 }
511 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800512 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
513 mFirstLayout = false;
514 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
516
Winson Chunge3193b92010-09-10 11:44:42 -0700517 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700518 if (mFadeInAdjacentScreens) {
519 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700520 int halfScreenSize = getMeasuredWidth() / 2;
521 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700522 final int childCount = getChildCount();
523 for (int i = 0; i < childCount; ++i) {
524 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800525 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700526 int halfChildWidth = (childWidth / 2);
527 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700528
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700529 // On the first layout, we may not have a width nor a proper offset, so for now
530 // we should just assume full page width (and calculate the offset according to
531 // that).
532 if (childWidth <= 0) {
533 childWidth = getMeasuredWidth();
534 childCenter = (i * childWidth) + (childWidth / 2);
535 }
536
Winson Chunge8878e32010-09-15 20:37:09 -0700537 int d = halfChildWidth;
538 int distanceFromScreenCenter = childCenter - screenCenter;
539 if (distanceFromScreenCenter > 0) {
540 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700542 }
Michael Jurka0142d492010-08-25 17:46:15 -0700543 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700544 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800545 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700546 }
Michael Jurka0142d492010-08-25 17:46:15 -0700547 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700548 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700549
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700550 // Preventing potential divide-by-zero
551 d = Math.max(1, d);
552
Winson Chunge8878e32010-09-15 20:37:09 -0700553 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
554 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
555 float alpha = 1.0f - dimAlpha;
556
Adam Cohenf34bab52010-09-30 14:11:56 -0700557 if (alpha < ALPHA_QUANTIZE_LEVEL) {
558 alpha = 0.0f;
559 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
560 alpha = 1.0f;
561 }
562
Michael Jurka0142d492010-08-25 17:46:15 -0700563 if (Float.compare(alpha, layout.getAlpha()) != 0) {
564 layout.setAlpha(alpha);
565 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 }
Michael Jurka0142d492010-08-25 17:46:15 -0700567 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569 }
Winson Chunge3193b92010-09-10 11:44:42 -0700570 }
571
Adam Cohenf34bab52010-09-30 14:11:56 -0700572 protected void screenScrolled(int screenCenter) {
573 }
574
Winson Chunge3193b92010-09-10 11:44:42 -0700575 @Override
576 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700577 int halfScreenSize = getMeasuredWidth() / 2;
578 int screenCenter = mScrollX + halfScreenSize;
579
580 if (screenCenter != mLastScreenCenter) {
581 screenScrolled(screenCenter);
582 updateAdjacentPagesAlpha();
583 mLastScreenCenter = screenCenter;
584 }
Michael Jurka0142d492010-08-25 17:46:15 -0700585
586 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700587 // As an optimization, this code assumes that all pages have the same width as the 0th
588 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700589 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700590 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800591 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700592 final int screenWidth = getMeasuredWidth();
593 int x = getRelativeChildOffset(0) + pageWidth;
594 int leftScreen = 0;
595 int rightScreen = 0;
596 while (x <= mScrollX) {
597 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800598 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700599 }
600 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800601 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700602 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800603 if (rightScreen < pageCount) {
604 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
605 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700606 }
607 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700608
Michael Jurkac4fb9172010-09-02 17:19:20 -0700609 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800610 // Clip to the bounds
611 canvas.save();
612 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
613 mScrollY + mBottom - mTop);
614
Michael Jurkac4fb9172010-09-02 17:19:20 -0700615 for (int i = leftScreen; i <= rightScreen; i++) {
616 drawChild(canvas, getChildAt(i), drawingTime);
617 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800618 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700619 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 }
621
622 @Override
623 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700624 int page = indexOfChild(child);
625 if (page != mCurrentPage || !mScroller.isFinished()) {
626 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 return true;
628 }
629 return false;
630 }
631
632 @Override
633 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 int focusablePage;
635 if (mNextPage != INVALID_PAGE) {
636 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700638 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 }
Winson Chung86f77532010-08-24 11:08:22 -0700640 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 if (v != null) {
642 v.requestFocus(direction, previouslyFocusedRect);
643 }
644 return false;
645 }
646
647 @Override
648 public boolean dispatchUnhandledMove(View focused, int direction) {
649 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700650 if (getCurrentPage() > 0) {
651 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700652 return true;
653 }
654 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700655 if (getCurrentPage() < getPageCount() - 1) {
656 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700657 return true;
658 }
659 }
660 return super.dispatchUnhandledMove(focused, direction);
661 }
662
663 @Override
664 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700665 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
666 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 }
668 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700669 if (mCurrentPage > 0) {
670 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700671 }
672 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700673 if (mCurrentPage < getPageCount() - 1) {
674 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 }
676 }
677 }
678
679 /**
680 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700681 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 *
Winson Chung86f77532010-08-24 11:08:22 -0700683 * This happens when live folders requery, and if they're off page, they
684 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 */
686 @Override
687 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700688 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 View v = focused;
690 while (true) {
691 if (v == current) {
692 super.focusableViewAvailable(focused);
693 return;
694 }
695 if (v == this) {
696 return;
697 }
698 ViewParent parent = v.getParent();
699 if (parent instanceof View) {
700 v = (View)v.getParent();
701 } else {
702 return;
703 }
704 }
705 }
706
707 /**
708 * {@inheritDoc}
709 */
710 @Override
711 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
712 if (disallowIntercept) {
713 // We need to make sure to cancel our long press if
714 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700715 final View currentPage = getChildAt(mCurrentPage);
716 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 }
718 super.requestDisallowInterceptTouchEvent(disallowIntercept);
719 }
720
721 @Override
722 public boolean onInterceptTouchEvent(MotionEvent ev) {
723 /*
724 * This method JUST determines whether we want to intercept the motion.
725 * If we return true, onTouchEvent will be called and we do the actual
726 * scrolling there.
727 */
728
Winson Chung45e1d6e2010-11-09 17:19:49 -0800729 // Skip touch handling if there are no pages to swipe
730 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
731
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 /*
733 * Shortcut the most recurring case: the user is in the dragging
734 * state and he is moving his finger. We want to intercept this
735 * motion.
736 */
737 final int action = ev.getAction();
738 if ((action == MotionEvent.ACTION_MOVE) &&
739 (mTouchState == TOUCH_STATE_SCROLLING)) {
740 return true;
741 }
742
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 switch (action & MotionEvent.ACTION_MASK) {
744 case MotionEvent.ACTION_MOVE: {
745 /*
746 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
747 * whether the user has moved far enough from his original down touch.
748 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700749 if (mActivePointerId != INVALID_POINTER) {
750 determineScrollingStart(ev);
751 break;
752 }
753 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
754 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
755 // i.e. fall through to the next case (don't break)
756 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
757 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 }
759
760 case MotionEvent.ACTION_DOWN: {
761 final float x = ev.getX();
762 final float y = ev.getY();
763 // Remember location of down touch
764 mDownMotionX = x;
765 mLastMotionX = x;
766 mLastMotionY = y;
767 mActivePointerId = ev.getPointerId(0);
768 mAllowLongPress = true;
769
770 /*
771 * If being flinged and user touches the screen, initiate drag;
772 * otherwise don't. mScroller.isFinished should be false when
773 * being flinged.
774 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700775 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700776 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
777 if (finishedScrolling) {
778 mTouchState = TOUCH_STATE_REST;
779 mScroller.abortAnimation();
780 } else {
781 mTouchState = TOUCH_STATE_SCROLLING;
782 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700783
Winson Chung86f77532010-08-24 11:08:22 -0700784 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700786 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
788 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700789 int width = getMeasuredWidth();
790 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700791 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700793 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 mTouchState = TOUCH_STATE_NEXT_PAGE;
795 }
796 }
797 }
798 break;
799 }
800
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800802 onWallpaperTap(ev);
803 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 mTouchState = TOUCH_STATE_REST;
805 mAllowLongPress = false;
806 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 break;
808
809 case MotionEvent.ACTION_POINTER_UP:
810 onSecondaryPointerUp(ev);
811 break;
812 }
813
814 /*
815 * The only time we want to intercept motion events is if we are in the
816 * drag mode.
817 */
818 return mTouchState != TOUCH_STATE_REST;
819 }
820
Winson Chung80baf5a2010-08-09 16:03:15 -0700821 protected void animateClickFeedback(View v, final Runnable r) {
822 // animate the view slightly to show click feedback running some logic after it is "pressed"
823 Animation anim = AnimationUtils.loadAnimation(getContext(),
824 R.anim.paged_view_click_feedback);
825 anim.setAnimationListener(new AnimationListener() {
826 @Override
827 public void onAnimationStart(Animation animation) {}
828 @Override
829 public void onAnimationRepeat(Animation animation) {
830 r.run();
831 }
832 @Override
833 public void onAnimationEnd(Animation animation) {}
834 });
835 v.startAnimation(anim);
836 }
837
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 /*
839 * Determines if we should change the touch state to start scrolling after the
840 * user moves their touch point too far.
841 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700842 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 /*
844 * Locally do absolute value. mLastMotionX is set to the y value
845 * of the down event.
846 */
847 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
848 final float x = ev.getX(pointerIndex);
849 final float y = ev.getY(pointerIndex);
850 final int xDiff = (int) Math.abs(x - mLastMotionX);
851 final int yDiff = (int) Math.abs(y - mLastMotionY);
852
853 final int touchSlop = mTouchSlop;
854 boolean xPaged = xDiff > mPagingTouchSlop;
855 boolean xMoved = xDiff > touchSlop;
856 boolean yMoved = yDiff > touchSlop;
857
858 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700859 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 // Scroll if the user moved far enough along the X axis
861 mTouchState = TOUCH_STATE_SCROLLING;
862 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700863 mTouchX = mScrollX;
864 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
865 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 }
867 // Either way, cancel any pending longpress
868 if (mAllowLongPress) {
869 mAllowLongPress = false;
870 // Try canceling the long press. It could also have been scheduled
871 // by a distant descendant, so use the mAllowLongPress flag to block
872 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700873 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700874 if (currentPage != null) {
875 currentPage.cancelLongPress();
876 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 }
878 }
879 }
880
Adam Cohen21f12b52010-10-07 17:15:37 -0700881 protected boolean handlePagingClicks() {
882 return false;
883 }
884
Adam Cohene0f66b52010-11-23 15:06:07 -0800885 // This curve determines how the effect of scrolling over the limits of the page dimishes
886 // as the user pulls further and further from the bounds
887 private float overScrollInfluenceCurve(float f) {
888 f -= 1.0f;
889 return f * f * f + 1.0f;
890 }
891
Adam Cohen68d73932010-11-15 10:50:58 -0800892 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800893 int screenSize = getMeasuredWidth();
894
895 float f = (amount / screenSize);
896
897 if (f == 0) return;
898 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
899
900 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800901 if (amount < 0) {
902 mScrollX = overScrollAmount;
903 } else {
904 mScrollX = mMaxScrollX + overScrollAmount;
905 }
906 invalidate();
907 }
908
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 @Override
910 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800911 // Skip touch handling if there are no pages to swipe
912 if (getChildCount() <= 0) return super.onTouchEvent(ev);
913
Michael Jurkab8f06722010-10-10 15:58:46 -0700914 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700915
916 final int action = ev.getAction();
917
918 switch (action & MotionEvent.ACTION_MASK) {
919 case MotionEvent.ACTION_DOWN:
920 /*
921 * If being flinged and user touches, stop the fling. isFinished
922 * will be false if being flinged.
923 */
924 if (!mScroller.isFinished()) {
925 mScroller.abortAnimation();
926 }
927
928 // Remember where the motion event started
929 mDownMotionX = mLastMotionX = ev.getX();
930 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700931 if (mTouchState == TOUCH_STATE_SCROLLING) {
932 pageBeginMoving();
933 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 break;
935
936 case MotionEvent.ACTION_MOVE:
937 if (mTouchState == TOUCH_STATE_SCROLLING) {
938 // Scroll to follow the motion event
939 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
940 final float x = ev.getX(pointerIndex);
941 final int deltaX = (int) (mLastMotionX - x);
942 mLastMotionX = x;
943
Adam Cohen68d73932010-11-15 10:50:58 -0800944 if (deltaX != 0) {
945 mTouchX += deltaX;
946 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
947 if (!mDeferScrollUpdate) {
948 scrollBy(deltaX, 0);
949 } else {
950 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 }
952 } else {
953 awakenScrollBars();
954 }
Adam Cohen564976a2010-10-13 18:52:07 -0700955 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 determineScrollingStart(ev);
957 }
958 break;
959
960 case MotionEvent.ACTION_UP:
961 if (mTouchState == TOUCH_STATE_SCROLLING) {
962 final int activePointerId = mActivePointerId;
963 final int pointerIndex = ev.findPointerIndex(activePointerId);
964 final float x = ev.getX(pointerIndex);
965 final VelocityTracker velocityTracker = mVelocityTracker;
966 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
967 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700968 final int deltaX = (int) (x - mDownMotionX);
969 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
970 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700971
Michael Jurka0142d492010-08-25 17:46:15 -0700972 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700973 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800974 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700975 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700976 } else if ((isSignificantMove && deltaX < 0 ||
977 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700978 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700979 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 } else {
981 snapToDestination();
982 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700983 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 // at this point we have not moved beyond the touch slop
985 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
986 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700987 int nextPage = Math.max(0, mCurrentPage - 1);
988 if (nextPage != mCurrentPage) {
989 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 } else {
991 snapToDestination();
992 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700993 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 // at this point we have not moved beyond the touch slop
995 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
996 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700997 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
998 if (nextPage != mCurrentPage) {
999 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 } else {
1001 snapToDestination();
1002 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001003 } else {
1004 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 }
1006 mTouchState = TOUCH_STATE_REST;
1007 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001008 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 break;
1010
1011 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001012 if (mTouchState == TOUCH_STATE_SCROLLING) {
1013 snapToDestination();
1014 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 mTouchState = TOUCH_STATE_REST;
1016 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001017 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 break;
1019
1020 case MotionEvent.ACTION_POINTER_UP:
1021 onSecondaryPointerUp(ev);
1022 break;
1023 }
1024
1025 return true;
1026 }
1027
Michael Jurkab8f06722010-10-10 15:58:46 -07001028 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1029 if (mVelocityTracker == null) {
1030 mVelocityTracker = VelocityTracker.obtain();
1031 }
1032 mVelocityTracker.addMovement(ev);
1033 }
1034
1035 private void releaseVelocityTracker() {
1036 if (mVelocityTracker != null) {
1037 mVelocityTracker.recycle();
1038 mVelocityTracker = null;
1039 }
1040 }
1041
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 private void onSecondaryPointerUp(MotionEvent ev) {
1043 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1044 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1045 final int pointerId = ev.getPointerId(pointerIndex);
1046 if (pointerId == mActivePointerId) {
1047 // This was our active pointer going up. Choose a new
1048 // active pointer and adjust accordingly.
1049 // TODO: Make this decision more intelligent.
1050 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1051 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1052 mLastMotionY = ev.getY(newPointerIndex);
1053 mActivePointerId = ev.getPointerId(newPointerIndex);
1054 if (mVelocityTracker != null) {
1055 mVelocityTracker.clear();
1056 }
1057 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001058 if (mTouchState == TOUCH_STATE_REST) {
1059 onWallpaperTap(ev);
1060 }
1061 }
1062
1063 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 }
1065
1066 @Override
1067 public void requestChildFocus(View child, View focused) {
1068 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001069 int page = indexOfChild(child);
1070 if (page >= 0 && !isInTouchMode()) {
1071 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 }
1073 }
1074
Winson Chunge3193b92010-09-10 11:44:42 -07001075 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1076 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001077 int left;
1078 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001079 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001080 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001081 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001082 if (left <= relativeOffset && relativeOffset <= right) {
1083 return i;
1084 }
Winson Chunge3193b92010-09-10 11:44:42 -07001085 }
1086 return -1;
1087 }
1088
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 protected int getRelativeChildOffset(int index) {
1090 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1091 }
1092
1093 protected int getChildOffset(int index) {
1094 if (getChildCount() == 0)
1095 return 0;
1096
1097 int offset = getRelativeChildOffset(0);
1098 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001099 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001100 }
1101 return offset;
1102 }
1103
Michael Jurkad3ef3062010-11-23 16:23:58 -08001104 protected int getScaledMeasuredWidth(View child) {
1105 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1106 }
1107
Adam Cohend19d3ca2010-09-15 14:43:42 -07001108 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001109 int minDistanceFromScreenCenter = getMeasuredWidth();
1110 int minDistanceFromScreenCenterIndex = -1;
1111 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1112 final int childCount = getChildCount();
1113 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001114 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001115 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001116 int halfChildWidth = (childWidth / 2);
1117 int childCenter = getChildOffset(i) + halfChildWidth;
1118 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1119 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1120 minDistanceFromScreenCenter = distanceFromScreenCenter;
1121 minDistanceFromScreenCenterIndex = i;
1122 }
1123 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001124 return minDistanceFromScreenCenterIndex;
1125 }
1126
1127 protected void snapToDestination() {
1128 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001129 }
1130
Adam Cohene0f66b52010-11-23 15:06:07 -08001131 private static class ScrollInterpolator implements Interpolator {
1132 public ScrollInterpolator() {
1133 }
1134
1135 public float getInterpolation(float t) {
1136 t -= 1.0f;
1137 return t*t*t*t*t + 1;
1138 }
1139 }
1140
1141 // We want the duration of the page snap animation to be influenced by the distance that
1142 // the screen has to travel, however, we don't want this duration to be effected in a
1143 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1144 // of travel has on the overall snap duration.
1145 float distanceInfluenceForSnapDuration(float f) {
1146 f -= 0.5f; // center the values about 0.
1147 f *= 0.3f * Math.PI / 2.0f;
1148 return (float) Math.sin(f);
1149 }
1150
Michael Jurka0142d492010-08-25 17:46:15 -07001151 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001152 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1153 int halfScreenSize = getMeasuredWidth() / 2;
1154
1155 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1156 int delta = newX - mUnboundedScrollX;
1157 int duration = 0;
1158
1159 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1160 // If the velocity is low enough, then treat this more as an automatic page advance
1161 // as opposed to an apparent physical response to flinging
1162 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1163 return;
1164 }
1165
1166 // Here we compute a "distance" that will be used in the computation of the overall
1167 // snap duration. This is a function of the actual distance that needs to be traveled;
1168 // we keep this value close to half screen size in order to reduce the variance in snap
1169 // duration as a function of the distance the page needs to travel.
1170 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1171 float distance = halfScreenSize + halfScreenSize *
1172 distanceInfluenceForSnapDuration(distanceRatio);
1173
1174 velocity = Math.abs(velocity);
1175 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1176
1177 // we want the page's snap velocity to approximately match the velocity at which the
1178 // user flings, so we scale the duration by a value near to the derivative of the scroll
1179 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1180 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1181
1182 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001183 }
1184
1185 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001186 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 }
1188
Michael Jurka0142d492010-08-25 17:46:15 -07001189 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001190 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001191
Winson Chung86f77532010-08-24 11:08:22 -07001192 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001193 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001195 snapToPage(whichPage, delta, duration);
1196 }
1197
1198 protected void snapToPage(int whichPage, int delta, int duration) {
1199 mNextPage = whichPage;
1200
1201 View focusedChild = getFocusedChild();
1202 if (focusedChild != null && whichPage != mCurrentPage &&
1203 focusedChild == getChildAt(mCurrentPage)) {
1204 focusedChild.clearFocus();
1205 }
1206
1207 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 awakenScrollBars(duration);
1209 if (duration == 0) {
1210 duration = Math.abs(delta);
1211 }
1212
1213 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001214 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001215
1216 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001217 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001218 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 invalidate();
1220 }
1221
1222 @Override
1223 protected Parcelable onSaveInstanceState() {
1224 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001225 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 return state;
1227 }
1228
1229 @Override
1230 protected void onRestoreInstanceState(Parcelable state) {
1231 SavedState savedState = (SavedState) state;
1232 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001233 if (savedState.currentPage != -1) {
1234 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 }
1236 }
1237
1238 public void scrollLeft() {
1239 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001240 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001242 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 }
1244 }
1245
1246 public void scrollRight() {
1247 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001248 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001250 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 }
1252 }
1253
Winson Chung86f77532010-08-24 11:08:22 -07001254 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 int result = -1;
1256 if (v != null) {
1257 ViewParent vp = v.getParent();
1258 int count = getChildCount();
1259 for (int i = 0; i < count; i++) {
1260 if (vp == getChildAt(i)) {
1261 return i;
1262 }
1263 }
1264 }
1265 return result;
1266 }
1267
1268 /**
1269 * @return True is long presses are still allowed for the current touch
1270 */
1271 public boolean allowLongPress() {
1272 return mAllowLongPress;
1273 }
1274
Michael Jurka0142d492010-08-25 17:46:15 -07001275 /**
1276 * Set true to allow long-press events to be triggered, usually checked by
1277 * {@link Launcher} to accept or block dpad-initiated long-presses.
1278 */
1279 public void setAllowLongPress(boolean allowLongPress) {
1280 mAllowLongPress = allowLongPress;
1281 }
1282
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001284 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001285
1286 SavedState(Parcelable superState) {
1287 super(superState);
1288 }
1289
1290 private SavedState(Parcel in) {
1291 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001292 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 }
1294
1295 @Override
1296 public void writeToParcel(Parcel out, int flags) {
1297 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001298 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 }
1300
1301 public static final Parcelable.Creator<SavedState> CREATOR =
1302 new Parcelable.Creator<SavedState>() {
1303 public SavedState createFromParcel(Parcel in) {
1304 return new SavedState(in);
1305 }
1306
1307 public SavedState[] newArray(int size) {
1308 return new SavedState[size];
1309 }
1310 };
1311 }
1312
Winson Chung86f77532010-08-24 11:08:22 -07001313 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001314 if (mContentIsRefreshable) {
1315 final int count = getChildCount();
1316 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001317 int lowerPageBound = getAssociatedLowerPageBound(page);
1318 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001319 for (int i = 0; i < count; ++i) {
1320 final ViewGroup layout = (ViewGroup) getChildAt(i);
1321 final int childCount = layout.getChildCount();
1322 if (lowerPageBound <= i && i <= upperPageBound) {
1323 if (mDirtyPageContent.get(i)) {
1324 syncPageItems(i);
1325 mDirtyPageContent.set(i, false);
1326 }
1327 } else {
1328 if (childCount > 0) {
1329 layout.removeAllViews();
1330 }
1331 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001332 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001333 }
1334 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001335 }
1336 }
1337
Winson Chunge3193b92010-09-10 11:44:42 -07001338 protected int getAssociatedLowerPageBound(int page) {
1339 return Math.max(0, page - 1);
1340 }
1341 protected int getAssociatedUpperPageBound(int page) {
1342 final int count = getChildCount();
1343 return Math.min(page + 1, count - 1);
1344 }
1345
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001346 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001347 if (isChoiceMode(CHOICE_MODE_NONE)) {
1348 mChoiceMode = mode;
1349 mActionMode = startActionMode(callback);
1350 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001351 }
1352
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001353 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001354 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001355 mChoiceMode = CHOICE_MODE_NONE;
1356 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001357 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001358 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001359 }
1360 }
1361
1362 protected boolean isChoiceMode(int mode) {
1363 return mChoiceMode == mode;
1364 }
1365
1366 protected ArrayList<Checkable> getCheckedGrandchildren() {
1367 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1368 final int childCount = getChildCount();
1369 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001370 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001371 final int grandChildCount = layout.getChildCount();
1372 for (int j = 0; j < grandChildCount; ++j) {
1373 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001374 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001375 checked.add((Checkable) v);
1376 }
1377 }
1378 }
1379 return checked;
1380 }
1381
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001382 /**
1383 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1384 * Otherwise, returns null.
1385 */
1386 protected Checkable getSingleCheckedGrandchild() {
1387 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1388 final int childCount = getChildCount();
1389 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001390 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001391 final int grandChildCount = layout.getChildCount();
1392 for (int j = 0; j < grandChildCount; ++j) {
1393 final View v = layout.getChildAt(j);
1394 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1395 return (Checkable) v;
1396 }
1397 }
1398 }
1399 }
1400 return null;
1401 }
1402
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001403 public Object getChosenItem() {
1404 View checkedView = (View) getSingleCheckedGrandchild();
1405 if (checkedView != null) {
1406 return checkedView.getTag();
1407 }
1408 return null;
1409 }
1410
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001411 protected void resetCheckedGrandchildren() {
1412 // loop through children, and set all of their children to _not_ be checked
1413 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1414 for (int i = 0; i < checked.size(); ++i) {
1415 final Checkable c = checked.get(i);
1416 c.setChecked(false);
1417 }
1418 }
1419
Winson Chung86f77532010-08-24 11:08:22 -07001420 /**
1421 * This method is called ONLY to synchronize the number of pages that the paged view has.
1422 * To actually fill the pages with information, implement syncPageItems() below. It is
1423 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1424 * and therefore, individual page items do not need to be updated in this method.
1425 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001426 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001427
1428 /**
1429 * This method is called to synchronize the items that are on a particular page. If views on
1430 * the page can be reused, then they should be updated within this method.
1431 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001433
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001435 if (mContentIsRefreshable) {
1436 // Update all the pages
1437 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001438
Michael Jurka0142d492010-08-25 17:46:15 -07001439 // Mark each of the pages as dirty
1440 final int count = getChildCount();
1441 mDirtyPageContent.clear();
1442 for (int i = 0; i < count; ++i) {
1443 mDirtyPageContent.add(true);
1444 }
1445
1446 // Load any pages that are necessary for the current window of views
1447 loadAssociatedPages(mCurrentPage);
1448 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001449 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001450 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001451 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 }
1453}