blob: a1b1e08bad904a2fef88636b188bd39e2d65339f [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
Winson Chunge3193b92010-09-10 11:44:42 -070019import java.util.ArrayList;
20import java.util.HashMap;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070021
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070023import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.graphics.Canvas;
25import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.os.Parcel;
27import android.os.Parcelable;
28import android.util.AttributeSet;
Winson Chunge3193b92010-09-10 11:44:42 -070029import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070030import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.view.MotionEvent;
32import android.view.VelocityTracker;
33import android.view.View;
34import android.view.ViewConfiguration;
35import android.view.ViewGroup;
36import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070037import android.view.animation.Animation;
Michael Jurka5f1c5092010-09-03 14:15:02 -070038import android.view.animation.Animation.AnimationListener;
Winson Chunge3193b92010-09-10 11:44:42 -070039import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070041import android.widget.LinearLayout;
Winson Chung321e9ee2010-08-09 13:37:56 -070042import android.widget.Scroller;
43
Winson Chunge3193b92010-09-10 11:44:42 -070044import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070045
Winson Chung321e9ee2010-08-09 13:37:56 -070046/**
47 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070048 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070049 */
50public abstract class PagedView extends ViewGroup {
51 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070052 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung86f77532010-08-24 11:08:22 -070054 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung321e9ee2010-08-09 13:37:56 -070055 private static final int MIN_LENGTH_FOR_FLING = 50;
56
Winson Chung5f2aa4e2010-08-20 14:49:25 -070057 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070058 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Michael Jurka0142d492010-08-25 17:46:15 -070060 // the velocity at which a fling gesture will cause us to snap to the next page
61 protected int mSnapVelocity = 500;
62
63 protected float mSmoothingTime;
64 protected float mTouchX;
65
66 protected boolean mFirstLayout = true;
67
68 protected int mCurrentPage;
69 protected int mNextPage = INVALID_PAGE;
70 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070071 private VelocityTracker mVelocityTracker;
72
73 private float mDownMotionX;
74 private float mLastMotionX;
75 private float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070076 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070077
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected final static int TOUCH_STATE_REST = 0;
79 protected final static int TOUCH_STATE_SCROLLING = 1;
80 protected final static int TOUCH_STATE_PREV_PAGE = 2;
81 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohenf34bab52010-09-30 14:11:56 -070082 protected final static float ALPHA_QUANTIZE_LEVEL = 0.01f;
Winson Chung321e9ee2010-08-09 13:37:56 -070083
Michael Jurka0142d492010-08-25 17:46:15 -070084 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070085
Michael Jurka0142d492010-08-25 17:46:15 -070086 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070087
88 private boolean mAllowLongPress = true;
89
90 private int mTouchSlop;
91 private int mPagingTouchSlop;
92 private int mMaximumVelocity;
93
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka5f1c5092010-09-03 14:15:02 -070096 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Winson Chung86f77532010-08-24 11:08:22 -070098 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Winson Chung86f77532010-08-24 11:08:22 -0700100 private ArrayList<Boolean> mDirtyPageContent;
101 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700103 // choice modes
104 protected static final int CHOICE_MODE_NONE = 0;
105 protected static final int CHOICE_MODE_SINGLE = 1;
106 // Multiple selection mode is not supported by all Launcher actions atm
107 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700108
Michael Jurkae17e19c2010-09-28 11:01:39 -0700109 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700110 private ActionMode mActionMode;
111
Winson Chung241c3b42010-08-25 16:53:03 -0700112 protected PagedViewIconCache mPageViewIconCache;
113
Michael Jurka0142d492010-08-25 17:46:15 -0700114 // If true, syncPages and syncPageItems will be called to refresh pages
115 protected boolean mContentIsRefreshable = true;
116
117 // If true, modify alpha of neighboring pages as user scrolls left/right
118 protected boolean mFadeInAdjacentScreens = true;
119
120 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
121 // to switch to a new page
122 protected boolean mUsePagingTouchSlop = true;
123
124 // If true, the subclass should directly update mScrollX itself in its computeScroll method
125 // (SmoothPagedView does this)
126 protected boolean mDeferScrollUpdate = false;
127
Winson Chung241c3b42010-08-25 16:53:03 -0700128 /**
129 * Simple cache mechanism for PagedViewIcon outlines.
130 */
131 class PagedViewIconCache {
132 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
133
134 public void clear() {
135 iconOutlineCache.clear();
136 }
137 public void addOutline(Object key, Bitmap b) {
138 iconOutlineCache.put(key, b);
139 }
140 public void removeOutline(Object key) {
141 if (iconOutlineCache.containsKey(key)) {
142 iconOutlineCache.remove(key);
143 }
144 }
145 public Bitmap getOutline(Object key) {
146 return iconOutlineCache.get(key);
147 }
148 }
149
Winson Chung86f77532010-08-24 11:08:22 -0700150 public interface PageSwitchListener {
151 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700152 }
153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 public interface PageMovingListener {
155 void onPageBeginMoving();
156 void onPageEndMoving();
157 }
158
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 public PagedView(Context context) {
160 this(context, null);
161 }
162
163 public PagedView(Context context, AttributeSet attrs) {
164 this(context, attrs, 0);
165 }
166
167 public PagedView(Context context, AttributeSet attrs, int defStyle) {
168 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700169 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700170
171 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700172 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 }
174
175 /**
176 * Initializes various states for this workspace.
177 */
Michael Jurka0142d492010-08-25 17:46:15 -0700178 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700179 mDirtyPageContent = new ArrayList<Boolean>();
180 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700181 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700182 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700183 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700184
185 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
186 mTouchSlop = configuration.getScaledTouchSlop();
187 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
188 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
189 }
190
Winson Chung86f77532010-08-24 11:08:22 -0700191 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
192 mPageSwitchListener = pageSwitchListener;
193 if (mPageSwitchListener != null) {
194 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700195 }
196 }
197
198 /**
Winson Chung86f77532010-08-24 11:08:22 -0700199 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 *
Winson Chung86f77532010-08-24 11:08:22 -0700201 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700202 */
Winson Chung86f77532010-08-24 11:08:22 -0700203 int getCurrentPage() {
204 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700205 }
206
Winson Chung86f77532010-08-24 11:08:22 -0700207 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 return getChildCount();
209 }
210
Winson Chung86f77532010-08-24 11:08:22 -0700211 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 return getChildAt(index);
213 }
214
215 int getScrollWidth() {
216 return getWidth();
217 }
218
219 /**
Winson Chung86f77532010-08-24 11:08:22 -0700220 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 */
Winson Chung86f77532010-08-24 11:08:22 -0700222 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 if (!mScroller.isFinished()) mScroller.abortAnimation();
224 if (getChildCount() == 0) return;
225
Winson Chung86f77532010-08-24 11:08:22 -0700226 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
227 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700228
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700230 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 }
232
Michael Jurka0142d492010-08-25 17:46:15 -0700233 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700234 if (mPageSwitchListener != null) {
235 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 }
237 }
238
Michael Jurka0142d492010-08-25 17:46:15 -0700239 // a method that subclasses can override to add behavior
240 protected void pageBeginMoving() {
241 }
242
243 // a method that subclasses can override to add behavior
244 protected void pageEndMoving() {
245 }
246
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 /**
Winson Chung86f77532010-08-24 11:08:22 -0700248 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 *
250 * @param l The listener used to respond to long clicks.
251 */
252 @Override
253 public void setOnLongClickListener(OnLongClickListener l) {
254 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700255 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700257 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 }
259 }
260
261 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700262 public void scrollTo(int x, int y) {
263 super.scrollTo(x, y);
264 mTouchX = x;
265 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
266 }
267
268 // we moved this functionality to a helper function so SmoothPagedView can reuse it
269 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700271 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700273 invalidate();
274 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700275 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700276 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700277 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700278 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700279 notifyPageSwitchListener();
280 pageEndMoving();
281 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 }
Michael Jurka0142d492010-08-25 17:46:15 -0700283 return false;
284 }
285
286 @Override
287 public void computeScroll() {
288 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 }
290
291 @Override
292 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
293 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
294 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
295 if (widthMode != MeasureSpec.EXACTLY) {
296 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
297 }
298
299 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
300 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
301 if (heightMode != MeasureSpec.EXACTLY) {
302 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
303 }
304
305 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700306 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 final int childCount = getChildCount();
308 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700309 // disallowing padding in paged view (just pass 0)
310 final View child = getChildAt(i);
311 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
312
313 int childWidthMode;
314 if (lp.width == LayoutParams.WRAP_CONTENT) {
315 childWidthMode = MeasureSpec.AT_MOST;
316 } else {
317 childWidthMode = MeasureSpec.EXACTLY;
318 }
319
320 int childHeightMode;
321 if (lp.height == LayoutParams.WRAP_CONTENT) {
322 childHeightMode = MeasureSpec.AT_MOST;
323 } else {
324 childHeightMode = MeasureSpec.EXACTLY;
325 }
326
327 final int childWidthMeasureSpec =
328 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
329 final int childHeightMeasureSpec =
330 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
331
332 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 }
334
335 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 }
337
338 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700339 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700340 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
341 setHorizontalScrollBarEnabled(false);
342 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
343 scrollTo(newX, 0);
344 mScroller.setFinalX(newX);
345 setHorizontalScrollBarEnabled(true);
346 mFirstLayout = false;
347 }
348
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 final int childCount = getChildCount();
350 int childLeft = 0;
351 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700352 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 }
354
355 for (int i = 0; i < childCount; i++) {
356 final View child = getChildAt(i);
357 if (child.getVisibility() != View.GONE) {
358 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700359 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
360 child.layout(childLeft, childHeight,
361 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 childLeft += childWidth;
363 }
364 }
365 }
366
Winson Chunge3193b92010-09-10 11:44:42 -0700367 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700368 if (mFadeInAdjacentScreens) {
369 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700370 int halfScreenSize = getMeasuredWidth() / 2;
371 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700372 final int childCount = getChildCount();
373 for (int i = 0; i < childCount; ++i) {
374 View layout = (View) getChildAt(i);
375 int childWidth = layout.getMeasuredWidth();
376 int halfChildWidth = (childWidth / 2);
377 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700378
379 int d = halfChildWidth;
380 int distanceFromScreenCenter = childCenter - screenCenter;
381 if (distanceFromScreenCenter > 0) {
382 if (i > 0) {
383 d += getChildAt(i - 1).getMeasuredWidth() / 2;
384 }
Michael Jurka0142d492010-08-25 17:46:15 -0700385 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700386 if (i < childCount - 1) {
387 d += getChildAt(i + 1).getMeasuredWidth() / 2;
388 }
Michael Jurka0142d492010-08-25 17:46:15 -0700389 }
Winson Chunge8878e32010-09-15 20:37:09 -0700390
391 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
392 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
393 float alpha = 1.0f - dimAlpha;
394
Adam Cohenf34bab52010-09-30 14:11:56 -0700395 if (alpha < ALPHA_QUANTIZE_LEVEL) {
396 alpha = 0.0f;
397 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
398 alpha = 1.0f;
399 }
400
Michael Jurka0142d492010-08-25 17:46:15 -0700401 if (Float.compare(alpha, layout.getAlpha()) != 0) {
402 layout.setAlpha(alpha);
403 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 }
Michael Jurka0142d492010-08-25 17:46:15 -0700405 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 }
407 }
Winson Chunge3193b92010-09-10 11:44:42 -0700408 }
409
Adam Cohenf34bab52010-09-30 14:11:56 -0700410 protected void screenScrolled(int screenCenter) {
411 }
412
Winson Chunge3193b92010-09-10 11:44:42 -0700413 @Override
414 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700415 int halfScreenSize = getMeasuredWidth() / 2;
416 int screenCenter = mScrollX + halfScreenSize;
417
418 if (screenCenter != mLastScreenCenter) {
419 screenScrolled(screenCenter);
420 updateAdjacentPagesAlpha();
421 mLastScreenCenter = screenCenter;
422 }
Michael Jurka0142d492010-08-25 17:46:15 -0700423
424 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700425 // As an optimization, this code assumes that all pages have the same width as the 0th
426 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700427 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700428 if (pageCount > 0) {
429 final int pageWidth = getChildAt(0).getMeasuredWidth();
430 final int screenWidth = getMeasuredWidth();
431 int x = getRelativeChildOffset(0) + pageWidth;
432 int leftScreen = 0;
433 int rightScreen = 0;
434 while (x <= mScrollX) {
435 leftScreen++;
436 x += pageWidth;
437 // replace above line with this if you don't assume all pages have same width as 0th
438 // page:
439 // x += getChildAt(leftScreen).getMeasuredWidth();
440 }
441 rightScreen = leftScreen;
442 while (x < mScrollX + screenWidth) {
443 rightScreen++;
444 x += pageWidth;
445 // replace above line with this if you don't assume all pages have same width as 0th
446 // page:
447 //if (rightScreen < pageCount) {
448 // x += getChildAt(rightScreen).getMeasuredWidth();
449 //}
450 }
451 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700452
Michael Jurkac4fb9172010-09-02 17:19:20 -0700453 final long drawingTime = getDrawingTime();
454 for (int i = leftScreen; i <= rightScreen; i++) {
455 drawChild(canvas, getChildAt(i), drawingTime);
456 }
Michael Jurka0142d492010-08-25 17:46:15 -0700457 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 }
459
460 @Override
461 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700462 int page = indexOfChild(child);
463 if (page != mCurrentPage || !mScroller.isFinished()) {
464 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 return true;
466 }
467 return false;
468 }
469
470 @Override
471 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700472 int focusablePage;
473 if (mNextPage != INVALID_PAGE) {
474 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700476 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 }
Winson Chung86f77532010-08-24 11:08:22 -0700478 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 if (v != null) {
480 v.requestFocus(direction, previouslyFocusedRect);
481 }
482 return false;
483 }
484
485 @Override
486 public boolean dispatchUnhandledMove(View focused, int direction) {
487 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700488 if (getCurrentPage() > 0) {
489 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 return true;
491 }
492 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700493 if (getCurrentPage() < getPageCount() - 1) {
494 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 return true;
496 }
497 }
498 return super.dispatchUnhandledMove(focused, direction);
499 }
500
501 @Override
502 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700503 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
504 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 }
506 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700507 if (mCurrentPage > 0) {
508 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700509 }
510 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700511 if (mCurrentPage < getPageCount() - 1) {
512 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 }
514 }
515 }
516
517 /**
518 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700519 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700520 *
Winson Chung86f77532010-08-24 11:08:22 -0700521 * This happens when live folders requery, and if they're off page, they
522 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700523 */
524 @Override
525 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700526 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 View v = focused;
528 while (true) {
529 if (v == current) {
530 super.focusableViewAvailable(focused);
531 return;
532 }
533 if (v == this) {
534 return;
535 }
536 ViewParent parent = v.getParent();
537 if (parent instanceof View) {
538 v = (View)v.getParent();
539 } else {
540 return;
541 }
542 }
543 }
544
545 /**
546 * {@inheritDoc}
547 */
548 @Override
549 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
550 if (disallowIntercept) {
551 // We need to make sure to cancel our long press if
552 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700553 final View currentPage = getChildAt(mCurrentPage);
554 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 }
556 super.requestDisallowInterceptTouchEvent(disallowIntercept);
557 }
558
559 @Override
560 public boolean onInterceptTouchEvent(MotionEvent ev) {
561 /*
562 * This method JUST determines whether we want to intercept the motion.
563 * If we return true, onTouchEvent will be called and we do the actual
564 * scrolling there.
565 */
566
567 /*
568 * Shortcut the most recurring case: the user is in the dragging
569 * state and he is moving his finger. We want to intercept this
570 * motion.
571 */
572 final int action = ev.getAction();
573 if ((action == MotionEvent.ACTION_MOVE) &&
574 (mTouchState == TOUCH_STATE_SCROLLING)) {
575 return true;
576 }
577
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 switch (action & MotionEvent.ACTION_MASK) {
579 case MotionEvent.ACTION_MOVE: {
580 /*
581 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
582 * whether the user has moved far enough from his original down touch.
583 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700584 if (mActivePointerId != INVALID_POINTER) {
585 determineScrollingStart(ev);
586 break;
587 }
588 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
589 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
590 // i.e. fall through to the next case (don't break)
591 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
592 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700593 }
594
595 case MotionEvent.ACTION_DOWN: {
596 final float x = ev.getX();
597 final float y = ev.getY();
598 // Remember location of down touch
599 mDownMotionX = x;
600 mLastMotionX = x;
601 mLastMotionY = y;
602 mActivePointerId = ev.getPointerId(0);
603 mAllowLongPress = true;
604
605 /*
606 * If being flinged and user touches the screen, initiate drag;
607 * otherwise don't. mScroller.isFinished should be false when
608 * being flinged.
609 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700610 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
611 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
612 if (finishedScrolling) {
613 mTouchState = TOUCH_STATE_REST;
614 mScroller.abortAnimation();
615 } else {
616 mTouchState = TOUCH_STATE_SCROLLING;
617 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700618
Winson Chung86f77532010-08-24 11:08:22 -0700619 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 // to scroll the current page
621 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
622 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
623 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700624 int width = getMeasuredWidth();
625 int offset = getRelativeChildOffset(mCurrentPage);
626 if (x < offset) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 mTouchState = TOUCH_STATE_PREV_PAGE;
Winson Chunge3193b92010-09-10 11:44:42 -0700628 } else if (x > (width - offset)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700629 mTouchState = TOUCH_STATE_NEXT_PAGE;
630 }
631 }
632 }
633 break;
634 }
635
636 case MotionEvent.ACTION_CANCEL:
637 case MotionEvent.ACTION_UP:
638 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700639 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 mTouchState = TOUCH_STATE_REST;
641 mAllowLongPress = false;
642 mActivePointerId = INVALID_POINTER;
643
644 break;
645
646 case MotionEvent.ACTION_POINTER_UP:
647 onSecondaryPointerUp(ev);
648 break;
649 }
650
651 /*
652 * The only time we want to intercept motion events is if we are in the
653 * drag mode.
654 */
655 return mTouchState != TOUCH_STATE_REST;
656 }
657
Winson Chung80baf5a2010-08-09 16:03:15 -0700658 protected void animateClickFeedback(View v, final Runnable r) {
659 // animate the view slightly to show click feedback running some logic after it is "pressed"
660 Animation anim = AnimationUtils.loadAnimation(getContext(),
661 R.anim.paged_view_click_feedback);
662 anim.setAnimationListener(new AnimationListener() {
663 @Override
664 public void onAnimationStart(Animation animation) {}
665 @Override
666 public void onAnimationRepeat(Animation animation) {
667 r.run();
668 }
669 @Override
670 public void onAnimationEnd(Animation animation) {}
671 });
672 v.startAnimation(anim);
673 }
674
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 /*
676 * Determines if we should change the touch state to start scrolling after the
677 * user moves their touch point too far.
678 */
679 private void determineScrollingStart(MotionEvent ev) {
680 /*
681 * Locally do absolute value. mLastMotionX is set to the y value
682 * of the down event.
683 */
684 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
685 final float x = ev.getX(pointerIndex);
686 final float y = ev.getY(pointerIndex);
687 final int xDiff = (int) Math.abs(x - mLastMotionX);
688 final int yDiff = (int) Math.abs(y - mLastMotionY);
689
690 final int touchSlop = mTouchSlop;
691 boolean xPaged = xDiff > mPagingTouchSlop;
692 boolean xMoved = xDiff > touchSlop;
693 boolean yMoved = yDiff > touchSlop;
694
695 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700696 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 // Scroll if the user moved far enough along the X axis
698 mTouchState = TOUCH_STATE_SCROLLING;
699 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700700 mTouchX = mScrollX;
701 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
702 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 }
704 // Either way, cancel any pending longpress
705 if (mAllowLongPress) {
706 mAllowLongPress = false;
707 // Try canceling the long press. It could also have been scheduled
708 // by a distant descendant, so use the mAllowLongPress flag to block
709 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700710 final View currentPage = getPageAt(mCurrentPage);
711 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700712 }
713 }
714 }
715
716 @Override
717 public boolean onTouchEvent(MotionEvent ev) {
718 if (mVelocityTracker == null) {
719 mVelocityTracker = VelocityTracker.obtain();
720 }
721 mVelocityTracker.addMovement(ev);
722
723 final int action = ev.getAction();
724
725 switch (action & MotionEvent.ACTION_MASK) {
726 case MotionEvent.ACTION_DOWN:
727 /*
728 * If being flinged and user touches, stop the fling. isFinished
729 * will be false if being flinged.
730 */
731 if (!mScroller.isFinished()) {
732 mScroller.abortAnimation();
733 }
734
735 // Remember where the motion event started
736 mDownMotionX = mLastMotionX = ev.getX();
737 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700738 if (mTouchState == TOUCH_STATE_SCROLLING) {
739 pageBeginMoving();
740 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 break;
742
743 case MotionEvent.ACTION_MOVE:
744 if (mTouchState == TOUCH_STATE_SCROLLING) {
745 // Scroll to follow the motion event
746 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
747 final float x = ev.getX(pointerIndex);
748 final int deltaX = (int) (mLastMotionX - x);
749 mLastMotionX = x;
750
751 int sx = getScrollX();
752 if (deltaX < 0) {
753 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700754 mTouchX += Math.max(-mTouchX, deltaX);
755 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
756 if (!mDeferScrollUpdate) {
757 scrollBy(Math.max(-sx, deltaX), 0);
758 } else {
759 // This will trigger a call to computeScroll() on next drawChild() call
760 invalidate();
761 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700762 }
763 } else if (deltaX > 0) {
764 final int lastChildIndex = getChildCount() - 1;
765 final int availableToScroll = getChildOffset(lastChildIndex) -
766 getRelativeChildOffset(lastChildIndex) - sx;
767 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700768 mTouchX += Math.min(availableToScroll, deltaX);
769 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
770 if (!mDeferScrollUpdate) {
771 scrollBy(Math.min(availableToScroll, deltaX), 0);
772 } else {
773 // This will trigger a call to computeScroll() on next drawChild() call
774 invalidate();
775 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 }
777 } else {
778 awakenScrollBars();
779 }
780 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
781 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
782 determineScrollingStart(ev);
783 }
784 break;
785
786 case MotionEvent.ACTION_UP:
787 if (mTouchState == TOUCH_STATE_SCROLLING) {
788 final int activePointerId = mActivePointerId;
789 final int pointerIndex = ev.findPointerIndex(activePointerId);
790 final float x = ev.getX(pointerIndex);
791 final VelocityTracker velocityTracker = mVelocityTracker;
792 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
793 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
794 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
795
Michael Jurka0142d492010-08-25 17:46:15 -0700796 final int snapVelocity = mSnapVelocity;
797 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
798 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
799 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700800 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700801 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 } else {
803 snapToDestination();
804 }
805
806 if (mVelocityTracker != null) {
807 mVelocityTracker.recycle();
808 mVelocityTracker = null;
809 }
810 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
811 // at this point we have not moved beyond the touch slop
812 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
813 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700814 int nextPage = Math.max(0, mCurrentPage - 1);
815 if (nextPage != mCurrentPage) {
816 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 } else {
818 snapToDestination();
819 }
820 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
821 // at this point we have not moved beyond the touch slop
822 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
823 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700824 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
825 if (nextPage != mCurrentPage) {
826 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 } else {
828 snapToDestination();
829 }
830 }
831 mTouchState = TOUCH_STATE_REST;
832 mActivePointerId = INVALID_POINTER;
833 break;
834
835 case MotionEvent.ACTION_CANCEL:
836 mTouchState = TOUCH_STATE_REST;
837 mActivePointerId = INVALID_POINTER;
838 break;
839
840 case MotionEvent.ACTION_POINTER_UP:
841 onSecondaryPointerUp(ev);
842 break;
843 }
844
845 return true;
846 }
847
848 private void onSecondaryPointerUp(MotionEvent ev) {
849 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
850 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
851 final int pointerId = ev.getPointerId(pointerIndex);
852 if (pointerId == mActivePointerId) {
853 // This was our active pointer going up. Choose a new
854 // active pointer and adjust accordingly.
855 // TODO: Make this decision more intelligent.
856 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
857 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
858 mLastMotionY = ev.getY(newPointerIndex);
859 mActivePointerId = ev.getPointerId(newPointerIndex);
860 if (mVelocityTracker != null) {
861 mVelocityTracker.clear();
862 }
863 }
864 }
865
866 @Override
867 public void requestChildFocus(View child, View focused) {
868 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700869 int page = indexOfChild(child);
870 if (page >= 0 && !isInTouchMode()) {
871 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 }
873 }
874
Winson Chunge3193b92010-09-10 11:44:42 -0700875 protected int getChildIndexForRelativeOffset(int relativeOffset) {
876 final int childCount = getChildCount();
877 int left = getRelativeChildOffset(0);
878 for (int i = 0; i < childCount; ++i) {
879 final int right = (left + getChildAt(i).getMeasuredWidth());
880 if (left <= relativeOffset && relativeOffset <= right) {
881 return i;
882 }
883 left = right;
884 }
885 return -1;
886 }
887
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 protected int getRelativeChildOffset(int index) {
889 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
890 }
891
892 protected int getChildOffset(int index) {
893 if (getChildCount() == 0)
894 return 0;
895
896 int offset = getRelativeChildOffset(0);
897 for (int i = 0; i < index; ++i) {
898 offset += getChildAt(i).getMeasuredWidth();
899 }
900 return offset;
901 }
902
Adam Cohend19d3ca2010-09-15 14:43:42 -0700903 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 int minDistanceFromScreenCenter = getMeasuredWidth();
905 int minDistanceFromScreenCenterIndex = -1;
906 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
907 final int childCount = getChildCount();
908 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700909 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 int childWidth = layout.getMeasuredWidth();
911 int halfChildWidth = (childWidth / 2);
912 int childCenter = getChildOffset(i) + halfChildWidth;
913 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
914 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
915 minDistanceFromScreenCenter = distanceFromScreenCenter;
916 minDistanceFromScreenCenterIndex = i;
917 }
918 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700919 return minDistanceFromScreenCenterIndex;
920 }
921
922 protected void snapToDestination() {
923 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 }
925
Michael Jurka0142d492010-08-25 17:46:15 -0700926 protected void snapToPageWithVelocity(int whichPage, int velocity) {
927 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
928 // can use it
929 snapToPage(whichPage);
930 }
931
932 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700933 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 }
935
Michael Jurka0142d492010-08-25 17:46:15 -0700936 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700937 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700938
Winson Chung86f77532010-08-24 11:08:22 -0700939 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 final int sX = getScrollX();
941 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700942 snapToPage(whichPage, delta, duration);
943 }
944
945 protected void snapToPage(int whichPage, int delta, int duration) {
946 mNextPage = whichPage;
947
948 View focusedChild = getFocusedChild();
949 if (focusedChild != null && whichPage != mCurrentPage &&
950 focusedChild == getChildAt(mCurrentPage)) {
951 focusedChild.clearFocus();
952 }
953
954 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 awakenScrollBars(duration);
956 if (duration == 0) {
957 duration = Math.abs(delta);
958 }
959
960 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700961 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700962
963 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700964 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700965 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 invalidate();
967 }
968
969 @Override
970 protected Parcelable onSaveInstanceState() {
971 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700972 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 return state;
974 }
975
976 @Override
977 protected void onRestoreInstanceState(Parcelable state) {
978 SavedState savedState = (SavedState) state;
979 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700980 if (savedState.currentPage != -1) {
981 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 }
983 }
984
985 public void scrollLeft() {
986 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700987 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700989 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 }
991 }
992
993 public void scrollRight() {
994 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700995 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700997 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 }
999 }
1000
Winson Chung86f77532010-08-24 11:08:22 -07001001 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 int result = -1;
1003 if (v != null) {
1004 ViewParent vp = v.getParent();
1005 int count = getChildCount();
1006 for (int i = 0; i < count; i++) {
1007 if (vp == getChildAt(i)) {
1008 return i;
1009 }
1010 }
1011 }
1012 return result;
1013 }
1014
1015 /**
1016 * @return True is long presses are still allowed for the current touch
1017 */
1018 public boolean allowLongPress() {
1019 return mAllowLongPress;
1020 }
1021
Michael Jurka0142d492010-08-25 17:46:15 -07001022 /**
1023 * Set true to allow long-press events to be triggered, usually checked by
1024 * {@link Launcher} to accept or block dpad-initiated long-presses.
1025 */
1026 public void setAllowLongPress(boolean allowLongPress) {
1027 mAllowLongPress = allowLongPress;
1028 }
1029
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001031 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001032
1033 SavedState(Parcelable superState) {
1034 super(superState);
1035 }
1036
1037 private SavedState(Parcel in) {
1038 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001039 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 }
1041
1042 @Override
1043 public void writeToParcel(Parcel out, int flags) {
1044 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001045 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 }
1047
1048 public static final Parcelable.Creator<SavedState> CREATOR =
1049 new Parcelable.Creator<SavedState>() {
1050 public SavedState createFromParcel(Parcel in) {
1051 return new SavedState(in);
1052 }
1053
1054 public SavedState[] newArray(int size) {
1055 return new SavedState[size];
1056 }
1057 };
1058 }
1059
Winson Chung86f77532010-08-24 11:08:22 -07001060 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001061 if (mContentIsRefreshable) {
1062 final int count = getChildCount();
1063 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001064 int lowerPageBound = getAssociatedLowerPageBound(page);
1065 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001066 for (int i = 0; i < count; ++i) {
1067 final ViewGroup layout = (ViewGroup) getChildAt(i);
1068 final int childCount = layout.getChildCount();
1069 if (lowerPageBound <= i && i <= upperPageBound) {
1070 if (mDirtyPageContent.get(i)) {
1071 syncPageItems(i);
1072 mDirtyPageContent.set(i, false);
1073 }
1074 } else {
1075 if (childCount > 0) {
1076 layout.removeAllViews();
1077 }
1078 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001079 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001080 }
1081 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001082 }
1083 }
1084
Winson Chunge3193b92010-09-10 11:44:42 -07001085 protected int getAssociatedLowerPageBound(int page) {
1086 return Math.max(0, page - 1);
1087 }
1088 protected int getAssociatedUpperPageBound(int page) {
1089 final int count = getChildCount();
1090 return Math.min(page + 1, count - 1);
1091 }
1092
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001093 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001094 if (isChoiceMode(CHOICE_MODE_NONE)) {
1095 mChoiceMode = mode;
1096 mActionMode = startActionMode(callback);
1097 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001098 }
1099
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001100 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001101 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001102 mChoiceMode = CHOICE_MODE_NONE;
1103 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001104 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001105 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001106 }
1107 }
1108
1109 protected boolean isChoiceMode(int mode) {
1110 return mChoiceMode == mode;
1111 }
1112
1113 protected ArrayList<Checkable> getCheckedGrandchildren() {
1114 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1115 final int childCount = getChildCount();
1116 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001117 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001118 final int grandChildCount = layout.getChildCount();
1119 for (int j = 0; j < grandChildCount; ++j) {
1120 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001121 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001122 checked.add((Checkable) v);
1123 }
1124 }
1125 }
1126 return checked;
1127 }
1128
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001129 /**
1130 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1131 * Otherwise, returns null.
1132 */
1133 protected Checkable getSingleCheckedGrandchild() {
1134 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1135 final int childCount = getChildCount();
1136 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001137 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001138 final int grandChildCount = layout.getChildCount();
1139 for (int j = 0; j < grandChildCount; ++j) {
1140 final View v = layout.getChildAt(j);
1141 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1142 return (Checkable) v;
1143 }
1144 }
1145 }
1146 }
1147 return null;
1148 }
1149
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001150 public Object getChosenItem() {
1151 View checkedView = (View) getSingleCheckedGrandchild();
1152 if (checkedView != null) {
1153 return checkedView.getTag();
1154 }
1155 return null;
1156 }
1157
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001158 protected void resetCheckedGrandchildren() {
1159 // loop through children, and set all of their children to _not_ be checked
1160 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1161 for (int i = 0; i < checked.size(); ++i) {
1162 final Checkable c = checked.get(i);
1163 c.setChecked(false);
1164 }
1165 }
1166
Winson Chung86f77532010-08-24 11:08:22 -07001167 /**
1168 * This method is called ONLY to synchronize the number of pages that the paged view has.
1169 * To actually fill the pages with information, implement syncPageItems() below. It is
1170 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1171 * and therefore, individual page items do not need to be updated in this method.
1172 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001174
1175 /**
1176 * This method is called to synchronize the items that are on a particular page. If views on
1177 * the page can be reused, then they should be updated within this method.
1178 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001180
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001182 if (mContentIsRefreshable) {
1183 // Update all the pages
1184 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001185
Michael Jurka0142d492010-08-25 17:46:15 -07001186 // Mark each of the pages as dirty
1187 final int count = getChildCount();
1188 mDirtyPageContent.clear();
1189 for (int i = 0; i < count; ++i) {
1190 mDirtyPageContent.add(true);
1191 }
1192
1193 // Load any pages that are necessary for the current window of views
1194 loadAssociatedPages(mCurrentPage);
1195 mDirtyPageAlpha = true;
1196 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001197 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001198 }
1199}