blob: d4dffe6b0088f3abc772e1592e54e867bd91218a [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 Chunge22a8e92010-11-12 13:40:58 -080060 private static final int PAGE_SNAP_ANIMATION_DURATION = 750;
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;
Michael Jurka7426c422010-11-11 15:23:47 -080077 protected float mLastMotionX;
78 protected 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
Michael Jurka7426c422010-11-11 15:23:47 -080091 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070092
Michael Jurka7426c422010-11-11 15:23:47 -080093 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070094 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 Chung7da10252010-10-28 16:07:04 -0700105 protected boolean mCenterPagesVertically;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka5f1c5092010-09-03 14:15:02 -0700107 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Michael Jurka5f1c5092010-09-03 14:15:02 -0700109 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Winson Chung86f77532010-08-24 11:08:22 -0700111 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Winson Chung86f77532010-08-24 11:08:22 -0700113 private ArrayList<Boolean> mDirtyPageContent;
114 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700116 // choice modes
117 protected static final int CHOICE_MODE_NONE = 0;
118 protected static final int CHOICE_MODE_SINGLE = 1;
119 // Multiple selection mode is not supported by all Launcher actions atm
120 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700121
Michael Jurkae17e19c2010-09-28 11:01:39 -0700122 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700123 private ActionMode mActionMode;
124
Winson Chung241c3b42010-08-25 16:53:03 -0700125 protected PagedViewIconCache mPageViewIconCache;
126
Michael Jurka0142d492010-08-25 17:46:15 -0700127 // If true, syncPages and syncPageItems will be called to refresh pages
128 protected boolean mContentIsRefreshable = true;
129
130 // If true, modify alpha of neighboring pages as user scrolls left/right
131 protected boolean mFadeInAdjacentScreens = true;
132
133 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
134 // to switch to a new page
135 protected boolean mUsePagingTouchSlop = true;
136
137 // If true, the subclass should directly update mScrollX itself in its computeScroll method
138 // (SmoothPagedView does this)
139 protected boolean mDeferScrollUpdate = false;
140
Patrick Dubroy1262e362010-10-06 15:49:50 -0700141 protected boolean mIsPageMoving = false;
142
Winson Chung241c3b42010-08-25 16:53:03 -0700143 /**
144 * Simple cache mechanism for PagedViewIcon outlines.
145 */
146 class PagedViewIconCache {
147 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
148
149 public void clear() {
150 iconOutlineCache.clear();
151 }
152 public void addOutline(Object key, Bitmap b) {
153 iconOutlineCache.put(key, b);
154 }
155 public void removeOutline(Object key) {
156 if (iconOutlineCache.containsKey(key)) {
157 iconOutlineCache.remove(key);
158 }
159 }
160 public Bitmap getOutline(Object key) {
161 return iconOutlineCache.get(key);
162 }
163 }
164
Winson Chung86f77532010-08-24 11:08:22 -0700165 public interface PageSwitchListener {
166 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 }
168
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 public PagedView(Context context) {
170 this(context, null);
171 }
172
173 public PagedView(Context context, AttributeSet attrs) {
174 this(context, attrs, 0);
175 }
176
177 public PagedView(Context context, AttributeSet attrs, int defStyle) {
178 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Adam Cohen9c4949e2010-10-05 12:27:22 -0700181 TypedArray a = context.obtainStyledAttributes(attrs,
182 R.styleable.PagedView, defStyle, 0);
183 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
184 mPageLayoutPaddingTop = a.getDimensionPixelSize(
185 R.styleable.PagedView_pageLayoutPaddingTop, 10);
186 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
187 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
188 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
189 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
190 mPageLayoutPaddingRight = a.getDimensionPixelSize(
191 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700192 mPageLayoutWidthGap = a.getDimensionPixelSize(
193 R.styleable.PagedView_pageLayoutWidthGap, -1);
194 mPageLayoutHeightGap = a.getDimensionPixelSize(
195 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 a.recycle();
197
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700199 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
202 /**
203 * Initializes various states for this workspace.
204 */
Michael Jurka0142d492010-08-25 17:46:15 -0700205 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700206 mDirtyPageContent = new ArrayList<Boolean>();
207 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700208 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700210 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700211 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
217 }
218
Winson Chung86f77532010-08-24 11:08:22 -0700219 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
220 mPageSwitchListener = pageSwitchListener;
221 if (mPageSwitchListener != null) {
222 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224 }
225
226 /**
Winson Chung86f77532010-08-24 11:08:22 -0700227 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 *
Winson Chung86f77532010-08-24 11:08:22 -0700229 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 */
Winson Chung86f77532010-08-24 11:08:22 -0700231 int getCurrentPage() {
232 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 }
234
Winson Chung86f77532010-08-24 11:08:22 -0700235 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Winson Chung86f77532010-08-24 11:08:22 -0700239 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
243 int getScrollWidth() {
244 return getWidth();
245 }
246
247 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 * Updates the scroll of the current page immediately to its final scroll position. We use this
249 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
250 * the previous tab page.
251 */
252 protected void updateCurrentPageScroll() {
253 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
254 scrollTo(newX, 0);
255 mScroller.setFinalX(newX);
256 }
257
258 /**
Winson Chung86f77532010-08-24 11:08:22 -0700259 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700260 */
Winson Chung86f77532010-08-24 11:08:22 -0700261 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800262 if (!mScroller.isFinished()) {
263 mScroller.abortAnimation();
264 }
265 if (getChildCount() == 0 || currentPage == mCurrentPage) {
266 return;
267 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700268
Winson Chung86f77532010-08-24 11:08:22 -0700269 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800270 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700271
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700273 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 }
275
Michael Jurka0142d492010-08-25 17:46:15 -0700276 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700277 if (mPageSwitchListener != null) {
278 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 }
280 }
281
Patrick Dubroy1262e362010-10-06 15:49:50 -0700282 private void pageBeginMoving() {
283 mIsPageMoving = true;
284 onPageBeginMoving();
285 }
286
287 private void pageEndMoving() {
288 onPageEndMoving();
289 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700290 }
291
292 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 protected void onPageBeginMoving() {
294 }
295
296 // a method that subclasses can override to add behavior
297 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700298 }
299
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 /**
Winson Chung86f77532010-08-24 11:08:22 -0700301 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 *
303 * @param l The listener used to respond to long clicks.
304 */
305 @Override
306 public void setOnLongClickListener(OnLongClickListener l) {
307 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700308 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700310 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 }
312 }
313
314 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700315 public void scrollTo(int x, int y) {
316 super.scrollTo(x, y);
317 mTouchX = x;
318 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
319 }
320
321 // we moved this functionality to a helper function so SmoothPagedView can reuse it
322 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700324 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700326 invalidate();
327 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700328 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700329 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700330 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700331 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700332 notifyPageSwitchListener();
333 pageEndMoving();
334 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 }
Michael Jurka0142d492010-08-25 17:46:15 -0700336 return false;
337 }
338
339 @Override
340 public void computeScroll() {
341 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 }
343
344 @Override
345 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
346 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
347 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
348 if (widthMode != MeasureSpec.EXACTLY) {
349 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
350 }
351
352 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
353 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
354 if (heightMode != MeasureSpec.EXACTLY) {
355 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
356 }
357
358 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700359 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 final int childCount = getChildCount();
361 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700362 // disallowing padding in paged view (just pass 0)
363 final View child = getChildAt(i);
364 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
365
366 int childWidthMode;
367 if (lp.width == LayoutParams.WRAP_CONTENT) {
368 childWidthMode = MeasureSpec.AT_MOST;
369 } else {
370 childWidthMode = MeasureSpec.EXACTLY;
371 }
372
373 int childHeightMode;
374 if (lp.height == LayoutParams.WRAP_CONTENT) {
375 childHeightMode = MeasureSpec.AT_MOST;
376 } else {
377 childHeightMode = MeasureSpec.EXACTLY;
378 }
379
380 final int childWidthMeasureSpec =
381 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
382 final int childHeightMeasureSpec =
383 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
384
385 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 }
387
388 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
390
391 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700392 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700393 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
394 setHorizontalScrollBarEnabled(false);
395 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
396 scrollTo(newX, 0);
397 mScroller.setFinalX(newX);
398 setHorizontalScrollBarEnabled(true);
399 mFirstLayout = false;
400 }
401
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 final int childCount = getChildCount();
403 int childLeft = 0;
404 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700405 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 }
407
408 for (int i = 0; i < childCount; i++) {
409 final View child = getChildAt(i);
410 if (child.getVisibility() != View.GONE) {
411 final int childWidth = child.getMeasuredWidth();
Winson Chung7da10252010-10-28 16:07:04 -0700412 final int childHeight = (mCenterPagesVertically ?
413 (getMeasuredHeight() - child.getMeasuredHeight()) / 2 : 0);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700414 child.layout(childLeft, childHeight,
415 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700416 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
418 }
419 }
420
Winson Chunge3193b92010-09-10 11:44:42 -0700421 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700422 if (mFadeInAdjacentScreens) {
423 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700424 int halfScreenSize = getMeasuredWidth() / 2;
425 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700426 final int childCount = getChildCount();
427 for (int i = 0; i < childCount; ++i) {
428 View layout = (View) getChildAt(i);
429 int childWidth = layout.getMeasuredWidth();
430 int halfChildWidth = (childWidth / 2);
431 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700432
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700433 // On the first layout, we may not have a width nor a proper offset, so for now
434 // we should just assume full page width (and calculate the offset according to
435 // that).
436 if (childWidth <= 0) {
437 childWidth = getMeasuredWidth();
438 childCenter = (i * childWidth) + (childWidth / 2);
439 }
440
Winson Chunge8878e32010-09-15 20:37:09 -0700441 int d = halfChildWidth;
442 int distanceFromScreenCenter = childCenter - screenCenter;
443 if (distanceFromScreenCenter > 0) {
444 if (i > 0) {
445 d += getChildAt(i - 1).getMeasuredWidth() / 2;
446 }
Michael Jurka0142d492010-08-25 17:46:15 -0700447 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700448 if (i < childCount - 1) {
449 d += getChildAt(i + 1).getMeasuredWidth() / 2;
450 }
Michael Jurka0142d492010-08-25 17:46:15 -0700451 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700452 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700453
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700454 // Preventing potential divide-by-zero
455 d = Math.max(1, d);
456
Winson Chunge8878e32010-09-15 20:37:09 -0700457 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
458 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
459 float alpha = 1.0f - dimAlpha;
460
Adam Cohenf34bab52010-09-30 14:11:56 -0700461 if (alpha < ALPHA_QUANTIZE_LEVEL) {
462 alpha = 0.0f;
463 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
464 alpha = 1.0f;
465 }
466
Michael Jurka0142d492010-08-25 17:46:15 -0700467 if (Float.compare(alpha, layout.getAlpha()) != 0) {
468 layout.setAlpha(alpha);
469 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 }
Michael Jurka0142d492010-08-25 17:46:15 -0700471 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 }
473 }
Winson Chunge3193b92010-09-10 11:44:42 -0700474 }
475
Adam Cohenf34bab52010-09-30 14:11:56 -0700476 protected void screenScrolled(int screenCenter) {
477 }
478
Winson Chunge3193b92010-09-10 11:44:42 -0700479 @Override
480 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700481 int halfScreenSize = getMeasuredWidth() / 2;
482 int screenCenter = mScrollX + halfScreenSize;
483
484 if (screenCenter != mLastScreenCenter) {
485 screenScrolled(screenCenter);
486 updateAdjacentPagesAlpha();
487 mLastScreenCenter = screenCenter;
488 }
Michael Jurka0142d492010-08-25 17:46:15 -0700489
490 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700491 // As an optimization, this code assumes that all pages have the same width as the 0th
492 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700493 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700494 if (pageCount > 0) {
495 final int pageWidth = getChildAt(0).getMeasuredWidth();
496 final int screenWidth = getMeasuredWidth();
497 int x = getRelativeChildOffset(0) + pageWidth;
498 int leftScreen = 0;
499 int rightScreen = 0;
500 while (x <= mScrollX) {
501 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700502 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700503 // replace above line with this if you don't assume all pages have same width as 0th
504 // page:
505 // x += getChildAt(leftScreen).getMeasuredWidth();
506 }
507 rightScreen = leftScreen;
508 while (x < mScrollX + screenWidth) {
509 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700510 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700511 // replace above line with this if you don't assume all pages have same width as 0th
512 // page:
513 //if (rightScreen < pageCount) {
514 // x += getChildAt(rightScreen).getMeasuredWidth();
515 //}
516 }
517 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700518
Michael Jurkac4fb9172010-09-02 17:19:20 -0700519 final long drawingTime = getDrawingTime();
520 for (int i = leftScreen; i <= rightScreen; i++) {
521 drawChild(canvas, getChildAt(i), drawingTime);
522 }
Michael Jurka0142d492010-08-25 17:46:15 -0700523 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525
526 @Override
527 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700528 int page = indexOfChild(child);
529 if (page != mCurrentPage || !mScroller.isFinished()) {
530 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700531 return true;
532 }
533 return false;
534 }
535
536 @Override
537 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700538 int focusablePage;
539 if (mNextPage != INVALID_PAGE) {
540 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700542 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 }
Winson Chung86f77532010-08-24 11:08:22 -0700544 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 if (v != null) {
546 v.requestFocus(direction, previouslyFocusedRect);
547 }
548 return false;
549 }
550
551 @Override
552 public boolean dispatchUnhandledMove(View focused, int direction) {
553 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700554 if (getCurrentPage() > 0) {
555 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 return true;
557 }
558 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700559 if (getCurrentPage() < getPageCount() - 1) {
560 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700561 return true;
562 }
563 }
564 return super.dispatchUnhandledMove(focused, direction);
565 }
566
567 @Override
568 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700569 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
570 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700571 }
572 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700573 if (mCurrentPage > 0) {
574 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 }
576 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700577 if (mCurrentPage < getPageCount() - 1) {
578 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 }
580 }
581 }
582
583 /**
584 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700585 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700586 *
Winson Chung86f77532010-08-24 11:08:22 -0700587 * This happens when live folders requery, and if they're off page, they
588 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 */
590 @Override
591 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700592 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700593 View v = focused;
594 while (true) {
595 if (v == current) {
596 super.focusableViewAvailable(focused);
597 return;
598 }
599 if (v == this) {
600 return;
601 }
602 ViewParent parent = v.getParent();
603 if (parent instanceof View) {
604 v = (View)v.getParent();
605 } else {
606 return;
607 }
608 }
609 }
610
611 /**
612 * {@inheritDoc}
613 */
614 @Override
615 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
616 if (disallowIntercept) {
617 // We need to make sure to cancel our long press if
618 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700619 final View currentPage = getChildAt(mCurrentPage);
620 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 }
622 super.requestDisallowInterceptTouchEvent(disallowIntercept);
623 }
624
625 @Override
626 public boolean onInterceptTouchEvent(MotionEvent ev) {
627 /*
628 * This method JUST determines whether we want to intercept the motion.
629 * If we return true, onTouchEvent will be called and we do the actual
630 * scrolling there.
631 */
632
Winson Chung45e1d6e2010-11-09 17:19:49 -0800633 // Skip touch handling if there are no pages to swipe
634 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
635
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 /*
637 * Shortcut the most recurring case: the user is in the dragging
638 * state and he is moving his finger. We want to intercept this
639 * motion.
640 */
641 final int action = ev.getAction();
642 if ((action == MotionEvent.ACTION_MOVE) &&
643 (mTouchState == TOUCH_STATE_SCROLLING)) {
644 return true;
645 }
646
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 switch (action & MotionEvent.ACTION_MASK) {
648 case MotionEvent.ACTION_MOVE: {
649 /*
650 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
651 * whether the user has moved far enough from his original down touch.
652 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700653 if (mActivePointerId != INVALID_POINTER) {
654 determineScrollingStart(ev);
655 break;
656 }
657 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
658 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
659 // i.e. fall through to the next case (don't break)
660 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
661 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 }
663
664 case MotionEvent.ACTION_DOWN: {
665 final float x = ev.getX();
666 final float y = ev.getY();
667 // Remember location of down touch
668 mDownMotionX = x;
669 mLastMotionX = x;
670 mLastMotionY = y;
671 mActivePointerId = ev.getPointerId(0);
672 mAllowLongPress = true;
673
674 /*
675 * If being flinged and user touches the screen, initiate drag;
676 * otherwise don't. mScroller.isFinished should be false when
677 * being flinged.
678 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700679 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700680 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
681 if (finishedScrolling) {
682 mTouchState = TOUCH_STATE_REST;
683 mScroller.abortAnimation();
684 } else {
685 mTouchState = TOUCH_STATE_SCROLLING;
686 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700687
Winson Chung86f77532010-08-24 11:08:22 -0700688 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700690 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
692 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700693 int width = getMeasuredWidth();
694 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700695 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700696 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700697 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700698 mTouchState = TOUCH_STATE_NEXT_PAGE;
699 }
700 }
701 }
702 break;
703 }
704
705 case MotionEvent.ACTION_CANCEL:
706 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700707 mTouchState = TOUCH_STATE_REST;
708 mAllowLongPress = false;
709 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 break;
711
712 case MotionEvent.ACTION_POINTER_UP:
713 onSecondaryPointerUp(ev);
714 break;
715 }
716
717 /*
718 * The only time we want to intercept motion events is if we are in the
719 * drag mode.
720 */
721 return mTouchState != TOUCH_STATE_REST;
722 }
723
Winson Chung80baf5a2010-08-09 16:03:15 -0700724 protected void animateClickFeedback(View v, final Runnable r) {
725 // animate the view slightly to show click feedback running some logic after it is "pressed"
726 Animation anim = AnimationUtils.loadAnimation(getContext(),
727 R.anim.paged_view_click_feedback);
728 anim.setAnimationListener(new AnimationListener() {
729 @Override
730 public void onAnimationStart(Animation animation) {}
731 @Override
732 public void onAnimationRepeat(Animation animation) {
733 r.run();
734 }
735 @Override
736 public void onAnimationEnd(Animation animation) {}
737 });
738 v.startAnimation(anim);
739 }
740
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 /*
742 * Determines if we should change the touch state to start scrolling after the
743 * user moves their touch point too far.
744 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700745 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 /*
747 * Locally do absolute value. mLastMotionX is set to the y value
748 * of the down event.
749 */
750 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
751 final float x = ev.getX(pointerIndex);
752 final float y = ev.getY(pointerIndex);
753 final int xDiff = (int) Math.abs(x - mLastMotionX);
754 final int yDiff = (int) Math.abs(y - mLastMotionY);
755
756 final int touchSlop = mTouchSlop;
757 boolean xPaged = xDiff > mPagingTouchSlop;
758 boolean xMoved = xDiff > touchSlop;
759 boolean yMoved = yDiff > touchSlop;
760
761 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700762 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 // Scroll if the user moved far enough along the X axis
764 mTouchState = TOUCH_STATE_SCROLLING;
765 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700766 mTouchX = mScrollX;
767 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
768 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 }
770 // Either way, cancel any pending longpress
771 if (mAllowLongPress) {
772 mAllowLongPress = false;
773 // Try canceling the long press. It could also have been scheduled
774 // by a distant descendant, so use the mAllowLongPress flag to block
775 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700776 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700777 if (currentPage != null) {
778 currentPage.cancelLongPress();
779 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 }
781 }
782 }
783
Adam Cohen21f12b52010-10-07 17:15:37 -0700784 protected boolean handlePagingClicks() {
785 return false;
786 }
787
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 @Override
789 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800790 // Skip touch handling if there are no pages to swipe
791 if (getChildCount() <= 0) return super.onTouchEvent(ev);
792
Michael Jurkab8f06722010-10-10 15:58:46 -0700793 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794
795 final int action = ev.getAction();
796
797 switch (action & MotionEvent.ACTION_MASK) {
798 case MotionEvent.ACTION_DOWN:
799 /*
800 * If being flinged and user touches, stop the fling. isFinished
801 * will be false if being flinged.
802 */
803 if (!mScroller.isFinished()) {
804 mScroller.abortAnimation();
805 }
806
807 // Remember where the motion event started
808 mDownMotionX = mLastMotionX = ev.getX();
809 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700810 if (mTouchState == TOUCH_STATE_SCROLLING) {
811 pageBeginMoving();
812 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 break;
814
815 case MotionEvent.ACTION_MOVE:
816 if (mTouchState == TOUCH_STATE_SCROLLING) {
817 // Scroll to follow the motion event
818 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
819 final float x = ev.getX(pointerIndex);
820 final int deltaX = (int) (mLastMotionX - x);
821 mLastMotionX = x;
822
823 int sx = getScrollX();
824 if (deltaX < 0) {
825 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700826 mTouchX += Math.max(-mTouchX, deltaX);
827 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
828 if (!mDeferScrollUpdate) {
829 scrollBy(Math.max(-sx, deltaX), 0);
830 } else {
831 // This will trigger a call to computeScroll() on next drawChild() call
832 invalidate();
833 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 }
835 } else if (deltaX > 0) {
836 final int lastChildIndex = getChildCount() - 1;
837 final int availableToScroll = getChildOffset(lastChildIndex) -
838 getRelativeChildOffset(lastChildIndex) - sx;
839 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700840 mTouchX += Math.min(availableToScroll, deltaX);
841 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
842 if (!mDeferScrollUpdate) {
843 scrollBy(Math.min(availableToScroll, deltaX), 0);
844 } else {
845 // This will trigger a call to computeScroll() on next drawChild() call
846 invalidate();
847 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 }
849 } else {
850 awakenScrollBars();
851 }
Adam Cohen564976a2010-10-13 18:52:07 -0700852 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 determineScrollingStart(ev);
854 }
855 break;
856
857 case MotionEvent.ACTION_UP:
858 if (mTouchState == TOUCH_STATE_SCROLLING) {
859 final int activePointerId = mActivePointerId;
860 final int pointerIndex = ev.findPointerIndex(activePointerId);
861 final float x = ev.getX(pointerIndex);
862 final VelocityTracker velocityTracker = mVelocityTracker;
863 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
864 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700865 final int deltaX = (int) (x - mDownMotionX);
866 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
867 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700868
Michael Jurka0142d492010-08-25 17:46:15 -0700869 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700870 if ((isSignificantMove && deltaX > 0 ||
871 (isfling && velocityX > snapVelocity)) &&
872 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700873 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700874 } else if ((isSignificantMove && deltaX < 0 ||
875 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700876 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700877 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 } else {
879 snapToDestination();
880 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700881 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 // at this point we have not moved beyond the touch slop
883 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
884 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700885 int nextPage = Math.max(0, mCurrentPage - 1);
886 if (nextPage != mCurrentPage) {
887 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 } else {
889 snapToDestination();
890 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700891 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 // at this point we have not moved beyond the touch slop
893 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
894 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700895 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
896 if (nextPage != mCurrentPage) {
897 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 } else {
899 snapToDestination();
900 }
901 }
902 mTouchState = TOUCH_STATE_REST;
903 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700904 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 break;
906
907 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700908 if (mTouchState == TOUCH_STATE_SCROLLING) {
909 snapToDestination();
910 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 mTouchState = TOUCH_STATE_REST;
912 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700913 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 break;
915
916 case MotionEvent.ACTION_POINTER_UP:
917 onSecondaryPointerUp(ev);
918 break;
919 }
920
921 return true;
922 }
923
Michael Jurkab8f06722010-10-10 15:58:46 -0700924 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
925 if (mVelocityTracker == null) {
926 mVelocityTracker = VelocityTracker.obtain();
927 }
928 mVelocityTracker.addMovement(ev);
929 }
930
931 private void releaseVelocityTracker() {
932 if (mVelocityTracker != null) {
933 mVelocityTracker.recycle();
934 mVelocityTracker = null;
935 }
936 }
937
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 private void onSecondaryPointerUp(MotionEvent ev) {
939 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
940 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
941 final int pointerId = ev.getPointerId(pointerIndex);
942 if (pointerId == mActivePointerId) {
943 // This was our active pointer going up. Choose a new
944 // active pointer and adjust accordingly.
945 // TODO: Make this decision more intelligent.
946 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
947 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
948 mLastMotionY = ev.getY(newPointerIndex);
949 mActivePointerId = ev.getPointerId(newPointerIndex);
950 if (mVelocityTracker != null) {
951 mVelocityTracker.clear();
952 }
953 }
954 }
955
956 @Override
957 public void requestChildFocus(View child, View focused) {
958 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700959 int page = indexOfChild(child);
960 if (page >= 0 && !isInTouchMode()) {
961 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 }
963 }
964
Winson Chunge3193b92010-09-10 11:44:42 -0700965 protected int getChildIndexForRelativeOffset(int relativeOffset) {
966 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700967 int left;
968 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700969 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700970 left = getRelativeChildOffset(i);
971 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700972 if (left <= relativeOffset && relativeOffset <= right) {
973 return i;
974 }
Winson Chunge3193b92010-09-10 11:44:42 -0700975 }
976 return -1;
977 }
978
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 protected int getRelativeChildOffset(int index) {
980 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
981 }
982
983 protected int getChildOffset(int index) {
984 if (getChildCount() == 0)
985 return 0;
986
987 int offset = getRelativeChildOffset(0);
988 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700989 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 }
991 return offset;
992 }
993
Adam Cohend19d3ca2010-09-15 14:43:42 -0700994 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 int minDistanceFromScreenCenter = getMeasuredWidth();
996 int minDistanceFromScreenCenterIndex = -1;
997 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
998 final int childCount = getChildCount();
999 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001000 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 int childWidth = layout.getMeasuredWidth();
1002 int halfChildWidth = (childWidth / 2);
1003 int childCenter = getChildOffset(i) + halfChildWidth;
1004 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1005 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1006 minDistanceFromScreenCenter = distanceFromScreenCenter;
1007 minDistanceFromScreenCenterIndex = i;
1008 }
1009 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001010 return minDistanceFromScreenCenterIndex;
1011 }
1012
1013 protected void snapToDestination() {
1014 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 }
1016
Michael Jurka0142d492010-08-25 17:46:15 -07001017 protected void snapToPageWithVelocity(int whichPage, int velocity) {
1018 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
1019 // can use it
1020 snapToPage(whichPage);
1021 }
1022
1023 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001024 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 }
1026
Michael Jurka0142d492010-08-25 17:46:15 -07001027 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001028 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001029
Winson Chung86f77532010-08-24 11:08:22 -07001030 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 final int sX = getScrollX();
1032 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001033 snapToPage(whichPage, delta, duration);
1034 }
1035
1036 protected void snapToPage(int whichPage, int delta, int duration) {
1037 mNextPage = whichPage;
1038
1039 View focusedChild = getFocusedChild();
1040 if (focusedChild != null && whichPage != mCurrentPage &&
1041 focusedChild == getChildAt(mCurrentPage)) {
1042 focusedChild.clearFocus();
1043 }
1044
1045 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 awakenScrollBars(duration);
1047 if (duration == 0) {
1048 duration = Math.abs(delta);
1049 }
1050
1051 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001052 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001053
1054 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001055 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001056 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 invalidate();
1058 }
1059
1060 @Override
1061 protected Parcelable onSaveInstanceState() {
1062 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001063 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 return state;
1065 }
1066
1067 @Override
1068 protected void onRestoreInstanceState(Parcelable state) {
1069 SavedState savedState = (SavedState) state;
1070 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001071 if (savedState.currentPage != -1) {
1072 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074 }
1075
1076 public void scrollLeft() {
1077 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001078 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001080 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 }
1082 }
1083
1084 public void scrollRight() {
1085 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001086 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001087 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001088 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 }
1090 }
1091
Winson Chung86f77532010-08-24 11:08:22 -07001092 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 int result = -1;
1094 if (v != null) {
1095 ViewParent vp = v.getParent();
1096 int count = getChildCount();
1097 for (int i = 0; i < count; i++) {
1098 if (vp == getChildAt(i)) {
1099 return i;
1100 }
1101 }
1102 }
1103 return result;
1104 }
1105
1106 /**
1107 * @return True is long presses are still allowed for the current touch
1108 */
1109 public boolean allowLongPress() {
1110 return mAllowLongPress;
1111 }
1112
Michael Jurka0142d492010-08-25 17:46:15 -07001113 /**
1114 * Set true to allow long-press events to be triggered, usually checked by
1115 * {@link Launcher} to accept or block dpad-initiated long-presses.
1116 */
1117 public void setAllowLongPress(boolean allowLongPress) {
1118 mAllowLongPress = allowLongPress;
1119 }
1120
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001122 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001123
1124 SavedState(Parcelable superState) {
1125 super(superState);
1126 }
1127
1128 private SavedState(Parcel in) {
1129 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001130 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 }
1132
1133 @Override
1134 public void writeToParcel(Parcel out, int flags) {
1135 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001136 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 }
1138
1139 public static final Parcelable.Creator<SavedState> CREATOR =
1140 new Parcelable.Creator<SavedState>() {
1141 public SavedState createFromParcel(Parcel in) {
1142 return new SavedState(in);
1143 }
1144
1145 public SavedState[] newArray(int size) {
1146 return new SavedState[size];
1147 }
1148 };
1149 }
1150
Winson Chung86f77532010-08-24 11:08:22 -07001151 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001152 if (mContentIsRefreshable) {
1153 final int count = getChildCount();
1154 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001155 int lowerPageBound = getAssociatedLowerPageBound(page);
1156 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001157 for (int i = 0; i < count; ++i) {
1158 final ViewGroup layout = (ViewGroup) getChildAt(i);
1159 final int childCount = layout.getChildCount();
1160 if (lowerPageBound <= i && i <= upperPageBound) {
1161 if (mDirtyPageContent.get(i)) {
1162 syncPageItems(i);
1163 mDirtyPageContent.set(i, false);
1164 }
1165 } else {
1166 if (childCount > 0) {
1167 layout.removeAllViews();
1168 }
1169 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001170 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001171 }
1172 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001173 }
1174 }
1175
Winson Chunge3193b92010-09-10 11:44:42 -07001176 protected int getAssociatedLowerPageBound(int page) {
1177 return Math.max(0, page - 1);
1178 }
1179 protected int getAssociatedUpperPageBound(int page) {
1180 final int count = getChildCount();
1181 return Math.min(page + 1, count - 1);
1182 }
1183
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001184 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001185 if (isChoiceMode(CHOICE_MODE_NONE)) {
1186 mChoiceMode = mode;
1187 mActionMode = startActionMode(callback);
1188 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001189 }
1190
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001191 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001192 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001193 mChoiceMode = CHOICE_MODE_NONE;
1194 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001195 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001196 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001197 }
1198 }
1199
1200 protected boolean isChoiceMode(int mode) {
1201 return mChoiceMode == mode;
1202 }
1203
1204 protected ArrayList<Checkable> getCheckedGrandchildren() {
1205 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1206 final int childCount = getChildCount();
1207 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001208 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001209 final int grandChildCount = layout.getChildCount();
1210 for (int j = 0; j < grandChildCount; ++j) {
1211 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001212 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001213 checked.add((Checkable) v);
1214 }
1215 }
1216 }
1217 return checked;
1218 }
1219
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001220 /**
1221 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1222 * Otherwise, returns null.
1223 */
1224 protected Checkable getSingleCheckedGrandchild() {
1225 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1226 final int childCount = getChildCount();
1227 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001228 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001229 final int grandChildCount = layout.getChildCount();
1230 for (int j = 0; j < grandChildCount; ++j) {
1231 final View v = layout.getChildAt(j);
1232 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1233 return (Checkable) v;
1234 }
1235 }
1236 }
1237 }
1238 return null;
1239 }
1240
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001241 public Object getChosenItem() {
1242 View checkedView = (View) getSingleCheckedGrandchild();
1243 if (checkedView != null) {
1244 return checkedView.getTag();
1245 }
1246 return null;
1247 }
1248
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001249 protected void resetCheckedGrandchildren() {
1250 // loop through children, and set all of their children to _not_ be checked
1251 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1252 for (int i = 0; i < checked.size(); ++i) {
1253 final Checkable c = checked.get(i);
1254 c.setChecked(false);
1255 }
1256 }
1257
Winson Chung86f77532010-08-24 11:08:22 -07001258 /**
1259 * This method is called ONLY to synchronize the number of pages that the paged view has.
1260 * To actually fill the pages with information, implement syncPageItems() below. It is
1261 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1262 * and therefore, individual page items do not need to be updated in this method.
1263 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001265
1266 /**
1267 * This method is called to synchronize the items that are on a particular page. If views on
1268 * the page can be reused, then they should be updated within this method.
1269 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001271
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001273 if (mContentIsRefreshable) {
1274 // Update all the pages
1275 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001276
Michael Jurka0142d492010-08-25 17:46:15 -07001277 // Mark each of the pages as dirty
1278 final int count = getChildCount();
1279 mDirtyPageContent.clear();
1280 for (int i = 0; i < count; ++i) {
1281 mDirtyPageContent.add(true);
1282 }
1283
1284 // Load any pages that are necessary for the current window of views
1285 loadAssociatedPages(mCurrentPage);
1286 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001287 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001288 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001289 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 }
1291}