blob: e3c36af3beedb1fb63ea6b57eee8a6f107c34acc [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
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070019import com.android.launcher.R;
20
Winson Chung321e9ee2010-08-09 13:37:56 -070021import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070022import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.graphics.Canvas;
24import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.os.Parcel;
26import android.os.Parcelable;
27import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070028import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.view.MotionEvent;
30import android.view.VelocityTracker;
31import android.view.View;
32import android.view.ViewConfiguration;
33import android.view.ViewGroup;
34import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.view.animation.Animation;
Winson Chung86f77532010-08-24 11:08:22 -070036import android.view.animation.AnimationUtils;
Michael Jurka5f1c5092010-09-03 14:15:02 -070037import android.view.animation.Animation.AnimationListener;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070038import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070039import android.widget.Scroller;
40
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070041import java.util.ArrayList;
42import java.util.HashMap;
Winson Chung80baf5a2010-08-09 16:03:15 -070043
Winson Chung321e9ee2010-08-09 13:37:56 -070044/**
45 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070046 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070047 */
48public abstract class PagedView extends ViewGroup {
49 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070050 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070051
Winson Chung86f77532010-08-24 11:08:22 -070052 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung321e9ee2010-08-09 13:37:56 -070053 private static final int MIN_LENGTH_FOR_FLING = 50;
54
Winson Chung5f2aa4e2010-08-20 14:49:25 -070055 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Michael Jurka0142d492010-08-25 17:46:15 -070058 // the velocity at which a fling gesture will cause us to snap to the next page
59 protected int mSnapVelocity = 500;
60
61 protected float mSmoothingTime;
62 protected float mTouchX;
63
64 protected boolean mFirstLayout = true;
65
66 protected int mCurrentPage;
67 protected int mNextPage = INVALID_PAGE;
68 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070069 private VelocityTracker mVelocityTracker;
70
71 private float mDownMotionX;
72 private float mLastMotionX;
73 private float mLastMotionY;
74
Michael Jurka0142d492010-08-25 17:46:15 -070075 protected final static int TOUCH_STATE_REST = 0;
76 protected final static int TOUCH_STATE_SCROLLING = 1;
77 protected final static int TOUCH_STATE_PREV_PAGE = 2;
78 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Michael Jurka0142d492010-08-25 17:46:15 -070082 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070083
84 private boolean mAllowLongPress = true;
85
86 private int mTouchSlop;
87 private int mPagingTouchSlop;
88 private int mMaximumVelocity;
89
Michael Jurka5f1c5092010-09-03 14:15:02 -070090 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070091
Michael Jurka5f1c5092010-09-03 14:15:02 -070092 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Winson Chung86f77532010-08-24 11:08:22 -070094 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Winson Chung86f77532010-08-24 11:08:22 -070096 private ArrayList<Boolean> mDirtyPageContent;
97 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -070098
Winson Chung5f2aa4e2010-08-20 14:49:25 -070099 // choice modes
100 protected static final int CHOICE_MODE_NONE = 0;
101 protected static final int CHOICE_MODE_SINGLE = 1;
102 // Multiple selection mode is not supported by all Launcher actions atm
103 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700104
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700105 private int mChoiceMode;
106 private ActionMode mActionMode;
107
Winson Chung241c3b42010-08-25 16:53:03 -0700108 protected PagedViewIconCache mPageViewIconCache;
109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 // If true, syncPages and syncPageItems will be called to refresh pages
111 protected boolean mContentIsRefreshable = true;
112
113 // If true, modify alpha of neighboring pages as user scrolls left/right
114 protected boolean mFadeInAdjacentScreens = true;
115
116 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
117 // to switch to a new page
118 protected boolean mUsePagingTouchSlop = true;
119
120 // If true, the subclass should directly update mScrollX itself in its computeScroll method
121 // (SmoothPagedView does this)
122 protected boolean mDeferScrollUpdate = false;
123
Winson Chung241c3b42010-08-25 16:53:03 -0700124 /**
125 * Simple cache mechanism for PagedViewIcon outlines.
126 */
127 class PagedViewIconCache {
128 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
129
130 public void clear() {
131 iconOutlineCache.clear();
132 }
133 public void addOutline(Object key, Bitmap b) {
134 iconOutlineCache.put(key, b);
135 }
136 public void removeOutline(Object key) {
137 if (iconOutlineCache.containsKey(key)) {
138 iconOutlineCache.remove(key);
139 }
140 }
141 public Bitmap getOutline(Object key) {
142 return iconOutlineCache.get(key);
143 }
144 }
145
Winson Chung86f77532010-08-24 11:08:22 -0700146 public interface PageSwitchListener {
147 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700148 }
149
Michael Jurka0142d492010-08-25 17:46:15 -0700150 public interface PageMovingListener {
151 void onPageBeginMoving();
152 void onPageEndMoving();
153 }
154
Winson Chung321e9ee2010-08-09 13:37:56 -0700155 public PagedView(Context context) {
156 this(context, null);
157 }
158
159 public PagedView(Context context, AttributeSet attrs) {
160 this(context, attrs, 0);
161 }
162
163 public PagedView(Context context, AttributeSet attrs, int defStyle) {
164 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700165 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700166
167 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700168 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 }
170
171 /**
172 * Initializes various states for this workspace.
173 */
Michael Jurka0142d492010-08-25 17:46:15 -0700174 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700175 mDirtyPageContent = new ArrayList<Boolean>();
176 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700177 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700178 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700179 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
181 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
182 mTouchSlop = configuration.getScaledTouchSlop();
183 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
184 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
185 }
186
Winson Chung86f77532010-08-24 11:08:22 -0700187 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
188 mPageSwitchListener = pageSwitchListener;
189 if (mPageSwitchListener != null) {
190 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192 }
193
194 /**
Winson Chung86f77532010-08-24 11:08:22 -0700195 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 *
Winson Chung86f77532010-08-24 11:08:22 -0700197 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 */
Winson Chung86f77532010-08-24 11:08:22 -0700199 int getCurrentPage() {
200 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202
Winson Chung86f77532010-08-24 11:08:22 -0700203 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 return getChildCount();
205 }
206
Winson Chung86f77532010-08-24 11:08:22 -0700207 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 return getChildAt(index);
209 }
210
211 int getScrollWidth() {
212 return getWidth();
213 }
214
215 /**
Winson Chung86f77532010-08-24 11:08:22 -0700216 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 */
Winson Chung86f77532010-08-24 11:08:22 -0700218 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 if (!mScroller.isFinished()) mScroller.abortAnimation();
220 if (getChildCount() == 0) return;
221
Winson Chung86f77532010-08-24 11:08:22 -0700222 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
223 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700224
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700226 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
228
Michael Jurka0142d492010-08-25 17:46:15 -0700229 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700230 if (mPageSwitchListener != null) {
231 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 }
233 }
234
Michael Jurka0142d492010-08-25 17:46:15 -0700235 // a method that subclasses can override to add behavior
236 protected void pageBeginMoving() {
237 }
238
239 // a method that subclasses can override to add behavior
240 protected void pageEndMoving() {
241 }
242
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 /**
Winson Chung86f77532010-08-24 11:08:22 -0700244 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 *
246 * @param l The listener used to respond to long clicks.
247 */
248 @Override
249 public void setOnLongClickListener(OnLongClickListener l) {
250 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700251 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700253 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 }
255 }
256
257 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700258 public void scrollTo(int x, int y) {
259 super.scrollTo(x, y);
260 mTouchX = x;
261 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
262 }
263
264 // we moved this functionality to a helper function so SmoothPagedView can reuse it
265 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700267 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700269 invalidate();
270 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700271 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700272 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700273 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
Michael Jurka5f1c5092010-09-03 14:15:02 -0700302 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 final int childCount = getChildCount();
304 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700305 // disallowing padding in paged view (just pass 0)
306 final View child = getChildAt(i);
307 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
308
309 int childWidthMode;
310 if (lp.width == LayoutParams.WRAP_CONTENT) {
311 childWidthMode = MeasureSpec.AT_MOST;
312 } else {
313 childWidthMode = MeasureSpec.EXACTLY;
314 }
315
316 int childHeightMode;
317 if (lp.height == LayoutParams.WRAP_CONTENT) {
318 childHeightMode = MeasureSpec.AT_MOST;
319 } else {
320 childHeightMode = MeasureSpec.EXACTLY;
321 }
322
323 final int childWidthMeasureSpec =
324 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
325 final int childHeightMeasureSpec =
326 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
327
328 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 }
330
331 setMeasuredDimension(widthSize, heightSize);
332
Michael Jurka5f1c5092010-09-03 14:15:02 -0700333 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 setHorizontalScrollBarEnabled(false);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700335 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
336 mScroller.setFinalX(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage));
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 setHorizontalScrollBarEnabled(true);
338 mFirstLayout = false;
339 }
340 }
341
342 @Override
343 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
344 final int childCount = getChildCount();
345 int childLeft = 0;
346 if (childCount > 0) {
347 childLeft = (getMeasuredWidth() - getChildAt(0).getMeasuredWidth()) / 2;
348 }
349
350 for (int i = 0; i < childCount; i++) {
351 final View child = getChildAt(i);
352 if (child.getVisibility() != View.GONE) {
353 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700354 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
355 child.layout(childLeft, childHeight,
356 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 childLeft += childWidth;
358 }
359 }
360 }
361
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 @Override
363 protected void dispatchDraw(Canvas canvas) {
Michael Jurka0142d492010-08-25 17:46:15 -0700364 if (mFadeInAdjacentScreens) {
365 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
366 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
367 final int childCount = getChildCount();
368 for (int i = 0; i < childCount; ++i) {
369 View layout = (View) getChildAt(i);
370 int childWidth = layout.getMeasuredWidth();
371 int halfChildWidth = (childWidth / 2);
372 int childCenter = getChildOffset(i) + halfChildWidth;
373 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
374 float alpha = 0.0f;
375 if (distanceFromScreenCenter < halfChildWidth) {
376 alpha = 1.0f;
377 } else if (distanceFromScreenCenter > childWidth) {
378 alpha = 0.0f;
379 } else {
380 float dimAlpha = (float) (distanceFromScreenCenter - halfChildWidth) / halfChildWidth;
381 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
382 alpha = 1.0f - dimAlpha;
383 }
384 if (Float.compare(alpha, layout.getAlpha()) != 0) {
385 layout.setAlpha(alpha);
386 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 }
Michael Jurka0142d492010-08-25 17:46:15 -0700388 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
390 }
Michael Jurka0142d492010-08-25 17:46:15 -0700391
392 // Find out which screens are visible; as an optimization we only call draw on them
393
394 // As an optimization, this code assumes that all pages have the same width as the 0th
395 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700396 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700397 if (pageCount > 0) {
398 final int pageWidth = getChildAt(0).getMeasuredWidth();
399 final int screenWidth = getMeasuredWidth();
400 int x = getRelativeChildOffset(0) + pageWidth;
401 int leftScreen = 0;
402 int rightScreen = 0;
403 while (x <= mScrollX) {
404 leftScreen++;
405 x += pageWidth;
406 // replace above line with this if you don't assume all pages have same width as 0th
407 // page:
408 // x += getChildAt(leftScreen).getMeasuredWidth();
409 }
410 rightScreen = leftScreen;
411 while (x < mScrollX + screenWidth) {
412 rightScreen++;
413 x += pageWidth;
414 // replace above line with this if you don't assume all pages have same width as 0th
415 // page:
416 //if (rightScreen < pageCount) {
417 // x += getChildAt(rightScreen).getMeasuredWidth();
418 //}
419 }
420 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700421
Michael Jurkac4fb9172010-09-02 17:19:20 -0700422 final long drawingTime = getDrawingTime();
423 for (int i = leftScreen; i <= rightScreen; i++) {
424 drawChild(canvas, getChildAt(i), drawingTime);
425 }
Michael Jurka0142d492010-08-25 17:46:15 -0700426 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
428
429 @Override
430 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700431 int page = indexOfChild(child);
432 if (page != mCurrentPage || !mScroller.isFinished()) {
433 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 return true;
435 }
436 return false;
437 }
438
439 @Override
440 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700441 int focusablePage;
442 if (mNextPage != INVALID_PAGE) {
443 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700445 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 }
Winson Chung86f77532010-08-24 11:08:22 -0700447 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 if (v != null) {
449 v.requestFocus(direction, previouslyFocusedRect);
450 }
451 return false;
452 }
453
454 @Override
455 public boolean dispatchUnhandledMove(View focused, int direction) {
456 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700457 if (getCurrentPage() > 0) {
458 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 return true;
460 }
461 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700462 if (getCurrentPage() < getPageCount() - 1) {
463 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 return true;
465 }
466 }
467 return super.dispatchUnhandledMove(focused, direction);
468 }
469
470 @Override
471 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700472 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
473 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
475 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700476 if (mCurrentPage > 0) {
477 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
479 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700480 if (mCurrentPage < getPageCount() - 1) {
481 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 }
483 }
484 }
485
486 /**
487 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700488 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 *
Winson Chung86f77532010-08-24 11:08:22 -0700490 * This happens when live folders requery, and if they're off page, they
491 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 */
493 @Override
494 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700495 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 View v = focused;
497 while (true) {
498 if (v == current) {
499 super.focusableViewAvailable(focused);
500 return;
501 }
502 if (v == this) {
503 return;
504 }
505 ViewParent parent = v.getParent();
506 if (parent instanceof View) {
507 v = (View)v.getParent();
508 } else {
509 return;
510 }
511 }
512 }
513
514 /**
515 * {@inheritDoc}
516 */
517 @Override
518 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
519 if (disallowIntercept) {
520 // We need to make sure to cancel our long press if
521 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700522 final View currentPage = getChildAt(mCurrentPage);
523 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525 super.requestDisallowInterceptTouchEvent(disallowIntercept);
526 }
527
528 @Override
529 public boolean onInterceptTouchEvent(MotionEvent ev) {
530 /*
531 * This method JUST determines whether we want to intercept the motion.
532 * If we return true, onTouchEvent will be called and we do the actual
533 * scrolling there.
534 */
535
536 /*
537 * Shortcut the most recurring case: the user is in the dragging
538 * state and he is moving his finger. We want to intercept this
539 * motion.
540 */
541 final int action = ev.getAction();
542 if ((action == MotionEvent.ACTION_MOVE) &&
543 (mTouchState == TOUCH_STATE_SCROLLING)) {
544 return true;
545 }
546
547
548 switch (action & MotionEvent.ACTION_MASK) {
549 case MotionEvent.ACTION_MOVE: {
550 /*
551 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
552 * whether the user has moved far enough from his original down touch.
553 */
554 determineScrollingStart(ev);
555 break;
556 }
557
558 case MotionEvent.ACTION_DOWN: {
559 final float x = ev.getX();
560 final float y = ev.getY();
561 // Remember location of down touch
562 mDownMotionX = x;
563 mLastMotionX = x;
564 mLastMotionY = y;
565 mActivePointerId = ev.getPointerId(0);
566 mAllowLongPress = true;
567
568 /*
569 * If being flinged and user touches the screen, initiate drag;
570 * otherwise don't. mScroller.isFinished should be false when
571 * being flinged.
572 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700573 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
574 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
575 if (finishedScrolling) {
576 mTouchState = TOUCH_STATE_REST;
577 mScroller.abortAnimation();
578 } else {
579 mTouchState = TOUCH_STATE_SCROLLING;
580 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700581
Winson Chung86f77532010-08-24 11:08:22 -0700582 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 // to scroll the current page
584 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
585 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
586 if (getChildCount() > 0) {
587 int relativeChildLeft = getChildOffset(0);
588 int relativeChildRight = relativeChildLeft + getChildAt(0).getMeasuredWidth();
589 if (x < relativeChildLeft) {
590 mTouchState = TOUCH_STATE_PREV_PAGE;
591 } else if (x > relativeChildRight) {
592 mTouchState = TOUCH_STATE_NEXT_PAGE;
593 }
594 }
595 }
596 break;
597 }
598
599 case MotionEvent.ACTION_CANCEL:
600 case MotionEvent.ACTION_UP:
601 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700602 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 mTouchState = TOUCH_STATE_REST;
604 mAllowLongPress = false;
605 mActivePointerId = INVALID_POINTER;
606
607 break;
608
609 case MotionEvent.ACTION_POINTER_UP:
610 onSecondaryPointerUp(ev);
611 break;
612 }
613
614 /*
615 * The only time we want to intercept motion events is if we are in the
616 * drag mode.
617 */
618 return mTouchState != TOUCH_STATE_REST;
619 }
620
Winson Chung80baf5a2010-08-09 16:03:15 -0700621 protected void animateClickFeedback(View v, final Runnable r) {
622 // animate the view slightly to show click feedback running some logic after it is "pressed"
623 Animation anim = AnimationUtils.loadAnimation(getContext(),
624 R.anim.paged_view_click_feedback);
625 anim.setAnimationListener(new AnimationListener() {
626 @Override
627 public void onAnimationStart(Animation animation) {}
628 @Override
629 public void onAnimationRepeat(Animation animation) {
630 r.run();
631 }
632 @Override
633 public void onAnimationEnd(Animation animation) {}
634 });
635 v.startAnimation(anim);
636 }
637
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 /*
639 * Determines if we should change the touch state to start scrolling after the
640 * user moves their touch point too far.
641 */
642 private void determineScrollingStart(MotionEvent ev) {
643 /*
644 * Locally do absolute value. mLastMotionX is set to the y value
645 * of the down event.
646 */
647 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
648 final float x = ev.getX(pointerIndex);
649 final float y = ev.getY(pointerIndex);
650 final int xDiff = (int) Math.abs(x - mLastMotionX);
651 final int yDiff = (int) Math.abs(y - mLastMotionY);
652
653 final int touchSlop = mTouchSlop;
654 boolean xPaged = xDiff > mPagingTouchSlop;
655 boolean xMoved = xDiff > touchSlop;
656 boolean yMoved = yDiff > touchSlop;
657
658 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700659 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 // Scroll if the user moved far enough along the X axis
661 mTouchState = TOUCH_STATE_SCROLLING;
662 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700663 mTouchX = mScrollX;
664 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
665 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700666 }
667 // Either way, cancel any pending longpress
668 if (mAllowLongPress) {
669 mAllowLongPress = false;
670 // Try canceling the long press. It could also have been scheduled
671 // by a distant descendant, so use the mAllowLongPress flag to block
672 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700673 final View currentPage = getPageAt(mCurrentPage);
674 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 }
676 }
677 }
678
679 @Override
680 public boolean onTouchEvent(MotionEvent ev) {
681 if (mVelocityTracker == null) {
682 mVelocityTracker = VelocityTracker.obtain();
683 }
684 mVelocityTracker.addMovement(ev);
685
686 final int action = ev.getAction();
687
688 switch (action & MotionEvent.ACTION_MASK) {
689 case MotionEvent.ACTION_DOWN:
690 /*
691 * If being flinged and user touches, stop the fling. isFinished
692 * will be false if being flinged.
693 */
694 if (!mScroller.isFinished()) {
695 mScroller.abortAnimation();
696 }
697
698 // Remember where the motion event started
699 mDownMotionX = mLastMotionX = ev.getX();
700 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700701 if (mTouchState == TOUCH_STATE_SCROLLING) {
702 pageBeginMoving();
703 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700704 break;
705
706 case MotionEvent.ACTION_MOVE:
707 if (mTouchState == TOUCH_STATE_SCROLLING) {
708 // Scroll to follow the motion event
709 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
710 final float x = ev.getX(pointerIndex);
711 final int deltaX = (int) (mLastMotionX - x);
712 mLastMotionX = x;
713
714 int sx = getScrollX();
715 if (deltaX < 0) {
716 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700717 mTouchX += Math.max(-mTouchX, deltaX);
718 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
719 if (!mDeferScrollUpdate) {
720 scrollBy(Math.max(-sx, deltaX), 0);
721 } else {
722 // This will trigger a call to computeScroll() on next drawChild() call
723 invalidate();
724 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 }
726 } else if (deltaX > 0) {
727 final int lastChildIndex = getChildCount() - 1;
728 final int availableToScroll = getChildOffset(lastChildIndex) -
729 getRelativeChildOffset(lastChildIndex) - sx;
730 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700731 mTouchX += Math.min(availableToScroll, deltaX);
732 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
733 if (!mDeferScrollUpdate) {
734 scrollBy(Math.min(availableToScroll, deltaX), 0);
735 } else {
736 // This will trigger a call to computeScroll() on next drawChild() call
737 invalidate();
738 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700739 }
740 } else {
741 awakenScrollBars();
742 }
743 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
744 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
745 determineScrollingStart(ev);
746 }
747 break;
748
749 case MotionEvent.ACTION_UP:
750 if (mTouchState == TOUCH_STATE_SCROLLING) {
751 final int activePointerId = mActivePointerId;
752 final int pointerIndex = ev.findPointerIndex(activePointerId);
753 final float x = ev.getX(pointerIndex);
754 final VelocityTracker velocityTracker = mVelocityTracker;
755 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
756 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
757 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
758
Michael Jurka0142d492010-08-25 17:46:15 -0700759 final int snapVelocity = mSnapVelocity;
760 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
761 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
762 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700763 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700764 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 } else {
766 snapToDestination();
767 }
768
769 if (mVelocityTracker != null) {
770 mVelocityTracker.recycle();
771 mVelocityTracker = null;
772 }
773 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
774 // at this point we have not moved beyond the touch slop
775 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
776 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700777 int nextPage = Math.max(0, mCurrentPage - 1);
778 if (nextPage != mCurrentPage) {
779 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 } else {
781 snapToDestination();
782 }
783 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
784 // at this point we have not moved beyond the touch slop
785 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
786 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700787 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
788 if (nextPage != mCurrentPage) {
789 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 } else {
791 snapToDestination();
792 }
793 }
794 mTouchState = TOUCH_STATE_REST;
795 mActivePointerId = INVALID_POINTER;
796 break;
797
798 case MotionEvent.ACTION_CANCEL:
799 mTouchState = TOUCH_STATE_REST;
800 mActivePointerId = INVALID_POINTER;
801 break;
802
803 case MotionEvent.ACTION_POINTER_UP:
804 onSecondaryPointerUp(ev);
805 break;
806 }
807
808 return true;
809 }
810
811 private void onSecondaryPointerUp(MotionEvent ev) {
812 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
813 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
814 final int pointerId = ev.getPointerId(pointerIndex);
815 if (pointerId == mActivePointerId) {
816 // This was our active pointer going up. Choose a new
817 // active pointer and adjust accordingly.
818 // TODO: Make this decision more intelligent.
819 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
820 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
821 mLastMotionY = ev.getY(newPointerIndex);
822 mActivePointerId = ev.getPointerId(newPointerIndex);
823 if (mVelocityTracker != null) {
824 mVelocityTracker.clear();
825 }
826 }
827 }
828
829 @Override
830 public void requestChildFocus(View child, View focused) {
831 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700832 int page = indexOfChild(child);
833 if (page >= 0 && !isInTouchMode()) {
834 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
836 }
837
838 protected int getRelativeChildOffset(int index) {
839 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
840 }
841
842 protected int getChildOffset(int index) {
843 if (getChildCount() == 0)
844 return 0;
845
846 int offset = getRelativeChildOffset(0);
847 for (int i = 0; i < index; ++i) {
848 offset += getChildAt(i).getMeasuredWidth();
849 }
850 return offset;
851 }
852
853 protected void snapToDestination() {
854 int minDistanceFromScreenCenter = getMeasuredWidth();
855 int minDistanceFromScreenCenterIndex = -1;
856 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
857 final int childCount = getChildCount();
858 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700859 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 int childWidth = layout.getMeasuredWidth();
861 int halfChildWidth = (childWidth / 2);
862 int childCenter = getChildOffset(i) + halfChildWidth;
863 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
864 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
865 minDistanceFromScreenCenter = distanceFromScreenCenter;
866 minDistanceFromScreenCenterIndex = i;
867 }
868 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700869 snapToPage(minDistanceFromScreenCenterIndex, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 }
871
Michael Jurka0142d492010-08-25 17:46:15 -0700872 protected void snapToPageWithVelocity(int whichPage, int velocity) {
873 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
874 // can use it
875 snapToPage(whichPage);
876 }
877
878 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700879 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 }
881
Michael Jurka0142d492010-08-25 17:46:15 -0700882 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700883 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700884
Winson Chung321e9ee2010-08-09 13:37:56 -0700885
Winson Chung86f77532010-08-24 11:08:22 -0700886 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 final int sX = getScrollX();
888 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700889 snapToPage(whichPage, delta, duration);
890 }
891
892 protected void snapToPage(int whichPage, int delta, int duration) {
893 mNextPage = whichPage;
894
895 View focusedChild = getFocusedChild();
896 if (focusedChild != null && whichPage != mCurrentPage &&
897 focusedChild == getChildAt(mCurrentPage)) {
898 focusedChild.clearFocus();
899 }
900
901 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 awakenScrollBars(duration);
903 if (duration == 0) {
904 duration = Math.abs(delta);
905 }
906
907 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700908 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700909
910 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700911 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700912 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700913 invalidate();
914 }
915
916 @Override
917 protected Parcelable onSaveInstanceState() {
918 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700919 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 return state;
921 }
922
923 @Override
924 protected void onRestoreInstanceState(Parcelable state) {
925 SavedState savedState = (SavedState) state;
926 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700927 if (savedState.currentPage != -1) {
928 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 }
930 }
931
932 public void scrollLeft() {
933 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700934 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700936 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 }
938 }
939
940 public void scrollRight() {
941 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700942 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700944 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 }
946 }
947
Winson Chung86f77532010-08-24 11:08:22 -0700948 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 int result = -1;
950 if (v != null) {
951 ViewParent vp = v.getParent();
952 int count = getChildCount();
953 for (int i = 0; i < count; i++) {
954 if (vp == getChildAt(i)) {
955 return i;
956 }
957 }
958 }
959 return result;
960 }
961
962 /**
963 * @return True is long presses are still allowed for the current touch
964 */
965 public boolean allowLongPress() {
966 return mAllowLongPress;
967 }
968
Michael Jurka0142d492010-08-25 17:46:15 -0700969 /**
970 * Set true to allow long-press events to be triggered, usually checked by
971 * {@link Launcher} to accept or block dpad-initiated long-presses.
972 */
973 public void setAllowLongPress(boolean allowLongPress) {
974 mAllowLongPress = allowLongPress;
975 }
976
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -0700978 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700979
980 SavedState(Parcelable superState) {
981 super(superState);
982 }
983
984 private SavedState(Parcel in) {
985 super(in);
Winson Chung86f77532010-08-24 11:08:22 -0700986 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 }
988
989 @Override
990 public void writeToParcel(Parcel out, int flags) {
991 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -0700992 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 }
994
995 public static final Parcelable.Creator<SavedState> CREATOR =
996 new Parcelable.Creator<SavedState>() {
997 public SavedState createFromParcel(Parcel in) {
998 return new SavedState(in);
999 }
1000
1001 public SavedState[] newArray(int size) {
1002 return new SavedState[size];
1003 }
1004 };
1005 }
1006
Winson Chung86f77532010-08-24 11:08:22 -07001007 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001008 if (mContentIsRefreshable) {
1009 final int count = getChildCount();
1010 if (page < count) {
1011 int lowerPageBound = Math.max(0, page - 1);
1012 int upperPageBound = Math.min(page + 1, count - 1);
1013 for (int i = 0; i < count; ++i) {
1014 final ViewGroup layout = (ViewGroup) getChildAt(i);
1015 final int childCount = layout.getChildCount();
1016 if (lowerPageBound <= i && i <= upperPageBound) {
1017 if (mDirtyPageContent.get(i)) {
1018 syncPageItems(i);
1019 mDirtyPageContent.set(i, false);
1020 }
1021 } else {
1022 if (childCount > 0) {
1023 layout.removeAllViews();
1024 }
1025 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001026 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001027 }
1028 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001029 }
1030 }
1031
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001032 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001033 if (isChoiceMode(CHOICE_MODE_NONE)) {
1034 mChoiceMode = mode;
1035 mActionMode = startActionMode(callback);
1036 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001037 }
1038
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001039 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001040 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001041 mChoiceMode = CHOICE_MODE_NONE;
1042 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001043 mActionMode.finish();
1044 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001045 }
1046 }
1047
1048 protected boolean isChoiceMode(int mode) {
1049 return mChoiceMode == mode;
1050 }
1051
1052 protected ArrayList<Checkable> getCheckedGrandchildren() {
1053 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1054 final int childCount = getChildCount();
1055 for (int i = 0; i < childCount; ++i) {
1056 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1057 final int grandChildCount = layout.getChildCount();
1058 for (int j = 0; j < grandChildCount; ++j) {
1059 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001060 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001061 checked.add((Checkable) v);
1062 }
1063 }
1064 }
1065 return checked;
1066 }
1067
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001068 /**
1069 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1070 * Otherwise, returns null.
1071 */
1072 protected Checkable getSingleCheckedGrandchild() {
1073 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1074 final int childCount = getChildCount();
1075 for (int i = 0; i < childCount; ++i) {
1076 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1077 final int grandChildCount = layout.getChildCount();
1078 for (int j = 0; j < grandChildCount; ++j) {
1079 final View v = layout.getChildAt(j);
1080 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1081 return (Checkable) v;
1082 }
1083 }
1084 }
1085 }
1086 return null;
1087 }
1088
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001089 public Object getChosenItem() {
1090 View checkedView = (View) getSingleCheckedGrandchild();
1091 if (checkedView != null) {
1092 return checkedView.getTag();
1093 }
1094 return null;
1095 }
1096
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001097 protected void resetCheckedGrandchildren() {
1098 // loop through children, and set all of their children to _not_ be checked
1099 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1100 for (int i = 0; i < checked.size(); ++i) {
1101 final Checkable c = checked.get(i);
1102 c.setChecked(false);
1103 }
1104 }
1105
Winson Chung86f77532010-08-24 11:08:22 -07001106 /**
1107 * This method is called ONLY to synchronize the number of pages that the paged view has.
1108 * To actually fill the pages with information, implement syncPageItems() below. It is
1109 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1110 * and therefore, individual page items do not need to be updated in this method.
1111 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001112 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001113
1114 /**
1115 * This method is called to synchronize the items that are on a particular page. If views on
1116 * the page can be reused, then they should be updated within this method.
1117 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001118 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001119
Winson Chung321e9ee2010-08-09 13:37:56 -07001120 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001121 if (mContentIsRefreshable) {
1122 // Update all the pages
1123 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001124
Michael Jurka0142d492010-08-25 17:46:15 -07001125 // Mark each of the pages as dirty
1126 final int count = getChildCount();
1127 mDirtyPageContent.clear();
1128 for (int i = 0; i < count; ++i) {
1129 mDirtyPageContent.add(true);
1130 }
1131
1132 // Load any pages that are necessary for the current window of views
1133 loadAssociatedPages(mCurrentPage);
1134 mDirtyPageAlpha = true;
1135 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001136 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 }
1138}