blob: 31cfb5b21e007733b9ad8412d8d4df3f5c9d8c39 [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 Chunge3193b92010-09-10 11:44:42 -070019import java.util.ArrayList;
20import java.util.HashMap;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070021
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung241c3b42010-08-25 16:53:03 -070024import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070030import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.view.MotionEvent;
32import android.view.VelocityTracker;
33import android.view.View;
34import android.view.ViewConfiguration;
35import android.view.ViewGroup;
36import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070037import android.view.animation.Animation;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.view.animation.AnimationUtils;
Adam Cohene0f66b52010-11-23 15:06:07 -080039import android.view.animation.Interpolator;
Adam Cohen68d73932010-11-15 10:50:58 -080040import android.view.animation.Animation.AnimationListener;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070041import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.widget.Scroller;
43
Winson Chunge3193b92010-09-10 11:44:42 -070044import com.android.launcher.R;
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 Jurka5f1c5092010-09-03 14:15:02 -0700113 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka5f1c5092010-09-03 14:15:02 -0700115 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Winson Chung86f77532010-08-24 11:08:22 -0700117 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Winson Chung86f77532010-08-24 11:08:22 -0700119 private ArrayList<Boolean> mDirtyPageContent;
120 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700122 // choice modes
123 protected static final int CHOICE_MODE_NONE = 0;
124 protected static final int CHOICE_MODE_SINGLE = 1;
125 // Multiple selection mode is not supported by all Launcher actions atm
126 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700127
Michael Jurkae17e19c2010-09-28 11:01:39 -0700128 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700129 private ActionMode mActionMode;
130
Winson Chung241c3b42010-08-25 16:53:03 -0700131 protected PagedViewIconCache mPageViewIconCache;
132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 // If true, syncPages and syncPageItems will be called to refresh pages
134 protected boolean mContentIsRefreshable = true;
135
136 // If true, modify alpha of neighboring pages as user scrolls left/right
137 protected boolean mFadeInAdjacentScreens = true;
138
139 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
140 // to switch to a new page
141 protected boolean mUsePagingTouchSlop = true;
142
143 // If true, the subclass should directly update mScrollX itself in its computeScroll method
144 // (SmoothPagedView does this)
145 protected boolean mDeferScrollUpdate = false;
146
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147 protected boolean mIsPageMoving = false;
148
Winson Chung241c3b42010-08-25 16:53:03 -0700149 /**
150 * Simple cache mechanism for PagedViewIcon outlines.
151 */
152 class PagedViewIconCache {
153 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
154
155 public void clear() {
156 iconOutlineCache.clear();
157 }
158 public void addOutline(Object key, Bitmap b) {
159 iconOutlineCache.put(key, b);
160 }
161 public void removeOutline(Object key) {
162 if (iconOutlineCache.containsKey(key)) {
163 iconOutlineCache.remove(key);
164 }
165 }
166 public Bitmap getOutline(Object key) {
167 return iconOutlineCache.get(key);
168 }
169 }
170
Winson Chung86f77532010-08-24 11:08:22 -0700171 public interface PageSwitchListener {
172 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 }
174
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 public PagedView(Context context) {
176 this(context, null);
177 }
178
179 public PagedView(Context context, AttributeSet attrs) {
180 this(context, attrs, 0);
181 }
182
183 public PagedView(Context context, AttributeSet attrs, int defStyle) {
184 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700185 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 TypedArray a = context.obtainStyledAttributes(attrs,
188 R.styleable.PagedView, defStyle, 0);
189 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
190 mPageLayoutPaddingTop = a.getDimensionPixelSize(
191 R.styleable.PagedView_pageLayoutPaddingTop, 10);
192 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
193 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
194 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
195 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
196 mPageLayoutPaddingRight = a.getDimensionPixelSize(
197 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700198 mPageLayoutWidthGap = a.getDimensionPixelSize(
199 R.styleable.PagedView_pageLayoutWidthGap, -1);
200 mPageLayoutHeightGap = a.getDimensionPixelSize(
201 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 a.recycle();
203
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700205 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 }
207
208 /**
209 * Initializes various states for this workspace.
210 */
Michael Jurka0142d492010-08-25 17:46:15 -0700211 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700212 mDirtyPageContent = new ArrayList<Boolean>();
213 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700214 mPageViewIconCache = new PagedViewIconCache();
Adam Cohene0f66b52010-11-23 15:06:07 -0800215 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700216 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700217 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700218
219 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
220 mTouchSlop = configuration.getScaledTouchSlop();
221 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
222 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
223 }
224
Winson Chung86f77532010-08-24 11:08:22 -0700225 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
226 mPageSwitchListener = pageSwitchListener;
227 if (mPageSwitchListener != null) {
228 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230 }
231
232 /**
Winson Chung86f77532010-08-24 11:08:22 -0700233 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 *
Winson Chung86f77532010-08-24 11:08:22 -0700235 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 */
Winson Chung86f77532010-08-24 11:08:22 -0700237 int getCurrentPage() {
238 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 }
240
Winson Chung86f77532010-08-24 11:08:22 -0700241 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 return getChildCount();
243 }
244
Winson Chung86f77532010-08-24 11:08:22 -0700245 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 return getChildAt(index);
247 }
248
249 int getScrollWidth() {
250 return getWidth();
251 }
252
253 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800254 * Updates the scroll of the current page immediately to its final scroll position. We use this
255 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
256 * the previous tab page.
257 */
258 protected void updateCurrentPageScroll() {
259 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
260 scrollTo(newX, 0);
261 mScroller.setFinalX(newX);
262 }
263
264 /**
Winson Chung86f77532010-08-24 11:08:22 -0700265 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 */
Winson Chung86f77532010-08-24 11:08:22 -0700267 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800268 if (!mScroller.isFinished()) {
269 mScroller.abortAnimation();
270 }
271 if (getChildCount() == 0 || currentPage == mCurrentPage) {
272 return;
273 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700274
Winson Chung86f77532010-08-24 11:08:22 -0700275 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800276 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700277
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700279 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 }
281
Michael Jurka0142d492010-08-25 17:46:15 -0700282 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700283 if (mPageSwitchListener != null) {
284 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 }
286 }
287
Patrick Dubroy1262e362010-10-06 15:49:50 -0700288 private void pageBeginMoving() {
289 mIsPageMoving = true;
290 onPageBeginMoving();
291 }
292
293 private void pageEndMoving() {
294 onPageEndMoving();
295 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700296 }
297
298 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700299 protected void onPageBeginMoving() {
300 }
301
302 // a method that subclasses can override to add behavior
303 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700304 }
305
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 /**
Winson Chung86f77532010-08-24 11:08:22 -0700307 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 *
309 * @param l The listener used to respond to long clicks.
310 */
311 @Override
312 public void setOnLongClickListener(OnLongClickListener l) {
313 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700314 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700316 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 }
318 }
319
320 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800321 public void scrollBy(int x, int y) {
322 scrollTo(mUnboundedScrollX + x, mScrollY + y);
323 }
324
325 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700326 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800327 mUnboundedScrollX = x;
328
329 if (x < 0) {
330 super.scrollTo(0, y);
331 if (mAllowOverScroll) {
332 overScroll(x);
333 }
334 } else if (x > mMaxScrollX) {
335 super.scrollTo(mMaxScrollX, y);
336 if (mAllowOverScroll) {
337 overScroll(x - mMaxScrollX);
338 }
339 } else {
340 super.scrollTo(x, y);
341 }
342
Michael Jurka0142d492010-08-25 17:46:15 -0700343 mTouchX = x;
344 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
345 }
346
347 // we moved this functionality to a helper function so SmoothPagedView can reuse it
348 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700350 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700352 invalidate();
353 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700354 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700355 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700356 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700357 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700358 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800359 // We don't want to trigger a page end moving unless the page has settled
360 // and the user has stopped scrolling
361 if (mTouchState == TOUCH_STATE_REST) {
362 pageEndMoving();
363 }
Michael Jurka0142d492010-08-25 17:46:15 -0700364 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700365 }
Michael Jurka0142d492010-08-25 17:46:15 -0700366 return false;
367 }
368
369 @Override
370 public void computeScroll() {
371 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373
374 @Override
375 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
376 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
377 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
378 if (widthMode != MeasureSpec.EXACTLY) {
379 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
380 }
381
Adam Lesinski6b879f02010-11-04 16:15:23 -0700382 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
383 * of the All apps view on XLarge displays to not take up more space then it needs. Width
384 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
385 * each page to have the same width.
386 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700388 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
389 int maxChildHeight = 0;
390
391 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700392
393 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700394 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 final int childCount = getChildCount();
396 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700397 // disallowing padding in paged view (just pass 0)
398 final View child = getChildAt(i);
399 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
400
401 int childWidthMode;
402 if (lp.width == LayoutParams.WRAP_CONTENT) {
403 childWidthMode = MeasureSpec.AT_MOST;
404 } else {
405 childWidthMode = MeasureSpec.EXACTLY;
406 }
407
408 int childHeightMode;
409 if (lp.height == LayoutParams.WRAP_CONTENT) {
410 childHeightMode = MeasureSpec.AT_MOST;
411 } else {
412 childHeightMode = MeasureSpec.EXACTLY;
413 }
414
415 final int childWidthMeasureSpec =
416 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
417 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700418 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700419
420 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700421 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
422 }
423
424 if (heightMode == MeasureSpec.AT_MOST) {
425 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800427 if (childCount > 0) {
428 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
429 } else {
430 mMaxScrollX = 0;
431 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700432
433 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
435
436 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700437 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700438 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
439 setHorizontalScrollBarEnabled(false);
440 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
441 scrollTo(newX, 0);
442 mScroller.setFinalX(newX);
443 setHorizontalScrollBarEnabled(true);
444 mFirstLayout = false;
445 }
446
Adam Lesinski6b879f02010-11-04 16:15:23 -0700447 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 final int childCount = getChildCount();
449 int childLeft = 0;
450 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700451 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 }
453
454 for (int i = 0; i < childCount; i++) {
455 final View child = getChildAt(i);
456 if (child.getVisibility() != View.GONE) {
457 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700458 final int childHeight = child.getMeasuredHeight();
459 int childTop = mPaddingTop;
460 if (mCenterPagesVertically) {
461 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
462 }
463 child.layout(childLeft, childTop,
464 childLeft + childWidth, childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700465 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 }
467 }
468 }
469
Winson Chunge3193b92010-09-10 11:44:42 -0700470 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700471 if (mFadeInAdjacentScreens) {
472 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700473 int halfScreenSize = getMeasuredWidth() / 2;
474 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700475 final int childCount = getChildCount();
476 for (int i = 0; i < childCount; ++i) {
477 View layout = (View) getChildAt(i);
478 int childWidth = layout.getMeasuredWidth();
479 int halfChildWidth = (childWidth / 2);
480 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700481
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700482 // On the first layout, we may not have a width nor a proper offset, so for now
483 // we should just assume full page width (and calculate the offset according to
484 // that).
485 if (childWidth <= 0) {
486 childWidth = getMeasuredWidth();
487 childCenter = (i * childWidth) + (childWidth / 2);
488 }
489
Winson Chunge8878e32010-09-15 20:37:09 -0700490 int d = halfChildWidth;
491 int distanceFromScreenCenter = childCenter - screenCenter;
492 if (distanceFromScreenCenter > 0) {
493 if (i > 0) {
494 d += getChildAt(i - 1).getMeasuredWidth() / 2;
495 }
Michael Jurka0142d492010-08-25 17:46:15 -0700496 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700497 if (i < childCount - 1) {
498 d += getChildAt(i + 1).getMeasuredWidth() / 2;
499 }
Michael Jurka0142d492010-08-25 17:46:15 -0700500 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700501 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700502
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700503 // Preventing potential divide-by-zero
504 d = Math.max(1, d);
505
Winson Chunge8878e32010-09-15 20:37:09 -0700506 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
507 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
508 float alpha = 1.0f - dimAlpha;
509
Adam Cohenf34bab52010-09-30 14:11:56 -0700510 if (alpha < ALPHA_QUANTIZE_LEVEL) {
511 alpha = 0.0f;
512 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
513 alpha = 1.0f;
514 }
515
Michael Jurka0142d492010-08-25 17:46:15 -0700516 if (Float.compare(alpha, layout.getAlpha()) != 0) {
517 layout.setAlpha(alpha);
518 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
Michael Jurka0142d492010-08-25 17:46:15 -0700520 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 }
522 }
Winson Chunge3193b92010-09-10 11:44:42 -0700523 }
524
Adam Cohenf34bab52010-09-30 14:11:56 -0700525 protected void screenScrolled(int screenCenter) {
526 }
527
Winson Chunge3193b92010-09-10 11:44:42 -0700528 @Override
529 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700530 int halfScreenSize = getMeasuredWidth() / 2;
531 int screenCenter = mScrollX + halfScreenSize;
532
533 if (screenCenter != mLastScreenCenter) {
534 screenScrolled(screenCenter);
535 updateAdjacentPagesAlpha();
536 mLastScreenCenter = screenCenter;
537 }
Michael Jurka0142d492010-08-25 17:46:15 -0700538
539 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700540 // As an optimization, this code assumes that all pages have the same width as the 0th
541 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700542 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700543 if (pageCount > 0) {
544 final int pageWidth = getChildAt(0).getMeasuredWidth();
545 final int screenWidth = getMeasuredWidth();
546 int x = getRelativeChildOffset(0) + pageWidth;
547 int leftScreen = 0;
548 int rightScreen = 0;
549 while (x <= mScrollX) {
550 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700551 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700552 // replace above line with this if you don't assume all pages have same width as 0th
553 // page:
554 // x += getChildAt(leftScreen).getMeasuredWidth();
555 }
556 rightScreen = leftScreen;
557 while (x < mScrollX + screenWidth) {
558 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700559 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700560 // replace above line with this if you don't assume all pages have same width as 0th
561 // page:
562 //if (rightScreen < pageCount) {
563 // x += getChildAt(rightScreen).getMeasuredWidth();
564 //}
565 }
566 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700567
Michael Jurkac4fb9172010-09-02 17:19:20 -0700568 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800569 // Clip to the bounds
570 canvas.save();
571 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
572 mScrollY + mBottom - mTop);
573
Michael Jurkac4fb9172010-09-02 17:19:20 -0700574 for (int i = leftScreen; i <= rightScreen; i++) {
575 drawChild(canvas, getChildAt(i), drawingTime);
576 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800577 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700578 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 }
580
581 @Override
582 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700583 int page = indexOfChild(child);
584 if (page != mCurrentPage || !mScroller.isFinished()) {
585 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700586 return true;
587 }
588 return false;
589 }
590
591 @Override
592 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700593 int focusablePage;
594 if (mNextPage != INVALID_PAGE) {
595 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700596 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700597 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
Winson Chung86f77532010-08-24 11:08:22 -0700599 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 if (v != null) {
601 v.requestFocus(direction, previouslyFocusedRect);
602 }
603 return false;
604 }
605
606 @Override
607 public boolean dispatchUnhandledMove(View focused, int direction) {
608 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700609 if (getCurrentPage() > 0) {
610 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 return true;
612 }
613 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700614 if (getCurrentPage() < getPageCount() - 1) {
615 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 return true;
617 }
618 }
619 return super.dispatchUnhandledMove(focused, direction);
620 }
621
622 @Override
623 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700624 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
625 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 }
627 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700628 if (mCurrentPage > 0) {
629 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700630 }
631 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700632 if (mCurrentPage < getPageCount() - 1) {
633 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 }
635 }
636 }
637
638 /**
639 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700640 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 *
Winson Chung86f77532010-08-24 11:08:22 -0700642 * This happens when live folders requery, and if they're off page, they
643 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 */
645 @Override
646 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700647 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 View v = focused;
649 while (true) {
650 if (v == current) {
651 super.focusableViewAvailable(focused);
652 return;
653 }
654 if (v == this) {
655 return;
656 }
657 ViewParent parent = v.getParent();
658 if (parent instanceof View) {
659 v = (View)v.getParent();
660 } else {
661 return;
662 }
663 }
664 }
665
666 /**
667 * {@inheritDoc}
668 */
669 @Override
670 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
671 if (disallowIntercept) {
672 // We need to make sure to cancel our long press if
673 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700674 final View currentPage = getChildAt(mCurrentPage);
675 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 }
677 super.requestDisallowInterceptTouchEvent(disallowIntercept);
678 }
679
680 @Override
681 public boolean onInterceptTouchEvent(MotionEvent ev) {
682 /*
683 * This method JUST determines whether we want to intercept the motion.
684 * If we return true, onTouchEvent will be called and we do the actual
685 * scrolling there.
686 */
687
Winson Chung45e1d6e2010-11-09 17:19:49 -0800688 // Skip touch handling if there are no pages to swipe
689 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
690
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 /*
692 * Shortcut the most recurring case: the user is in the dragging
693 * state and he is moving his finger. We want to intercept this
694 * motion.
695 */
696 final int action = ev.getAction();
697 if ((action == MotionEvent.ACTION_MOVE) &&
698 (mTouchState == TOUCH_STATE_SCROLLING)) {
699 return true;
700 }
701
Winson Chung321e9ee2010-08-09 13:37:56 -0700702 switch (action & MotionEvent.ACTION_MASK) {
703 case MotionEvent.ACTION_MOVE: {
704 /*
705 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
706 * whether the user has moved far enough from his original down touch.
707 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700708 if (mActivePointerId != INVALID_POINTER) {
709 determineScrollingStart(ev);
710 break;
711 }
712 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
713 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
714 // i.e. fall through to the next case (don't break)
715 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
716 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 }
718
719 case MotionEvent.ACTION_DOWN: {
720 final float x = ev.getX();
721 final float y = ev.getY();
722 // Remember location of down touch
723 mDownMotionX = x;
724 mLastMotionX = x;
725 mLastMotionY = y;
726 mActivePointerId = ev.getPointerId(0);
727 mAllowLongPress = true;
728
729 /*
730 * If being flinged and user touches the screen, initiate drag;
731 * otherwise don't. mScroller.isFinished should be false when
732 * being flinged.
733 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700734 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700735 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
736 if (finishedScrolling) {
737 mTouchState = TOUCH_STATE_REST;
738 mScroller.abortAnimation();
739 } else {
740 mTouchState = TOUCH_STATE_SCROLLING;
741 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700742
Winson Chung86f77532010-08-24 11:08:22 -0700743 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700744 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700745 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
747 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700748 int width = getMeasuredWidth();
749 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700750 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700752 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 mTouchState = TOUCH_STATE_NEXT_PAGE;
754 }
755 }
756 }
757 break;
758 }
759
760 case MotionEvent.ACTION_CANCEL:
761 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 mTouchState = TOUCH_STATE_REST;
763 mAllowLongPress = false;
764 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 break;
766
767 case MotionEvent.ACTION_POINTER_UP:
768 onSecondaryPointerUp(ev);
769 break;
770 }
771
772 /*
773 * The only time we want to intercept motion events is if we are in the
774 * drag mode.
775 */
776 return mTouchState != TOUCH_STATE_REST;
777 }
778
Winson Chung80baf5a2010-08-09 16:03:15 -0700779 protected void animateClickFeedback(View v, final Runnable r) {
780 // animate the view slightly to show click feedback running some logic after it is "pressed"
781 Animation anim = AnimationUtils.loadAnimation(getContext(),
782 R.anim.paged_view_click_feedback);
783 anim.setAnimationListener(new AnimationListener() {
784 @Override
785 public void onAnimationStart(Animation animation) {}
786 @Override
787 public void onAnimationRepeat(Animation animation) {
788 r.run();
789 }
790 @Override
791 public void onAnimationEnd(Animation animation) {}
792 });
793 v.startAnimation(anim);
794 }
795
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 /*
797 * Determines if we should change the touch state to start scrolling after the
798 * user moves their touch point too far.
799 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700800 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 /*
802 * Locally do absolute value. mLastMotionX is set to the y value
803 * of the down event.
804 */
805 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
806 final float x = ev.getX(pointerIndex);
807 final float y = ev.getY(pointerIndex);
808 final int xDiff = (int) Math.abs(x - mLastMotionX);
809 final int yDiff = (int) Math.abs(y - mLastMotionY);
810
811 final int touchSlop = mTouchSlop;
812 boolean xPaged = xDiff > mPagingTouchSlop;
813 boolean xMoved = xDiff > touchSlop;
814 boolean yMoved = yDiff > touchSlop;
815
816 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700817 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 // Scroll if the user moved far enough along the X axis
819 mTouchState = TOUCH_STATE_SCROLLING;
820 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700821 mTouchX = mScrollX;
822 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
823 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
825 // Either way, cancel any pending longpress
826 if (mAllowLongPress) {
827 mAllowLongPress = false;
828 // Try canceling the long press. It could also have been scheduled
829 // by a distant descendant, so use the mAllowLongPress flag to block
830 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700831 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700832 if (currentPage != null) {
833 currentPage.cancelLongPress();
834 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
836 }
837 }
838
Adam Cohen21f12b52010-10-07 17:15:37 -0700839 protected boolean handlePagingClicks() {
840 return false;
841 }
842
Adam Cohene0f66b52010-11-23 15:06:07 -0800843 // This curve determines how the effect of scrolling over the limits of the page dimishes
844 // as the user pulls further and further from the bounds
845 private float overScrollInfluenceCurve(float f) {
846 f -= 1.0f;
847 return f * f * f + 1.0f;
848 }
849
Adam Cohen68d73932010-11-15 10:50:58 -0800850 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800851 int screenSize = getMeasuredWidth();
852
853 float f = (amount / screenSize);
854
855 if (f == 0) return;
856 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
857
858 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800859 if (amount < 0) {
860 mScrollX = overScrollAmount;
861 } else {
862 mScrollX = mMaxScrollX + overScrollAmount;
863 }
864 invalidate();
865 }
866
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 @Override
868 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800869 // Skip touch handling if there are no pages to swipe
870 if (getChildCount() <= 0) return super.onTouchEvent(ev);
871
Michael Jurkab8f06722010-10-10 15:58:46 -0700872 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700873
874 final int action = ev.getAction();
875
876 switch (action & MotionEvent.ACTION_MASK) {
877 case MotionEvent.ACTION_DOWN:
878 /*
879 * If being flinged and user touches, stop the fling. isFinished
880 * will be false if being flinged.
881 */
882 if (!mScroller.isFinished()) {
883 mScroller.abortAnimation();
884 }
885
886 // Remember where the motion event started
887 mDownMotionX = mLastMotionX = ev.getX();
888 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700889 if (mTouchState == TOUCH_STATE_SCROLLING) {
890 pageBeginMoving();
891 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 break;
893
894 case MotionEvent.ACTION_MOVE:
895 if (mTouchState == TOUCH_STATE_SCROLLING) {
896 // Scroll to follow the motion event
897 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
898 final float x = ev.getX(pointerIndex);
899 final int deltaX = (int) (mLastMotionX - x);
900 mLastMotionX = x;
901
Adam Cohen68d73932010-11-15 10:50:58 -0800902 if (deltaX != 0) {
903 mTouchX += deltaX;
904 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
905 if (!mDeferScrollUpdate) {
906 scrollBy(deltaX, 0);
907 } else {
908 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 }
910 } else {
911 awakenScrollBars();
912 }
Adam Cohen564976a2010-10-13 18:52:07 -0700913 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 determineScrollingStart(ev);
915 }
916 break;
917
918 case MotionEvent.ACTION_UP:
919 if (mTouchState == TOUCH_STATE_SCROLLING) {
920 final int activePointerId = mActivePointerId;
921 final int pointerIndex = ev.findPointerIndex(activePointerId);
922 final float x = ev.getX(pointerIndex);
923 final VelocityTracker velocityTracker = mVelocityTracker;
924 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
925 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700926 final int deltaX = (int) (x - mDownMotionX);
927 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
928 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700929
Michael Jurka0142d492010-08-25 17:46:15 -0700930 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700931 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800932 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700933 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700934 } else if ((isSignificantMove && deltaX < 0 ||
935 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700936 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700937 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 } else {
939 snapToDestination();
940 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700941 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 // at this point we have not moved beyond the touch slop
943 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
944 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700945 int nextPage = Math.max(0, mCurrentPage - 1);
946 if (nextPage != mCurrentPage) {
947 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 } else {
949 snapToDestination();
950 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700951 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 // at this point we have not moved beyond the touch slop
953 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
954 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700955 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
956 if (nextPage != mCurrentPage) {
957 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 } else {
959 snapToDestination();
960 }
961 }
962 mTouchState = TOUCH_STATE_REST;
963 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700964 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 break;
966
967 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700968 if (mTouchState == TOUCH_STATE_SCROLLING) {
969 snapToDestination();
970 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 mTouchState = TOUCH_STATE_REST;
972 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700973 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 break;
975
976 case MotionEvent.ACTION_POINTER_UP:
977 onSecondaryPointerUp(ev);
978 break;
979 }
980
981 return true;
982 }
983
Michael Jurkab8f06722010-10-10 15:58:46 -0700984 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
985 if (mVelocityTracker == null) {
986 mVelocityTracker = VelocityTracker.obtain();
987 }
988 mVelocityTracker.addMovement(ev);
989 }
990
991 private void releaseVelocityTracker() {
992 if (mVelocityTracker != null) {
993 mVelocityTracker.recycle();
994 mVelocityTracker = null;
995 }
996 }
997
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 private void onSecondaryPointerUp(MotionEvent ev) {
999 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1000 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1001 final int pointerId = ev.getPointerId(pointerIndex);
1002 if (pointerId == mActivePointerId) {
1003 // This was our active pointer going up. Choose a new
1004 // active pointer and adjust accordingly.
1005 // TODO: Make this decision more intelligent.
1006 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1007 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1008 mLastMotionY = ev.getY(newPointerIndex);
1009 mActivePointerId = ev.getPointerId(newPointerIndex);
1010 if (mVelocityTracker != null) {
1011 mVelocityTracker.clear();
1012 }
1013 }
1014 }
1015
1016 @Override
1017 public void requestChildFocus(View child, View focused) {
1018 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001019 int page = indexOfChild(child);
1020 if (page >= 0 && !isInTouchMode()) {
1021 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 }
1023 }
1024
Winson Chunge3193b92010-09-10 11:44:42 -07001025 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1026 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001027 int left;
1028 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001029 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001030 left = getRelativeChildOffset(i);
1031 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -07001032 if (left <= relativeOffset && relativeOffset <= right) {
1033 return i;
1034 }
Winson Chunge3193b92010-09-10 11:44:42 -07001035 }
1036 return -1;
1037 }
1038
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 protected int getRelativeChildOffset(int index) {
1040 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1041 }
1042
1043 protected int getChildOffset(int index) {
1044 if (getChildCount() == 0)
1045 return 0;
1046
1047 int offset = getRelativeChildOffset(0);
1048 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001049 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001050 }
1051 return offset;
1052 }
1053
Adam Cohend19d3ca2010-09-15 14:43:42 -07001054 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 int minDistanceFromScreenCenter = getMeasuredWidth();
1056 int minDistanceFromScreenCenterIndex = -1;
1057 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1058 final int childCount = getChildCount();
1059 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001060 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 int childWidth = layout.getMeasuredWidth();
1062 int halfChildWidth = (childWidth / 2);
1063 int childCenter = getChildOffset(i) + halfChildWidth;
1064 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1065 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1066 minDistanceFromScreenCenter = distanceFromScreenCenter;
1067 minDistanceFromScreenCenterIndex = i;
1068 }
1069 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001070 return minDistanceFromScreenCenterIndex;
1071 }
1072
1073 protected void snapToDestination() {
1074 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 }
1076
Adam Cohene0f66b52010-11-23 15:06:07 -08001077 private static class ScrollInterpolator implements Interpolator {
1078 public ScrollInterpolator() {
1079 }
1080
1081 public float getInterpolation(float t) {
1082 t -= 1.0f;
1083 return t*t*t*t*t + 1;
1084 }
1085 }
1086
1087 // We want the duration of the page snap animation to be influenced by the distance that
1088 // the screen has to travel, however, we don't want this duration to be effected in a
1089 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1090 // of travel has on the overall snap duration.
1091 float distanceInfluenceForSnapDuration(float f) {
1092 f -= 0.5f; // center the values about 0.
1093 f *= 0.3f * Math.PI / 2.0f;
1094 return (float) Math.sin(f);
1095 }
1096
Michael Jurka0142d492010-08-25 17:46:15 -07001097 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001098 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1099 int halfScreenSize = getMeasuredWidth() / 2;
1100
1101 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1102 int delta = newX - mUnboundedScrollX;
1103 int duration = 0;
1104
1105 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1106 // If the velocity is low enough, then treat this more as an automatic page advance
1107 // as opposed to an apparent physical response to flinging
1108 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1109 return;
1110 }
1111
1112 // Here we compute a "distance" that will be used in the computation of the overall
1113 // snap duration. This is a function of the actual distance that needs to be traveled;
1114 // we keep this value close to half screen size in order to reduce the variance in snap
1115 // duration as a function of the distance the page needs to travel.
1116 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1117 float distance = halfScreenSize + halfScreenSize *
1118 distanceInfluenceForSnapDuration(distanceRatio);
1119
1120 velocity = Math.abs(velocity);
1121 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1122
1123 // we want the page's snap velocity to approximately match the velocity at which the
1124 // user flings, so we scale the duration by a value near to the derivative of the scroll
1125 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1126 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1127
1128 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001129 }
1130
1131 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001132 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001133 }
1134
Michael Jurka0142d492010-08-25 17:46:15 -07001135 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001136 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001137
Winson Chung86f77532010-08-24 11:08:22 -07001138 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001139 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001140 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001141 snapToPage(whichPage, delta, duration);
1142 }
1143
1144 protected void snapToPage(int whichPage, int delta, int duration) {
1145 mNextPage = whichPage;
1146
1147 View focusedChild = getFocusedChild();
1148 if (focusedChild != null && whichPage != mCurrentPage &&
1149 focusedChild == getChildAt(mCurrentPage)) {
1150 focusedChild.clearFocus();
1151 }
1152
1153 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001154 awakenScrollBars(duration);
1155 if (duration == 0) {
1156 duration = Math.abs(delta);
1157 }
1158
1159 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001160 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001161
1162 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001163 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001164 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 invalidate();
1166 }
1167
1168 @Override
1169 protected Parcelable onSaveInstanceState() {
1170 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001171 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 return state;
1173 }
1174
1175 @Override
1176 protected void onRestoreInstanceState(Parcelable state) {
1177 SavedState savedState = (SavedState) state;
1178 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001179 if (savedState.currentPage != -1) {
1180 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 }
1182 }
1183
1184 public void scrollLeft() {
1185 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001186 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001188 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 }
1190 }
1191
1192 public void scrollRight() {
1193 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001194 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001196 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
1198 }
1199
Winson Chung86f77532010-08-24 11:08:22 -07001200 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 int result = -1;
1202 if (v != null) {
1203 ViewParent vp = v.getParent();
1204 int count = getChildCount();
1205 for (int i = 0; i < count; i++) {
1206 if (vp == getChildAt(i)) {
1207 return i;
1208 }
1209 }
1210 }
1211 return result;
1212 }
1213
1214 /**
1215 * @return True is long presses are still allowed for the current touch
1216 */
1217 public boolean allowLongPress() {
1218 return mAllowLongPress;
1219 }
1220
Michael Jurka0142d492010-08-25 17:46:15 -07001221 /**
1222 * Set true to allow long-press events to be triggered, usually checked by
1223 * {@link Launcher} to accept or block dpad-initiated long-presses.
1224 */
1225 public void setAllowLongPress(boolean allowLongPress) {
1226 mAllowLongPress = allowLongPress;
1227 }
1228
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001230 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001231
1232 SavedState(Parcelable superState) {
1233 super(superState);
1234 }
1235
1236 private SavedState(Parcel in) {
1237 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001238 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 }
1240
1241 @Override
1242 public void writeToParcel(Parcel out, int flags) {
1243 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001244 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 }
1246
1247 public static final Parcelable.Creator<SavedState> CREATOR =
1248 new Parcelable.Creator<SavedState>() {
1249 public SavedState createFromParcel(Parcel in) {
1250 return new SavedState(in);
1251 }
1252
1253 public SavedState[] newArray(int size) {
1254 return new SavedState[size];
1255 }
1256 };
1257 }
1258
Winson Chung86f77532010-08-24 11:08:22 -07001259 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001260 if (mContentIsRefreshable) {
1261 final int count = getChildCount();
1262 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001263 int lowerPageBound = getAssociatedLowerPageBound(page);
1264 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001265 for (int i = 0; i < count; ++i) {
1266 final ViewGroup layout = (ViewGroup) getChildAt(i);
1267 final int childCount = layout.getChildCount();
1268 if (lowerPageBound <= i && i <= upperPageBound) {
1269 if (mDirtyPageContent.get(i)) {
1270 syncPageItems(i);
1271 mDirtyPageContent.set(i, false);
1272 }
1273 } else {
1274 if (childCount > 0) {
1275 layout.removeAllViews();
1276 }
1277 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001278 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001279 }
1280 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001281 }
1282 }
1283
Winson Chunge3193b92010-09-10 11:44:42 -07001284 protected int getAssociatedLowerPageBound(int page) {
1285 return Math.max(0, page - 1);
1286 }
1287 protected int getAssociatedUpperPageBound(int page) {
1288 final int count = getChildCount();
1289 return Math.min(page + 1, count - 1);
1290 }
1291
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001292 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001293 if (isChoiceMode(CHOICE_MODE_NONE)) {
1294 mChoiceMode = mode;
1295 mActionMode = startActionMode(callback);
1296 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001297 }
1298
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001299 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001300 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001301 mChoiceMode = CHOICE_MODE_NONE;
1302 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001303 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001304 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001305 }
1306 }
1307
1308 protected boolean isChoiceMode(int mode) {
1309 return mChoiceMode == mode;
1310 }
1311
1312 protected ArrayList<Checkable> getCheckedGrandchildren() {
1313 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1314 final int childCount = getChildCount();
1315 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001316 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001317 final int grandChildCount = layout.getChildCount();
1318 for (int j = 0; j < grandChildCount; ++j) {
1319 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001320 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001321 checked.add((Checkable) v);
1322 }
1323 }
1324 }
1325 return checked;
1326 }
1327
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001328 /**
1329 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1330 * Otherwise, returns null.
1331 */
1332 protected Checkable getSingleCheckedGrandchild() {
1333 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1334 final int childCount = getChildCount();
1335 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001336 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001337 final int grandChildCount = layout.getChildCount();
1338 for (int j = 0; j < grandChildCount; ++j) {
1339 final View v = layout.getChildAt(j);
1340 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1341 return (Checkable) v;
1342 }
1343 }
1344 }
1345 }
1346 return null;
1347 }
1348
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001349 public Object getChosenItem() {
1350 View checkedView = (View) getSingleCheckedGrandchild();
1351 if (checkedView != null) {
1352 return checkedView.getTag();
1353 }
1354 return null;
1355 }
1356
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001357 protected void resetCheckedGrandchildren() {
1358 // loop through children, and set all of their children to _not_ be checked
1359 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1360 for (int i = 0; i < checked.size(); ++i) {
1361 final Checkable c = checked.get(i);
1362 c.setChecked(false);
1363 }
1364 }
1365
Winson Chung86f77532010-08-24 11:08:22 -07001366 /**
1367 * This method is called ONLY to synchronize the number of pages that the paged view has.
1368 * To actually fill the pages with information, implement syncPageItems() below. It is
1369 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1370 * and therefore, individual page items do not need to be updated in this method.
1371 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001373
1374 /**
1375 * This method is called to synchronize the items that are on a particular page. If views on
1376 * the page can be reused, then they should be updated within this method.
1377 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001379
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001381 if (mContentIsRefreshable) {
1382 // Update all the pages
1383 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001384
Michael Jurka0142d492010-08-25 17:46:15 -07001385 // Mark each of the pages as dirty
1386 final int count = getChildCount();
1387 mDirtyPageContent.clear();
1388 for (int i = 0; i < count; ++i) {
1389 mDirtyPageContent.add(true);
1390 }
1391
1392 // Load any pages that are necessary for the current window of views
1393 loadAssociatedPages(mCurrentPage);
1394 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001395 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001396 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001397 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 }
1399}