blob: 8e0203b8af2f716d51597f83371b3446a3a31f19 [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.
370 final int pageWidth = getChildAt(0).getMeasuredWidth();
371 final int pageCount = getChildCount();
372 final int screenWidth = getMeasuredWidth();
373 int x = getRelativeChildOffset(0) + pageWidth;
374 int leftScreen = 0;
375 int rightScreen = 0;
376 while (x <= mScrollX) {
377 leftScreen++;
378 x += pageWidth;
379 // replace above line with this if you don't assume all pages have same width as 0th
380 // page:
381 // x += getChildAt(leftScreen).getMeasuredWidth();
382 }
383 rightScreen = leftScreen;
384 while (x < mScrollX + screenWidth) {
385 rightScreen++;
386 x += pageWidth;
387 // replace above line with this if you don't assume all pages have same width as 0th
388 // page:
389 //if (rightScreen < pageCount) {
390 // x += getChildAt(rightScreen).getMeasuredWidth();
391 //}
392 }
393 rightScreen = Math.min(getChildCount() - 1, rightScreen);
394
395 final long drawingTime = getDrawingTime();
396 for (int i = leftScreen; i <= rightScreen; i++) {
397 drawChild(canvas, getChildAt(i), drawingTime);
398 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 }
400
401 @Override
402 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700403 int page = indexOfChild(child);
404 if (page != mCurrentPage || !mScroller.isFinished()) {
405 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 return true;
407 }
408 return false;
409 }
410
411 @Override
412 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700413 int focusablePage;
414 if (mNextPage != INVALID_PAGE) {
415 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700417 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 }
Winson Chung86f77532010-08-24 11:08:22 -0700419 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700420 if (v != null) {
421 v.requestFocus(direction, previouslyFocusedRect);
422 }
423 return false;
424 }
425
426 @Override
427 public boolean dispatchUnhandledMove(View focused, int direction) {
428 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700429 if (getCurrentPage() > 0) {
430 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 return true;
432 }
433 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700434 if (getCurrentPage() < getPageCount() - 1) {
435 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 return true;
437 }
438 }
439 return super.dispatchUnhandledMove(focused, direction);
440 }
441
442 @Override
443 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700444 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
445 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 }
447 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700448 if (mCurrentPage > 0) {
449 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 }
451 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700452 if (mCurrentPage < getPageCount() - 1) {
453 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
455 }
456 }
457
458 /**
459 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700460 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 *
Winson Chung86f77532010-08-24 11:08:22 -0700462 * This happens when live folders requery, and if they're off page, they
463 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 */
465 @Override
466 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700467 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700468 View v = focused;
469 while (true) {
470 if (v == current) {
471 super.focusableViewAvailable(focused);
472 return;
473 }
474 if (v == this) {
475 return;
476 }
477 ViewParent parent = v.getParent();
478 if (parent instanceof View) {
479 v = (View)v.getParent();
480 } else {
481 return;
482 }
483 }
484 }
485
486 /**
487 * {@inheritDoc}
488 */
489 @Override
490 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
491 if (disallowIntercept) {
492 // We need to make sure to cancel our long press if
493 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700494 final View currentPage = getChildAt(mCurrentPage);
495 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 }
497 super.requestDisallowInterceptTouchEvent(disallowIntercept);
498 }
499
500 @Override
501 public boolean onInterceptTouchEvent(MotionEvent ev) {
502 /*
503 * This method JUST determines whether we want to intercept the motion.
504 * If we return true, onTouchEvent will be called and we do the actual
505 * scrolling there.
506 */
507
508 /*
509 * Shortcut the most recurring case: the user is in the dragging
510 * state and he is moving his finger. We want to intercept this
511 * motion.
512 */
513 final int action = ev.getAction();
514 if ((action == MotionEvent.ACTION_MOVE) &&
515 (mTouchState == TOUCH_STATE_SCROLLING)) {
516 return true;
517 }
518
519
520 switch (action & MotionEvent.ACTION_MASK) {
521 case MotionEvent.ACTION_MOVE: {
522 /*
523 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
524 * whether the user has moved far enough from his original down touch.
525 */
526 determineScrollingStart(ev);
527 break;
528 }
529
530 case MotionEvent.ACTION_DOWN: {
531 final float x = ev.getX();
532 final float y = ev.getY();
533 // Remember location of down touch
534 mDownMotionX = x;
535 mLastMotionX = x;
536 mLastMotionY = y;
537 mActivePointerId = ev.getPointerId(0);
538 mAllowLongPress = true;
539
540 /*
541 * If being flinged and user touches the screen, initiate drag;
542 * otherwise don't. mScroller.isFinished should be false when
543 * being flinged.
544 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700545 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
546 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
547 if (finishedScrolling) {
548 mTouchState = TOUCH_STATE_REST;
549 mScroller.abortAnimation();
550 } else {
551 mTouchState = TOUCH_STATE_SCROLLING;
552 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700553
Winson Chung86f77532010-08-24 11:08:22 -0700554 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 // to scroll the current page
556 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
557 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
558 if (getChildCount() > 0) {
559 int relativeChildLeft = getChildOffset(0);
560 int relativeChildRight = relativeChildLeft + getChildAt(0).getMeasuredWidth();
561 if (x < relativeChildLeft) {
562 mTouchState = TOUCH_STATE_PREV_PAGE;
563 } else if (x > relativeChildRight) {
564 mTouchState = TOUCH_STATE_NEXT_PAGE;
565 }
566 }
567 }
568 break;
569 }
570
571 case MotionEvent.ACTION_CANCEL:
572 case MotionEvent.ACTION_UP:
573 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700574 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 mTouchState = TOUCH_STATE_REST;
576 mAllowLongPress = false;
577 mActivePointerId = INVALID_POINTER;
578
579 break;
580
581 case MotionEvent.ACTION_POINTER_UP:
582 onSecondaryPointerUp(ev);
583 break;
584 }
585
586 /*
587 * The only time we want to intercept motion events is if we are in the
588 * drag mode.
589 */
590 return mTouchState != TOUCH_STATE_REST;
591 }
592
Winson Chung80baf5a2010-08-09 16:03:15 -0700593 protected void animateClickFeedback(View v, final Runnable r) {
594 // animate the view slightly to show click feedback running some logic after it is "pressed"
595 Animation anim = AnimationUtils.loadAnimation(getContext(),
596 R.anim.paged_view_click_feedback);
597 anim.setAnimationListener(new AnimationListener() {
598 @Override
599 public void onAnimationStart(Animation animation) {}
600 @Override
601 public void onAnimationRepeat(Animation animation) {
602 r.run();
603 }
604 @Override
605 public void onAnimationEnd(Animation animation) {}
606 });
607 v.startAnimation(anim);
608 }
609
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 /*
611 * Determines if we should change the touch state to start scrolling after the
612 * user moves their touch point too far.
613 */
614 private void determineScrollingStart(MotionEvent ev) {
615 /*
616 * Locally do absolute value. mLastMotionX is set to the y value
617 * of the down event.
618 */
619 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
620 final float x = ev.getX(pointerIndex);
621 final float y = ev.getY(pointerIndex);
622 final int xDiff = (int) Math.abs(x - mLastMotionX);
623 final int yDiff = (int) Math.abs(y - mLastMotionY);
624
625 final int touchSlop = mTouchSlop;
626 boolean xPaged = xDiff > mPagingTouchSlop;
627 boolean xMoved = xDiff > touchSlop;
628 boolean yMoved = yDiff > touchSlop;
629
630 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700631 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700632 // Scroll if the user moved far enough along the X axis
633 mTouchState = TOUCH_STATE_SCROLLING;
634 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700635 mTouchX = mScrollX;
636 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
637 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
639 // Either way, cancel any pending longpress
640 if (mAllowLongPress) {
641 mAllowLongPress = false;
642 // Try canceling the long press. It could also have been scheduled
643 // by a distant descendant, so use the mAllowLongPress flag to block
644 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700645 final View currentPage = getPageAt(mCurrentPage);
646 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 }
648 }
649 }
650
651 @Override
652 public boolean onTouchEvent(MotionEvent ev) {
653 if (mVelocityTracker == null) {
654 mVelocityTracker = VelocityTracker.obtain();
655 }
656 mVelocityTracker.addMovement(ev);
657
658 final int action = ev.getAction();
659
660 switch (action & MotionEvent.ACTION_MASK) {
661 case MotionEvent.ACTION_DOWN:
662 /*
663 * If being flinged and user touches, stop the fling. isFinished
664 * will be false if being flinged.
665 */
666 if (!mScroller.isFinished()) {
667 mScroller.abortAnimation();
668 }
669
670 // Remember where the motion event started
671 mDownMotionX = mLastMotionX = ev.getX();
672 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700673 if (mTouchState == TOUCH_STATE_SCROLLING) {
674 pageBeginMoving();
675 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 break;
677
678 case MotionEvent.ACTION_MOVE:
679 if (mTouchState == TOUCH_STATE_SCROLLING) {
680 // Scroll to follow the motion event
681 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
682 final float x = ev.getX(pointerIndex);
683 final int deltaX = (int) (mLastMotionX - x);
684 mLastMotionX = x;
685
686 int sx = getScrollX();
687 if (deltaX < 0) {
688 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700689 mTouchX += Math.max(-mTouchX, deltaX);
690 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
691 if (!mDeferScrollUpdate) {
692 scrollBy(Math.max(-sx, deltaX), 0);
693 } else {
694 // This will trigger a call to computeScroll() on next drawChild() call
695 invalidate();
696 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 }
698 } else if (deltaX > 0) {
699 final int lastChildIndex = getChildCount() - 1;
700 final int availableToScroll = getChildOffset(lastChildIndex) -
701 getRelativeChildOffset(lastChildIndex) - sx;
702 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700703 mTouchX += Math.min(availableToScroll, deltaX);
704 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
705 if (!mDeferScrollUpdate) {
706 scrollBy(Math.min(availableToScroll, deltaX), 0);
707 } else {
708 // This will trigger a call to computeScroll() on next drawChild() call
709 invalidate();
710 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 }
712 } else {
713 awakenScrollBars();
714 }
715 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
716 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
717 determineScrollingStart(ev);
718 }
719 break;
720
721 case MotionEvent.ACTION_UP:
722 if (mTouchState == TOUCH_STATE_SCROLLING) {
723 final int activePointerId = mActivePointerId;
724 final int pointerIndex = ev.findPointerIndex(activePointerId);
725 final float x = ev.getX(pointerIndex);
726 final VelocityTracker velocityTracker = mVelocityTracker;
727 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
728 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
729 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
730
Michael Jurka0142d492010-08-25 17:46:15 -0700731 final int snapVelocity = mSnapVelocity;
732 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
733 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
734 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700735 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700736 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700737 } else {
738 snapToDestination();
739 }
740
741 if (mVelocityTracker != null) {
742 mVelocityTracker.recycle();
743 mVelocityTracker = null;
744 }
745 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
746 // at this point we have not moved beyond the touch slop
747 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
748 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700749 int nextPage = Math.max(0, mCurrentPage - 1);
750 if (nextPage != mCurrentPage) {
751 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700752 } else {
753 snapToDestination();
754 }
755 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
756 // at this point we have not moved beyond the touch slop
757 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
758 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700759 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
760 if (nextPage != mCurrentPage) {
761 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 } else {
763 snapToDestination();
764 }
765 }
766 mTouchState = TOUCH_STATE_REST;
767 mActivePointerId = INVALID_POINTER;
768 break;
769
770 case MotionEvent.ACTION_CANCEL:
771 mTouchState = TOUCH_STATE_REST;
772 mActivePointerId = INVALID_POINTER;
773 break;
774
775 case MotionEvent.ACTION_POINTER_UP:
776 onSecondaryPointerUp(ev);
777 break;
778 }
779
780 return true;
781 }
782
783 private void onSecondaryPointerUp(MotionEvent ev) {
784 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
785 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
786 final int pointerId = ev.getPointerId(pointerIndex);
787 if (pointerId == mActivePointerId) {
788 // This was our active pointer going up. Choose a new
789 // active pointer and adjust accordingly.
790 // TODO: Make this decision more intelligent.
791 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
792 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
793 mLastMotionY = ev.getY(newPointerIndex);
794 mActivePointerId = ev.getPointerId(newPointerIndex);
795 if (mVelocityTracker != null) {
796 mVelocityTracker.clear();
797 }
798 }
799 }
800
801 @Override
802 public void requestChildFocus(View child, View focused) {
803 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700804 int page = indexOfChild(child);
805 if (page >= 0 && !isInTouchMode()) {
806 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 }
808 }
809
810 protected int getRelativeChildOffset(int index) {
811 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
812 }
813
814 protected int getChildOffset(int index) {
815 if (getChildCount() == 0)
816 return 0;
817
818 int offset = getRelativeChildOffset(0);
819 for (int i = 0; i < index; ++i) {
820 offset += getChildAt(i).getMeasuredWidth();
821 }
822 return offset;
823 }
824
825 protected void snapToDestination() {
826 int minDistanceFromScreenCenter = getMeasuredWidth();
827 int minDistanceFromScreenCenterIndex = -1;
828 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
829 final int childCount = getChildCount();
830 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700831 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 int childWidth = layout.getMeasuredWidth();
833 int halfChildWidth = (childWidth / 2);
834 int childCenter = getChildOffset(i) + halfChildWidth;
835 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
836 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
837 minDistanceFromScreenCenter = distanceFromScreenCenter;
838 minDistanceFromScreenCenterIndex = i;
839 }
840 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700841 snapToPage(minDistanceFromScreenCenterIndex, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843
Michael Jurka0142d492010-08-25 17:46:15 -0700844 protected void snapToPageWithVelocity(int whichPage, int velocity) {
845 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
846 // can use it
847 snapToPage(whichPage);
848 }
849
850 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700851 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 }
853
Michael Jurka0142d492010-08-25 17:46:15 -0700854 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700855 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700856
Winson Chung321e9ee2010-08-09 13:37:56 -0700857
Winson Chung86f77532010-08-24 11:08:22 -0700858 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 final int sX = getScrollX();
860 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700861 snapToPage(whichPage, delta, duration);
862 }
863
864 protected void snapToPage(int whichPage, int delta, int duration) {
865 mNextPage = whichPage;
866
867 View focusedChild = getFocusedChild();
868 if (focusedChild != null && whichPage != mCurrentPage &&
869 focusedChild == getChildAt(mCurrentPage)) {
870 focusedChild.clearFocus();
871 }
872
873 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 awakenScrollBars(duration);
875 if (duration == 0) {
876 duration = Math.abs(delta);
877 }
878
879 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700880 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700881
882 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700883 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700884 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 invalidate();
886 }
887
888 @Override
889 protected Parcelable onSaveInstanceState() {
890 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700891 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 return state;
893 }
894
895 @Override
896 protected void onRestoreInstanceState(Parcelable state) {
897 SavedState savedState = (SavedState) state;
898 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700899 if (savedState.currentPage != -1) {
900 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 }
902 }
903
904 public void scrollLeft() {
905 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700906 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700908 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 }
910 }
911
912 public void scrollRight() {
913 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700914 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700916 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 }
918 }
919
Winson Chung86f77532010-08-24 11:08:22 -0700920 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 int result = -1;
922 if (v != null) {
923 ViewParent vp = v.getParent();
924 int count = getChildCount();
925 for (int i = 0; i < count; i++) {
926 if (vp == getChildAt(i)) {
927 return i;
928 }
929 }
930 }
931 return result;
932 }
933
934 /**
935 * @return True is long presses are still allowed for the current touch
936 */
937 public boolean allowLongPress() {
938 return mAllowLongPress;
939 }
940
Michael Jurka0142d492010-08-25 17:46:15 -0700941 /**
942 * Set true to allow long-press events to be triggered, usually checked by
943 * {@link Launcher} to accept or block dpad-initiated long-presses.
944 */
945 public void setAllowLongPress(boolean allowLongPress) {
946 mAllowLongPress = allowLongPress;
947 }
948
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -0700950 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700951
952 SavedState(Parcelable superState) {
953 super(superState);
954 }
955
956 private SavedState(Parcel in) {
957 super(in);
Winson Chung86f77532010-08-24 11:08:22 -0700958 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 }
960
961 @Override
962 public void writeToParcel(Parcel out, int flags) {
963 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -0700964 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 }
966
967 public static final Parcelable.Creator<SavedState> CREATOR =
968 new Parcelable.Creator<SavedState>() {
969 public SavedState createFromParcel(Parcel in) {
970 return new SavedState(in);
971 }
972
973 public SavedState[] newArray(int size) {
974 return new SavedState[size];
975 }
976 };
977 }
978
Winson Chung86f77532010-08-24 11:08:22 -0700979 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -0700980 if (mContentIsRefreshable) {
981 final int count = getChildCount();
982 if (page < count) {
983 int lowerPageBound = Math.max(0, page - 1);
984 int upperPageBound = Math.min(page + 1, count - 1);
985 for (int i = 0; i < count; ++i) {
986 final ViewGroup layout = (ViewGroup) getChildAt(i);
987 final int childCount = layout.getChildCount();
988 if (lowerPageBound <= i && i <= upperPageBound) {
989 if (mDirtyPageContent.get(i)) {
990 syncPageItems(i);
991 mDirtyPageContent.set(i, false);
992 }
993 } else {
994 if (childCount > 0) {
995 layout.removeAllViews();
996 }
997 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -0700998 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700999 }
1000 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001001 }
1002 }
1003
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001004 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
1005 // StartActionMode may call through toendChoiceMode, so we should do this first
1006 mActionMode = startActionMode(callback);
1007 mChoiceMode = mode;
1008 }
1009
1010 protected void endChoiceMode() {
1011 if (!isChoiceMode(CHOICE_MODE_NONE)) {
1012 mActionMode.finish();
1013 mActionMode = null;
1014 mChoiceMode = CHOICE_MODE_NONE;
1015 resetCheckedGrandchildren();
1016 }
1017 }
1018
1019 protected boolean isChoiceMode(int mode) {
1020 return mChoiceMode == mode;
1021 }
1022
1023 protected ArrayList<Checkable> getCheckedGrandchildren() {
1024 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1025 final int childCount = getChildCount();
1026 for (int i = 0; i < childCount; ++i) {
1027 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1028 final int grandChildCount = layout.getChildCount();
1029 for (int j = 0; j < grandChildCount; ++j) {
1030 final View v = layout.getChildAt(j);
1031 if (v instanceof Checkable) {
1032 checked.add((Checkable) v);
1033 }
1034 }
1035 }
1036 return checked;
1037 }
1038
1039 protected void resetCheckedGrandchildren() {
1040 // loop through children, and set all of their children to _not_ be checked
1041 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1042 for (int i = 0; i < checked.size(); ++i) {
1043 final Checkable c = checked.get(i);
1044 c.setChecked(false);
1045 }
1046 }
1047
Winson Chung86f77532010-08-24 11:08:22 -07001048 /**
1049 * This method is called ONLY to synchronize the number of pages that the paged view has.
1050 * To actually fill the pages with information, implement syncPageItems() below. It is
1051 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1052 * and therefore, individual page items do not need to be updated in this method.
1053 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001054 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001055
1056 /**
1057 * This method is called to synchronize the items that are on a particular page. If views on
1058 * the page can be reused, then they should be updated within this method.
1059 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001061
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001063 if (mContentIsRefreshable) {
1064 // Update all the pages
1065 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001066
Michael Jurka0142d492010-08-25 17:46:15 -07001067 // Mark each of the pages as dirty
1068 final int count = getChildCount();
1069 mDirtyPageContent.clear();
1070 for (int i = 0; i < count; ++i) {
1071 mDirtyPageContent.add(true);
1072 }
1073
1074 // Load any pages that are necessary for the current window of views
1075 loadAssociatedPages(mCurrentPage);
1076 mDirtyPageAlpha = true;
1077 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001078 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 }
1080}