blob: 48f42a1ca1a83074217149029d4044e1ba7cc628 [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
19import java.util.ArrayList;
Winson Chung86f77532010-08-24 11:08:22 -070020import java.util.Arrays;
Winson Chung241c3b42010-08-25 16:53:03 -070021import java.util.HashMap;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
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.util.Log;
30import 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 Chung80baf5a2010-08-09 16:03:15 -070038import android.view.animation.Animation.AnimationListener;
Winson Chung86f77532010-08-24 11:08:22 -070039import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Winson Chung80baf5a2010-08-09 16:03:15 -070043import com.android.launcher.R;
44
Winson Chung321e9ee2010-08-09 13:37:56 -070045/**
46 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070047 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070048 */
49public abstract class PagedView extends ViewGroup {
50 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070051 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070052
Winson Chung86f77532010-08-24 11:08:22 -070053 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung321e9ee2010-08-09 13:37:56 -070054 private static final int MIN_LENGTH_FOR_FLING = 50;
55
Winson Chung5f2aa4e2010-08-20 14:49:25 -070056 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070057 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070058
Michael Jurka0142d492010-08-25 17:46:15 -070059 // the velocity at which a fling gesture will cause us to snap to the next page
60 protected int mSnapVelocity = 500;
61
62 protected float mSmoothingTime;
63 protected float mTouchX;
64
65 protected boolean mFirstLayout = true;
66
67 protected int mCurrentPage;
68 protected int mNextPage = INVALID_PAGE;
69 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070070 private VelocityTracker mVelocityTracker;
71
72 private float mDownMotionX;
73 private float mLastMotionX;
74 private float mLastMotionY;
75
Michael Jurka0142d492010-08-25 17:46:15 -070076 protected final static int TOUCH_STATE_REST = 0;
77 protected final static int TOUCH_STATE_SCROLLING = 1;
78 protected final static int TOUCH_STATE_PREV_PAGE = 2;
79 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Michael Jurka0142d492010-08-25 17:46:15 -070081 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070082
Michael Jurka0142d492010-08-25 17:46:15 -070083 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070084
85 private boolean mAllowLongPress = true;
86
87 private int mTouchSlop;
88 private int mPagingTouchSlop;
89 private int mMaximumVelocity;
90
91 private static final int INVALID_POINTER = -1;
92
93 private int mActivePointerId = INVALID_POINTER;
94
Michael Jurka0142d492010-08-25 17:46:15 -070095 private enum PageMovingState { PAGE_BEGIN_MOVING, PAGE_END_MOVING };
Winson Chung86f77532010-08-24 11:08:22 -070096 private PageSwitchListener mPageSwitchListener;
Michael Jurka0142d492010-08-25 17:46:15 -070097 private PageMovingListener mPageMovingListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070098
Winson Chung86f77532010-08-24 11:08:22 -070099 private ArrayList<Boolean> mDirtyPageContent;
100 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700102 // choice modes
103 protected static final int CHOICE_MODE_NONE = 0;
104 protected static final int CHOICE_MODE_SINGLE = 1;
105 // Multiple selection mode is not supported by all Launcher actions atm
106 protected static final int CHOICE_MODE_MULTIPLE = 2;
107 private int mChoiceMode;
108 private ActionMode mActionMode;
109
Winson Chung241c3b42010-08-25 16:53:03 -0700110 protected PagedViewIconCache mPageViewIconCache;
111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 // If true, syncPages and syncPageItems will be called to refresh pages
113 protected boolean mContentIsRefreshable = true;
114
115 // If true, modify alpha of neighboring pages as user scrolls left/right
116 protected boolean mFadeInAdjacentScreens = true;
117
118 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
119 // to switch to a new page
120 protected boolean mUsePagingTouchSlop = true;
121
122 // If true, the subclass should directly update mScrollX itself in its computeScroll method
123 // (SmoothPagedView does this)
124 protected boolean mDeferScrollUpdate = false;
125
Winson Chung241c3b42010-08-25 16:53:03 -0700126 /**
127 * Simple cache mechanism for PagedViewIcon outlines.
128 */
129 class PagedViewIconCache {
130 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
131
132 public void clear() {
133 iconOutlineCache.clear();
134 }
135 public void addOutline(Object key, Bitmap b) {
136 iconOutlineCache.put(key, b);
137 }
138 public void removeOutline(Object key) {
139 if (iconOutlineCache.containsKey(key)) {
140 iconOutlineCache.remove(key);
141 }
142 }
143 public Bitmap getOutline(Object key) {
144 return iconOutlineCache.get(key);
145 }
146 }
147
Winson Chung86f77532010-08-24 11:08:22 -0700148 public interface PageSwitchListener {
149 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700150 }
151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 public interface PageMovingListener {
153 void onPageBeginMoving();
154 void onPageEndMoving();
155 }
156
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 public PagedView(Context context) {
158 this(context, null);
159 }
160
161 public PagedView(Context context, AttributeSet attrs) {
162 this(context, attrs, 0);
163 }
164
165 public PagedView(Context context, AttributeSet attrs, int defStyle) {
166 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700167 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700168
169 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700170 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 }
172
173 /**
174 * Initializes various states for this workspace.
175 */
Michael Jurka0142d492010-08-25 17:46:15 -0700176 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700177 mDirtyPageContent = new ArrayList<Boolean>();
178 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700179 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700181 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
183 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
184 mTouchSlop = configuration.getScaledTouchSlop();
185 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
186 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
187 }
188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
190 mPageSwitchListener = pageSwitchListener;
191 if (mPageSwitchListener != null) {
192 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 }
194 }
195
196 /**
Winson Chung86f77532010-08-24 11:08:22 -0700197 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 *
Winson Chung86f77532010-08-24 11:08:22 -0700199 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 */
Winson Chung86f77532010-08-24 11:08:22 -0700201 int getCurrentPage() {
202 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 }
204
Winson Chung86f77532010-08-24 11:08:22 -0700205 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 return getChildCount();
207 }
208
Winson Chung86f77532010-08-24 11:08:22 -0700209 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 return getChildAt(index);
211 }
212
213 int getScrollWidth() {
214 return getWidth();
215 }
216
217 /**
Winson Chung86f77532010-08-24 11:08:22 -0700218 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 */
Winson Chung86f77532010-08-24 11:08:22 -0700220 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 if (!mScroller.isFinished()) mScroller.abortAnimation();
222 if (getChildCount() == 0) return;
223
Winson Chung86f77532010-08-24 11:08:22 -0700224 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
225 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700226
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700228 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230
Michael Jurka0142d492010-08-25 17:46:15 -0700231 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700232 if (mPageSwitchListener != null) {
233 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 }
235 }
236
Michael Jurka0142d492010-08-25 17:46:15 -0700237 // a method that subclasses can override to add behavior
238 protected void pageBeginMoving() {
239 }
240
241 // a method that subclasses can override to add behavior
242 protected void pageEndMoving() {
243 }
244
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 /**
Winson Chung86f77532010-08-24 11:08:22 -0700246 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 *
248 * @param l The listener used to respond to long clicks.
249 */
250 @Override
251 public void setOnLongClickListener(OnLongClickListener l) {
252 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700253 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700255 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 }
257 }
258
259 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700260 public void scrollTo(int x, int y) {
261 super.scrollTo(x, y);
262 mTouchX = x;
263 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
264 }
265
266 // we moved this functionality to a helper function so SmoothPagedView can reuse it
267 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 if (mScroller.computeScrollOffset()) {
269 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700270 invalidate();
271 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700272 } else if (mNextPage != INVALID_PAGE) {
273 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700274 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700275 notifyPageSwitchListener();
276 pageEndMoving();
277 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 }
Michael Jurka0142d492010-08-25 17:46:15 -0700279 return false;
280 }
281
282 @Override
283 public void computeScroll() {
284 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 }
286
287 @Override
288 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
289 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
290 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
291 if (widthMode != MeasureSpec.EXACTLY) {
292 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
293 }
294
295 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
296 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
297 if (heightMode != MeasureSpec.EXACTLY) {
298 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
299 }
300
301 // The children are given the same width and height as the workspace
302 final int childCount = getChildCount();
303 for (int i = 0; i < childCount; i++) {
304 getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
305 }
306
307 setMeasuredDimension(widthSize, heightSize);
308
309 if (mFirstLayout) {
310 setHorizontalScrollBarEnabled(false);
Winson Chung86f77532010-08-24 11:08:22 -0700311 scrollTo(mCurrentPage * widthSize, 0);
Michael Jurka0142d492010-08-25 17:46:15 -0700312 mScroller.setFinalX(mCurrentPage * widthSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700313 setHorizontalScrollBarEnabled(true);
314 mFirstLayout = false;
315 }
316 }
317
318 @Override
319 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
320 final int childCount = getChildCount();
321 int childLeft = 0;
322 if (childCount > 0) {
323 childLeft = (getMeasuredWidth() - getChildAt(0).getMeasuredWidth()) / 2;
324 }
325
326 for (int i = 0; i < childCount; i++) {
327 final View child = getChildAt(i);
328 if (child.getVisibility() != View.GONE) {
329 final int childWidth = child.getMeasuredWidth();
330 child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
331 childLeft += childWidth;
332 }
333 }
334 }
335
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 @Override
337 protected void dispatchDraw(Canvas canvas) {
Michael Jurka0142d492010-08-25 17:46:15 -0700338 if (mFadeInAdjacentScreens) {
339 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
340 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
341 final int childCount = getChildCount();
342 for (int i = 0; i < childCount; ++i) {
343 View layout = (View) getChildAt(i);
344 int childWidth = layout.getMeasuredWidth();
345 int halfChildWidth = (childWidth / 2);
346 int childCenter = getChildOffset(i) + halfChildWidth;
347 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
348 float alpha = 0.0f;
349 if (distanceFromScreenCenter < halfChildWidth) {
350 alpha = 1.0f;
351 } else if (distanceFromScreenCenter > childWidth) {
352 alpha = 0.0f;
353 } else {
354 float dimAlpha = (float) (distanceFromScreenCenter - halfChildWidth) / halfChildWidth;
355 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
356 alpha = 1.0f - dimAlpha;
357 }
358 if (Float.compare(alpha, layout.getAlpha()) != 0) {
359 layout.setAlpha(alpha);
360 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 }
Michael Jurka0142d492010-08-25 17:46:15 -0700362 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
364 }
Michael Jurka0142d492010-08-25 17:46:15 -0700365
366 // Find out which screens are visible; as an optimization we only call draw on them
367
368 // As an optimization, this code assumes that all pages have the same width as the 0th
369 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700370 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700371 if (pageCount > 0) {
372 final int pageWidth = getChildAt(0).getMeasuredWidth();
373 final int screenWidth = getMeasuredWidth();
374 int x = getRelativeChildOffset(0) + pageWidth;
375 int leftScreen = 0;
376 int rightScreen = 0;
377 while (x <= mScrollX) {
378 leftScreen++;
379 x += pageWidth;
380 // replace above line with this if you don't assume all pages have same width as 0th
381 // page:
382 // x += getChildAt(leftScreen).getMeasuredWidth();
383 }
384 rightScreen = leftScreen;
385 while (x < mScrollX + screenWidth) {
386 rightScreen++;
387 x += pageWidth;
388 // replace above line with this if you don't assume all pages have same width as 0th
389 // page:
390 //if (rightScreen < pageCount) {
391 // x += getChildAt(rightScreen).getMeasuredWidth();
392 //}
393 }
394 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700395
Michael Jurkac4fb9172010-09-02 17:19:20 -0700396 final long drawingTime = getDrawingTime();
397 for (int i = leftScreen; i <= rightScreen; i++) {
398 drawChild(canvas, getChildAt(i), drawingTime);
399 }
Michael Jurka0142d492010-08-25 17:46:15 -0700400 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 }
402
403 @Override
404 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700405 int page = indexOfChild(child);
406 if (page != mCurrentPage || !mScroller.isFinished()) {
407 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700408 return true;
409 }
410 return false;
411 }
412
413 @Override
414 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700415 int focusablePage;
416 if (mNextPage != INVALID_PAGE) {
417 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700419 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700420 }
Winson Chung86f77532010-08-24 11:08:22 -0700421 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 if (v != null) {
423 v.requestFocus(direction, previouslyFocusedRect);
424 }
425 return false;
426 }
427
428 @Override
429 public boolean dispatchUnhandledMove(View focused, int direction) {
430 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700431 if (getCurrentPage() > 0) {
432 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 return true;
434 }
435 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700436 if (getCurrentPage() < getPageCount() - 1) {
437 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 return true;
439 }
440 }
441 return super.dispatchUnhandledMove(focused, direction);
442 }
443
444 @Override
445 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700446 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
447 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 }
449 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700450 if (mCurrentPage > 0) {
451 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 }
453 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700454 if (mCurrentPage < getPageCount() - 1) {
455 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 }
457 }
458 }
459
460 /**
461 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700462 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 *
Winson Chung86f77532010-08-24 11:08:22 -0700464 * This happens when live folders requery, and if they're off page, they
465 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 */
467 @Override
468 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700469 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 View v = focused;
471 while (true) {
472 if (v == current) {
473 super.focusableViewAvailable(focused);
474 return;
475 }
476 if (v == this) {
477 return;
478 }
479 ViewParent parent = v.getParent();
480 if (parent instanceof View) {
481 v = (View)v.getParent();
482 } else {
483 return;
484 }
485 }
486 }
487
488 /**
489 * {@inheritDoc}
490 */
491 @Override
492 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
493 if (disallowIntercept) {
494 // We need to make sure to cancel our long press if
495 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700496 final View currentPage = getChildAt(mCurrentPage);
497 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
499 super.requestDisallowInterceptTouchEvent(disallowIntercept);
500 }
501
502 @Override
503 public boolean onInterceptTouchEvent(MotionEvent ev) {
504 /*
505 * This method JUST determines whether we want to intercept the motion.
506 * If we return true, onTouchEvent will be called and we do the actual
507 * scrolling there.
508 */
509
510 /*
511 * Shortcut the most recurring case: the user is in the dragging
512 * state and he is moving his finger. We want to intercept this
513 * motion.
514 */
515 final int action = ev.getAction();
516 if ((action == MotionEvent.ACTION_MOVE) &&
517 (mTouchState == TOUCH_STATE_SCROLLING)) {
518 return true;
519 }
520
521
522 switch (action & MotionEvent.ACTION_MASK) {
523 case MotionEvent.ACTION_MOVE: {
524 /*
525 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
526 * whether the user has moved far enough from his original down touch.
527 */
528 determineScrollingStart(ev);
529 break;
530 }
531
532 case MotionEvent.ACTION_DOWN: {
533 final float x = ev.getX();
534 final float y = ev.getY();
535 // Remember location of down touch
536 mDownMotionX = x;
537 mLastMotionX = x;
538 mLastMotionY = y;
539 mActivePointerId = ev.getPointerId(0);
540 mAllowLongPress = true;
541
542 /*
543 * If being flinged and user touches the screen, initiate drag;
544 * otherwise don't. mScroller.isFinished should be false when
545 * being flinged.
546 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700547 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
548 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
549 if (finishedScrolling) {
550 mTouchState = TOUCH_STATE_REST;
551 mScroller.abortAnimation();
552 } else {
553 mTouchState = TOUCH_STATE_SCROLLING;
554 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700555
Winson Chung86f77532010-08-24 11:08:22 -0700556 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 // to scroll the current page
558 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
559 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
560 if (getChildCount() > 0) {
561 int relativeChildLeft = getChildOffset(0);
562 int relativeChildRight = relativeChildLeft + getChildAt(0).getMeasuredWidth();
563 if (x < relativeChildLeft) {
564 mTouchState = TOUCH_STATE_PREV_PAGE;
565 } else if (x > relativeChildRight) {
566 mTouchState = TOUCH_STATE_NEXT_PAGE;
567 }
568 }
569 }
570 break;
571 }
572
573 case MotionEvent.ACTION_CANCEL:
574 case MotionEvent.ACTION_UP:
575 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700576 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 mTouchState = TOUCH_STATE_REST;
578 mAllowLongPress = false;
579 mActivePointerId = INVALID_POINTER;
580
581 break;
582
583 case MotionEvent.ACTION_POINTER_UP:
584 onSecondaryPointerUp(ev);
585 break;
586 }
587
588 /*
589 * The only time we want to intercept motion events is if we are in the
590 * drag mode.
591 */
592 return mTouchState != TOUCH_STATE_REST;
593 }
594
Winson Chung80baf5a2010-08-09 16:03:15 -0700595 protected void animateClickFeedback(View v, final Runnable r) {
596 // animate the view slightly to show click feedback running some logic after it is "pressed"
597 Animation anim = AnimationUtils.loadAnimation(getContext(),
598 R.anim.paged_view_click_feedback);
599 anim.setAnimationListener(new AnimationListener() {
600 @Override
601 public void onAnimationStart(Animation animation) {}
602 @Override
603 public void onAnimationRepeat(Animation animation) {
604 r.run();
605 }
606 @Override
607 public void onAnimationEnd(Animation animation) {}
608 });
609 v.startAnimation(anim);
610 }
611
Winson Chung321e9ee2010-08-09 13:37:56 -0700612 /*
613 * Determines if we should change the touch state to start scrolling after the
614 * user moves their touch point too far.
615 */
616 private void determineScrollingStart(MotionEvent ev) {
617 /*
618 * Locally do absolute value. mLastMotionX is set to the y value
619 * of the down event.
620 */
621 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
622 final float x = ev.getX(pointerIndex);
623 final float y = ev.getY(pointerIndex);
624 final int xDiff = (int) Math.abs(x - mLastMotionX);
625 final int yDiff = (int) Math.abs(y - mLastMotionY);
626
627 final int touchSlop = mTouchSlop;
628 boolean xPaged = xDiff > mPagingTouchSlop;
629 boolean xMoved = xDiff > touchSlop;
630 boolean yMoved = yDiff > touchSlop;
631
632 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700633 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 // Scroll if the user moved far enough along the X axis
635 mTouchState = TOUCH_STATE_SCROLLING;
636 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700637 mTouchX = mScrollX;
638 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
639 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 }
641 // Either way, cancel any pending longpress
642 if (mAllowLongPress) {
643 mAllowLongPress = false;
644 // Try canceling the long press. It could also have been scheduled
645 // by a distant descendant, so use the mAllowLongPress flag to block
646 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700647 final View currentPage = getPageAt(mCurrentPage);
648 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
650 }
651 }
652
653 @Override
654 public boolean onTouchEvent(MotionEvent ev) {
655 if (mVelocityTracker == null) {
656 mVelocityTracker = VelocityTracker.obtain();
657 }
658 mVelocityTracker.addMovement(ev);
659
660 final int action = ev.getAction();
661
662 switch (action & MotionEvent.ACTION_MASK) {
663 case MotionEvent.ACTION_DOWN:
664 /*
665 * If being flinged and user touches, stop the fling. isFinished
666 * will be false if being flinged.
667 */
668 if (!mScroller.isFinished()) {
669 mScroller.abortAnimation();
670 }
671
672 // Remember where the motion event started
673 mDownMotionX = mLastMotionX = ev.getX();
674 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700675 if (mTouchState == TOUCH_STATE_SCROLLING) {
676 pageBeginMoving();
677 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700678 break;
679
680 case MotionEvent.ACTION_MOVE:
681 if (mTouchState == TOUCH_STATE_SCROLLING) {
682 // Scroll to follow the motion event
683 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
684 final float x = ev.getX(pointerIndex);
685 final int deltaX = (int) (mLastMotionX - x);
686 mLastMotionX = x;
687
688 int sx = getScrollX();
689 if (deltaX < 0) {
690 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700691 mTouchX += Math.max(-mTouchX, deltaX);
692 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
693 if (!mDeferScrollUpdate) {
694 scrollBy(Math.max(-sx, deltaX), 0);
695 } else {
696 // This will trigger a call to computeScroll() on next drawChild() call
697 invalidate();
698 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 }
700 } else if (deltaX > 0) {
701 final int lastChildIndex = getChildCount() - 1;
702 final int availableToScroll = getChildOffset(lastChildIndex) -
703 getRelativeChildOffset(lastChildIndex) - sx;
704 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700705 mTouchX += Math.min(availableToScroll, deltaX);
706 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
707 if (!mDeferScrollUpdate) {
708 scrollBy(Math.min(availableToScroll, deltaX), 0);
709 } else {
710 // This will trigger a call to computeScroll() on next drawChild() call
711 invalidate();
712 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 }
714 } else {
715 awakenScrollBars();
716 }
717 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
718 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
719 determineScrollingStart(ev);
720 }
721 break;
722
723 case MotionEvent.ACTION_UP:
724 if (mTouchState == TOUCH_STATE_SCROLLING) {
725 final int activePointerId = mActivePointerId;
726 final int pointerIndex = ev.findPointerIndex(activePointerId);
727 final float x = ev.getX(pointerIndex);
728 final VelocityTracker velocityTracker = mVelocityTracker;
729 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
730 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
731 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
732
Michael Jurka0142d492010-08-25 17:46:15 -0700733 final int snapVelocity = mSnapVelocity;
734 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
735 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
736 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700737 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700738 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700739 } else {
740 snapToDestination();
741 }
742
743 if (mVelocityTracker != null) {
744 mVelocityTracker.recycle();
745 mVelocityTracker = null;
746 }
747 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
748 // at this point we have not moved beyond the touch slop
749 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
750 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700751 int nextPage = Math.max(0, mCurrentPage - 1);
752 if (nextPage != mCurrentPage) {
753 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 } else {
755 snapToDestination();
756 }
757 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
758 // at this point we have not moved beyond the touch slop
759 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
760 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700761 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
762 if (nextPage != mCurrentPage) {
763 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 } else {
765 snapToDestination();
766 }
767 }
768 mTouchState = TOUCH_STATE_REST;
769 mActivePointerId = INVALID_POINTER;
770 break;
771
772 case MotionEvent.ACTION_CANCEL:
773 mTouchState = TOUCH_STATE_REST;
774 mActivePointerId = INVALID_POINTER;
775 break;
776
777 case MotionEvent.ACTION_POINTER_UP:
778 onSecondaryPointerUp(ev);
779 break;
780 }
781
782 return true;
783 }
784
785 private void onSecondaryPointerUp(MotionEvent ev) {
786 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
787 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
788 final int pointerId = ev.getPointerId(pointerIndex);
789 if (pointerId == mActivePointerId) {
790 // This was our active pointer going up. Choose a new
791 // active pointer and adjust accordingly.
792 // TODO: Make this decision more intelligent.
793 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
794 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
795 mLastMotionY = ev.getY(newPointerIndex);
796 mActivePointerId = ev.getPointerId(newPointerIndex);
797 if (mVelocityTracker != null) {
798 mVelocityTracker.clear();
799 }
800 }
801 }
802
803 @Override
804 public void requestChildFocus(View child, View focused) {
805 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700806 int page = indexOfChild(child);
807 if (page >= 0 && !isInTouchMode()) {
808 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 }
810 }
811
812 protected int getRelativeChildOffset(int index) {
813 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
814 }
815
816 protected int getChildOffset(int index) {
817 if (getChildCount() == 0)
818 return 0;
819
820 int offset = getRelativeChildOffset(0);
821 for (int i = 0; i < index; ++i) {
822 offset += getChildAt(i).getMeasuredWidth();
823 }
824 return offset;
825 }
826
827 protected void snapToDestination() {
828 int minDistanceFromScreenCenter = getMeasuredWidth();
829 int minDistanceFromScreenCenterIndex = -1;
830 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
831 final int childCount = getChildCount();
832 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700833 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 int childWidth = layout.getMeasuredWidth();
835 int halfChildWidth = (childWidth / 2);
836 int childCenter = getChildOffset(i) + halfChildWidth;
837 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
838 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
839 minDistanceFromScreenCenter = distanceFromScreenCenter;
840 minDistanceFromScreenCenterIndex = i;
841 }
842 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700843 snapToPage(minDistanceFromScreenCenterIndex, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 }
845
Michael Jurka0142d492010-08-25 17:46:15 -0700846 protected void snapToPageWithVelocity(int whichPage, int velocity) {
847 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
848 // can use it
849 snapToPage(whichPage);
850 }
851
852 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700853 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 }
855
Michael Jurka0142d492010-08-25 17:46:15 -0700856 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700857 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700858
Winson Chung321e9ee2010-08-09 13:37:56 -0700859
Winson Chung86f77532010-08-24 11:08:22 -0700860 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 final int sX = getScrollX();
862 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700863 snapToPage(whichPage, delta, duration);
864 }
865
866 protected void snapToPage(int whichPage, int delta, int duration) {
867 mNextPage = whichPage;
868
869 View focusedChild = getFocusedChild();
870 if (focusedChild != null && whichPage != mCurrentPage &&
871 focusedChild == getChildAt(mCurrentPage)) {
872 focusedChild.clearFocus();
873 }
874
875 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 awakenScrollBars(duration);
877 if (duration == 0) {
878 duration = Math.abs(delta);
879 }
880
881 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700882 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700883
884 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700885 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700886 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 invalidate();
888 }
889
890 @Override
891 protected Parcelable onSaveInstanceState() {
892 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700893 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 return state;
895 }
896
897 @Override
898 protected void onRestoreInstanceState(Parcelable state) {
899 SavedState savedState = (SavedState) state;
900 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700901 if (savedState.currentPage != -1) {
902 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 }
904 }
905
906 public void scrollLeft() {
907 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700908 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700910 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 }
912 }
913
914 public void scrollRight() {
915 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700916 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700918 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 }
920 }
921
Winson Chung86f77532010-08-24 11:08:22 -0700922 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 int result = -1;
924 if (v != null) {
925 ViewParent vp = v.getParent();
926 int count = getChildCount();
927 for (int i = 0; i < count; i++) {
928 if (vp == getChildAt(i)) {
929 return i;
930 }
931 }
932 }
933 return result;
934 }
935
936 /**
937 * @return True is long presses are still allowed for the current touch
938 */
939 public boolean allowLongPress() {
940 return mAllowLongPress;
941 }
942
Michael Jurka0142d492010-08-25 17:46:15 -0700943 /**
944 * Set true to allow long-press events to be triggered, usually checked by
945 * {@link Launcher} to accept or block dpad-initiated long-presses.
946 */
947 public void setAllowLongPress(boolean allowLongPress) {
948 mAllowLongPress = allowLongPress;
949 }
950
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -0700952 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700953
954 SavedState(Parcelable superState) {
955 super(superState);
956 }
957
958 private SavedState(Parcel in) {
959 super(in);
Winson Chung86f77532010-08-24 11:08:22 -0700960 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 }
962
963 @Override
964 public void writeToParcel(Parcel out, int flags) {
965 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -0700966 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 }
968
969 public static final Parcelable.Creator<SavedState> CREATOR =
970 new Parcelable.Creator<SavedState>() {
971 public SavedState createFromParcel(Parcel in) {
972 return new SavedState(in);
973 }
974
975 public SavedState[] newArray(int size) {
976 return new SavedState[size];
977 }
978 };
979 }
980
Winson Chung86f77532010-08-24 11:08:22 -0700981 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -0700982 if (mContentIsRefreshable) {
983 final int count = getChildCount();
984 if (page < count) {
985 int lowerPageBound = Math.max(0, page - 1);
986 int upperPageBound = Math.min(page + 1, count - 1);
987 for (int i = 0; i < count; ++i) {
988 final ViewGroup layout = (ViewGroup) getChildAt(i);
989 final int childCount = layout.getChildCount();
990 if (lowerPageBound <= i && i <= upperPageBound) {
991 if (mDirtyPageContent.get(i)) {
992 syncPageItems(i);
993 mDirtyPageContent.set(i, false);
994 }
995 } else {
996 if (childCount > 0) {
997 layout.removeAllViews();
998 }
999 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001000 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001001 }
1002 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001003 }
1004 }
1005
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001006 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
1007 // StartActionMode may call through toendChoiceMode, so we should do this first
1008 mActionMode = startActionMode(callback);
1009 mChoiceMode = mode;
1010 }
1011
1012 protected void endChoiceMode() {
1013 if (!isChoiceMode(CHOICE_MODE_NONE)) {
1014 mActionMode.finish();
1015 mActionMode = null;
1016 mChoiceMode = CHOICE_MODE_NONE;
1017 resetCheckedGrandchildren();
1018 }
1019 }
1020
1021 protected boolean isChoiceMode(int mode) {
1022 return mChoiceMode == mode;
1023 }
1024
1025 protected ArrayList<Checkable> getCheckedGrandchildren() {
1026 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1027 final int childCount = getChildCount();
1028 for (int i = 0; i < childCount; ++i) {
1029 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1030 final int grandChildCount = layout.getChildCount();
1031 for (int j = 0; j < grandChildCount; ++j) {
1032 final View v = layout.getChildAt(j);
1033 if (v instanceof Checkable) {
1034 checked.add((Checkable) v);
1035 }
1036 }
1037 }
1038 return checked;
1039 }
1040
1041 protected void resetCheckedGrandchildren() {
1042 // loop through children, and set all of their children to _not_ be checked
1043 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1044 for (int i = 0; i < checked.size(); ++i) {
1045 final Checkable c = checked.get(i);
1046 c.setChecked(false);
1047 }
1048 }
1049
Winson Chung86f77532010-08-24 11:08:22 -07001050 /**
1051 * This method is called ONLY to synchronize the number of pages that the paged view has.
1052 * To actually fill the pages with information, implement syncPageItems() below. It is
1053 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1054 * and therefore, individual page items do not need to be updated in this method.
1055 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001057
1058 /**
1059 * This method is called to synchronize the items that are on a particular page. If views on
1060 * the page can be reused, then they should be updated within this method.
1061 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001063
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001065 if (mContentIsRefreshable) {
1066 // Update all the pages
1067 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001068
Michael Jurka0142d492010-08-25 17:46:15 -07001069 // Mark each of the pages as dirty
1070 final int count = getChildCount();
1071 mDirtyPageContent.clear();
1072 for (int i = 0; i < count; ++i) {
1073 mDirtyPageContent.add(true);
1074 }
1075
1076 // Load any pages that are necessary for the current window of views
1077 loadAssociatedPages(mCurrentPage);
1078 mDirtyPageAlpha = true;
1079 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001080 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 }
1082}