blob: 732bfbd9b9cb1a9e2bd5937b7f4bc124f950d09e [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;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung241c3b42010-08-25 16:53:03 -070024import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chunge3193b92010-09-10 11:44:42 -070030import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070031import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.MotionEvent;
33import android.view.VelocityTracker;
34import android.view.View;
35import android.view.ViewConfiguration;
36import android.view.ViewGroup;
37import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.view.animation.Animation;
Michael Jurka5f1c5092010-09-03 14:15:02 -070039import android.view.animation.Animation.AnimationListener;
Winson Chunge3193b92010-09-10 11:44:42 -070040import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070041import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070042import android.widget.LinearLayout;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chunge3193b92010-09-10 11:44:42 -070045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung321e9ee2010-08-09 13:37:56 -070047/**
48 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070049 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070050 */
51public abstract class PagedView extends ViewGroup {
52 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070053 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Winson Chung86f77532010-08-24 11:08:22 -070055 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung321e9ee2010-08-09 13:37:56 -070056 private static final int MIN_LENGTH_FOR_FLING = 50;
57
Winson Chung5f2aa4e2010-08-20 14:49:25 -070058 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Michael Jurka0142d492010-08-25 17:46:15 -070061 // the velocity at which a fling gesture will cause us to snap to the next page
62 protected int mSnapVelocity = 500;
63
64 protected float mSmoothingTime;
65 protected float mTouchX;
66
67 protected boolean mFirstLayout = true;
68
69 protected int mCurrentPage;
70 protected int mNextPage = INVALID_PAGE;
71 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private VelocityTracker mVelocityTracker;
73
74 private float mDownMotionX;
75 private float mLastMotionX;
76 private float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070077 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070078
Michael Jurka0142d492010-08-25 17:46:15 -070079 protected final static int TOUCH_STATE_REST = 0;
80 protected final static int TOUCH_STATE_SCROLLING = 1;
81 protected final static int TOUCH_STATE_PREV_PAGE = 2;
82 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohenf34bab52010-09-30 14:11:56 -070083 protected final static float ALPHA_QUANTIZE_LEVEL = 0.01f;
Winson Chung321e9ee2010-08-09 13:37:56 -070084
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070086
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070088
89 private boolean mAllowLongPress = true;
90
91 private int mTouchSlop;
92 private int mPagingTouchSlop;
93 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070094 protected int mPageSpacing;
95 protected int mPageLayoutPaddingTop;
96 protected int mPageLayoutPaddingBottom;
97 protected int mPageLayoutPaddingLeft;
98 protected int mPageLayoutPaddingRight;
99 protected int mCellCountX;
100 protected int mCellCountY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka5f1c5092010-09-03 14:15:02 -0700102 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Winson Chung86f77532010-08-24 11:08:22 -0700106 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Winson Chung86f77532010-08-24 11:08:22 -0700108 private ArrayList<Boolean> mDirtyPageContent;
109 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700111 // choice modes
112 protected static final int CHOICE_MODE_NONE = 0;
113 protected static final int CHOICE_MODE_SINGLE = 1;
114 // Multiple selection mode is not supported by all Launcher actions atm
115 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700116
Michael Jurkae17e19c2010-09-28 11:01:39 -0700117 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700118 private ActionMode mActionMode;
119
Winson Chung241c3b42010-08-25 16:53:03 -0700120 protected PagedViewIconCache mPageViewIconCache;
121
Michael Jurka0142d492010-08-25 17:46:15 -0700122 // If true, syncPages and syncPageItems will be called to refresh pages
123 protected boolean mContentIsRefreshable = true;
124
125 // If true, modify alpha of neighboring pages as user scrolls left/right
126 protected boolean mFadeInAdjacentScreens = true;
127
128 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
129 // to switch to a new page
130 protected boolean mUsePagingTouchSlop = true;
131
132 // If true, the subclass should directly update mScrollX itself in its computeScroll method
133 // (SmoothPagedView does this)
134 protected boolean mDeferScrollUpdate = false;
135
Winson Chung241c3b42010-08-25 16:53:03 -0700136 /**
137 * Simple cache mechanism for PagedViewIcon outlines.
138 */
139 class PagedViewIconCache {
140 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
141
142 public void clear() {
143 iconOutlineCache.clear();
144 }
145 public void addOutline(Object key, Bitmap b) {
146 iconOutlineCache.put(key, b);
147 }
148 public void removeOutline(Object key) {
149 if (iconOutlineCache.containsKey(key)) {
150 iconOutlineCache.remove(key);
151 }
152 }
153 public Bitmap getOutline(Object key) {
154 return iconOutlineCache.get(key);
155 }
156 }
157
Winson Chung86f77532010-08-24 11:08:22 -0700158 public interface PageSwitchListener {
159 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 }
161
Michael Jurka0142d492010-08-25 17:46:15 -0700162 public interface PageMovingListener {
163 void onPageBeginMoving();
164 void onPageEndMoving();
165 }
166
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 public PagedView(Context context) {
168 this(context, null);
169 }
170
171 public PagedView(Context context, AttributeSet attrs) {
172 this(context, attrs, 0);
173 }
174
175 public PagedView(Context context, AttributeSet attrs, int defStyle) {
176 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700177 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700178
Adam Cohen9c4949e2010-10-05 12:27:22 -0700179 TypedArray a = context.obtainStyledAttributes(attrs,
180 R.styleable.PagedView, defStyle, 0);
181 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
182 mPageLayoutPaddingTop = a.getDimensionPixelSize(
183 R.styleable.PagedView_pageLayoutPaddingTop, 10);
184 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
185 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
186 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
187 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
188 mPageLayoutPaddingRight = a.getDimensionPixelSize(
189 R.styleable.PagedView_pageLayoutPaddingRight, 10);
190 a.recycle();
191
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700193 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 }
195
196 /**
197 * Initializes various states for this workspace.
198 */
Michael Jurka0142d492010-08-25 17:46:15 -0700199 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700200 mDirtyPageContent = new ArrayList<Boolean>();
201 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700202 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700204 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700205
206 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
207 mTouchSlop = configuration.getScaledTouchSlop();
208 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
209 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
210 }
211
Winson Chung86f77532010-08-24 11:08:22 -0700212 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
213 mPageSwitchListener = pageSwitchListener;
214 if (mPageSwitchListener != null) {
215 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 }
217 }
218
219 /**
Winson Chung86f77532010-08-24 11:08:22 -0700220 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 *
Winson Chung86f77532010-08-24 11:08:22 -0700222 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 */
Winson Chung86f77532010-08-24 11:08:22 -0700224 int getCurrentPage() {
225 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227
Winson Chung86f77532010-08-24 11:08:22 -0700228 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 return getChildCount();
230 }
231
Winson Chung86f77532010-08-24 11:08:22 -0700232 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 return getChildAt(index);
234 }
235
236 int getScrollWidth() {
237 return getWidth();
238 }
239
240 /**
Winson Chung86f77532010-08-24 11:08:22 -0700241 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 */
Winson Chung86f77532010-08-24 11:08:22 -0700243 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 if (!mScroller.isFinished()) mScroller.abortAnimation();
245 if (getChildCount() == 0) return;
246
Winson Chung86f77532010-08-24 11:08:22 -0700247 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
248 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700249
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700251 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 }
253
Michael Jurka0142d492010-08-25 17:46:15 -0700254 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700255 if (mPageSwitchListener != null) {
256 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 }
258 }
259
Michael Jurka0142d492010-08-25 17:46:15 -0700260 // a method that subclasses can override to add behavior
261 protected void pageBeginMoving() {
262 }
263
264 // a method that subclasses can override to add behavior
265 protected void pageEndMoving() {
266 }
267
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 /**
Winson Chung86f77532010-08-24 11:08:22 -0700269 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 *
271 * @param l The listener used to respond to long clicks.
272 */
273 @Override
274 public void setOnLongClickListener(OnLongClickListener l) {
275 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700276 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700278 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 }
280 }
281
282 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700283 public void scrollTo(int x, int y) {
284 super.scrollTo(x, y);
285 mTouchX = x;
286 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
287 }
288
289 // we moved this functionality to a helper function so SmoothPagedView can reuse it
290 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700292 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700294 invalidate();
295 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700296 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700297 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700298 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700299 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700300 notifyPageSwitchListener();
301 pageEndMoving();
302 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
Michael Jurka0142d492010-08-25 17:46:15 -0700304 return false;
305 }
306
307 @Override
308 public void computeScroll() {
309 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 }
311
312 @Override
313 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
314 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
315 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
316 if (widthMode != MeasureSpec.EXACTLY) {
317 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
318 }
319
320 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
321 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
322 if (heightMode != MeasureSpec.EXACTLY) {
323 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
324 }
325
326 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700327 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 final int childCount = getChildCount();
329 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700330 // disallowing padding in paged view (just pass 0)
331 final View child = getChildAt(i);
332 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
333
334 int childWidthMode;
335 if (lp.width == LayoutParams.WRAP_CONTENT) {
336 childWidthMode = MeasureSpec.AT_MOST;
337 } else {
338 childWidthMode = MeasureSpec.EXACTLY;
339 }
340
341 int childHeightMode;
342 if (lp.height == LayoutParams.WRAP_CONTENT) {
343 childHeightMode = MeasureSpec.AT_MOST;
344 } else {
345 childHeightMode = MeasureSpec.EXACTLY;
346 }
347
348 final int childWidthMeasureSpec =
349 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
350 final int childHeightMeasureSpec =
351 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
352
353 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 }
355
356 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 }
358
359 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700360 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700361 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
362 setHorizontalScrollBarEnabled(false);
363 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
364 scrollTo(newX, 0);
365 mScroller.setFinalX(newX);
366 setHorizontalScrollBarEnabled(true);
367 mFirstLayout = false;
368 }
369
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 final int childCount = getChildCount();
371 int childLeft = 0;
372 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700373 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 }
375
376 for (int i = 0; i < childCount; i++) {
377 final View child = getChildAt(i);
378 if (child.getVisibility() != View.GONE) {
379 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700380 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
381 child.layout(childLeft, childHeight,
382 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700383 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 }
385 }
386 }
387
Winson Chunge3193b92010-09-10 11:44:42 -0700388 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700389 if (mFadeInAdjacentScreens) {
390 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700391 int halfScreenSize = getMeasuredWidth() / 2;
392 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700393 final int childCount = getChildCount();
394 for (int i = 0; i < childCount; ++i) {
395 View layout = (View) getChildAt(i);
396 int childWidth = layout.getMeasuredWidth();
397 int halfChildWidth = (childWidth / 2);
398 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700399
400 int d = halfChildWidth;
401 int distanceFromScreenCenter = childCenter - screenCenter;
402 if (distanceFromScreenCenter > 0) {
403 if (i > 0) {
404 d += getChildAt(i - 1).getMeasuredWidth() / 2;
405 }
Michael Jurka0142d492010-08-25 17:46:15 -0700406 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700407 if (i < childCount - 1) {
408 d += getChildAt(i + 1).getMeasuredWidth() / 2;
409 }
Michael Jurka0142d492010-08-25 17:46:15 -0700410 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700411 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700412
413 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
414 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
415 float alpha = 1.0f - dimAlpha;
416
Adam Cohenf34bab52010-09-30 14:11:56 -0700417 if (alpha < ALPHA_QUANTIZE_LEVEL) {
418 alpha = 0.0f;
419 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
420 alpha = 1.0f;
421 }
422
Michael Jurka0142d492010-08-25 17:46:15 -0700423 if (Float.compare(alpha, layout.getAlpha()) != 0) {
424 layout.setAlpha(alpha);
425 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 }
Michael Jurka0142d492010-08-25 17:46:15 -0700427 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
429 }
Winson Chunge3193b92010-09-10 11:44:42 -0700430 }
431
Adam Cohenf34bab52010-09-30 14:11:56 -0700432 protected void screenScrolled(int screenCenter) {
433 }
434
Winson Chunge3193b92010-09-10 11:44:42 -0700435 @Override
436 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 int halfScreenSize = getMeasuredWidth() / 2;
438 int screenCenter = mScrollX + halfScreenSize;
439
440 if (screenCenter != mLastScreenCenter) {
441 screenScrolled(screenCenter);
442 updateAdjacentPagesAlpha();
443 mLastScreenCenter = screenCenter;
444 }
Michael Jurka0142d492010-08-25 17:46:15 -0700445
446 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700447 // As an optimization, this code assumes that all pages have the same width as the 0th
448 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700449 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700450 if (pageCount > 0) {
451 final int pageWidth = getChildAt(0).getMeasuredWidth();
452 final int screenWidth = getMeasuredWidth();
453 int x = getRelativeChildOffset(0) + pageWidth;
454 int leftScreen = 0;
455 int rightScreen = 0;
456 while (x <= mScrollX) {
457 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700458 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700459 // replace above line with this if you don't assume all pages have same width as 0th
460 // page:
461 // x += getChildAt(leftScreen).getMeasuredWidth();
462 }
463 rightScreen = leftScreen;
464 while (x < mScrollX + screenWidth) {
465 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700466 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700467 // replace above line with this if you don't assume all pages have same width as 0th
468 // page:
469 //if (rightScreen < pageCount) {
470 // x += getChildAt(rightScreen).getMeasuredWidth();
471 //}
472 }
473 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700474
Michael Jurkac4fb9172010-09-02 17:19:20 -0700475 final long drawingTime = getDrawingTime();
476 for (int i = leftScreen; i <= rightScreen; i++) {
477 drawChild(canvas, getChildAt(i), drawingTime);
478 }
Michael Jurka0142d492010-08-25 17:46:15 -0700479 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 }
481
482 @Override
483 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700484 int page = indexOfChild(child);
485 if (page != mCurrentPage || !mScroller.isFinished()) {
486 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 return true;
488 }
489 return false;
490 }
491
492 @Override
493 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700494 int focusablePage;
495 if (mNextPage != INVALID_PAGE) {
496 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700498 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 }
Winson Chung86f77532010-08-24 11:08:22 -0700500 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 if (v != null) {
502 v.requestFocus(direction, previouslyFocusedRect);
503 }
504 return false;
505 }
506
507 @Override
508 public boolean dispatchUnhandledMove(View focused, int direction) {
509 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700510 if (getCurrentPage() > 0) {
511 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 return true;
513 }
514 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700515 if (getCurrentPage() < getPageCount() - 1) {
516 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 return true;
518 }
519 }
520 return super.dispatchUnhandledMove(focused, direction);
521 }
522
523 @Override
524 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700525 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
526 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 }
528 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700529 if (mCurrentPage > 0) {
530 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700531 }
532 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700533 if (mCurrentPage < getPageCount() - 1) {
534 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 }
536 }
537 }
538
539 /**
540 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700541 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700542 *
Winson Chung86f77532010-08-24 11:08:22 -0700543 * This happens when live folders requery, and if they're off page, they
544 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 */
546 @Override
547 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700548 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 View v = focused;
550 while (true) {
551 if (v == current) {
552 super.focusableViewAvailable(focused);
553 return;
554 }
555 if (v == this) {
556 return;
557 }
558 ViewParent parent = v.getParent();
559 if (parent instanceof View) {
560 v = (View)v.getParent();
561 } else {
562 return;
563 }
564 }
565 }
566
567 /**
568 * {@inheritDoc}
569 */
570 @Override
571 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
572 if (disallowIntercept) {
573 // We need to make sure to cancel our long press if
574 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700575 final View currentPage = getChildAt(mCurrentPage);
576 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 }
578 super.requestDisallowInterceptTouchEvent(disallowIntercept);
579 }
580
581 @Override
582 public boolean onInterceptTouchEvent(MotionEvent ev) {
583 /*
584 * This method JUST determines whether we want to intercept the motion.
585 * If we return true, onTouchEvent will be called and we do the actual
586 * scrolling there.
587 */
588
589 /*
590 * Shortcut the most recurring case: the user is in the dragging
591 * state and he is moving his finger. We want to intercept this
592 * motion.
593 */
594 final int action = ev.getAction();
595 if ((action == MotionEvent.ACTION_MOVE) &&
596 (mTouchState == TOUCH_STATE_SCROLLING)) {
597 return true;
598 }
599
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 switch (action & MotionEvent.ACTION_MASK) {
601 case MotionEvent.ACTION_MOVE: {
602 /*
603 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
604 * whether the user has moved far enough from his original down touch.
605 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700606 if (mActivePointerId != INVALID_POINTER) {
607 determineScrollingStart(ev);
608 break;
609 }
610 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
611 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
612 // i.e. fall through to the next case (don't break)
613 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
614 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 }
616
617 case MotionEvent.ACTION_DOWN: {
618 final float x = ev.getX();
619 final float y = ev.getY();
620 // Remember location of down touch
621 mDownMotionX = x;
622 mLastMotionX = x;
623 mLastMotionY = y;
624 mActivePointerId = ev.getPointerId(0);
625 mAllowLongPress = true;
626
627 /*
628 * If being flinged and user touches the screen, initiate drag;
629 * otherwise don't. mScroller.isFinished should be false when
630 * being flinged.
631 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700632 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
633 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
634 if (finishedScrolling) {
635 mTouchState = TOUCH_STATE_REST;
636 mScroller.abortAnimation();
637 } else {
638 mTouchState = TOUCH_STATE_SCROLLING;
639 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700640
Winson Chung86f77532010-08-24 11:08:22 -0700641 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700642 // to scroll the current page
643 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
644 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
645 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700646 int width = getMeasuredWidth();
647 int offset = getRelativeChildOffset(mCurrentPage);
648 if (x < offset) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 mTouchState = TOUCH_STATE_PREV_PAGE;
Winson Chunge3193b92010-09-10 11:44:42 -0700650 } else if (x > (width - offset)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 mTouchState = TOUCH_STATE_NEXT_PAGE;
652 }
653 }
654 }
655 break;
656 }
657
658 case MotionEvent.ACTION_CANCEL:
659 case MotionEvent.ACTION_UP:
660 // Release the drag
Michael Jurka0142d492010-08-25 17:46:15 -0700661 pageEndMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 mTouchState = TOUCH_STATE_REST;
663 mAllowLongPress = false;
664 mActivePointerId = INVALID_POINTER;
665
666 break;
667
668 case MotionEvent.ACTION_POINTER_UP:
669 onSecondaryPointerUp(ev);
670 break;
671 }
672
673 /*
674 * The only time we want to intercept motion events is if we are in the
675 * drag mode.
676 */
677 return mTouchState != TOUCH_STATE_REST;
678 }
679
Winson Chung80baf5a2010-08-09 16:03:15 -0700680 protected void animateClickFeedback(View v, final Runnable r) {
681 // animate the view slightly to show click feedback running some logic after it is "pressed"
682 Animation anim = AnimationUtils.loadAnimation(getContext(),
683 R.anim.paged_view_click_feedback);
684 anim.setAnimationListener(new AnimationListener() {
685 @Override
686 public void onAnimationStart(Animation animation) {}
687 @Override
688 public void onAnimationRepeat(Animation animation) {
689 r.run();
690 }
691 @Override
692 public void onAnimationEnd(Animation animation) {}
693 });
694 v.startAnimation(anim);
695 }
696
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 /*
698 * Determines if we should change the touch state to start scrolling after the
699 * user moves their touch point too far.
700 */
701 private void determineScrollingStart(MotionEvent ev) {
702 /*
703 * Locally do absolute value. mLastMotionX is set to the y value
704 * of the down event.
705 */
706 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
707 final float x = ev.getX(pointerIndex);
708 final float y = ev.getY(pointerIndex);
709 final int xDiff = (int) Math.abs(x - mLastMotionX);
710 final int yDiff = (int) Math.abs(y - mLastMotionY);
711
712 final int touchSlop = mTouchSlop;
713 boolean xPaged = xDiff > mPagingTouchSlop;
714 boolean xMoved = xDiff > touchSlop;
715 boolean yMoved = yDiff > touchSlop;
716
717 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700718 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700719 // Scroll if the user moved far enough along the X axis
720 mTouchState = TOUCH_STATE_SCROLLING;
721 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700722 mTouchX = mScrollX;
723 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
724 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 }
726 // Either way, cancel any pending longpress
727 if (mAllowLongPress) {
728 mAllowLongPress = false;
729 // Try canceling the long press. It could also have been scheduled
730 // by a distant descendant, so use the mAllowLongPress flag to block
731 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700732 final View currentPage = getPageAt(mCurrentPage);
733 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 }
735 }
736 }
737
738 @Override
739 public boolean onTouchEvent(MotionEvent ev) {
740 if (mVelocityTracker == null) {
741 mVelocityTracker = VelocityTracker.obtain();
742 }
743 mVelocityTracker.addMovement(ev);
744
745 final int action = ev.getAction();
746
747 switch (action & MotionEvent.ACTION_MASK) {
748 case MotionEvent.ACTION_DOWN:
749 /*
750 * If being flinged and user touches, stop the fling. isFinished
751 * will be false if being flinged.
752 */
753 if (!mScroller.isFinished()) {
754 mScroller.abortAnimation();
755 }
756
757 // Remember where the motion event started
758 mDownMotionX = mLastMotionX = ev.getX();
759 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700760 if (mTouchState == TOUCH_STATE_SCROLLING) {
761 pageBeginMoving();
762 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 break;
764
765 case MotionEvent.ACTION_MOVE:
766 if (mTouchState == TOUCH_STATE_SCROLLING) {
767 // Scroll to follow the motion event
768 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
769 final float x = ev.getX(pointerIndex);
770 final int deltaX = (int) (mLastMotionX - x);
771 mLastMotionX = x;
772
773 int sx = getScrollX();
774 if (deltaX < 0) {
775 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700776 mTouchX += Math.max(-mTouchX, deltaX);
777 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
778 if (!mDeferScrollUpdate) {
779 scrollBy(Math.max(-sx, deltaX), 0);
780 } else {
781 // This will trigger a call to computeScroll() on next drawChild() call
782 invalidate();
783 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 }
785 } else if (deltaX > 0) {
786 final int lastChildIndex = getChildCount() - 1;
787 final int availableToScroll = getChildOffset(lastChildIndex) -
788 getRelativeChildOffset(lastChildIndex) - sx;
789 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700790 mTouchX += Math.min(availableToScroll, deltaX);
791 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
792 if (!mDeferScrollUpdate) {
793 scrollBy(Math.min(availableToScroll, deltaX), 0);
794 } else {
795 // This will trigger a call to computeScroll() on next drawChild() call
796 invalidate();
797 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 }
799 } else {
800 awakenScrollBars();
801 }
802 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
803 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
804 determineScrollingStart(ev);
805 }
806 break;
807
808 case MotionEvent.ACTION_UP:
809 if (mTouchState == TOUCH_STATE_SCROLLING) {
810 final int activePointerId = mActivePointerId;
811 final int pointerIndex = ev.findPointerIndex(activePointerId);
812 final float x = ev.getX(pointerIndex);
813 final VelocityTracker velocityTracker = mVelocityTracker;
814 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
815 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
816 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
817
Michael Jurka0142d492010-08-25 17:46:15 -0700818 final int snapVelocity = mSnapVelocity;
819 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
820 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
821 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700822 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700823 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 } else {
825 snapToDestination();
826 }
827
828 if (mVelocityTracker != null) {
829 mVelocityTracker.recycle();
830 mVelocityTracker = null;
831 }
832 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
833 // at this point we have not moved beyond the touch slop
834 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
835 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700836 int nextPage = Math.max(0, mCurrentPage - 1);
837 if (nextPage != mCurrentPage) {
838 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 } else {
840 snapToDestination();
841 }
842 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
843 // at this point we have not moved beyond the touch slop
844 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
845 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700846 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
847 if (nextPage != mCurrentPage) {
848 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 } else {
850 snapToDestination();
851 }
852 }
853 mTouchState = TOUCH_STATE_REST;
854 mActivePointerId = INVALID_POINTER;
855 break;
856
857 case MotionEvent.ACTION_CANCEL:
858 mTouchState = TOUCH_STATE_REST;
859 mActivePointerId = INVALID_POINTER;
860 break;
861
862 case MotionEvent.ACTION_POINTER_UP:
863 onSecondaryPointerUp(ev);
864 break;
865 }
866
867 return true;
868 }
869
870 private void onSecondaryPointerUp(MotionEvent ev) {
871 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
872 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
873 final int pointerId = ev.getPointerId(pointerIndex);
874 if (pointerId == mActivePointerId) {
875 // This was our active pointer going up. Choose a new
876 // active pointer and adjust accordingly.
877 // TODO: Make this decision more intelligent.
878 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
879 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
880 mLastMotionY = ev.getY(newPointerIndex);
881 mActivePointerId = ev.getPointerId(newPointerIndex);
882 if (mVelocityTracker != null) {
883 mVelocityTracker.clear();
884 }
885 }
886 }
887
888 @Override
889 public void requestChildFocus(View child, View focused) {
890 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700891 int page = indexOfChild(child);
892 if (page >= 0 && !isInTouchMode()) {
893 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 }
895 }
896
Winson Chunge3193b92010-09-10 11:44:42 -0700897 protected int getChildIndexForRelativeOffset(int relativeOffset) {
898 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700899 int left;
900 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700901 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700902 left = getRelativeChildOffset(i);
903 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700904 if (left <= relativeOffset && relativeOffset <= right) {
905 return i;
906 }
Winson Chunge3193b92010-09-10 11:44:42 -0700907 }
908 return -1;
909 }
910
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 protected int getRelativeChildOffset(int index) {
912 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
913 }
914
915 protected int getChildOffset(int index) {
916 if (getChildCount() == 0)
917 return 0;
918
919 int offset = getRelativeChildOffset(0);
920 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700921 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 }
923 return offset;
924 }
925
Adam Cohend19d3ca2010-09-15 14:43:42 -0700926 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 int minDistanceFromScreenCenter = getMeasuredWidth();
928 int minDistanceFromScreenCenterIndex = -1;
929 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
930 final int childCount = getChildCount();
931 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700932 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 int childWidth = layout.getMeasuredWidth();
934 int halfChildWidth = (childWidth / 2);
935 int childCenter = getChildOffset(i) + halfChildWidth;
936 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
937 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
938 minDistanceFromScreenCenter = distanceFromScreenCenter;
939 minDistanceFromScreenCenterIndex = i;
940 }
941 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700942 return minDistanceFromScreenCenterIndex;
943 }
944
945 protected void snapToDestination() {
946 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 }
948
Michael Jurka0142d492010-08-25 17:46:15 -0700949 protected void snapToPageWithVelocity(int whichPage, int velocity) {
950 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
951 // can use it
952 snapToPage(whichPage);
953 }
954
955 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700956 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 }
958
Michael Jurka0142d492010-08-25 17:46:15 -0700959 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700960 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700961
Winson Chung86f77532010-08-24 11:08:22 -0700962 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 final int sX = getScrollX();
964 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700965 snapToPage(whichPage, delta, duration);
966 }
967
968 protected void snapToPage(int whichPage, int delta, int duration) {
969 mNextPage = whichPage;
970
971 View focusedChild = getFocusedChild();
972 if (focusedChild != null && whichPage != mCurrentPage &&
973 focusedChild == getChildAt(mCurrentPage)) {
974 focusedChild.clearFocus();
975 }
976
977 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 awakenScrollBars(duration);
979 if (duration == 0) {
980 duration = Math.abs(delta);
981 }
982
983 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700984 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700985
986 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700987 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700988 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 invalidate();
990 }
991
992 @Override
993 protected Parcelable onSaveInstanceState() {
994 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700995 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 return state;
997 }
998
999 @Override
1000 protected void onRestoreInstanceState(Parcelable state) {
1001 SavedState savedState = (SavedState) state;
1002 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001003 if (savedState.currentPage != -1) {
1004 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 }
1006 }
1007
1008 public void scrollLeft() {
1009 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001010 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001012 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 }
1014 }
1015
1016 public void scrollRight() {
1017 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001018 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001020 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 }
1022 }
1023
Winson Chung86f77532010-08-24 11:08:22 -07001024 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 int result = -1;
1026 if (v != null) {
1027 ViewParent vp = v.getParent();
1028 int count = getChildCount();
1029 for (int i = 0; i < count; i++) {
1030 if (vp == getChildAt(i)) {
1031 return i;
1032 }
1033 }
1034 }
1035 return result;
1036 }
1037
1038 /**
1039 * @return True is long presses are still allowed for the current touch
1040 */
1041 public boolean allowLongPress() {
1042 return mAllowLongPress;
1043 }
1044
Michael Jurka0142d492010-08-25 17:46:15 -07001045 /**
1046 * Set true to allow long-press events to be triggered, usually checked by
1047 * {@link Launcher} to accept or block dpad-initiated long-presses.
1048 */
1049 public void setAllowLongPress(boolean allowLongPress) {
1050 mAllowLongPress = allowLongPress;
1051 }
1052
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001054 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001055
1056 SavedState(Parcelable superState) {
1057 super(superState);
1058 }
1059
1060 private SavedState(Parcel in) {
1061 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001062 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 }
1064
1065 @Override
1066 public void writeToParcel(Parcel out, int flags) {
1067 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001068 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 }
1070
1071 public static final Parcelable.Creator<SavedState> CREATOR =
1072 new Parcelable.Creator<SavedState>() {
1073 public SavedState createFromParcel(Parcel in) {
1074 return new SavedState(in);
1075 }
1076
1077 public SavedState[] newArray(int size) {
1078 return new SavedState[size];
1079 }
1080 };
1081 }
1082
Winson Chung86f77532010-08-24 11:08:22 -07001083 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001084 if (mContentIsRefreshable) {
1085 final int count = getChildCount();
1086 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001087 int lowerPageBound = getAssociatedLowerPageBound(page);
1088 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001089 for (int i = 0; i < count; ++i) {
1090 final ViewGroup layout = (ViewGroup) getChildAt(i);
1091 final int childCount = layout.getChildCount();
1092 if (lowerPageBound <= i && i <= upperPageBound) {
1093 if (mDirtyPageContent.get(i)) {
1094 syncPageItems(i);
1095 mDirtyPageContent.set(i, false);
1096 }
1097 } else {
1098 if (childCount > 0) {
1099 layout.removeAllViews();
1100 }
1101 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001102 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001103 }
1104 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001105 }
1106 }
1107
Winson Chunge3193b92010-09-10 11:44:42 -07001108 protected int getAssociatedLowerPageBound(int page) {
1109 return Math.max(0, page - 1);
1110 }
1111 protected int getAssociatedUpperPageBound(int page) {
1112 final int count = getChildCount();
1113 return Math.min(page + 1, count - 1);
1114 }
1115
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001116 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001117 if (isChoiceMode(CHOICE_MODE_NONE)) {
1118 mChoiceMode = mode;
1119 mActionMode = startActionMode(callback);
1120 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001121 }
1122
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001123 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001124 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001125 mChoiceMode = CHOICE_MODE_NONE;
1126 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001127 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001128 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001129 }
1130 }
1131
1132 protected boolean isChoiceMode(int mode) {
1133 return mChoiceMode == mode;
1134 }
1135
1136 protected ArrayList<Checkable> getCheckedGrandchildren() {
1137 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1138 final int childCount = getChildCount();
1139 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001140 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001141 final int grandChildCount = layout.getChildCount();
1142 for (int j = 0; j < grandChildCount; ++j) {
1143 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001144 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001145 checked.add((Checkable) v);
1146 }
1147 }
1148 }
1149 return checked;
1150 }
1151
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001152 /**
1153 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1154 * Otherwise, returns null.
1155 */
1156 protected Checkable getSingleCheckedGrandchild() {
1157 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1158 final int childCount = getChildCount();
1159 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001160 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001161 final int grandChildCount = layout.getChildCount();
1162 for (int j = 0; j < grandChildCount; ++j) {
1163 final View v = layout.getChildAt(j);
1164 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1165 return (Checkable) v;
1166 }
1167 }
1168 }
1169 }
1170 return null;
1171 }
1172
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001173 public Object getChosenItem() {
1174 View checkedView = (View) getSingleCheckedGrandchild();
1175 if (checkedView != null) {
1176 return checkedView.getTag();
1177 }
1178 return null;
1179 }
1180
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001181 protected void resetCheckedGrandchildren() {
1182 // loop through children, and set all of their children to _not_ be checked
1183 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1184 for (int i = 0; i < checked.size(); ++i) {
1185 final Checkable c = checked.get(i);
1186 c.setChecked(false);
1187 }
1188 }
1189
Winson Chung86f77532010-08-24 11:08:22 -07001190 /**
1191 * This method is called ONLY to synchronize the number of pages that the paged view has.
1192 * To actually fill the pages with information, implement syncPageItems() below. It is
1193 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1194 * and therefore, individual page items do not need to be updated in this method.
1195 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001197
1198 /**
1199 * This method is called to synchronize the items that are on a particular page. If views on
1200 * the page can be reused, then they should be updated within this method.
1201 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001203
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001205 if (mContentIsRefreshable) {
1206 // Update all the pages
1207 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001208
Michael Jurka0142d492010-08-25 17:46:15 -07001209 // Mark each of the pages as dirty
1210 final int count = getChildCount();
1211 mDirtyPageContent.clear();
1212 for (int i = 0; i < count; ++i) {
1213 mDirtyPageContent.add(true);
1214 }
1215
1216 // Load any pages that are necessary for the current window of views
1217 loadAssociatedPages(mCurrentPage);
1218 mDirtyPageAlpha = true;
1219 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001220 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 }
1222}