blob: 1e0dad4aef5e17e520e40236918bbb36061e9c71 [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);
Winson Chung321e9ee2010-08-09 13:37:56 -0700332 }
333
334 @Override
335 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700336 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
337 setHorizontalScrollBarEnabled(false);
338 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
339 scrollTo(newX, 0);
340 mScroller.setFinalX(newX);
341 setHorizontalScrollBarEnabled(true);
342 mFirstLayout = false;
343 }
344
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 final int childCount = getChildCount();
346 int childLeft = 0;
347 if (childCount > 0) {
348 childLeft = (getMeasuredWidth() - getChildAt(0).getMeasuredWidth()) / 2;
349 }
350
351 for (int i = 0; i < childCount; i++) {
352 final View child = getChildAt(i);
353 if (child.getVisibility() != View.GONE) {
354 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700355 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
356 child.layout(childLeft, childHeight,
357 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 childLeft += childWidth;
359 }
360 }
361 }
362
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 @Override
364 protected void dispatchDraw(Canvas canvas) {
Michael Jurka0142d492010-08-25 17:46:15 -0700365 if (mFadeInAdjacentScreens) {
366 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
367 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
368 final int childCount = getChildCount();
369 for (int i = 0; i < childCount; ++i) {
370 View layout = (View) getChildAt(i);
371 int childWidth = layout.getMeasuredWidth();
372 int halfChildWidth = (childWidth / 2);
373 int childCenter = getChildOffset(i) + halfChildWidth;
374 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
375 float alpha = 0.0f;
376 if (distanceFromScreenCenter < halfChildWidth) {
377 alpha = 1.0f;
378 } else if (distanceFromScreenCenter > childWidth) {
379 alpha = 0.0f;
380 } else {
381 float dimAlpha = (float) (distanceFromScreenCenter - halfChildWidth) / halfChildWidth;
382 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
383 alpha = 1.0f - dimAlpha;
384 }
385 if (Float.compare(alpha, layout.getAlpha()) != 0) {
386 layout.setAlpha(alpha);
387 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700388 }
Michael Jurka0142d492010-08-25 17:46:15 -0700389 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 }
391 }
Michael Jurka0142d492010-08-25 17:46:15 -0700392
393 // Find out which screens are visible; as an optimization we only call draw on them
394
395 // As an optimization, this code assumes that all pages have the same width as the 0th
396 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700397 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700398 if (pageCount > 0) {
399 final int pageWidth = getChildAt(0).getMeasuredWidth();
400 final int screenWidth = getMeasuredWidth();
401 int x = getRelativeChildOffset(0) + pageWidth;
402 int leftScreen = 0;
403 int rightScreen = 0;
404 while (x <= mScrollX) {
405 leftScreen++;
406 x += pageWidth;
407 // replace above line with this if you don't assume all pages have same width as 0th
408 // page:
409 // x += getChildAt(leftScreen).getMeasuredWidth();
410 }
411 rightScreen = leftScreen;
412 while (x < mScrollX + screenWidth) {
413 rightScreen++;
414 x += pageWidth;
415 // replace above line with this if you don't assume all pages have same width as 0th
416 // page:
417 //if (rightScreen < pageCount) {
418 // x += getChildAt(rightScreen).getMeasuredWidth();
419 //}
420 }
421 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700422
Michael Jurkac4fb9172010-09-02 17:19:20 -0700423 final long drawingTime = getDrawingTime();
424 for (int i = leftScreen; i <= rightScreen; i++) {
425 drawChild(canvas, getChildAt(i), drawingTime);
426 }
Michael Jurka0142d492010-08-25 17:46:15 -0700427 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
429
430 @Override
431 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700432 int page = indexOfChild(child);
433 if (page != mCurrentPage || !mScroller.isFinished()) {
434 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 return true;
436 }
437 return false;
438 }
439
440 @Override
441 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700442 int focusablePage;
443 if (mNextPage != INVALID_PAGE) {
444 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700446 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 }
Winson Chung86f77532010-08-24 11:08:22 -0700448 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700449 if (v != null) {
450 v.requestFocus(direction, previouslyFocusedRect);
451 }
452 return false;
453 }
454
455 @Override
456 public boolean dispatchUnhandledMove(View focused, int direction) {
457 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700458 if (getCurrentPage() > 0) {
459 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 return true;
461 }
462 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700463 if (getCurrentPage() < getPageCount() - 1) {
464 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 return true;
466 }
467 }
468 return super.dispatchUnhandledMove(focused, direction);
469 }
470
471 @Override
472 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700473 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
474 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 }
476 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700477 if (mCurrentPage > 0) {
478 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 }
480 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700481 if (mCurrentPage < getPageCount() - 1) {
482 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
484 }
485 }
486
487 /**
488 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700489 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 *
Winson Chung86f77532010-08-24 11:08:22 -0700491 * This happens when live folders requery, and if they're off page, they
492 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700493 */
494 @Override
495 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700496 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 View v = focused;
498 while (true) {
499 if (v == current) {
500 super.focusableViewAvailable(focused);
501 return;
502 }
503 if (v == this) {
504 return;
505 }
506 ViewParent parent = v.getParent();
507 if (parent instanceof View) {
508 v = (View)v.getParent();
509 } else {
510 return;
511 }
512 }
513 }
514
515 /**
516 * {@inheritDoc}
517 */
518 @Override
519 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
520 if (disallowIntercept) {
521 // We need to make sure to cancel our long press if
522 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700523 final View currentPage = getChildAt(mCurrentPage);
524 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 }
526 super.requestDisallowInterceptTouchEvent(disallowIntercept);
527 }
528
529 @Override
530 public boolean onInterceptTouchEvent(MotionEvent ev) {
531 /*
532 * This method JUST determines whether we want to intercept the motion.
533 * If we return true, onTouchEvent will be called and we do the actual
534 * scrolling there.
535 */
536
537 /*
538 * Shortcut the most recurring case: the user is in the dragging
539 * state and he is moving his finger. We want to intercept this
540 * motion.
541 */
542 final int action = ev.getAction();
543 if ((action == MotionEvent.ACTION_MOVE) &&
544 (mTouchState == TOUCH_STATE_SCROLLING)) {
545 return true;
546 }
547
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 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 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700554 if (mActivePointerId != INVALID_POINTER) {
555 determineScrollingStart(ev);
556 break;
557 }
558 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
559 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
560 // i.e. fall through to the next case (don't break)
561 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
562 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564
565 case MotionEvent.ACTION_DOWN: {
566 final float x = ev.getX();
567 final float y = ev.getY();
568 // Remember location of down touch
569 mDownMotionX = x;
570 mLastMotionX = x;
571 mLastMotionY = y;
572 mActivePointerId = ev.getPointerId(0);
573 mAllowLongPress = true;
574
575 /*
576 * If being flinged and user touches the screen, initiate drag;
577 * otherwise don't. mScroller.isFinished should be false when
578 * being flinged.
579 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700580 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
581 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
582 if (finishedScrolling) {
583 mTouchState = TOUCH_STATE_REST;
584 mScroller.abortAnimation();
585 } else {
586 mTouchState = TOUCH_STATE_SCROLLING;
587 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700588
Winson Chung86f77532010-08-24 11:08:22 -0700589 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700590 // to scroll the current page
591 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
592 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
593 if (getChildCount() > 0) {
594 int relativeChildLeft = getChildOffset(0);
595 int relativeChildRight = relativeChildLeft + getChildAt(0).getMeasuredWidth();
596 if (x < relativeChildLeft) {
597 mTouchState = TOUCH_STATE_PREV_PAGE;
598 } else if (x > relativeChildRight) {
599 mTouchState = TOUCH_STATE_NEXT_PAGE;
600 }
601 }
602 }
603 break;
604 }
605
606 case MotionEvent.ACTION_CANCEL:
607 case MotionEvent.ACTION_UP:
608 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700609 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 mTouchState = TOUCH_STATE_REST;
611 mAllowLongPress = false;
612 mActivePointerId = INVALID_POINTER;
613
614 break;
615
616 case MotionEvent.ACTION_POINTER_UP:
617 onSecondaryPointerUp(ev);
618 break;
619 }
620
621 /*
622 * The only time we want to intercept motion events is if we are in the
623 * drag mode.
624 */
625 return mTouchState != TOUCH_STATE_REST;
626 }
627
Winson Chung80baf5a2010-08-09 16:03:15 -0700628 protected void animateClickFeedback(View v, final Runnable r) {
629 // animate the view slightly to show click feedback running some logic after it is "pressed"
630 Animation anim = AnimationUtils.loadAnimation(getContext(),
631 R.anim.paged_view_click_feedback);
632 anim.setAnimationListener(new AnimationListener() {
633 @Override
634 public void onAnimationStart(Animation animation) {}
635 @Override
636 public void onAnimationRepeat(Animation animation) {
637 r.run();
638 }
639 @Override
640 public void onAnimationEnd(Animation animation) {}
641 });
642 v.startAnimation(anim);
643 }
644
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 /*
646 * Determines if we should change the touch state to start scrolling after the
647 * user moves their touch point too far.
648 */
649 private void determineScrollingStart(MotionEvent ev) {
650 /*
651 * Locally do absolute value. mLastMotionX is set to the y value
652 * of the down event.
653 */
654 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
655 final float x = ev.getX(pointerIndex);
656 final float y = ev.getY(pointerIndex);
657 final int xDiff = (int) Math.abs(x - mLastMotionX);
658 final int yDiff = (int) Math.abs(y - mLastMotionY);
659
660 final int touchSlop = mTouchSlop;
661 boolean xPaged = xDiff > mPagingTouchSlop;
662 boolean xMoved = xDiff > touchSlop;
663 boolean yMoved = yDiff > touchSlop;
664
665 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700666 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 // Scroll if the user moved far enough along the X axis
668 mTouchState = TOUCH_STATE_SCROLLING;
669 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700670 mTouchX = mScrollX;
671 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
672 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 }
674 // Either way, cancel any pending longpress
675 if (mAllowLongPress) {
676 mAllowLongPress = false;
677 // Try canceling the long press. It could also have been scheduled
678 // by a distant descendant, so use the mAllowLongPress flag to block
679 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700680 final View currentPage = getPageAt(mCurrentPage);
681 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 }
683 }
684 }
685
686 @Override
687 public boolean onTouchEvent(MotionEvent ev) {
688 if (mVelocityTracker == null) {
689 mVelocityTracker = VelocityTracker.obtain();
690 }
691 mVelocityTracker.addMovement(ev);
692
693 final int action = ev.getAction();
694
695 switch (action & MotionEvent.ACTION_MASK) {
696 case MotionEvent.ACTION_DOWN:
697 /*
698 * If being flinged and user touches, stop the fling. isFinished
699 * will be false if being flinged.
700 */
701 if (!mScroller.isFinished()) {
702 mScroller.abortAnimation();
703 }
704
705 // Remember where the motion event started
706 mDownMotionX = mLastMotionX = ev.getX();
707 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700708 if (mTouchState == TOUCH_STATE_SCROLLING) {
709 pageBeginMoving();
710 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 break;
712
713 case MotionEvent.ACTION_MOVE:
714 if (mTouchState == TOUCH_STATE_SCROLLING) {
715 // Scroll to follow the motion event
716 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
717 final float x = ev.getX(pointerIndex);
718 final int deltaX = (int) (mLastMotionX - x);
719 mLastMotionX = x;
720
721 int sx = getScrollX();
722 if (deltaX < 0) {
723 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700724 mTouchX += Math.max(-mTouchX, deltaX);
725 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
726 if (!mDeferScrollUpdate) {
727 scrollBy(Math.max(-sx, deltaX), 0);
728 } else {
729 // This will trigger a call to computeScroll() on next drawChild() call
730 invalidate();
731 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 }
733 } else if (deltaX > 0) {
734 final int lastChildIndex = getChildCount() - 1;
735 final int availableToScroll = getChildOffset(lastChildIndex) -
736 getRelativeChildOffset(lastChildIndex) - sx;
737 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700738 mTouchX += Math.min(availableToScroll, deltaX);
739 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
740 if (!mDeferScrollUpdate) {
741 scrollBy(Math.min(availableToScroll, deltaX), 0);
742 } else {
743 // This will trigger a call to computeScroll() on next drawChild() call
744 invalidate();
745 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 }
747 } else {
748 awakenScrollBars();
749 }
750 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
751 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
752 determineScrollingStart(ev);
753 }
754 break;
755
756 case MotionEvent.ACTION_UP:
757 if (mTouchState == TOUCH_STATE_SCROLLING) {
758 final int activePointerId = mActivePointerId;
759 final int pointerIndex = ev.findPointerIndex(activePointerId);
760 final float x = ev.getX(pointerIndex);
761 final VelocityTracker velocityTracker = mVelocityTracker;
762 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
763 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
764 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
765
Michael Jurka0142d492010-08-25 17:46:15 -0700766 final int snapVelocity = mSnapVelocity;
767 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
768 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
769 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700770 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700771 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 } else {
773 snapToDestination();
774 }
775
776 if (mVelocityTracker != null) {
777 mVelocityTracker.recycle();
778 mVelocityTracker = null;
779 }
780 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
781 // at this point we have not moved beyond the touch slop
782 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
783 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700784 int nextPage = Math.max(0, mCurrentPage - 1);
785 if (nextPage != mCurrentPage) {
786 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 } else {
788 snapToDestination();
789 }
790 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
791 // at this point we have not moved beyond the touch slop
792 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
793 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700794 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
795 if (nextPage != mCurrentPage) {
796 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 } else {
798 snapToDestination();
799 }
800 }
801 mTouchState = TOUCH_STATE_REST;
802 mActivePointerId = INVALID_POINTER;
803 break;
804
805 case MotionEvent.ACTION_CANCEL:
806 mTouchState = TOUCH_STATE_REST;
807 mActivePointerId = INVALID_POINTER;
808 break;
809
810 case MotionEvent.ACTION_POINTER_UP:
811 onSecondaryPointerUp(ev);
812 break;
813 }
814
815 return true;
816 }
817
818 private void onSecondaryPointerUp(MotionEvent ev) {
819 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
820 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
821 final int pointerId = ev.getPointerId(pointerIndex);
822 if (pointerId == mActivePointerId) {
823 // This was our active pointer going up. Choose a new
824 // active pointer and adjust accordingly.
825 // TODO: Make this decision more intelligent.
826 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
827 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
828 mLastMotionY = ev.getY(newPointerIndex);
829 mActivePointerId = ev.getPointerId(newPointerIndex);
830 if (mVelocityTracker != null) {
831 mVelocityTracker.clear();
832 }
833 }
834 }
835
836 @Override
837 public void requestChildFocus(View child, View focused) {
838 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700839 int page = indexOfChild(child);
840 if (page >= 0 && !isInTouchMode()) {
841 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843 }
844
845 protected int getRelativeChildOffset(int index) {
846 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
847 }
848
849 protected int getChildOffset(int index) {
850 if (getChildCount() == 0)
851 return 0;
852
853 int offset = getRelativeChildOffset(0);
854 for (int i = 0; i < index; ++i) {
855 offset += getChildAt(i).getMeasuredWidth();
856 }
857 return offset;
858 }
859
860 protected void snapToDestination() {
861 int minDistanceFromScreenCenter = getMeasuredWidth();
862 int minDistanceFromScreenCenterIndex = -1;
863 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
864 final int childCount = getChildCount();
865 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700866 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 int childWidth = layout.getMeasuredWidth();
868 int halfChildWidth = (childWidth / 2);
869 int childCenter = getChildOffset(i) + halfChildWidth;
870 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
871 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
872 minDistanceFromScreenCenter = distanceFromScreenCenter;
873 minDistanceFromScreenCenterIndex = i;
874 }
875 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700876 snapToPage(minDistanceFromScreenCenterIndex, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 }
878
Michael Jurka0142d492010-08-25 17:46:15 -0700879 protected void snapToPageWithVelocity(int whichPage, int velocity) {
880 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
881 // can use it
882 snapToPage(whichPage);
883 }
884
885 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700886 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 }
888
Michael Jurka0142d492010-08-25 17:46:15 -0700889 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700890 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700891
Winson Chung321e9ee2010-08-09 13:37:56 -0700892
Winson Chung86f77532010-08-24 11:08:22 -0700893 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 final int sX = getScrollX();
895 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700896 snapToPage(whichPage, delta, duration);
897 }
898
899 protected void snapToPage(int whichPage, int delta, int duration) {
900 mNextPage = whichPage;
901
902 View focusedChild = getFocusedChild();
903 if (focusedChild != null && whichPage != mCurrentPage &&
904 focusedChild == getChildAt(mCurrentPage)) {
905 focusedChild.clearFocus();
906 }
907
908 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 awakenScrollBars(duration);
910 if (duration == 0) {
911 duration = Math.abs(delta);
912 }
913
914 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700915 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700916
917 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700918 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700919 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 invalidate();
921 }
922
923 @Override
924 protected Parcelable onSaveInstanceState() {
925 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700926 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 return state;
928 }
929
930 @Override
931 protected void onRestoreInstanceState(Parcelable state) {
932 SavedState savedState = (SavedState) state;
933 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700934 if (savedState.currentPage != -1) {
935 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 }
937 }
938
939 public void scrollLeft() {
940 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700941 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700943 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 }
945 }
946
947 public void scrollRight() {
948 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700949 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700951 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 }
953 }
954
Winson Chung86f77532010-08-24 11:08:22 -0700955 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 int result = -1;
957 if (v != null) {
958 ViewParent vp = v.getParent();
959 int count = getChildCount();
960 for (int i = 0; i < count; i++) {
961 if (vp == getChildAt(i)) {
962 return i;
963 }
964 }
965 }
966 return result;
967 }
968
969 /**
970 * @return True is long presses are still allowed for the current touch
971 */
972 public boolean allowLongPress() {
973 return mAllowLongPress;
974 }
975
Michael Jurka0142d492010-08-25 17:46:15 -0700976 /**
977 * Set true to allow long-press events to be triggered, usually checked by
978 * {@link Launcher} to accept or block dpad-initiated long-presses.
979 */
980 public void setAllowLongPress(boolean allowLongPress) {
981 mAllowLongPress = allowLongPress;
982 }
983
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -0700985 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700986
987 SavedState(Parcelable superState) {
988 super(superState);
989 }
990
991 private SavedState(Parcel in) {
992 super(in);
Winson Chung86f77532010-08-24 11:08:22 -0700993 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 }
995
996 @Override
997 public void writeToParcel(Parcel out, int flags) {
998 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -0700999 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 }
1001
1002 public static final Parcelable.Creator<SavedState> CREATOR =
1003 new Parcelable.Creator<SavedState>() {
1004 public SavedState createFromParcel(Parcel in) {
1005 return new SavedState(in);
1006 }
1007
1008 public SavedState[] newArray(int size) {
1009 return new SavedState[size];
1010 }
1011 };
1012 }
1013
Winson Chung86f77532010-08-24 11:08:22 -07001014 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001015 if (mContentIsRefreshable) {
1016 final int count = getChildCount();
1017 if (page < count) {
1018 int lowerPageBound = Math.max(0, page - 1);
1019 int upperPageBound = Math.min(page + 1, count - 1);
1020 for (int i = 0; i < count; ++i) {
1021 final ViewGroup layout = (ViewGroup) getChildAt(i);
1022 final int childCount = layout.getChildCount();
1023 if (lowerPageBound <= i && i <= upperPageBound) {
1024 if (mDirtyPageContent.get(i)) {
1025 syncPageItems(i);
1026 mDirtyPageContent.set(i, false);
1027 }
1028 } else {
1029 if (childCount > 0) {
1030 layout.removeAllViews();
1031 }
1032 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001033 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001034 }
1035 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001036 }
1037 }
1038
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001039 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001040 if (isChoiceMode(CHOICE_MODE_NONE)) {
1041 mChoiceMode = mode;
1042 mActionMode = startActionMode(callback);
1043 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001044 }
1045
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001046 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001047 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001048 mChoiceMode = CHOICE_MODE_NONE;
1049 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001050 mActionMode.finish();
1051 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001052 }
1053 }
1054
1055 protected boolean isChoiceMode(int mode) {
1056 return mChoiceMode == mode;
1057 }
1058
1059 protected ArrayList<Checkable> getCheckedGrandchildren() {
1060 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1061 final int childCount = getChildCount();
1062 for (int i = 0; i < childCount; ++i) {
1063 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1064 final int grandChildCount = layout.getChildCount();
1065 for (int j = 0; j < grandChildCount; ++j) {
1066 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001067 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001068 checked.add((Checkable) v);
1069 }
1070 }
1071 }
1072 return checked;
1073 }
1074
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001075 /**
1076 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1077 * Otherwise, returns null.
1078 */
1079 protected Checkable getSingleCheckedGrandchild() {
1080 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1081 final int childCount = getChildCount();
1082 for (int i = 0; i < childCount; ++i) {
1083 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1084 final int grandChildCount = layout.getChildCount();
1085 for (int j = 0; j < grandChildCount; ++j) {
1086 final View v = layout.getChildAt(j);
1087 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1088 return (Checkable) v;
1089 }
1090 }
1091 }
1092 }
1093 return null;
1094 }
1095
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001096 public Object getChosenItem() {
1097 View checkedView = (View) getSingleCheckedGrandchild();
1098 if (checkedView != null) {
1099 return checkedView.getTag();
1100 }
1101 return null;
1102 }
1103
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001104 protected void resetCheckedGrandchildren() {
1105 // loop through children, and set all of their children to _not_ be checked
1106 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1107 for (int i = 0; i < checked.size(); ++i) {
1108 final Checkable c = checked.get(i);
1109 c.setChecked(false);
1110 }
1111 }
1112
Winson Chung86f77532010-08-24 11:08:22 -07001113 /**
1114 * This method is called ONLY to synchronize the number of pages that the paged view has.
1115 * To actually fill the pages with information, implement syncPageItems() below. It is
1116 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1117 * and therefore, individual page items do not need to be updated in this method.
1118 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001120
1121 /**
1122 * This method is called to synchronize the items that are on a particular page. If views on
1123 * the page can be reused, then they should be updated within this method.
1124 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001126
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001128 if (mContentIsRefreshable) {
1129 // Update all the pages
1130 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001131
Michael Jurka0142d492010-08-25 17:46:15 -07001132 // Mark each of the pages as dirty
1133 final int count = getChildCount();
1134 mDirtyPageContent.clear();
1135 for (int i = 0; i < count; ++i) {
1136 mDirtyPageContent.add(true);
1137 }
1138
1139 // Load any pages that are necessary for the current window of views
1140 loadAssociatedPages(mCurrentPage);
1141 mDirtyPageAlpha = true;
1142 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001143 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001144 }
1145}