blob: ebb28f9281975e0b26754589c3e59f0a541408bf [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
548
549 switch (action & MotionEvent.ACTION_MASK) {
550 case MotionEvent.ACTION_MOVE: {
551 /*
552 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
553 * whether the user has moved far enough from his original down touch.
554 */
555 determineScrollingStart(ev);
556 break;
557 }
558
559 case MotionEvent.ACTION_DOWN: {
560 final float x = ev.getX();
561 final float y = ev.getY();
562 // Remember location of down touch
563 mDownMotionX = x;
564 mLastMotionX = x;
565 mLastMotionY = y;
566 mActivePointerId = ev.getPointerId(0);
567 mAllowLongPress = true;
568
569 /*
570 * If being flinged and user touches the screen, initiate drag;
571 * otherwise don't. mScroller.isFinished should be false when
572 * being flinged.
573 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700574 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
575 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
576 if (finishedScrolling) {
577 mTouchState = TOUCH_STATE_REST;
578 mScroller.abortAnimation();
579 } else {
580 mTouchState = TOUCH_STATE_SCROLLING;
581 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700582
Winson Chung86f77532010-08-24 11:08:22 -0700583 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 // to scroll the current page
585 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
586 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
587 if (getChildCount() > 0) {
588 int relativeChildLeft = getChildOffset(0);
589 int relativeChildRight = relativeChildLeft + getChildAt(0).getMeasuredWidth();
590 if (x < relativeChildLeft) {
591 mTouchState = TOUCH_STATE_PREV_PAGE;
592 } else if (x > relativeChildRight) {
593 mTouchState = TOUCH_STATE_NEXT_PAGE;
594 }
595 }
596 }
597 break;
598 }
599
600 case MotionEvent.ACTION_CANCEL:
601 case MotionEvent.ACTION_UP:
602 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700603 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 mTouchState = TOUCH_STATE_REST;
605 mAllowLongPress = false;
606 mActivePointerId = INVALID_POINTER;
607
608 break;
609
610 case MotionEvent.ACTION_POINTER_UP:
611 onSecondaryPointerUp(ev);
612 break;
613 }
614
615 /*
616 * The only time we want to intercept motion events is if we are in the
617 * drag mode.
618 */
619 return mTouchState != TOUCH_STATE_REST;
620 }
621
Winson Chung80baf5a2010-08-09 16:03:15 -0700622 protected void animateClickFeedback(View v, final Runnable r) {
623 // animate the view slightly to show click feedback running some logic after it is "pressed"
624 Animation anim = AnimationUtils.loadAnimation(getContext(),
625 R.anim.paged_view_click_feedback);
626 anim.setAnimationListener(new AnimationListener() {
627 @Override
628 public void onAnimationStart(Animation animation) {}
629 @Override
630 public void onAnimationRepeat(Animation animation) {
631 r.run();
632 }
633 @Override
634 public void onAnimationEnd(Animation animation) {}
635 });
636 v.startAnimation(anim);
637 }
638
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 /*
640 * Determines if we should change the touch state to start scrolling after the
641 * user moves their touch point too far.
642 */
643 private void determineScrollingStart(MotionEvent ev) {
644 /*
645 * Locally do absolute value. mLastMotionX is set to the y value
646 * of the down event.
647 */
648 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
649 final float x = ev.getX(pointerIndex);
650 final float y = ev.getY(pointerIndex);
651 final int xDiff = (int) Math.abs(x - mLastMotionX);
652 final int yDiff = (int) Math.abs(y - mLastMotionY);
653
654 final int touchSlop = mTouchSlop;
655 boolean xPaged = xDiff > mPagingTouchSlop;
656 boolean xMoved = xDiff > touchSlop;
657 boolean yMoved = yDiff > touchSlop;
658
659 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700660 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700661 // Scroll if the user moved far enough along the X axis
662 mTouchState = TOUCH_STATE_SCROLLING;
663 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700664 mTouchX = mScrollX;
665 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
666 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 }
668 // Either way, cancel any pending longpress
669 if (mAllowLongPress) {
670 mAllowLongPress = false;
671 // Try canceling the long press. It could also have been scheduled
672 // by a distant descendant, so use the mAllowLongPress flag to block
673 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700674 final View currentPage = getPageAt(mCurrentPage);
675 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 }
677 }
678 }
679
680 @Override
681 public boolean onTouchEvent(MotionEvent ev) {
682 if (mVelocityTracker == null) {
683 mVelocityTracker = VelocityTracker.obtain();
684 }
685 mVelocityTracker.addMovement(ev);
686
687 final int action = ev.getAction();
688
689 switch (action & MotionEvent.ACTION_MASK) {
690 case MotionEvent.ACTION_DOWN:
691 /*
692 * If being flinged and user touches, stop the fling. isFinished
693 * will be false if being flinged.
694 */
695 if (!mScroller.isFinished()) {
696 mScroller.abortAnimation();
697 }
698
699 // Remember where the motion event started
700 mDownMotionX = mLastMotionX = ev.getX();
701 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700702 if (mTouchState == TOUCH_STATE_SCROLLING) {
703 pageBeginMoving();
704 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 break;
706
707 case MotionEvent.ACTION_MOVE:
708 if (mTouchState == TOUCH_STATE_SCROLLING) {
709 // Scroll to follow the motion event
710 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
711 final float x = ev.getX(pointerIndex);
712 final int deltaX = (int) (mLastMotionX - x);
713 mLastMotionX = x;
714
715 int sx = getScrollX();
716 if (deltaX < 0) {
717 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700718 mTouchX += Math.max(-mTouchX, deltaX);
719 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
720 if (!mDeferScrollUpdate) {
721 scrollBy(Math.max(-sx, deltaX), 0);
722 } else {
723 // This will trigger a call to computeScroll() on next drawChild() call
724 invalidate();
725 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 }
727 } else if (deltaX > 0) {
728 final int lastChildIndex = getChildCount() - 1;
729 final int availableToScroll = getChildOffset(lastChildIndex) -
730 getRelativeChildOffset(lastChildIndex) - sx;
731 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700732 mTouchX += Math.min(availableToScroll, deltaX);
733 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
734 if (!mDeferScrollUpdate) {
735 scrollBy(Math.min(availableToScroll, deltaX), 0);
736 } else {
737 // This will trigger a call to computeScroll() on next drawChild() call
738 invalidate();
739 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741 } else {
742 awakenScrollBars();
743 }
744 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
745 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
746 determineScrollingStart(ev);
747 }
748 break;
749
750 case MotionEvent.ACTION_UP:
751 if (mTouchState == TOUCH_STATE_SCROLLING) {
752 final int activePointerId = mActivePointerId;
753 final int pointerIndex = ev.findPointerIndex(activePointerId);
754 final float x = ev.getX(pointerIndex);
755 final VelocityTracker velocityTracker = mVelocityTracker;
756 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
757 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
758 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
759
Michael Jurka0142d492010-08-25 17:46:15 -0700760 final int snapVelocity = mSnapVelocity;
761 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
762 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
763 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700764 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700765 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700766 } else {
767 snapToDestination();
768 }
769
770 if (mVelocityTracker != null) {
771 mVelocityTracker.recycle();
772 mVelocityTracker = null;
773 }
774 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
775 // at this point we have not moved beyond the touch slop
776 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
777 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700778 int nextPage = Math.max(0, mCurrentPage - 1);
779 if (nextPage != mCurrentPage) {
780 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 } else {
782 snapToDestination();
783 }
784 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
785 // at this point we have not moved beyond the touch slop
786 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
787 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700788 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
789 if (nextPage != mCurrentPage) {
790 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 } else {
792 snapToDestination();
793 }
794 }
795 mTouchState = TOUCH_STATE_REST;
796 mActivePointerId = INVALID_POINTER;
797 break;
798
799 case MotionEvent.ACTION_CANCEL:
800 mTouchState = TOUCH_STATE_REST;
801 mActivePointerId = INVALID_POINTER;
802 break;
803
804 case MotionEvent.ACTION_POINTER_UP:
805 onSecondaryPointerUp(ev);
806 break;
807 }
808
809 return true;
810 }
811
812 private void onSecondaryPointerUp(MotionEvent ev) {
813 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
814 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
815 final int pointerId = ev.getPointerId(pointerIndex);
816 if (pointerId == mActivePointerId) {
817 // This was our active pointer going up. Choose a new
818 // active pointer and adjust accordingly.
819 // TODO: Make this decision more intelligent.
820 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
821 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
822 mLastMotionY = ev.getY(newPointerIndex);
823 mActivePointerId = ev.getPointerId(newPointerIndex);
824 if (mVelocityTracker != null) {
825 mVelocityTracker.clear();
826 }
827 }
828 }
829
830 @Override
831 public void requestChildFocus(View child, View focused) {
832 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700833 int page = indexOfChild(child);
834 if (page >= 0 && !isInTouchMode()) {
835 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 }
837 }
838
839 protected int getRelativeChildOffset(int index) {
840 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
841 }
842
843 protected int getChildOffset(int index) {
844 if (getChildCount() == 0)
845 return 0;
846
847 int offset = getRelativeChildOffset(0);
848 for (int i = 0; i < index; ++i) {
849 offset += getChildAt(i).getMeasuredWidth();
850 }
851 return offset;
852 }
853
854 protected void snapToDestination() {
855 int minDistanceFromScreenCenter = getMeasuredWidth();
856 int minDistanceFromScreenCenterIndex = -1;
857 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
858 final int childCount = getChildCount();
859 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700860 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 int childWidth = layout.getMeasuredWidth();
862 int halfChildWidth = (childWidth / 2);
863 int childCenter = getChildOffset(i) + halfChildWidth;
864 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
865 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
866 minDistanceFromScreenCenter = distanceFromScreenCenter;
867 minDistanceFromScreenCenterIndex = i;
868 }
869 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700870 snapToPage(minDistanceFromScreenCenterIndex, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 }
872
Michael Jurka0142d492010-08-25 17:46:15 -0700873 protected void snapToPageWithVelocity(int whichPage, int velocity) {
874 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
875 // can use it
876 snapToPage(whichPage);
877 }
878
879 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700880 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 }
882
Michael Jurka0142d492010-08-25 17:46:15 -0700883 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700884 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700885
Winson Chung321e9ee2010-08-09 13:37:56 -0700886
Winson Chung86f77532010-08-24 11:08:22 -0700887 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 final int sX = getScrollX();
889 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700890 snapToPage(whichPage, delta, duration);
891 }
892
893 protected void snapToPage(int whichPage, int delta, int duration) {
894 mNextPage = whichPage;
895
896 View focusedChild = getFocusedChild();
897 if (focusedChild != null && whichPage != mCurrentPage &&
898 focusedChild == getChildAt(mCurrentPage)) {
899 focusedChild.clearFocus();
900 }
901
902 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 awakenScrollBars(duration);
904 if (duration == 0) {
905 duration = Math.abs(delta);
906 }
907
908 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700909 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700910
911 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700912 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700913 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 invalidate();
915 }
916
917 @Override
918 protected Parcelable onSaveInstanceState() {
919 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700920 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 return state;
922 }
923
924 @Override
925 protected void onRestoreInstanceState(Parcelable state) {
926 SavedState savedState = (SavedState) state;
927 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700928 if (savedState.currentPage != -1) {
929 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 }
931 }
932
933 public void scrollLeft() {
934 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700935 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700937 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 }
939 }
940
941 public void scrollRight() {
942 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700943 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700945 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 }
947 }
948
Winson Chung86f77532010-08-24 11:08:22 -0700949 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 int result = -1;
951 if (v != null) {
952 ViewParent vp = v.getParent();
953 int count = getChildCount();
954 for (int i = 0; i < count; i++) {
955 if (vp == getChildAt(i)) {
956 return i;
957 }
958 }
959 }
960 return result;
961 }
962
963 /**
964 * @return True is long presses are still allowed for the current touch
965 */
966 public boolean allowLongPress() {
967 return mAllowLongPress;
968 }
969
Michael Jurka0142d492010-08-25 17:46:15 -0700970 /**
971 * Set true to allow long-press events to be triggered, usually checked by
972 * {@link Launcher} to accept or block dpad-initiated long-presses.
973 */
974 public void setAllowLongPress(boolean allowLongPress) {
975 mAllowLongPress = allowLongPress;
976 }
977
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -0700979 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700980
981 SavedState(Parcelable superState) {
982 super(superState);
983 }
984
985 private SavedState(Parcel in) {
986 super(in);
Winson Chung86f77532010-08-24 11:08:22 -0700987 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 }
989
990 @Override
991 public void writeToParcel(Parcel out, int flags) {
992 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -0700993 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 }
995
996 public static final Parcelable.Creator<SavedState> CREATOR =
997 new Parcelable.Creator<SavedState>() {
998 public SavedState createFromParcel(Parcel in) {
999 return new SavedState(in);
1000 }
1001
1002 public SavedState[] newArray(int size) {
1003 return new SavedState[size];
1004 }
1005 };
1006 }
1007
Winson Chung86f77532010-08-24 11:08:22 -07001008 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001009 if (mContentIsRefreshable) {
1010 final int count = getChildCount();
1011 if (page < count) {
1012 int lowerPageBound = Math.max(0, page - 1);
1013 int upperPageBound = Math.min(page + 1, count - 1);
1014 for (int i = 0; i < count; ++i) {
1015 final ViewGroup layout = (ViewGroup) getChildAt(i);
1016 final int childCount = layout.getChildCount();
1017 if (lowerPageBound <= i && i <= upperPageBound) {
1018 if (mDirtyPageContent.get(i)) {
1019 syncPageItems(i);
1020 mDirtyPageContent.set(i, false);
1021 }
1022 } else {
1023 if (childCount > 0) {
1024 layout.removeAllViews();
1025 }
1026 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001027 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001028 }
1029 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001030 }
1031 }
1032
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001033 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001034 if (isChoiceMode(CHOICE_MODE_NONE)) {
1035 mChoiceMode = mode;
1036 mActionMode = startActionMode(callback);
1037 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001038 }
1039
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001040 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001041 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001042 mChoiceMode = CHOICE_MODE_NONE;
1043 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001044 mActionMode.finish();
1045 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001046 }
1047 }
1048
1049 protected boolean isChoiceMode(int mode) {
1050 return mChoiceMode == mode;
1051 }
1052
1053 protected ArrayList<Checkable> getCheckedGrandchildren() {
1054 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1055 final int childCount = getChildCount();
1056 for (int i = 0; i < childCount; ++i) {
1057 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1058 final int grandChildCount = layout.getChildCount();
1059 for (int j = 0; j < grandChildCount; ++j) {
1060 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001061 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001062 checked.add((Checkable) v);
1063 }
1064 }
1065 }
1066 return checked;
1067 }
1068
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001069 /**
1070 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1071 * Otherwise, returns null.
1072 */
1073 protected Checkable getSingleCheckedGrandchild() {
1074 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1075 final int childCount = getChildCount();
1076 for (int i = 0; i < childCount; ++i) {
1077 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1078 final int grandChildCount = layout.getChildCount();
1079 for (int j = 0; j < grandChildCount; ++j) {
1080 final View v = layout.getChildAt(j);
1081 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1082 return (Checkable) v;
1083 }
1084 }
1085 }
1086 }
1087 return null;
1088 }
1089
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001090 public Object getChosenItem() {
1091 View checkedView = (View) getSingleCheckedGrandchild();
1092 if (checkedView != null) {
1093 return checkedView.getTag();
1094 }
1095 return null;
1096 }
1097
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001098 protected void resetCheckedGrandchildren() {
1099 // loop through children, and set all of their children to _not_ be checked
1100 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1101 for (int i = 0; i < checked.size(); ++i) {
1102 final Checkable c = checked.get(i);
1103 c.setChecked(false);
1104 }
1105 }
1106
Winson Chung86f77532010-08-24 11:08:22 -07001107 /**
1108 * This method is called ONLY to synchronize the number of pages that the paged view has.
1109 * To actually fill the pages with information, implement syncPageItems() below. It is
1110 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1111 * and therefore, individual page items do not need to be updated in this method.
1112 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001113 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001114
1115 /**
1116 * This method is called to synchronize the items that are on a particular page. If views on
1117 * the page can be reused, then they should be updated within this method.
1118 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001120
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001122 if (mContentIsRefreshable) {
1123 // Update all the pages
1124 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001125
Michael Jurka0142d492010-08-25 17:46:15 -07001126 // Mark each of the pages as dirty
1127 final int count = getChildCount();
1128 mDirtyPageContent.clear();
1129 for (int i = 0; i < count; ++i) {
1130 mDirtyPageContent.add(true);
1131 }
1132
1133 // Load any pages that are necessary for the current window of views
1134 loadAssociatedPages(mCurrentPage);
1135 mDirtyPageAlpha = true;
1136 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001137 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001138 }
1139}