blob: c1256969b3371cc4902ce8932deb7a9c25b4b185 [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 Chung9cfd25f2010-10-24 16:09:28 -070056 private static final int MIN_LENGTH_FOR_FLING = 25;
57 // The min drag distance to trigger a page shift (regardless of velocity)
58 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Winson Chung5f2aa4e2010-08-20 14:49:25 -070060 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Michael Jurka0142d492010-08-25 17:46:15 -070063 // the velocity at which a fling gesture will cause us to snap to the next page
64 protected int mSnapVelocity = 500;
65
66 protected float mSmoothingTime;
67 protected float mTouchX;
68
69 protected boolean mFirstLayout = true;
70
71 protected int mCurrentPage;
72 protected int mNextPage = INVALID_PAGE;
73 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070074 private VelocityTracker mVelocityTracker;
75
76 private float mDownMotionX;
77 private float mLastMotionX;
78 private float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070079 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Michael Jurka0142d492010-08-25 17:46:15 -070081 protected final static int TOUCH_STATE_REST = 0;
82 protected final static int TOUCH_STATE_SCROLLING = 1;
83 protected final static int TOUCH_STATE_PREV_PAGE = 2;
84 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070085 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070086
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070088
Michael Jurka0142d492010-08-25 17:46:15 -070089 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070090
91 private boolean mAllowLongPress = true;
92
93 private int mTouchSlop;
94 private int mPagingTouchSlop;
95 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070096 protected int mPageSpacing;
97 protected int mPageLayoutPaddingTop;
98 protected int mPageLayoutPaddingBottom;
99 protected int mPageLayoutPaddingLeft;
100 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700101 protected int mPageLayoutWidthGap;
102 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700103 protected int mCellCountX;
104 protected int mCellCountY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka5f1c5092010-09-03 14:15:02 -0700106 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Michael Jurka5f1c5092010-09-03 14:15:02 -0700108 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Winson Chung86f77532010-08-24 11:08:22 -0700110 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Winson Chung86f77532010-08-24 11:08:22 -0700112 private ArrayList<Boolean> mDirtyPageContent;
113 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700115 // choice modes
116 protected static final int CHOICE_MODE_NONE = 0;
117 protected static final int CHOICE_MODE_SINGLE = 1;
118 // Multiple selection mode is not supported by all Launcher actions atm
119 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700120
Michael Jurkae17e19c2010-09-28 11:01:39 -0700121 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700122 private ActionMode mActionMode;
123
Winson Chung241c3b42010-08-25 16:53:03 -0700124 protected PagedViewIconCache mPageViewIconCache;
125
Michael Jurka0142d492010-08-25 17:46:15 -0700126 // If true, syncPages and syncPageItems will be called to refresh pages
127 protected boolean mContentIsRefreshable = true;
128
129 // If true, modify alpha of neighboring pages as user scrolls left/right
130 protected boolean mFadeInAdjacentScreens = true;
131
132 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
133 // to switch to a new page
134 protected boolean mUsePagingTouchSlop = true;
135
136 // If true, the subclass should directly update mScrollX itself in its computeScroll method
137 // (SmoothPagedView does this)
138 protected boolean mDeferScrollUpdate = false;
139
Patrick Dubroy1262e362010-10-06 15:49:50 -0700140 protected boolean mIsPageMoving = false;
141
Winson Chung241c3b42010-08-25 16:53:03 -0700142 /**
143 * Simple cache mechanism for PagedViewIcon outlines.
144 */
145 class PagedViewIconCache {
146 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
147
148 public void clear() {
149 iconOutlineCache.clear();
150 }
151 public void addOutline(Object key, Bitmap b) {
152 iconOutlineCache.put(key, b);
153 }
154 public void removeOutline(Object key) {
155 if (iconOutlineCache.containsKey(key)) {
156 iconOutlineCache.remove(key);
157 }
158 }
159 public Bitmap getOutline(Object key) {
160 return iconOutlineCache.get(key);
161 }
162 }
163
Winson Chung86f77532010-08-24 11:08:22 -0700164 public interface PageSwitchListener {
165 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 }
167
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 public PagedView(Context context) {
169 this(context, null);
170 }
171
172 public PagedView(Context context, AttributeSet attrs) {
173 this(context, attrs, 0);
174 }
175
176 public PagedView(Context context, AttributeSet attrs, int defStyle) {
177 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700178 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700179
Adam Cohen9c4949e2010-10-05 12:27:22 -0700180 TypedArray a = context.obtainStyledAttributes(attrs,
181 R.styleable.PagedView, defStyle, 0);
182 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
183 mPageLayoutPaddingTop = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutPaddingTop, 10);
185 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
187 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
188 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
189 mPageLayoutPaddingRight = a.getDimensionPixelSize(
190 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700191 mPageLayoutWidthGap = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutWidthGap, -1);
193 mPageLayoutHeightGap = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700195 a.recycle();
196
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700198 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 }
200
201 /**
202 * Initializes various states for this workspace.
203 */
Michael Jurka0142d492010-08-25 17:46:15 -0700204 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700205 mDirtyPageContent = new ArrayList<Boolean>();
206 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700207 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700209 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700210
211 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
212 mTouchSlop = configuration.getScaledTouchSlop();
213 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
214 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
215 }
216
Winson Chung86f77532010-08-24 11:08:22 -0700217 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
218 mPageSwitchListener = pageSwitchListener;
219 if (mPageSwitchListener != null) {
220 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222 }
223
224 /**
Winson Chung86f77532010-08-24 11:08:22 -0700225 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 *
Winson Chung86f77532010-08-24 11:08:22 -0700227 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 */
Winson Chung86f77532010-08-24 11:08:22 -0700229 int getCurrentPage() {
230 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 }
232
Winson Chung86f77532010-08-24 11:08:22 -0700233 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 return getChildCount();
235 }
236
Winson Chung86f77532010-08-24 11:08:22 -0700237 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 return getChildAt(index);
239 }
240
241 int getScrollWidth() {
242 return getWidth();
243 }
244
245 /**
Winson Chung86f77532010-08-24 11:08:22 -0700246 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 */
Winson Chung86f77532010-08-24 11:08:22 -0700248 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 if (!mScroller.isFinished()) mScroller.abortAnimation();
250 if (getChildCount() == 0) return;
251
Winson Chung86f77532010-08-24 11:08:22 -0700252 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700253 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
254 scrollTo(newX, 0);
255 mScroller.setFinalX(newX);
Winson Chung80baf5a2010-08-09 16:03:15 -0700256
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700258 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 }
260
Michael Jurka0142d492010-08-25 17:46:15 -0700261 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700262 if (mPageSwitchListener != null) {
263 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 }
265 }
266
Patrick Dubroy1262e362010-10-06 15:49:50 -0700267 private void pageBeginMoving() {
268 mIsPageMoving = true;
269 onPageBeginMoving();
270 }
271
272 private void pageEndMoving() {
273 onPageEndMoving();
274 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700275 }
276
277 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700278 protected void onPageBeginMoving() {
279 }
280
281 // a method that subclasses can override to add behavior
282 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700283 }
284
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 /**
Winson Chung86f77532010-08-24 11:08:22 -0700286 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 *
288 * @param l The listener used to respond to long clicks.
289 */
290 @Override
291 public void setOnLongClickListener(OnLongClickListener l) {
292 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700293 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700295 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 }
297 }
298
299 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700300 public void scrollTo(int x, int y) {
301 super.scrollTo(x, y);
302 mTouchX = x;
303 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
304 }
305
306 // we moved this functionality to a helper function so SmoothPagedView can reuse it
307 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700309 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700311 invalidate();
312 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700313 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700314 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700315 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700316 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700317 notifyPageSwitchListener();
318 pageEndMoving();
319 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
Michael Jurka0142d492010-08-25 17:46:15 -0700321 return false;
322 }
323
324 @Override
325 public void computeScroll() {
326 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 }
328
329 @Override
330 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
331 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
332 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
333 if (widthMode != MeasureSpec.EXACTLY) {
334 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
335 }
336
337 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
338 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
339 if (heightMode != MeasureSpec.EXACTLY) {
340 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
341 }
342
343 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700344 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 final int childCount = getChildCount();
346 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700347 // disallowing padding in paged view (just pass 0)
348 final View child = getChildAt(i);
349 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
350
351 int childWidthMode;
352 if (lp.width == LayoutParams.WRAP_CONTENT) {
353 childWidthMode = MeasureSpec.AT_MOST;
354 } else {
355 childWidthMode = MeasureSpec.EXACTLY;
356 }
357
358 int childHeightMode;
359 if (lp.height == LayoutParams.WRAP_CONTENT) {
360 childHeightMode = MeasureSpec.AT_MOST;
361 } else {
362 childHeightMode = MeasureSpec.EXACTLY;
363 }
364
365 final int childWidthMeasureSpec =
366 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
367 final int childHeightMeasureSpec =
368 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
369
370 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372
373 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 }
375
376 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700377 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700378 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
379 setHorizontalScrollBarEnabled(false);
380 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
381 scrollTo(newX, 0);
382 mScroller.setFinalX(newX);
383 setHorizontalScrollBarEnabled(true);
384 mFirstLayout = false;
385 }
386
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 final int childCount = getChildCount();
388 int childLeft = 0;
389 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700390 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700391 }
392
393 for (int i = 0; i < childCount; i++) {
394 final View child = getChildAt(i);
395 if (child.getVisibility() != View.GONE) {
396 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700397 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
398 child.layout(childLeft, childHeight,
399 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700400 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 }
402 }
403 }
404
Winson Chunge3193b92010-09-10 11:44:42 -0700405 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700406 if (mFadeInAdjacentScreens) {
407 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700408 int halfScreenSize = getMeasuredWidth() / 2;
409 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700410 final int childCount = getChildCount();
411 for (int i = 0; i < childCount; ++i) {
412 View layout = (View) getChildAt(i);
413 int childWidth = layout.getMeasuredWidth();
414 int halfChildWidth = (childWidth / 2);
415 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700416
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700417 // On the first layout, we may not have a width nor a proper offset, so for now
418 // we should just assume full page width (and calculate the offset according to
419 // that).
420 if (childWidth <= 0) {
421 childWidth = getMeasuredWidth();
422 childCenter = (i * childWidth) + (childWidth / 2);
423 }
424
Winson Chunge8878e32010-09-15 20:37:09 -0700425 int d = halfChildWidth;
426 int distanceFromScreenCenter = childCenter - screenCenter;
427 if (distanceFromScreenCenter > 0) {
428 if (i > 0) {
429 d += getChildAt(i - 1).getMeasuredWidth() / 2;
430 }
Michael Jurka0142d492010-08-25 17:46:15 -0700431 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700432 if (i < childCount - 1) {
433 d += getChildAt(i + 1).getMeasuredWidth() / 2;
434 }
Michael Jurka0142d492010-08-25 17:46:15 -0700435 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700436 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700437
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700438 // Preventing potential divide-by-zero
439 d = Math.max(1, d);
440
Winson Chunge8878e32010-09-15 20:37:09 -0700441 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
442 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
443 float alpha = 1.0f - dimAlpha;
444
Adam Cohenf34bab52010-09-30 14:11:56 -0700445 if (alpha < ALPHA_QUANTIZE_LEVEL) {
446 alpha = 0.0f;
447 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
448 alpha = 1.0f;
449 }
450
Michael Jurka0142d492010-08-25 17:46:15 -0700451 if (Float.compare(alpha, layout.getAlpha()) != 0) {
452 layout.setAlpha(alpha);
453 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
Michael Jurka0142d492010-08-25 17:46:15 -0700455 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 }
457 }
Winson Chunge3193b92010-09-10 11:44:42 -0700458 }
459
Adam Cohenf34bab52010-09-30 14:11:56 -0700460 protected void screenScrolled(int screenCenter) {
461 }
462
Winson Chunge3193b92010-09-10 11:44:42 -0700463 @Override
464 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700465 int halfScreenSize = getMeasuredWidth() / 2;
466 int screenCenter = mScrollX + halfScreenSize;
467
468 if (screenCenter != mLastScreenCenter) {
469 screenScrolled(screenCenter);
470 updateAdjacentPagesAlpha();
471 mLastScreenCenter = screenCenter;
472 }
Michael Jurka0142d492010-08-25 17:46:15 -0700473
474 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700475 // As an optimization, this code assumes that all pages have the same width as the 0th
476 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700477 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700478 if (pageCount > 0) {
479 final int pageWidth = getChildAt(0).getMeasuredWidth();
480 final int screenWidth = getMeasuredWidth();
481 int x = getRelativeChildOffset(0) + pageWidth;
482 int leftScreen = 0;
483 int rightScreen = 0;
484 while (x <= mScrollX) {
485 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700486 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700487 // replace above line with this if you don't assume all pages have same width as 0th
488 // page:
489 // x += getChildAt(leftScreen).getMeasuredWidth();
490 }
491 rightScreen = leftScreen;
492 while (x < mScrollX + screenWidth) {
493 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700494 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700495 // replace above line with this if you don't assume all pages have same width as 0th
496 // page:
497 //if (rightScreen < pageCount) {
498 // x += getChildAt(rightScreen).getMeasuredWidth();
499 //}
500 }
501 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700502
Michael Jurkac4fb9172010-09-02 17:19:20 -0700503 final long drawingTime = getDrawingTime();
504 for (int i = leftScreen; i <= rightScreen; i++) {
505 drawChild(canvas, getChildAt(i), drawingTime);
506 }
Michael Jurka0142d492010-08-25 17:46:15 -0700507 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 }
509
510 @Override
511 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700512 int page = indexOfChild(child);
513 if (page != mCurrentPage || !mScroller.isFinished()) {
514 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 return true;
516 }
517 return false;
518 }
519
520 @Override
521 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700522 int focusablePage;
523 if (mNextPage != INVALID_PAGE) {
524 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700526 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 }
Winson Chung86f77532010-08-24 11:08:22 -0700528 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700529 if (v != null) {
530 v.requestFocus(direction, previouslyFocusedRect);
531 }
532 return false;
533 }
534
535 @Override
536 public boolean dispatchUnhandledMove(View focused, int direction) {
537 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700538 if (getCurrentPage() > 0) {
539 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700540 return true;
541 }
542 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700543 if (getCurrentPage() < getPageCount() - 1) {
544 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 return true;
546 }
547 }
548 return super.dispatchUnhandledMove(focused, direction);
549 }
550
551 @Override
552 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700553 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
554 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 }
556 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700557 if (mCurrentPage > 0) {
558 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 }
560 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700561 if (mCurrentPage < getPageCount() - 1) {
562 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564 }
565 }
566
567 /**
568 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700569 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 *
Winson Chung86f77532010-08-24 11:08:22 -0700571 * This happens when live folders requery, and if they're off page, they
572 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 */
574 @Override
575 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700576 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 View v = focused;
578 while (true) {
579 if (v == current) {
580 super.focusableViewAvailable(focused);
581 return;
582 }
583 if (v == this) {
584 return;
585 }
586 ViewParent parent = v.getParent();
587 if (parent instanceof View) {
588 v = (View)v.getParent();
589 } else {
590 return;
591 }
592 }
593 }
594
595 /**
596 * {@inheritDoc}
597 */
598 @Override
599 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
600 if (disallowIntercept) {
601 // We need to make sure to cancel our long press if
602 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700603 final View currentPage = getChildAt(mCurrentPage);
604 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 }
606 super.requestDisallowInterceptTouchEvent(disallowIntercept);
607 }
608
609 @Override
610 public boolean onInterceptTouchEvent(MotionEvent ev) {
611 /*
612 * This method JUST determines whether we want to intercept the motion.
613 * If we return true, onTouchEvent will be called and we do the actual
614 * scrolling there.
615 */
616
617 /*
618 * Shortcut the most recurring case: the user is in the dragging
619 * state and he is moving his finger. We want to intercept this
620 * motion.
621 */
622 final int action = ev.getAction();
623 if ((action == MotionEvent.ACTION_MOVE) &&
624 (mTouchState == TOUCH_STATE_SCROLLING)) {
625 return true;
626 }
627
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 switch (action & MotionEvent.ACTION_MASK) {
629 case MotionEvent.ACTION_MOVE: {
630 /*
631 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
632 * whether the user has moved far enough from his original down touch.
633 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700634 if (mActivePointerId != INVALID_POINTER) {
635 determineScrollingStart(ev);
636 break;
637 }
638 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
639 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
640 // i.e. fall through to the next case (don't break)
641 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
642 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 }
644
645 case MotionEvent.ACTION_DOWN: {
646 final float x = ev.getX();
647 final float y = ev.getY();
648 // Remember location of down touch
649 mDownMotionX = x;
650 mLastMotionX = x;
651 mLastMotionY = y;
652 mActivePointerId = ev.getPointerId(0);
653 mAllowLongPress = true;
654
655 /*
656 * If being flinged and user touches the screen, initiate drag;
657 * otherwise don't. mScroller.isFinished should be false when
658 * being flinged.
659 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700660 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700661 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
662 if (finishedScrolling) {
663 mTouchState = TOUCH_STATE_REST;
664 mScroller.abortAnimation();
665 } else {
666 mTouchState = TOUCH_STATE_SCROLLING;
667 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700668
Winson Chung86f77532010-08-24 11:08:22 -0700669 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700671 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
673 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700674 int width = getMeasuredWidth();
675 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700676 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700678 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 mTouchState = TOUCH_STATE_NEXT_PAGE;
680 }
681 }
682 }
683 break;
684 }
685
686 case MotionEvent.ACTION_CANCEL:
687 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 mTouchState = TOUCH_STATE_REST;
689 mAllowLongPress = false;
690 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 break;
692
693 case MotionEvent.ACTION_POINTER_UP:
694 onSecondaryPointerUp(ev);
695 break;
696 }
697
698 /*
699 * The only time we want to intercept motion events is if we are in the
700 * drag mode.
701 */
702 return mTouchState != TOUCH_STATE_REST;
703 }
704
Winson Chung80baf5a2010-08-09 16:03:15 -0700705 protected void animateClickFeedback(View v, final Runnable r) {
706 // animate the view slightly to show click feedback running some logic after it is "pressed"
707 Animation anim = AnimationUtils.loadAnimation(getContext(),
708 R.anim.paged_view_click_feedback);
709 anim.setAnimationListener(new AnimationListener() {
710 @Override
711 public void onAnimationStart(Animation animation) {}
712 @Override
713 public void onAnimationRepeat(Animation animation) {
714 r.run();
715 }
716 @Override
717 public void onAnimationEnd(Animation animation) {}
718 });
719 v.startAnimation(anim);
720 }
721
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 /*
723 * Determines if we should change the touch state to start scrolling after the
724 * user moves their touch point too far.
725 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700726 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 /*
728 * Locally do absolute value. mLastMotionX is set to the y value
729 * of the down event.
730 */
731 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
732 final float x = ev.getX(pointerIndex);
733 final float y = ev.getY(pointerIndex);
734 final int xDiff = (int) Math.abs(x - mLastMotionX);
735 final int yDiff = (int) Math.abs(y - mLastMotionY);
736
737 final int touchSlop = mTouchSlop;
738 boolean xPaged = xDiff > mPagingTouchSlop;
739 boolean xMoved = xDiff > touchSlop;
740 boolean yMoved = yDiff > touchSlop;
741
742 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700743 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700744 // Scroll if the user moved far enough along the X axis
745 mTouchState = TOUCH_STATE_SCROLLING;
746 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700747 mTouchX = mScrollX;
748 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
749 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 }
751 // Either way, cancel any pending longpress
752 if (mAllowLongPress) {
753 mAllowLongPress = false;
754 // Try canceling the long press. It could also have been scheduled
755 // by a distant descendant, so use the mAllowLongPress flag to block
756 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700757 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700758 if (currentPage != null) {
759 currentPage.cancelLongPress();
760 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700761 }
762 }
763 }
764
Adam Cohen21f12b52010-10-07 17:15:37 -0700765 protected boolean handlePagingClicks() {
766 return false;
767 }
768
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 @Override
770 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700771 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700772
773 final int action = ev.getAction();
774
775 switch (action & MotionEvent.ACTION_MASK) {
776 case MotionEvent.ACTION_DOWN:
777 /*
778 * If being flinged and user touches, stop the fling. isFinished
779 * will be false if being flinged.
780 */
781 if (!mScroller.isFinished()) {
782 mScroller.abortAnimation();
783 }
784
785 // Remember where the motion event started
786 mDownMotionX = mLastMotionX = ev.getX();
787 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700788 if (mTouchState == TOUCH_STATE_SCROLLING) {
789 pageBeginMoving();
790 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 break;
792
793 case MotionEvent.ACTION_MOVE:
794 if (mTouchState == TOUCH_STATE_SCROLLING) {
795 // Scroll to follow the motion event
796 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
797 final float x = ev.getX(pointerIndex);
798 final int deltaX = (int) (mLastMotionX - x);
799 mLastMotionX = x;
800
801 int sx = getScrollX();
802 if (deltaX < 0) {
803 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700804 mTouchX += Math.max(-mTouchX, deltaX);
805 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
806 if (!mDeferScrollUpdate) {
807 scrollBy(Math.max(-sx, deltaX), 0);
808 } else {
809 // This will trigger a call to computeScroll() on next drawChild() call
810 invalidate();
811 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 }
813 } else if (deltaX > 0) {
814 final int lastChildIndex = getChildCount() - 1;
815 final int availableToScroll = getChildOffset(lastChildIndex) -
816 getRelativeChildOffset(lastChildIndex) - sx;
817 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700818 mTouchX += Math.min(availableToScroll, deltaX);
819 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
820 if (!mDeferScrollUpdate) {
821 scrollBy(Math.min(availableToScroll, deltaX), 0);
822 } else {
823 // This will trigger a call to computeScroll() on next drawChild() call
824 invalidate();
825 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 }
827 } else {
828 awakenScrollBars();
829 }
Adam Cohen564976a2010-10-13 18:52:07 -0700830 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 determineScrollingStart(ev);
832 }
833 break;
834
835 case MotionEvent.ACTION_UP:
836 if (mTouchState == TOUCH_STATE_SCROLLING) {
837 final int activePointerId = mActivePointerId;
838 final int pointerIndex = ev.findPointerIndex(activePointerId);
839 final float x = ev.getX(pointerIndex);
840 final VelocityTracker velocityTracker = mVelocityTracker;
841 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
842 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700843 final int deltaX = (int) (x - mDownMotionX);
844 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
845 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700846
Michael Jurka0142d492010-08-25 17:46:15 -0700847 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700848 if ((isSignificantMove && deltaX > 0 ||
849 (isfling && velocityX > snapVelocity)) &&
850 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700851 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700852 } else if ((isSignificantMove && deltaX < 0 ||
853 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700854 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700855 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 } else {
857 snapToDestination();
858 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700859 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 // at this point we have not moved beyond the touch slop
861 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
862 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700863 int nextPage = Math.max(0, mCurrentPage - 1);
864 if (nextPage != mCurrentPage) {
865 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 } else {
867 snapToDestination();
868 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700869 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 // at this point we have not moved beyond the touch slop
871 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
872 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700873 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
874 if (nextPage != mCurrentPage) {
875 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 } else {
877 snapToDestination();
878 }
879 }
880 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_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700886 if (mTouchState == TOUCH_STATE_SCROLLING) {
887 snapToDestination();
888 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 mTouchState = TOUCH_STATE_REST;
890 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700891 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 break;
893
894 case MotionEvent.ACTION_POINTER_UP:
895 onSecondaryPointerUp(ev);
896 break;
897 }
898
899 return true;
900 }
901
Michael Jurkab8f06722010-10-10 15:58:46 -0700902 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
903 if (mVelocityTracker == null) {
904 mVelocityTracker = VelocityTracker.obtain();
905 }
906 mVelocityTracker.addMovement(ev);
907 }
908
909 private void releaseVelocityTracker() {
910 if (mVelocityTracker != null) {
911 mVelocityTracker.recycle();
912 mVelocityTracker = null;
913 }
914 }
915
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 private void onSecondaryPointerUp(MotionEvent ev) {
917 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
918 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
919 final int pointerId = ev.getPointerId(pointerIndex);
920 if (pointerId == mActivePointerId) {
921 // This was our active pointer going up. Choose a new
922 // active pointer and adjust accordingly.
923 // TODO: Make this decision more intelligent.
924 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
925 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
926 mLastMotionY = ev.getY(newPointerIndex);
927 mActivePointerId = ev.getPointerId(newPointerIndex);
928 if (mVelocityTracker != null) {
929 mVelocityTracker.clear();
930 }
931 }
932 }
933
934 @Override
935 public void requestChildFocus(View child, View focused) {
936 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700937 int page = indexOfChild(child);
938 if (page >= 0 && !isInTouchMode()) {
939 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 }
941 }
942
Winson Chunge3193b92010-09-10 11:44:42 -0700943 protected int getChildIndexForRelativeOffset(int relativeOffset) {
944 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700945 int left;
946 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700947 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700948 left = getRelativeChildOffset(i);
949 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700950 if (left <= relativeOffset && relativeOffset <= right) {
951 return i;
952 }
Winson Chunge3193b92010-09-10 11:44:42 -0700953 }
954 return -1;
955 }
956
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 protected int getRelativeChildOffset(int index) {
958 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
959 }
960
961 protected int getChildOffset(int index) {
962 if (getChildCount() == 0)
963 return 0;
964
965 int offset = getRelativeChildOffset(0);
966 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700967 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 }
969 return offset;
970 }
971
Adam Cohend19d3ca2010-09-15 14:43:42 -0700972 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 int minDistanceFromScreenCenter = getMeasuredWidth();
974 int minDistanceFromScreenCenterIndex = -1;
975 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
976 final int childCount = getChildCount();
977 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700978 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 int childWidth = layout.getMeasuredWidth();
980 int halfChildWidth = (childWidth / 2);
981 int childCenter = getChildOffset(i) + halfChildWidth;
982 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
983 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
984 minDistanceFromScreenCenter = distanceFromScreenCenter;
985 minDistanceFromScreenCenterIndex = i;
986 }
987 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700988 return minDistanceFromScreenCenterIndex;
989 }
990
991 protected void snapToDestination() {
992 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 }
994
Michael Jurka0142d492010-08-25 17:46:15 -0700995 protected void snapToPageWithVelocity(int whichPage, int velocity) {
996 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
997 // can use it
998 snapToPage(whichPage);
999 }
1000
1001 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001002 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 }
1004
Michael Jurka0142d492010-08-25 17:46:15 -07001005 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001006 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001007
Winson Chung86f77532010-08-24 11:08:22 -07001008 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 final int sX = getScrollX();
1010 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001011 snapToPage(whichPage, delta, duration);
1012 }
1013
1014 protected void snapToPage(int whichPage, int delta, int duration) {
1015 mNextPage = whichPage;
1016
1017 View focusedChild = getFocusedChild();
1018 if (focusedChild != null && whichPage != mCurrentPage &&
1019 focusedChild == getChildAt(mCurrentPage)) {
1020 focusedChild.clearFocus();
1021 }
1022
1023 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 awakenScrollBars(duration);
1025 if (duration == 0) {
1026 duration = Math.abs(delta);
1027 }
1028
1029 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001030 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001031
1032 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001033 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001034 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 invalidate();
1036 }
1037
1038 @Override
1039 protected Parcelable onSaveInstanceState() {
1040 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001041 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 return state;
1043 }
1044
1045 @Override
1046 protected void onRestoreInstanceState(Parcelable state) {
1047 SavedState savedState = (SavedState) state;
1048 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001049 if (savedState.currentPage != -1) {
1050 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 }
1052 }
1053
1054 public void scrollLeft() {
1055 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001056 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001058 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 }
1060 }
1061
1062 public void scrollRight() {
1063 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001064 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001066 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068 }
1069
Winson Chung86f77532010-08-24 11:08:22 -07001070 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 int result = -1;
1072 if (v != null) {
1073 ViewParent vp = v.getParent();
1074 int count = getChildCount();
1075 for (int i = 0; i < count; i++) {
1076 if (vp == getChildAt(i)) {
1077 return i;
1078 }
1079 }
1080 }
1081 return result;
1082 }
1083
1084 /**
1085 * @return True is long presses are still allowed for the current touch
1086 */
1087 public boolean allowLongPress() {
1088 return mAllowLongPress;
1089 }
1090
Michael Jurka0142d492010-08-25 17:46:15 -07001091 /**
1092 * Set true to allow long-press events to be triggered, usually checked by
1093 * {@link Launcher} to accept or block dpad-initiated long-presses.
1094 */
1095 public void setAllowLongPress(boolean allowLongPress) {
1096 mAllowLongPress = allowLongPress;
1097 }
1098
Winson Chung321e9ee2010-08-09 13:37:56 -07001099 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001100 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001101
1102 SavedState(Parcelable superState) {
1103 super(superState);
1104 }
1105
1106 private SavedState(Parcel in) {
1107 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001108 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001109 }
1110
1111 @Override
1112 public void writeToParcel(Parcel out, int flags) {
1113 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001114 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 }
1116
1117 public static final Parcelable.Creator<SavedState> CREATOR =
1118 new Parcelable.Creator<SavedState>() {
1119 public SavedState createFromParcel(Parcel in) {
1120 return new SavedState(in);
1121 }
1122
1123 public SavedState[] newArray(int size) {
1124 return new SavedState[size];
1125 }
1126 };
1127 }
1128
Winson Chung86f77532010-08-24 11:08:22 -07001129 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001130 if (mContentIsRefreshable) {
1131 final int count = getChildCount();
1132 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001133 int lowerPageBound = getAssociatedLowerPageBound(page);
1134 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001135 for (int i = 0; i < count; ++i) {
1136 final ViewGroup layout = (ViewGroup) getChildAt(i);
1137 final int childCount = layout.getChildCount();
1138 if (lowerPageBound <= i && i <= upperPageBound) {
1139 if (mDirtyPageContent.get(i)) {
1140 syncPageItems(i);
1141 mDirtyPageContent.set(i, false);
1142 }
1143 } else {
1144 if (childCount > 0) {
1145 layout.removeAllViews();
1146 }
1147 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001148 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001149 }
1150 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001151 }
1152 }
1153
Winson Chunge3193b92010-09-10 11:44:42 -07001154 protected int getAssociatedLowerPageBound(int page) {
1155 return Math.max(0, page - 1);
1156 }
1157 protected int getAssociatedUpperPageBound(int page) {
1158 final int count = getChildCount();
1159 return Math.min(page + 1, count - 1);
1160 }
1161
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001162 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001163 if (isChoiceMode(CHOICE_MODE_NONE)) {
1164 mChoiceMode = mode;
1165 mActionMode = startActionMode(callback);
1166 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001167 }
1168
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001169 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001170 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001171 mChoiceMode = CHOICE_MODE_NONE;
1172 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001173 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001174 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001175 }
1176 }
1177
1178 protected boolean isChoiceMode(int mode) {
1179 return mChoiceMode == mode;
1180 }
1181
1182 protected ArrayList<Checkable> getCheckedGrandchildren() {
1183 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1184 final int childCount = getChildCount();
1185 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001186 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001187 final int grandChildCount = layout.getChildCount();
1188 for (int j = 0; j < grandChildCount; ++j) {
1189 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001190 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001191 checked.add((Checkable) v);
1192 }
1193 }
1194 }
1195 return checked;
1196 }
1197
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001198 /**
1199 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1200 * Otherwise, returns null.
1201 */
1202 protected Checkable getSingleCheckedGrandchild() {
1203 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1204 final int childCount = getChildCount();
1205 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001206 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001207 final int grandChildCount = layout.getChildCount();
1208 for (int j = 0; j < grandChildCount; ++j) {
1209 final View v = layout.getChildAt(j);
1210 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1211 return (Checkable) v;
1212 }
1213 }
1214 }
1215 }
1216 return null;
1217 }
1218
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001219 public Object getChosenItem() {
1220 View checkedView = (View) getSingleCheckedGrandchild();
1221 if (checkedView != null) {
1222 return checkedView.getTag();
1223 }
1224 return null;
1225 }
1226
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001227 protected void resetCheckedGrandchildren() {
1228 // loop through children, and set all of their children to _not_ be checked
1229 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1230 for (int i = 0; i < checked.size(); ++i) {
1231 final Checkable c = checked.get(i);
1232 c.setChecked(false);
1233 }
1234 }
1235
Winson Chung86f77532010-08-24 11:08:22 -07001236 /**
1237 * This method is called ONLY to synchronize the number of pages that the paged view has.
1238 * To actually fill the pages with information, implement syncPageItems() below. It is
1239 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1240 * and therefore, individual page items do not need to be updated in this method.
1241 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001243
1244 /**
1245 * This method is called to synchronize the items that are on a particular page. If views on
1246 * the page can be reused, then they should be updated within this method.
1247 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001249
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001251 if (mContentIsRefreshable) {
1252 // Update all the pages
1253 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001254
Michael Jurka0142d492010-08-25 17:46:15 -07001255 // Mark each of the pages as dirty
1256 final int count = getChildCount();
1257 mDirtyPageContent.clear();
1258 for (int i = 0; i < count; ++i) {
1259 mDirtyPageContent.add(true);
1260 }
1261
1262 // Load any pages that are necessary for the current window of views
1263 loadAssociatedPages(mCurrentPage);
1264 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001265 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001266 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001267 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 }
1269}