blob: 4ccc4aeab03dabf5a58c1e5105e1120ca6b89704 [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 Cohene45440e2010-10-14 18:33:38 -070083 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
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;
Winson Chungdf4b83d2010-10-20 17:49:27 -070099 protected int mPageLayoutWidthGap;
100 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700101 protected int mCellCountX;
102 protected int mCellCountY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka5f1c5092010-09-03 14:15:02 -0700106 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Winson Chung86f77532010-08-24 11:08:22 -0700108 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Winson Chung86f77532010-08-24 11:08:22 -0700110 private ArrayList<Boolean> mDirtyPageContent;
111 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700113 // choice modes
114 protected static final int CHOICE_MODE_NONE = 0;
115 protected static final int CHOICE_MODE_SINGLE = 1;
116 // Multiple selection mode is not supported by all Launcher actions atm
117 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700118
Michael Jurkae17e19c2010-09-28 11:01:39 -0700119 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700120 private ActionMode mActionMode;
121
Winson Chung241c3b42010-08-25 16:53:03 -0700122 protected PagedViewIconCache mPageViewIconCache;
123
Michael Jurka0142d492010-08-25 17:46:15 -0700124 // If true, syncPages and syncPageItems will be called to refresh pages
125 protected boolean mContentIsRefreshable = true;
126
127 // If true, modify alpha of neighboring pages as user scrolls left/right
128 protected boolean mFadeInAdjacentScreens = true;
129
130 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
131 // to switch to a new page
132 protected boolean mUsePagingTouchSlop = true;
133
134 // If true, the subclass should directly update mScrollX itself in its computeScroll method
135 // (SmoothPagedView does this)
136 protected boolean mDeferScrollUpdate = false;
137
Patrick Dubroy1262e362010-10-06 15:49:50 -0700138 protected boolean mIsPageMoving = false;
139
Winson Chung241c3b42010-08-25 16:53:03 -0700140 /**
141 * Simple cache mechanism for PagedViewIcon outlines.
142 */
143 class PagedViewIconCache {
144 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
145
146 public void clear() {
147 iconOutlineCache.clear();
148 }
149 public void addOutline(Object key, Bitmap b) {
150 iconOutlineCache.put(key, b);
151 }
152 public void removeOutline(Object key) {
153 if (iconOutlineCache.containsKey(key)) {
154 iconOutlineCache.remove(key);
155 }
156 }
157 public Bitmap getOutline(Object key) {
158 return iconOutlineCache.get(key);
159 }
160 }
161
Winson Chung86f77532010-08-24 11:08:22 -0700162 public interface PageSwitchListener {
163 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 }
165
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 public PagedView(Context context) {
167 this(context, null);
168 }
169
170 public PagedView(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public PagedView(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700176 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700177
Adam Cohen9c4949e2010-10-05 12:27:22 -0700178 TypedArray a = context.obtainStyledAttributes(attrs,
179 R.styleable.PagedView, defStyle, 0);
180 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
181 mPageLayoutPaddingTop = a.getDimensionPixelSize(
182 R.styleable.PagedView_pageLayoutPaddingTop, 10);
183 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
185 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
187 mPageLayoutPaddingRight = a.getDimensionPixelSize(
188 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700189 mPageLayoutWidthGap = a.getDimensionPixelSize(
190 R.styleable.PagedView_pageLayoutWidthGap, -1);
191 mPageLayoutHeightGap = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700193 a.recycle();
194
Winson Chung321e9ee2010-08-09 13:37:56 -0700195 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700196 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198
199 /**
200 * Initializes various states for this workspace.
201 */
Michael Jurka0142d492010-08-25 17:46:15 -0700202 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700203 mDirtyPageContent = new ArrayList<Boolean>();
204 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700205 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700207 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700208
209 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
210 mTouchSlop = configuration.getScaledTouchSlop();
211 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
212 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
213 }
214
Winson Chung86f77532010-08-24 11:08:22 -0700215 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
216 mPageSwitchListener = pageSwitchListener;
217 if (mPageSwitchListener != null) {
218 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 }
220 }
221
222 /**
Winson Chung86f77532010-08-24 11:08:22 -0700223 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 *
Winson Chung86f77532010-08-24 11:08:22 -0700225 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 */
Winson Chung86f77532010-08-24 11:08:22 -0700227 int getCurrentPage() {
228 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230
Winson Chung86f77532010-08-24 11:08:22 -0700231 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 return getChildCount();
233 }
234
Winson Chung86f77532010-08-24 11:08:22 -0700235 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildAt(index);
237 }
238
239 int getScrollWidth() {
240 return getWidth();
241 }
242
243 /**
Winson Chung86f77532010-08-24 11:08:22 -0700244 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 */
Winson Chung86f77532010-08-24 11:08:22 -0700246 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 if (!mScroller.isFinished()) mScroller.abortAnimation();
248 if (getChildCount() == 0) return;
249
Winson Chung86f77532010-08-24 11:08:22 -0700250 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700251 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
252 scrollTo(newX, 0);
253 mScroller.setFinalX(newX);
Winson Chung80baf5a2010-08-09 16:03:15 -0700254
Winson Chung321e9ee2010-08-09 13:37:56 -0700255 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700256 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 }
258
Michael Jurka0142d492010-08-25 17:46:15 -0700259 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700260 if (mPageSwitchListener != null) {
261 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 }
263 }
264
Patrick Dubroy1262e362010-10-06 15:49:50 -0700265 private void pageBeginMoving() {
266 mIsPageMoving = true;
267 onPageBeginMoving();
268 }
269
270 private void pageEndMoving() {
271 onPageEndMoving();
272 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700273 }
274
275 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700276 protected void onPageBeginMoving() {
277 }
278
279 // a method that subclasses can override to add behavior
280 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700281 }
282
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 /**
Winson Chung86f77532010-08-24 11:08:22 -0700284 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 *
286 * @param l The listener used to respond to long clicks.
287 */
288 @Override
289 public void setOnLongClickListener(OnLongClickListener l) {
290 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700291 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700293 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 }
295 }
296
297 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700298 public void scrollTo(int x, int y) {
299 super.scrollTo(x, y);
300 mTouchX = x;
301 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
302 }
303
304 // we moved this functionality to a helper function so SmoothPagedView can reuse it
305 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700307 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700309 invalidate();
310 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700311 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700312 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700313 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700314 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700315 notifyPageSwitchListener();
316 pageEndMoving();
317 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 }
Michael Jurka0142d492010-08-25 17:46:15 -0700319 return false;
320 }
321
322 @Override
323 public void computeScroll() {
324 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 }
326
327 @Override
328 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
329 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
330 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
331 if (widthMode != MeasureSpec.EXACTLY) {
332 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
333 }
334
335 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
336 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
337 if (heightMode != MeasureSpec.EXACTLY) {
338 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
339 }
340
341 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700342 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 final int childCount = getChildCount();
344 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700345 // disallowing padding in paged view (just pass 0)
346 final View child = getChildAt(i);
347 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
348
349 int childWidthMode;
350 if (lp.width == LayoutParams.WRAP_CONTENT) {
351 childWidthMode = MeasureSpec.AT_MOST;
352 } else {
353 childWidthMode = MeasureSpec.EXACTLY;
354 }
355
356 int childHeightMode;
357 if (lp.height == LayoutParams.WRAP_CONTENT) {
358 childHeightMode = MeasureSpec.AT_MOST;
359 } else {
360 childHeightMode = MeasureSpec.EXACTLY;
361 }
362
363 final int childWidthMeasureSpec =
364 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
365 final int childHeightMeasureSpec =
366 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
367
368 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 }
370
371 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373
374 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700375 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700376 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
377 setHorizontalScrollBarEnabled(false);
378 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
379 scrollTo(newX, 0);
380 mScroller.setFinalX(newX);
381 setHorizontalScrollBarEnabled(true);
382 mFirstLayout = false;
383 }
384
Winson Chung321e9ee2010-08-09 13:37:56 -0700385 final int childCount = getChildCount();
386 int childLeft = 0;
387 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700388 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
390
391 for (int i = 0; i < childCount; i++) {
392 final View child = getChildAt(i);
393 if (child.getVisibility() != View.GONE) {
394 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700395 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
396 child.layout(childLeft, childHeight,
397 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700398 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 }
400 }
401 }
402
Winson Chunge3193b92010-09-10 11:44:42 -0700403 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700404 if (mFadeInAdjacentScreens) {
405 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700406 int halfScreenSize = getMeasuredWidth() / 2;
407 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700408 final int childCount = getChildCount();
409 for (int i = 0; i < childCount; ++i) {
410 View layout = (View) getChildAt(i);
411 int childWidth = layout.getMeasuredWidth();
412 int halfChildWidth = (childWidth / 2);
413 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700414
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700415 // On the first layout, we may not have a width nor a proper offset, so for now
416 // we should just assume full page width (and calculate the offset according to
417 // that).
418 if (childWidth <= 0) {
419 childWidth = getMeasuredWidth();
420 childCenter = (i * childWidth) + (childWidth / 2);
421 }
422
Winson Chunge8878e32010-09-15 20:37:09 -0700423 int d = halfChildWidth;
424 int distanceFromScreenCenter = childCenter - screenCenter;
425 if (distanceFromScreenCenter > 0) {
426 if (i > 0) {
427 d += getChildAt(i - 1).getMeasuredWidth() / 2;
428 }
Michael Jurka0142d492010-08-25 17:46:15 -0700429 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700430 if (i < childCount - 1) {
431 d += getChildAt(i + 1).getMeasuredWidth() / 2;
432 }
Michael Jurka0142d492010-08-25 17:46:15 -0700433 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700434 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700435
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700436 // Preventing potential divide-by-zero
437 d = Math.max(1, d);
438
Winson Chunge8878e32010-09-15 20:37:09 -0700439 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
440 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
441 float alpha = 1.0f - dimAlpha;
442
Adam Cohenf34bab52010-09-30 14:11:56 -0700443 if (alpha < ALPHA_QUANTIZE_LEVEL) {
444 alpha = 0.0f;
445 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
446 alpha = 1.0f;
447 }
448
Michael Jurka0142d492010-08-25 17:46:15 -0700449 if (Float.compare(alpha, layout.getAlpha()) != 0) {
450 layout.setAlpha(alpha);
451 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 }
Michael Jurka0142d492010-08-25 17:46:15 -0700453 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
455 }
Winson Chunge3193b92010-09-10 11:44:42 -0700456 }
457
Adam Cohenf34bab52010-09-30 14:11:56 -0700458 protected void screenScrolled(int screenCenter) {
459 }
460
Winson Chunge3193b92010-09-10 11:44:42 -0700461 @Override
462 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700463 int halfScreenSize = getMeasuredWidth() / 2;
464 int screenCenter = mScrollX + halfScreenSize;
465
466 if (screenCenter != mLastScreenCenter) {
467 screenScrolled(screenCenter);
468 updateAdjacentPagesAlpha();
469 mLastScreenCenter = screenCenter;
470 }
Michael Jurka0142d492010-08-25 17:46:15 -0700471
472 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700473 // As an optimization, this code assumes that all pages have the same width as the 0th
474 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700475 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700476 if (pageCount > 0) {
477 final int pageWidth = getChildAt(0).getMeasuredWidth();
478 final int screenWidth = getMeasuredWidth();
479 int x = getRelativeChildOffset(0) + pageWidth;
480 int leftScreen = 0;
481 int rightScreen = 0;
482 while (x <= mScrollX) {
483 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700484 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700485 // replace above line with this if you don't assume all pages have same width as 0th
486 // page:
487 // x += getChildAt(leftScreen).getMeasuredWidth();
488 }
489 rightScreen = leftScreen;
490 while (x < mScrollX + screenWidth) {
491 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700492 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700493 // replace above line with this if you don't assume all pages have same width as 0th
494 // page:
495 //if (rightScreen < pageCount) {
496 // x += getChildAt(rightScreen).getMeasuredWidth();
497 //}
498 }
499 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700500
Michael Jurkac4fb9172010-09-02 17:19:20 -0700501 final long drawingTime = getDrawingTime();
502 for (int i = leftScreen; i <= rightScreen; i++) {
503 drawChild(canvas, getChildAt(i), drawingTime);
504 }
Michael Jurka0142d492010-08-25 17:46:15 -0700505 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
507
508 @Override
509 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700510 int page = indexOfChild(child);
511 if (page != mCurrentPage || !mScroller.isFinished()) {
512 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 return true;
514 }
515 return false;
516 }
517
518 @Override
519 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700520 int focusablePage;
521 if (mNextPage != INVALID_PAGE) {
522 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700523 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700524 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 }
Winson Chung86f77532010-08-24 11:08:22 -0700526 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 if (v != null) {
528 v.requestFocus(direction, previouslyFocusedRect);
529 }
530 return false;
531 }
532
533 @Override
534 public boolean dispatchUnhandledMove(View focused, int direction) {
535 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700536 if (getCurrentPage() > 0) {
537 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700538 return true;
539 }
540 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700541 if (getCurrentPage() < getPageCount() - 1) {
542 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 return true;
544 }
545 }
546 return super.dispatchUnhandledMove(focused, direction);
547 }
548
549 @Override
550 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700551 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
552 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 }
554 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700555 if (mCurrentPage > 0) {
556 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
558 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700559 if (mCurrentPage < getPageCount() - 1) {
560 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700561 }
562 }
563 }
564
565 /**
566 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700567 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 *
Winson Chung86f77532010-08-24 11:08:22 -0700569 * This happens when live folders requery, and if they're off page, they
570 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700571 */
572 @Override
573 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700574 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 View v = focused;
576 while (true) {
577 if (v == current) {
578 super.focusableViewAvailable(focused);
579 return;
580 }
581 if (v == this) {
582 return;
583 }
584 ViewParent parent = v.getParent();
585 if (parent instanceof View) {
586 v = (View)v.getParent();
587 } else {
588 return;
589 }
590 }
591 }
592
593 /**
594 * {@inheritDoc}
595 */
596 @Override
597 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
598 if (disallowIntercept) {
599 // We need to make sure to cancel our long press if
600 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700601 final View currentPage = getChildAt(mCurrentPage);
602 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 }
604 super.requestDisallowInterceptTouchEvent(disallowIntercept);
605 }
606
607 @Override
608 public boolean onInterceptTouchEvent(MotionEvent ev) {
609 /*
610 * This method JUST determines whether we want to intercept the motion.
611 * If we return true, onTouchEvent will be called and we do the actual
612 * scrolling there.
613 */
614
615 /*
616 * Shortcut the most recurring case: the user is in the dragging
617 * state and he is moving his finger. We want to intercept this
618 * motion.
619 */
620 final int action = ev.getAction();
621 if ((action == MotionEvent.ACTION_MOVE) &&
622 (mTouchState == TOUCH_STATE_SCROLLING)) {
623 return true;
624 }
625
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 switch (action & MotionEvent.ACTION_MASK) {
627 case MotionEvent.ACTION_MOVE: {
628 /*
629 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
630 * whether the user has moved far enough from his original down touch.
631 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700632 if (mActivePointerId != INVALID_POINTER) {
633 determineScrollingStart(ev);
634 break;
635 }
636 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
637 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
638 // i.e. fall through to the next case (don't break)
639 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
640 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 }
642
643 case MotionEvent.ACTION_DOWN: {
644 final float x = ev.getX();
645 final float y = ev.getY();
646 // Remember location of down touch
647 mDownMotionX = x;
648 mLastMotionX = x;
649 mLastMotionY = y;
650 mActivePointerId = ev.getPointerId(0);
651 mAllowLongPress = true;
652
653 /*
654 * If being flinged and user touches the screen, initiate drag;
655 * otherwise don't. mScroller.isFinished should be false when
656 * being flinged.
657 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700658 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700659 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
660 if (finishedScrolling) {
661 mTouchState = TOUCH_STATE_REST;
662 mScroller.abortAnimation();
663 } else {
664 mTouchState = TOUCH_STATE_SCROLLING;
665 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700666
Winson Chung86f77532010-08-24 11:08:22 -0700667 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700668 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700669 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
671 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700672 int width = getMeasuredWidth();
673 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700674 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700676 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 mTouchState = TOUCH_STATE_NEXT_PAGE;
678 }
679 }
680 }
681 break;
682 }
683
684 case MotionEvent.ACTION_CANCEL:
685 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 mTouchState = TOUCH_STATE_REST;
687 mAllowLongPress = false;
688 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 break;
690
691 case MotionEvent.ACTION_POINTER_UP:
692 onSecondaryPointerUp(ev);
693 break;
694 }
695
696 /*
697 * The only time we want to intercept motion events is if we are in the
698 * drag mode.
699 */
700 return mTouchState != TOUCH_STATE_REST;
701 }
702
Winson Chung80baf5a2010-08-09 16:03:15 -0700703 protected void animateClickFeedback(View v, final Runnable r) {
704 // animate the view slightly to show click feedback running some logic after it is "pressed"
705 Animation anim = AnimationUtils.loadAnimation(getContext(),
706 R.anim.paged_view_click_feedback);
707 anim.setAnimationListener(new AnimationListener() {
708 @Override
709 public void onAnimationStart(Animation animation) {}
710 @Override
711 public void onAnimationRepeat(Animation animation) {
712 r.run();
713 }
714 @Override
715 public void onAnimationEnd(Animation animation) {}
716 });
717 v.startAnimation(anim);
718 }
719
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 /*
721 * Determines if we should change the touch state to start scrolling after the
722 * user moves their touch point too far.
723 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700724 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 /*
726 * Locally do absolute value. mLastMotionX is set to the y value
727 * of the down event.
728 */
729 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
730 final float x = ev.getX(pointerIndex);
731 final float y = ev.getY(pointerIndex);
732 final int xDiff = (int) Math.abs(x - mLastMotionX);
733 final int yDiff = (int) Math.abs(y - mLastMotionY);
734
735 final int touchSlop = mTouchSlop;
736 boolean xPaged = xDiff > mPagingTouchSlop;
737 boolean xMoved = xDiff > touchSlop;
738 boolean yMoved = yDiff > touchSlop;
739
740 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700741 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 // Scroll if the user moved far enough along the X axis
743 mTouchState = TOUCH_STATE_SCROLLING;
744 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700745 mTouchX = mScrollX;
746 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
747 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 }
749 // Either way, cancel any pending longpress
750 if (mAllowLongPress) {
751 mAllowLongPress = false;
752 // Try canceling the long press. It could also have been scheduled
753 // by a distant descendant, so use the mAllowLongPress flag to block
754 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700755 final View currentPage = getPageAt(mCurrentPage);
756 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 }
758 }
759 }
760
Adam Cohen21f12b52010-10-07 17:15:37 -0700761 protected boolean handlePagingClicks() {
762 return false;
763 }
764
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 @Override
766 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700767 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700768
769 final int action = ev.getAction();
770
771 switch (action & MotionEvent.ACTION_MASK) {
772 case MotionEvent.ACTION_DOWN:
773 /*
774 * If being flinged and user touches, stop the fling. isFinished
775 * will be false if being flinged.
776 */
777 if (!mScroller.isFinished()) {
778 mScroller.abortAnimation();
779 }
780
781 // Remember where the motion event started
782 mDownMotionX = mLastMotionX = ev.getX();
783 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700784 if (mTouchState == TOUCH_STATE_SCROLLING) {
785 pageBeginMoving();
786 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 break;
788
789 case MotionEvent.ACTION_MOVE:
790 if (mTouchState == TOUCH_STATE_SCROLLING) {
791 // Scroll to follow the motion event
792 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
793 final float x = ev.getX(pointerIndex);
794 final int deltaX = (int) (mLastMotionX - x);
795 mLastMotionX = x;
796
797 int sx = getScrollX();
798 if (deltaX < 0) {
799 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700800 mTouchX += Math.max(-mTouchX, deltaX);
801 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
802 if (!mDeferScrollUpdate) {
803 scrollBy(Math.max(-sx, deltaX), 0);
804 } else {
805 // This will trigger a call to computeScroll() on next drawChild() call
806 invalidate();
807 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 }
809 } else if (deltaX > 0) {
810 final int lastChildIndex = getChildCount() - 1;
811 final int availableToScroll = getChildOffset(lastChildIndex) -
812 getRelativeChildOffset(lastChildIndex) - sx;
813 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700814 mTouchX += Math.min(availableToScroll, deltaX);
815 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
816 if (!mDeferScrollUpdate) {
817 scrollBy(Math.min(availableToScroll, deltaX), 0);
818 } else {
819 // This will trigger a call to computeScroll() on next drawChild() call
820 invalidate();
821 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 }
823 } else {
824 awakenScrollBars();
825 }
Adam Cohen564976a2010-10-13 18:52:07 -0700826 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 determineScrollingStart(ev);
828 }
829 break;
830
831 case MotionEvent.ACTION_UP:
832 if (mTouchState == TOUCH_STATE_SCROLLING) {
833 final int activePointerId = mActivePointerId;
834 final int pointerIndex = ev.findPointerIndex(activePointerId);
835 final float x = ev.getX(pointerIndex);
836 final VelocityTracker velocityTracker = mVelocityTracker;
837 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
838 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
839 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
840
Michael Jurka0142d492010-08-25 17:46:15 -0700841 final int snapVelocity = mSnapVelocity;
842 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
843 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
844 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700845 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700846 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 } else {
848 snapToDestination();
849 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700850 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 // at this point we have not moved beyond the touch slop
852 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
853 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700854 int nextPage = Math.max(0, mCurrentPage - 1);
855 if (nextPage != mCurrentPage) {
856 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 } else {
858 snapToDestination();
859 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700860 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 // at this point we have not moved beyond the touch slop
862 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
863 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700864 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
865 if (nextPage != mCurrentPage) {
866 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 } else {
868 snapToDestination();
869 }
870 }
871 mTouchState = TOUCH_STATE_REST;
872 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700873 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 break;
875
876 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700877 if (mTouchState == TOUCH_STATE_SCROLLING) {
878 snapToDestination();
879 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 mTouchState = TOUCH_STATE_REST;
881 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700882 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 break;
884
885 case MotionEvent.ACTION_POINTER_UP:
886 onSecondaryPointerUp(ev);
887 break;
888 }
889
890 return true;
891 }
892
Michael Jurkab8f06722010-10-10 15:58:46 -0700893 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
894 if (mVelocityTracker == null) {
895 mVelocityTracker = VelocityTracker.obtain();
896 }
897 mVelocityTracker.addMovement(ev);
898 }
899
900 private void releaseVelocityTracker() {
901 if (mVelocityTracker != null) {
902 mVelocityTracker.recycle();
903 mVelocityTracker = null;
904 }
905 }
906
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 private void onSecondaryPointerUp(MotionEvent ev) {
908 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
909 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
910 final int pointerId = ev.getPointerId(pointerIndex);
911 if (pointerId == mActivePointerId) {
912 // This was our active pointer going up. Choose a new
913 // active pointer and adjust accordingly.
914 // TODO: Make this decision more intelligent.
915 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
916 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
917 mLastMotionY = ev.getY(newPointerIndex);
918 mActivePointerId = ev.getPointerId(newPointerIndex);
919 if (mVelocityTracker != null) {
920 mVelocityTracker.clear();
921 }
922 }
923 }
924
925 @Override
926 public void requestChildFocus(View child, View focused) {
927 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700928 int page = indexOfChild(child);
929 if (page >= 0 && !isInTouchMode()) {
930 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 }
932 }
933
Winson Chunge3193b92010-09-10 11:44:42 -0700934 protected int getChildIndexForRelativeOffset(int relativeOffset) {
935 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700936 int left;
937 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700938 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700939 left = getRelativeChildOffset(i);
940 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700941 if (left <= relativeOffset && relativeOffset <= right) {
942 return i;
943 }
Winson Chunge3193b92010-09-10 11:44:42 -0700944 }
945 return -1;
946 }
947
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 protected int getRelativeChildOffset(int index) {
949 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
950 }
951
952 protected int getChildOffset(int index) {
953 if (getChildCount() == 0)
954 return 0;
955
956 int offset = getRelativeChildOffset(0);
957 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700958 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 }
960 return offset;
961 }
962
Adam Cohend19d3ca2010-09-15 14:43:42 -0700963 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 int minDistanceFromScreenCenter = getMeasuredWidth();
965 int minDistanceFromScreenCenterIndex = -1;
966 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
967 final int childCount = getChildCount();
968 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700969 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 int childWidth = layout.getMeasuredWidth();
971 int halfChildWidth = (childWidth / 2);
972 int childCenter = getChildOffset(i) + halfChildWidth;
973 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
974 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
975 minDistanceFromScreenCenter = distanceFromScreenCenter;
976 minDistanceFromScreenCenterIndex = i;
977 }
978 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700979 return minDistanceFromScreenCenterIndex;
980 }
981
982 protected void snapToDestination() {
983 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 }
985
Michael Jurka0142d492010-08-25 17:46:15 -0700986 protected void snapToPageWithVelocity(int whichPage, int velocity) {
987 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
988 // can use it
989 snapToPage(whichPage);
990 }
991
992 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700993 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 }
995
Michael Jurka0142d492010-08-25 17:46:15 -0700996 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700997 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700998
Winson Chung86f77532010-08-24 11:08:22 -0700999 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 final int sX = getScrollX();
1001 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001002 snapToPage(whichPage, delta, duration);
1003 }
1004
1005 protected void snapToPage(int whichPage, int delta, int duration) {
1006 mNextPage = whichPage;
1007
1008 View focusedChild = getFocusedChild();
1009 if (focusedChild != null && whichPage != mCurrentPage &&
1010 focusedChild == getChildAt(mCurrentPage)) {
1011 focusedChild.clearFocus();
1012 }
1013
1014 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 awakenScrollBars(duration);
1016 if (duration == 0) {
1017 duration = Math.abs(delta);
1018 }
1019
1020 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001021 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001022
1023 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001024 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001025 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001026 invalidate();
1027 }
1028
1029 @Override
1030 protected Parcelable onSaveInstanceState() {
1031 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001032 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 return state;
1034 }
1035
1036 @Override
1037 protected void onRestoreInstanceState(Parcelable state) {
1038 SavedState savedState = (SavedState) state;
1039 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001040 if (savedState.currentPage != -1) {
1041 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 }
1043 }
1044
1045 public void scrollLeft() {
1046 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001047 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001049 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001050 }
1051 }
1052
1053 public void scrollRight() {
1054 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001055 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001057 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 }
1059 }
1060
Winson Chung86f77532010-08-24 11:08:22 -07001061 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 int result = -1;
1063 if (v != null) {
1064 ViewParent vp = v.getParent();
1065 int count = getChildCount();
1066 for (int i = 0; i < count; i++) {
1067 if (vp == getChildAt(i)) {
1068 return i;
1069 }
1070 }
1071 }
1072 return result;
1073 }
1074
1075 /**
1076 * @return True is long presses are still allowed for the current touch
1077 */
1078 public boolean allowLongPress() {
1079 return mAllowLongPress;
1080 }
1081
Michael Jurka0142d492010-08-25 17:46:15 -07001082 /**
1083 * Set true to allow long-press events to be triggered, usually checked by
1084 * {@link Launcher} to accept or block dpad-initiated long-presses.
1085 */
1086 public void setAllowLongPress(boolean allowLongPress) {
1087 mAllowLongPress = allowLongPress;
1088 }
1089
Winson Chung321e9ee2010-08-09 13:37:56 -07001090 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001091 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001092
1093 SavedState(Parcelable superState) {
1094 super(superState);
1095 }
1096
1097 private SavedState(Parcel in) {
1098 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001099 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001100 }
1101
1102 @Override
1103 public void writeToParcel(Parcel out, int flags) {
1104 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001105 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 }
1107
1108 public static final Parcelable.Creator<SavedState> CREATOR =
1109 new Parcelable.Creator<SavedState>() {
1110 public SavedState createFromParcel(Parcel in) {
1111 return new SavedState(in);
1112 }
1113
1114 public SavedState[] newArray(int size) {
1115 return new SavedState[size];
1116 }
1117 };
1118 }
1119
Winson Chung86f77532010-08-24 11:08:22 -07001120 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001121 if (mContentIsRefreshable) {
1122 final int count = getChildCount();
1123 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001124 int lowerPageBound = getAssociatedLowerPageBound(page);
1125 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001126 for (int i = 0; i < count; ++i) {
1127 final ViewGroup layout = (ViewGroup) getChildAt(i);
1128 final int childCount = layout.getChildCount();
1129 if (lowerPageBound <= i && i <= upperPageBound) {
1130 if (mDirtyPageContent.get(i)) {
1131 syncPageItems(i);
1132 mDirtyPageContent.set(i, false);
1133 }
1134 } else {
1135 if (childCount > 0) {
1136 layout.removeAllViews();
1137 }
1138 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001139 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001140 }
1141 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001142 }
1143 }
1144
Winson Chunge3193b92010-09-10 11:44:42 -07001145 protected int getAssociatedLowerPageBound(int page) {
1146 return Math.max(0, page - 1);
1147 }
1148 protected int getAssociatedUpperPageBound(int page) {
1149 final int count = getChildCount();
1150 return Math.min(page + 1, count - 1);
1151 }
1152
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001153 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001154 if (isChoiceMode(CHOICE_MODE_NONE)) {
1155 mChoiceMode = mode;
1156 mActionMode = startActionMode(callback);
1157 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001158 }
1159
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001160 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001161 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001162 mChoiceMode = CHOICE_MODE_NONE;
1163 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001164 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001165 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001166 }
1167 }
1168
1169 protected boolean isChoiceMode(int mode) {
1170 return mChoiceMode == mode;
1171 }
1172
1173 protected ArrayList<Checkable> getCheckedGrandchildren() {
1174 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1175 final int childCount = getChildCount();
1176 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001177 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001178 final int grandChildCount = layout.getChildCount();
1179 for (int j = 0; j < grandChildCount; ++j) {
1180 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001181 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001182 checked.add((Checkable) v);
1183 }
1184 }
1185 }
1186 return checked;
1187 }
1188
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001189 /**
1190 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1191 * Otherwise, returns null.
1192 */
1193 protected Checkable getSingleCheckedGrandchild() {
1194 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1195 final int childCount = getChildCount();
1196 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001197 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001198 final int grandChildCount = layout.getChildCount();
1199 for (int j = 0; j < grandChildCount; ++j) {
1200 final View v = layout.getChildAt(j);
1201 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1202 return (Checkable) v;
1203 }
1204 }
1205 }
1206 }
1207 return null;
1208 }
1209
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001210 public Object getChosenItem() {
1211 View checkedView = (View) getSingleCheckedGrandchild();
1212 if (checkedView != null) {
1213 return checkedView.getTag();
1214 }
1215 return null;
1216 }
1217
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001218 protected void resetCheckedGrandchildren() {
1219 // loop through children, and set all of their children to _not_ be checked
1220 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1221 for (int i = 0; i < checked.size(); ++i) {
1222 final Checkable c = checked.get(i);
1223 c.setChecked(false);
1224 }
1225 }
1226
Winson Chung86f77532010-08-24 11:08:22 -07001227 /**
1228 * This method is called ONLY to synchronize the number of pages that the paged view has.
1229 * To actually fill the pages with information, implement syncPageItems() below. It is
1230 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1231 * and therefore, individual page items do not need to be updated in this method.
1232 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001234
1235 /**
1236 * This method is called to synchronize the items that are on a particular page. If views on
1237 * the page can be reused, then they should be updated within this method.
1238 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001240
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001242 if (mContentIsRefreshable) {
1243 // Update all the pages
1244 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001245
Michael Jurka0142d492010-08-25 17:46:15 -07001246 // Mark each of the pages as dirty
1247 final int count = getChildCount();
1248 mDirtyPageContent.clear();
1249 for (int i = 0; i < count; ++i) {
1250 mDirtyPageContent.add(true);
1251 }
1252
1253 // Load any pages that are necessary for the current window of views
1254 loadAssociatedPages(mCurrentPage);
1255 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001256 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001257 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001258 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 }
1260}