blob: 2cd7f206e5bfb86bf25282cafea970656db71b4f [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;
76
Michael Jurka0142d492010-08-25 17:46:15 -070077 protected final static int TOUCH_STATE_REST = 0;
78 protected final static int TOUCH_STATE_SCROLLING = 1;
79 protected final static int TOUCH_STATE_PREV_PAGE = 2;
80 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Michael Jurka0142d492010-08-25 17:46:15 -070082 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070083
Michael Jurka0142d492010-08-25 17:46:15 -070084 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070085
86 private boolean mAllowLongPress = true;
87
88 private int mTouchSlop;
89 private int mPagingTouchSlop;
90 private int mMaximumVelocity;
91
Michael Jurka5f1c5092010-09-03 14:15:02 -070092 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Winson Chung86f77532010-08-24 11:08:22 -070096 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Winson Chung86f77532010-08-24 11:08:22 -070098 private ArrayList<Boolean> mDirtyPageContent;
99 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700100
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700101 // choice modes
102 protected static final int CHOICE_MODE_NONE = 0;
103 protected static final int CHOICE_MODE_SINGLE = 1;
104 // Multiple selection mode is not supported by all Launcher actions atm
105 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700106
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700107 private int mChoiceMode;
108 private ActionMode mActionMode;
109
Winson Chung241c3b42010-08-25 16:53:03 -0700110 protected PagedViewIconCache mPageViewIconCache;
111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 // If true, syncPages and syncPageItems will be called to refresh pages
113 protected boolean mContentIsRefreshable = true;
114
115 // If true, modify alpha of neighboring pages as user scrolls left/right
116 protected boolean mFadeInAdjacentScreens = true;
117
118 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
119 // to switch to a new page
120 protected boolean mUsePagingTouchSlop = true;
121
122 // If true, the subclass should directly update mScrollX itself in its computeScroll method
123 // (SmoothPagedView does this)
124 protected boolean mDeferScrollUpdate = false;
125
Winson Chung241c3b42010-08-25 16:53:03 -0700126 /**
127 * Simple cache mechanism for PagedViewIcon outlines.
128 */
129 class PagedViewIconCache {
130 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
131
132 public void clear() {
133 iconOutlineCache.clear();
134 }
135 public void addOutline(Object key, Bitmap b) {
136 iconOutlineCache.put(key, b);
137 }
138 public void removeOutline(Object key) {
139 if (iconOutlineCache.containsKey(key)) {
140 iconOutlineCache.remove(key);
141 }
142 }
143 public Bitmap getOutline(Object key) {
144 return iconOutlineCache.get(key);
145 }
146 }
147
Winson Chung86f77532010-08-24 11:08:22 -0700148 public interface PageSwitchListener {
149 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700150 }
151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 public interface PageMovingListener {
153 void onPageBeginMoving();
154 void onPageEndMoving();
155 }
156
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 public PagedView(Context context) {
158 this(context, null);
159 }
160
161 public PagedView(Context context, AttributeSet attrs) {
162 this(context, attrs, 0);
163 }
164
165 public PagedView(Context context, AttributeSet attrs, int defStyle) {
166 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700167 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700168
169 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700170 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 }
172
173 /**
174 * Initializes various states for this workspace.
175 */
Michael Jurka0142d492010-08-25 17:46:15 -0700176 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700177 mDirtyPageContent = new ArrayList<Boolean>();
178 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700179 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700181 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700182
183 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
184 mTouchSlop = configuration.getScaledTouchSlop();
185 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
186 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
187 }
188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
190 mPageSwitchListener = pageSwitchListener;
191 if (mPageSwitchListener != null) {
192 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 }
194 }
195
196 /**
Winson Chung86f77532010-08-24 11:08:22 -0700197 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 *
Winson Chung86f77532010-08-24 11:08:22 -0700199 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 */
Winson Chung86f77532010-08-24 11:08:22 -0700201 int getCurrentPage() {
202 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 }
204
Winson Chung86f77532010-08-24 11:08:22 -0700205 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 return getChildCount();
207 }
208
Winson Chung86f77532010-08-24 11:08:22 -0700209 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 return getChildAt(index);
211 }
212
213 int getScrollWidth() {
214 return getWidth();
215 }
216
217 /**
Winson Chung86f77532010-08-24 11:08:22 -0700218 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 */
Winson Chung86f77532010-08-24 11:08:22 -0700220 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 if (!mScroller.isFinished()) mScroller.abortAnimation();
222 if (getChildCount() == 0) return;
223
Winson Chung86f77532010-08-24 11:08:22 -0700224 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
225 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700226
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700228 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230
Michael Jurka0142d492010-08-25 17:46:15 -0700231 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700232 if (mPageSwitchListener != null) {
233 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 }
235 }
236
Michael Jurka0142d492010-08-25 17:46:15 -0700237 // a method that subclasses can override to add behavior
238 protected void pageBeginMoving() {
239 }
240
241 // a method that subclasses can override to add behavior
242 protected void pageEndMoving() {
243 }
244
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 /**
Winson Chung86f77532010-08-24 11:08:22 -0700246 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 *
248 * @param l The listener used to respond to long clicks.
249 */
250 @Override
251 public void setOnLongClickListener(OnLongClickListener l) {
252 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700253 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700255 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 }
257 }
258
259 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700260 public void scrollTo(int x, int y) {
261 super.scrollTo(x, y);
262 mTouchX = x;
263 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
264 }
265
266 // we moved this functionality to a helper function so SmoothPagedView can reuse it
267 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700269 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700271 invalidate();
272 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700273 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700274 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700275 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700276 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700277 notifyPageSwitchListener();
278 pageEndMoving();
279 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 }
Michael Jurka0142d492010-08-25 17:46:15 -0700281 return false;
282 }
283
284 @Override
285 public void computeScroll() {
286 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 }
288
289 @Override
290 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
291 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
292 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
293 if (widthMode != MeasureSpec.EXACTLY) {
294 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
295 }
296
297 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
298 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
299 if (heightMode != MeasureSpec.EXACTLY) {
300 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
301 }
302
303 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700304 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 final int childCount = getChildCount();
306 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700307 // disallowing padding in paged view (just pass 0)
308 final View child = getChildAt(i);
309 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
310
311 int childWidthMode;
312 if (lp.width == LayoutParams.WRAP_CONTENT) {
313 childWidthMode = MeasureSpec.AT_MOST;
314 } else {
315 childWidthMode = MeasureSpec.EXACTLY;
316 }
317
318 int childHeightMode;
319 if (lp.height == LayoutParams.WRAP_CONTENT) {
320 childHeightMode = MeasureSpec.AT_MOST;
321 } else {
322 childHeightMode = MeasureSpec.EXACTLY;
323 }
324
325 final int childWidthMeasureSpec =
326 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
327 final int childHeightMeasureSpec =
328 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
329
330 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 }
332
333 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 }
335
336 @Override
337 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700338 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
339 setHorizontalScrollBarEnabled(false);
340 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
341 scrollTo(newX, 0);
342 mScroller.setFinalX(newX);
343 setHorizontalScrollBarEnabled(true);
344 mFirstLayout = false;
345 }
346
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 final int childCount = getChildCount();
348 int childLeft = 0;
349 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700350 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 }
352
353 for (int i = 0; i < childCount; i++) {
354 final View child = getChildAt(i);
355 if (child.getVisibility() != View.GONE) {
356 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700357 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
358 child.layout(childLeft, childHeight,
359 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 childLeft += childWidth;
361 }
362 }
363 }
364
Winson Chunge3193b92010-09-10 11:44:42 -0700365 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700366 if (mFadeInAdjacentScreens) {
367 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700368 int halfScreenSize = getMeasuredWidth() / 2;
369 int screenCenter = mScrollX + halfScreenSize;
370
Michael Jurka0142d492010-08-25 17:46:15 -0700371 final int childCount = getChildCount();
372 for (int i = 0; i < childCount; ++i) {
373 View layout = (View) getChildAt(i);
374 int childWidth = layout.getMeasuredWidth();
375 int halfChildWidth = (childWidth / 2);
376 int childCenter = getChildOffset(i) + halfChildWidth;
377 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
378 float alpha = 0.0f;
379 if (distanceFromScreenCenter < halfChildWidth) {
380 alpha = 1.0f;
381 } else if (distanceFromScreenCenter > childWidth) {
382 alpha = 0.0f;
383 } else {
384 float dimAlpha = (float) (distanceFromScreenCenter - halfChildWidth) / halfChildWidth;
385 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
386 alpha = 1.0f - dimAlpha;
387 }
388 if (Float.compare(alpha, layout.getAlpha()) != 0) {
389 layout.setAlpha(alpha);
390 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700391 }
Michael Jurka0142d492010-08-25 17:46:15 -0700392 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 }
394 }
Winson Chunge3193b92010-09-10 11:44:42 -0700395 }
396
397 @Override
398 protected void dispatchDraw(Canvas canvas) {
399 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -0700400
401 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700402 // As an optimization, this code assumes that all pages have the same width as the 0th
403 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700404 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700405 if (pageCount > 0) {
406 final int pageWidth = getChildAt(0).getMeasuredWidth();
407 final int screenWidth = getMeasuredWidth();
408 int x = getRelativeChildOffset(0) + pageWidth;
409 int leftScreen = 0;
410 int rightScreen = 0;
411 while (x <= mScrollX) {
412 leftScreen++;
413 x += pageWidth;
414 // replace above line with this if you don't assume all pages have same width as 0th
415 // page:
416 // x += getChildAt(leftScreen).getMeasuredWidth();
417 }
418 rightScreen = leftScreen;
419 while (x < mScrollX + screenWidth) {
420 rightScreen++;
421 x += pageWidth;
422 // replace above line with this if you don't assume all pages have same width as 0th
423 // page:
424 //if (rightScreen < pageCount) {
425 // x += getChildAt(rightScreen).getMeasuredWidth();
426 //}
427 }
428 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700429
Michael Jurkac4fb9172010-09-02 17:19:20 -0700430 final long drawingTime = getDrawingTime();
431 for (int i = leftScreen; i <= rightScreen; i++) {
432 drawChild(canvas, getChildAt(i), drawingTime);
433 }
Michael Jurka0142d492010-08-25 17:46:15 -0700434 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
436
437 @Override
438 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700439 int page = indexOfChild(child);
440 if (page != mCurrentPage || !mScroller.isFinished()) {
441 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 return true;
443 }
444 return false;
445 }
446
447 @Override
448 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700449 int focusablePage;
450 if (mNextPage != INVALID_PAGE) {
451 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700453 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
Winson Chung86f77532010-08-24 11:08:22 -0700455 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 if (v != null) {
457 v.requestFocus(direction, previouslyFocusedRect);
458 }
459 return false;
460 }
461
462 @Override
463 public boolean dispatchUnhandledMove(View focused, int direction) {
464 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700465 if (getCurrentPage() > 0) {
466 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700467 return true;
468 }
469 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700470 if (getCurrentPage() < getPageCount() - 1) {
471 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 return true;
473 }
474 }
475 return super.dispatchUnhandledMove(focused, direction);
476 }
477
478 @Override
479 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700480 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
481 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 }
483 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700484 if (mCurrentPage > 0) {
485 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
487 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700488 if (mCurrentPage < getPageCount() - 1) {
489 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 }
491 }
492 }
493
494 /**
495 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700496 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 *
Winson Chung86f77532010-08-24 11:08:22 -0700498 * This happens when live folders requery, and if they're off page, they
499 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 */
501 @Override
502 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700503 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700504 View v = focused;
505 while (true) {
506 if (v == current) {
507 super.focusableViewAvailable(focused);
508 return;
509 }
510 if (v == this) {
511 return;
512 }
513 ViewParent parent = v.getParent();
514 if (parent instanceof View) {
515 v = (View)v.getParent();
516 } else {
517 return;
518 }
519 }
520 }
521
522 /**
523 * {@inheritDoc}
524 */
525 @Override
526 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
527 if (disallowIntercept) {
528 // We need to make sure to cancel our long press if
529 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700530 final View currentPage = getChildAt(mCurrentPage);
531 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 }
533 super.requestDisallowInterceptTouchEvent(disallowIntercept);
534 }
535
536 @Override
537 public boolean onInterceptTouchEvent(MotionEvent ev) {
538 /*
539 * This method JUST determines whether we want to intercept the motion.
540 * If we return true, onTouchEvent will be called and we do the actual
541 * scrolling there.
542 */
543
544 /*
545 * Shortcut the most recurring case: the user is in the dragging
546 * state and he is moving his finger. We want to intercept this
547 * motion.
548 */
549 final int action = ev.getAction();
550 if ((action == MotionEvent.ACTION_MOVE) &&
551 (mTouchState == TOUCH_STATE_SCROLLING)) {
552 return true;
553 }
554
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 switch (action & MotionEvent.ACTION_MASK) {
556 case MotionEvent.ACTION_MOVE: {
557 /*
558 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
559 * whether the user has moved far enough from his original down touch.
560 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700561 if (mActivePointerId != INVALID_POINTER) {
562 determineScrollingStart(ev);
563 break;
564 }
565 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
566 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
567 // i.e. fall through to the next case (don't break)
568 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
569 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571
572 case MotionEvent.ACTION_DOWN: {
573 final float x = ev.getX();
574 final float y = ev.getY();
575 // Remember location of down touch
576 mDownMotionX = x;
577 mLastMotionX = x;
578 mLastMotionY = y;
579 mActivePointerId = ev.getPointerId(0);
580 mAllowLongPress = true;
581
582 /*
583 * If being flinged and user touches the screen, initiate drag;
584 * otherwise don't. mScroller.isFinished should be false when
585 * being flinged.
586 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700587 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
588 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
589 if (finishedScrolling) {
590 mTouchState = TOUCH_STATE_REST;
591 mScroller.abortAnimation();
592 } else {
593 mTouchState = TOUCH_STATE_SCROLLING;
594 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700595
Winson Chung86f77532010-08-24 11:08:22 -0700596 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 // to scroll the current page
598 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
599 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
600 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700601 int width = getMeasuredWidth();
602 int offset = getRelativeChildOffset(mCurrentPage);
603 if (x < offset) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 mTouchState = TOUCH_STATE_PREV_PAGE;
Winson Chunge3193b92010-09-10 11:44:42 -0700605 } else if (x > (width - offset)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700606 mTouchState = TOUCH_STATE_NEXT_PAGE;
607 }
608 }
609 }
610 break;
611 }
612
613 case MotionEvent.ACTION_CANCEL:
614 case MotionEvent.ACTION_UP:
615 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700616 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 mTouchState = TOUCH_STATE_REST;
618 mAllowLongPress = false;
619 mActivePointerId = INVALID_POINTER;
620
621 break;
622
623 case MotionEvent.ACTION_POINTER_UP:
624 onSecondaryPointerUp(ev);
625 break;
626 }
627
628 /*
629 * The only time we want to intercept motion events is if we are in the
630 * drag mode.
631 */
632 return mTouchState != TOUCH_STATE_REST;
633 }
634
Winson Chung80baf5a2010-08-09 16:03:15 -0700635 protected void animateClickFeedback(View v, final Runnable r) {
636 // animate the view slightly to show click feedback running some logic after it is "pressed"
637 Animation anim = AnimationUtils.loadAnimation(getContext(),
638 R.anim.paged_view_click_feedback);
639 anim.setAnimationListener(new AnimationListener() {
640 @Override
641 public void onAnimationStart(Animation animation) {}
642 @Override
643 public void onAnimationRepeat(Animation animation) {
644 r.run();
645 }
646 @Override
647 public void onAnimationEnd(Animation animation) {}
648 });
649 v.startAnimation(anim);
650 }
651
Winson Chung321e9ee2010-08-09 13:37:56 -0700652 /*
653 * Determines if we should change the touch state to start scrolling after the
654 * user moves their touch point too far.
655 */
656 private void determineScrollingStart(MotionEvent ev) {
657 /*
658 * Locally do absolute value. mLastMotionX is set to the y value
659 * of the down event.
660 */
661 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
662 final float x = ev.getX(pointerIndex);
663 final float y = ev.getY(pointerIndex);
664 final int xDiff = (int) Math.abs(x - mLastMotionX);
665 final int yDiff = (int) Math.abs(y - mLastMotionY);
666
667 final int touchSlop = mTouchSlop;
668 boolean xPaged = xDiff > mPagingTouchSlop;
669 boolean xMoved = xDiff > touchSlop;
670 boolean yMoved = yDiff > touchSlop;
671
672 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700673 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700674 // Scroll if the user moved far enough along the X axis
675 mTouchState = TOUCH_STATE_SCROLLING;
676 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700677 mTouchX = mScrollX;
678 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
679 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 }
681 // Either way, cancel any pending longpress
682 if (mAllowLongPress) {
683 mAllowLongPress = false;
684 // Try canceling the long press. It could also have been scheduled
685 // by a distant descendant, so use the mAllowLongPress flag to block
686 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700687 final View currentPage = getPageAt(mCurrentPage);
688 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 }
690 }
691 }
692
693 @Override
694 public boolean onTouchEvent(MotionEvent ev) {
695 if (mVelocityTracker == null) {
696 mVelocityTracker = VelocityTracker.obtain();
697 }
698 mVelocityTracker.addMovement(ev);
699
700 final int action = ev.getAction();
701
702 switch (action & MotionEvent.ACTION_MASK) {
703 case MotionEvent.ACTION_DOWN:
704 /*
705 * If being flinged and user touches, stop the fling. isFinished
706 * will be false if being flinged.
707 */
708 if (!mScroller.isFinished()) {
709 mScroller.abortAnimation();
710 }
711
712 // Remember where the motion event started
713 mDownMotionX = mLastMotionX = ev.getX();
714 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700715 if (mTouchState == TOUCH_STATE_SCROLLING) {
716 pageBeginMoving();
717 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 break;
719
720 case MotionEvent.ACTION_MOVE:
721 if (mTouchState == TOUCH_STATE_SCROLLING) {
722 // Scroll to follow the motion event
723 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
724 final float x = ev.getX(pointerIndex);
725 final int deltaX = (int) (mLastMotionX - x);
726 mLastMotionX = x;
727
728 int sx = getScrollX();
729 if (deltaX < 0) {
730 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700731 mTouchX += Math.max(-mTouchX, deltaX);
732 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
733 if (!mDeferScrollUpdate) {
734 scrollBy(Math.max(-sx, deltaX), 0);
735 } else {
736 // This will trigger a call to computeScroll() on next drawChild() call
737 invalidate();
738 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700739 }
740 } else if (deltaX > 0) {
741 final int lastChildIndex = getChildCount() - 1;
742 final int availableToScroll = getChildOffset(lastChildIndex) -
743 getRelativeChildOffset(lastChildIndex) - sx;
744 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700745 mTouchX += Math.min(availableToScroll, deltaX);
746 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
747 if (!mDeferScrollUpdate) {
748 scrollBy(Math.min(availableToScroll, deltaX), 0);
749 } else {
750 // This will trigger a call to computeScroll() on next drawChild() call
751 invalidate();
752 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 }
754 } else {
755 awakenScrollBars();
756 }
757 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
758 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
759 determineScrollingStart(ev);
760 }
761 break;
762
763 case MotionEvent.ACTION_UP:
764 if (mTouchState == TOUCH_STATE_SCROLLING) {
765 final int activePointerId = mActivePointerId;
766 final int pointerIndex = ev.findPointerIndex(activePointerId);
767 final float x = ev.getX(pointerIndex);
768 final VelocityTracker velocityTracker = mVelocityTracker;
769 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
770 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
771 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
772
Michael Jurka0142d492010-08-25 17:46:15 -0700773 final int snapVelocity = mSnapVelocity;
774 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
775 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
776 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700777 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700778 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 } else {
780 snapToDestination();
781 }
782
783 if (mVelocityTracker != null) {
784 mVelocityTracker.recycle();
785 mVelocityTracker = null;
786 }
787 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
788 // at this point we have not moved beyond the touch slop
789 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
790 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700791 int nextPage = Math.max(0, mCurrentPage - 1);
792 if (nextPage != mCurrentPage) {
793 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 } else {
795 snapToDestination();
796 }
797 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
798 // at this point we have not moved beyond the touch slop
799 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
800 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700801 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
802 if (nextPage != mCurrentPage) {
803 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 } else {
805 snapToDestination();
806 }
807 }
808 mTouchState = TOUCH_STATE_REST;
809 mActivePointerId = INVALID_POINTER;
810 break;
811
812 case MotionEvent.ACTION_CANCEL:
813 mTouchState = TOUCH_STATE_REST;
814 mActivePointerId = INVALID_POINTER;
815 break;
816
817 case MotionEvent.ACTION_POINTER_UP:
818 onSecondaryPointerUp(ev);
819 break;
820 }
821
822 return true;
823 }
824
825 private void onSecondaryPointerUp(MotionEvent ev) {
826 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
827 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
828 final int pointerId = ev.getPointerId(pointerIndex);
829 if (pointerId == mActivePointerId) {
830 // This was our active pointer going up. Choose a new
831 // active pointer and adjust accordingly.
832 // TODO: Make this decision more intelligent.
833 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
834 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
835 mLastMotionY = ev.getY(newPointerIndex);
836 mActivePointerId = ev.getPointerId(newPointerIndex);
837 if (mVelocityTracker != null) {
838 mVelocityTracker.clear();
839 }
840 }
841 }
842
843 @Override
844 public void requestChildFocus(View child, View focused) {
845 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700846 int page = indexOfChild(child);
847 if (page >= 0 && !isInTouchMode()) {
848 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 }
850 }
851
Winson Chunge3193b92010-09-10 11:44:42 -0700852 protected int getChildIndexForRelativeOffset(int relativeOffset) {
853 final int childCount = getChildCount();
854 int left = getRelativeChildOffset(0);
855 for (int i = 0; i < childCount; ++i) {
856 final int right = (left + getChildAt(i).getMeasuredWidth());
857 if (left <= relativeOffset && relativeOffset <= right) {
858 return i;
859 }
860 left = right;
861 }
862 return -1;
863 }
864
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 protected int getRelativeChildOffset(int index) {
866 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
867 }
868
869 protected int getChildOffset(int index) {
870 if (getChildCount() == 0)
871 return 0;
872
873 int offset = getRelativeChildOffset(0);
874 for (int i = 0; i < index; ++i) {
875 offset += getChildAt(i).getMeasuredWidth();
876 }
877 return offset;
878 }
879
Adam Cohend19d3ca2010-09-15 14:43:42 -0700880 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 int minDistanceFromScreenCenter = getMeasuredWidth();
882 int minDistanceFromScreenCenterIndex = -1;
883 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
884 final int childCount = getChildCount();
885 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700886 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 int childWidth = layout.getMeasuredWidth();
888 int halfChildWidth = (childWidth / 2);
889 int childCenter = getChildOffset(i) + halfChildWidth;
890 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
891 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
892 minDistanceFromScreenCenter = distanceFromScreenCenter;
893 minDistanceFromScreenCenterIndex = i;
894 }
895 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700896 return minDistanceFromScreenCenterIndex;
897 }
898
899 protected void snapToDestination() {
900 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 }
902
Michael Jurka0142d492010-08-25 17:46:15 -0700903 protected void snapToPageWithVelocity(int whichPage, int velocity) {
904 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
905 // can use it
906 snapToPage(whichPage);
907 }
908
909 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700910 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 }
912
Michael Jurka0142d492010-08-25 17:46:15 -0700913 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700914 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700915
Winson Chung86f77532010-08-24 11:08:22 -0700916 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 final int sX = getScrollX();
918 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700919 snapToPage(whichPage, delta, duration);
920 }
921
922 protected void snapToPage(int whichPage, int delta, int duration) {
923 mNextPage = whichPage;
924
925 View focusedChild = getFocusedChild();
926 if (focusedChild != null && whichPage != mCurrentPage &&
927 focusedChild == getChildAt(mCurrentPage)) {
928 focusedChild.clearFocus();
929 }
930
931 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 awakenScrollBars(duration);
933 if (duration == 0) {
934 duration = Math.abs(delta);
935 }
936
937 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700938 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700939
940 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700941 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700942 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 invalidate();
944 }
945
946 @Override
947 protected Parcelable onSaveInstanceState() {
948 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700949 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 return state;
951 }
952
953 @Override
954 protected void onRestoreInstanceState(Parcelable state) {
955 SavedState savedState = (SavedState) state;
956 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -0700957 if (savedState.currentPage != -1) {
958 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 }
960 }
961
962 public void scrollLeft() {
963 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700964 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700966 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 }
968 }
969
970 public void scrollRight() {
971 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -0700972 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700974 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 }
976 }
977
Winson Chung86f77532010-08-24 11:08:22 -0700978 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 int result = -1;
980 if (v != null) {
981 ViewParent vp = v.getParent();
982 int count = getChildCount();
983 for (int i = 0; i < count; i++) {
984 if (vp == getChildAt(i)) {
985 return i;
986 }
987 }
988 }
989 return result;
990 }
991
992 /**
993 * @return True is long presses are still allowed for the current touch
994 */
995 public boolean allowLongPress() {
996 return mAllowLongPress;
997 }
998
Michael Jurka0142d492010-08-25 17:46:15 -0700999 /**
1000 * Set true to allow long-press events to be triggered, usually checked by
1001 * {@link Launcher} to accept or block dpad-initiated long-presses.
1002 */
1003 public void setAllowLongPress(boolean allowLongPress) {
1004 mAllowLongPress = allowLongPress;
1005 }
1006
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001008 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001009
1010 SavedState(Parcelable superState) {
1011 super(superState);
1012 }
1013
1014 private SavedState(Parcel in) {
1015 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001016 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 }
1018
1019 @Override
1020 public void writeToParcel(Parcel out, int flags) {
1021 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001022 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 }
1024
1025 public static final Parcelable.Creator<SavedState> CREATOR =
1026 new Parcelable.Creator<SavedState>() {
1027 public SavedState createFromParcel(Parcel in) {
1028 return new SavedState(in);
1029 }
1030
1031 public SavedState[] newArray(int size) {
1032 return new SavedState[size];
1033 }
1034 };
1035 }
1036
Winson Chung86f77532010-08-24 11:08:22 -07001037 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001038 if (mContentIsRefreshable) {
1039 final int count = getChildCount();
1040 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001041 int lowerPageBound = getAssociatedLowerPageBound(page);
1042 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001043 for (int i = 0; i < count; ++i) {
1044 final ViewGroup layout = (ViewGroup) getChildAt(i);
1045 final int childCount = layout.getChildCount();
1046 if (lowerPageBound <= i && i <= upperPageBound) {
1047 if (mDirtyPageContent.get(i)) {
1048 syncPageItems(i);
1049 mDirtyPageContent.set(i, false);
1050 }
1051 } else {
1052 if (childCount > 0) {
1053 layout.removeAllViews();
1054 }
1055 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001056 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001057 }
1058 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001059 }
1060 }
1061
Winson Chunge3193b92010-09-10 11:44:42 -07001062 protected int getAssociatedLowerPageBound(int page) {
1063 return Math.max(0, page - 1);
1064 }
1065 protected int getAssociatedUpperPageBound(int page) {
1066 final int count = getChildCount();
1067 return Math.min(page + 1, count - 1);
1068 }
1069
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001070 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001071 if (isChoiceMode(CHOICE_MODE_NONE)) {
1072 mChoiceMode = mode;
1073 mActionMode = startActionMode(callback);
1074 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001075 }
1076
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001077 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001078 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001079 mChoiceMode = CHOICE_MODE_NONE;
1080 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001081 mActionMode.finish();
1082 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001083 }
1084 }
1085
1086 protected boolean isChoiceMode(int mode) {
1087 return mChoiceMode == mode;
1088 }
1089
1090 protected ArrayList<Checkable> getCheckedGrandchildren() {
1091 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1092 final int childCount = getChildCount();
1093 for (int i = 0; i < childCount; ++i) {
1094 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1095 final int grandChildCount = layout.getChildCount();
1096 for (int j = 0; j < grandChildCount; ++j) {
1097 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001098 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001099 checked.add((Checkable) v);
1100 }
1101 }
1102 }
1103 return checked;
1104 }
1105
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001106 /**
1107 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1108 * Otherwise, returns null.
1109 */
1110 protected Checkable getSingleCheckedGrandchild() {
1111 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1112 final int childCount = getChildCount();
1113 for (int i = 0; i < childCount; ++i) {
1114 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1115 final int grandChildCount = layout.getChildCount();
1116 for (int j = 0; j < grandChildCount; ++j) {
1117 final View v = layout.getChildAt(j);
1118 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1119 return (Checkable) v;
1120 }
1121 }
1122 }
1123 }
1124 return null;
1125 }
1126
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001127 public Object getChosenItem() {
1128 View checkedView = (View) getSingleCheckedGrandchild();
1129 if (checkedView != null) {
1130 return checkedView.getTag();
1131 }
1132 return null;
1133 }
1134
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001135 protected void resetCheckedGrandchildren() {
1136 // loop through children, and set all of their children to _not_ be checked
1137 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1138 for (int i = 0; i < checked.size(); ++i) {
1139 final Checkable c = checked.get(i);
1140 c.setChecked(false);
1141 }
1142 }
1143
Winson Chung86f77532010-08-24 11:08:22 -07001144 /**
1145 * This method is called ONLY to synchronize the number of pages that the paged view has.
1146 * To actually fill the pages with information, implement syncPageItems() below. It is
1147 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1148 * and therefore, individual page items do not need to be updated in this method.
1149 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001151
1152 /**
1153 * This method is called to synchronize the items that are on a particular page. If views on
1154 * the page can be reused, then they should be updated within this method.
1155 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001157
Winson Chung321e9ee2010-08-09 13:37:56 -07001158 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001159 if (mContentIsRefreshable) {
1160 // Update all the pages
1161 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001162
Michael Jurka0142d492010-08-25 17:46:15 -07001163 // Mark each of the pages as dirty
1164 final int count = getChildCount();
1165 mDirtyPageContent.clear();
1166 for (int i = 0; i < count; ++i) {
1167 mDirtyPageContent.add(true);
1168 }
1169
1170 // Load any pages that are necessary for the current window of views
1171 loadAssociatedPages(mCurrentPage);
1172 mDirtyPageAlpha = true;
1173 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001174 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 }
1176}