blob: 264e83907c4d1e028f7d34f521a530bdb844d7cb [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 Chung5f2aa4e2010-08-20 14:49:25 -070030import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.view.MotionEvent;
32import android.view.VelocityTracker;
33import android.view.View;
34import android.view.ViewConfiguration;
35import android.view.ViewGroup;
36import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070037import android.view.animation.Animation;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.view.animation.AnimationUtils;
Adam Cohen68d73932010-11-15 10:50:58 -080039import android.view.animation.Animation.AnimationListener;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Winson Chunge3193b92010-09-10 11:44:42 -070043import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070044
Winson Chung321e9ee2010-08-09 13:37:56 -070045/**
46 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070047 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070048 */
49public abstract class PagedView extends ViewGroup {
50 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070051 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070052
Winson Chung86f77532010-08-24 11:08:22 -070053 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070054 private static final int MIN_LENGTH_FOR_FLING = 25;
55 // The min drag distance to trigger a page shift (regardless of velocity)
56 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chunge22a8e92010-11-12 13:40:58 -080058 private static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Adam Cohen68d73932010-11-15 10:50:58 -080061 private static final float OVERSCROLL_DAMP_FACTOR = 0.22f;
62
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;
Adam Cohen68d73932010-11-15 10:50:58 -080073 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070074 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070075 private VelocityTracker mVelocityTracker;
76
77 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080078 protected float mLastMotionX;
79 protected float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070080 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070081
Michael Jurka0142d492010-08-25 17:46:15 -070082 protected final static int TOUCH_STATE_REST = 0;
83 protected final static int TOUCH_STATE_SCROLLING = 1;
84 protected final static int TOUCH_STATE_PREV_PAGE = 2;
85 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070086 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070087
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070089
Michael Jurka0142d492010-08-25 17:46:15 -070090 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070091
Michael Jurka7426c422010-11-11 15:23:47 -080092 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka7426c422010-11-11 15:23:47 -080094 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070095 private int mPagingTouchSlop;
96 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070097 protected int mPageSpacing;
98 protected int mPageLayoutPaddingTop;
99 protected int mPageLayoutPaddingBottom;
100 protected int mPageLayoutPaddingLeft;
101 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700102 protected int mPageLayoutWidthGap;
103 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700104 protected int mCellCountX;
105 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700106 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800107 protected boolean mAllowOverScroll = true;
108 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka5f1c5092010-09-03 14:15:02 -0700110 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurka5f1c5092010-09-03 14:15:02 -0700112 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Winson Chung86f77532010-08-24 11:08:22 -0700114 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Winson Chung86f77532010-08-24 11:08:22 -0700116 private ArrayList<Boolean> mDirtyPageContent;
117 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700119 // choice modes
120 protected static final int CHOICE_MODE_NONE = 0;
121 protected static final int CHOICE_MODE_SINGLE = 1;
122 // Multiple selection mode is not supported by all Launcher actions atm
123 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700124
Michael Jurkae17e19c2010-09-28 11:01:39 -0700125 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700126 private ActionMode mActionMode;
127
Winson Chung241c3b42010-08-25 16:53:03 -0700128 protected PagedViewIconCache mPageViewIconCache;
129
Michael Jurka0142d492010-08-25 17:46:15 -0700130 // If true, syncPages and syncPageItems will be called to refresh pages
131 protected boolean mContentIsRefreshable = true;
132
133 // If true, modify alpha of neighboring pages as user scrolls left/right
134 protected boolean mFadeInAdjacentScreens = true;
135
136 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
137 // to switch to a new page
138 protected boolean mUsePagingTouchSlop = true;
139
140 // If true, the subclass should directly update mScrollX itself in its computeScroll method
141 // (SmoothPagedView does this)
142 protected boolean mDeferScrollUpdate = false;
143
Patrick Dubroy1262e362010-10-06 15:49:50 -0700144 protected boolean mIsPageMoving = false;
145
Winson Chung241c3b42010-08-25 16:53:03 -0700146 /**
147 * Simple cache mechanism for PagedViewIcon outlines.
148 */
149 class PagedViewIconCache {
150 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
151
152 public void clear() {
153 iconOutlineCache.clear();
154 }
155 public void addOutline(Object key, Bitmap b) {
156 iconOutlineCache.put(key, b);
157 }
158 public void removeOutline(Object key) {
159 if (iconOutlineCache.containsKey(key)) {
160 iconOutlineCache.remove(key);
161 }
162 }
163 public Bitmap getOutline(Object key) {
164 return iconOutlineCache.get(key);
165 }
166 }
167
Winson Chung86f77532010-08-24 11:08:22 -0700168 public interface PageSwitchListener {
169 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700170 }
171
Winson Chung321e9ee2010-08-09 13:37:56 -0700172 public PagedView(Context context) {
173 this(context, null);
174 }
175
176 public PagedView(Context context, AttributeSet attrs) {
177 this(context, attrs, 0);
178 }
179
180 public PagedView(Context context, AttributeSet attrs, int defStyle) {
181 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700182 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700183
Adam Cohen9c4949e2010-10-05 12:27:22 -0700184 TypedArray a = context.obtainStyledAttributes(attrs,
185 R.styleable.PagedView, defStyle, 0);
186 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
187 mPageLayoutPaddingTop = a.getDimensionPixelSize(
188 R.styleable.PagedView_pageLayoutPaddingTop, 10);
189 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
190 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
191 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
193 mPageLayoutPaddingRight = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700195 mPageLayoutWidthGap = a.getDimensionPixelSize(
196 R.styleable.PagedView_pageLayoutWidthGap, -1);
197 mPageLayoutHeightGap = a.getDimensionPixelSize(
198 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700199 a.recycle();
200
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700202 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700203 }
204
205 /**
206 * Initializes various states for this workspace.
207 */
Michael Jurka0142d492010-08-25 17:46:15 -0700208 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700209 mDirtyPageContent = new ArrayList<Boolean>();
210 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700211 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700213 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700214 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700215
216 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
217 mTouchSlop = configuration.getScaledTouchSlop();
218 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
219 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
220 }
221
Winson Chung86f77532010-08-24 11:08:22 -0700222 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
223 mPageSwitchListener = pageSwitchListener;
224 if (mPageSwitchListener != null) {
225 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227 }
228
229 /**
Winson Chung86f77532010-08-24 11:08:22 -0700230 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 *
Winson Chung86f77532010-08-24 11:08:22 -0700232 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 */
Winson Chung86f77532010-08-24 11:08:22 -0700234 int getCurrentPage() {
235 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 }
237
Winson Chung86f77532010-08-24 11:08:22 -0700238 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 return getChildCount();
240 }
241
Winson Chung86f77532010-08-24 11:08:22 -0700242 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 return getChildAt(index);
244 }
245
246 int getScrollWidth() {
247 return getWidth();
248 }
249
250 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800251 * Updates the scroll of the current page immediately to its final scroll position. We use this
252 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
253 * the previous tab page.
254 */
255 protected void updateCurrentPageScroll() {
256 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
257 scrollTo(newX, 0);
258 mScroller.setFinalX(newX);
259 }
260
261 /**
Winson Chung86f77532010-08-24 11:08:22 -0700262 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700263 */
Winson Chung86f77532010-08-24 11:08:22 -0700264 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800265 if (!mScroller.isFinished()) {
266 mScroller.abortAnimation();
267 }
268 if (getChildCount() == 0 || currentPage == mCurrentPage) {
269 return;
270 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700271
Winson Chung86f77532010-08-24 11:08:22 -0700272 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800273 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700274
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700276 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 }
278
Michael Jurka0142d492010-08-25 17:46:15 -0700279 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700280 if (mPageSwitchListener != null) {
281 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 }
283 }
284
Patrick Dubroy1262e362010-10-06 15:49:50 -0700285 private void pageBeginMoving() {
286 mIsPageMoving = true;
287 onPageBeginMoving();
288 }
289
290 private void pageEndMoving() {
291 onPageEndMoving();
292 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700293 }
294
295 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700296 protected void onPageBeginMoving() {
297 }
298
299 // a method that subclasses can override to add behavior
300 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700301 }
302
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 /**
Winson Chung86f77532010-08-24 11:08:22 -0700304 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 *
306 * @param l The listener used to respond to long clicks.
307 */
308 @Override
309 public void setOnLongClickListener(OnLongClickListener l) {
310 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700311 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700313 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 }
315 }
316
317 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800318 public void scrollBy(int x, int y) {
319 scrollTo(mUnboundedScrollX + x, mScrollY + y);
320 }
321
322 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700323 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800324 mUnboundedScrollX = x;
325
326 if (x < 0) {
327 super.scrollTo(0, y);
328 if (mAllowOverScroll) {
329 overScroll(x);
330 }
331 } else if (x > mMaxScrollX) {
332 super.scrollTo(mMaxScrollX, y);
333 if (mAllowOverScroll) {
334 overScroll(x - mMaxScrollX);
335 }
336 } else {
337 super.scrollTo(x, y);
338 }
339
Michael Jurka0142d492010-08-25 17:46:15 -0700340 mTouchX = x;
341 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
342 }
343
344 // we moved this functionality to a helper function so SmoothPagedView can reuse it
345 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700346 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700347 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700349 invalidate();
350 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700351 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700352 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700353 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700354 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700355 notifyPageSwitchListener();
356 pageEndMoving();
357 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 }
Michael Jurka0142d492010-08-25 17:46:15 -0700359 return false;
360 }
361
362 @Override
363 public void computeScroll() {
364 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700365 }
366
367 @Override
368 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
369 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
370 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
371 if (widthMode != MeasureSpec.EXACTLY) {
372 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
373 }
374
Adam Lesinski6b879f02010-11-04 16:15:23 -0700375 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
376 * of the All apps view on XLarge displays to not take up more space then it needs. Width
377 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
378 * each page to have the same width.
379 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700381 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
382 int maxChildHeight = 0;
383
384 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700385
386 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700387 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700388 final int childCount = getChildCount();
389 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700390 // disallowing padding in paged view (just pass 0)
391 final View child = getChildAt(i);
392 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
393
394 int childWidthMode;
395 if (lp.width == LayoutParams.WRAP_CONTENT) {
396 childWidthMode = MeasureSpec.AT_MOST;
397 } else {
398 childWidthMode = MeasureSpec.EXACTLY;
399 }
400
401 int childHeightMode;
402 if (lp.height == LayoutParams.WRAP_CONTENT) {
403 childHeightMode = MeasureSpec.AT_MOST;
404 } else {
405 childHeightMode = MeasureSpec.EXACTLY;
406 }
407
408 final int childWidthMeasureSpec =
409 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
410 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700411 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700412
413 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700414 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
415 }
416
417 if (heightMode == MeasureSpec.AT_MOST) {
418 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800420 if (childCount > 0) {
421 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
422 } else {
423 mMaxScrollX = 0;
424 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700425
426 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
428
429 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700430 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700431 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
432 setHorizontalScrollBarEnabled(false);
433 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
434 scrollTo(newX, 0);
435 mScroller.setFinalX(newX);
436 setHorizontalScrollBarEnabled(true);
437 mFirstLayout = false;
438 }
439
Adam Lesinski6b879f02010-11-04 16:15:23 -0700440 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700441 final int childCount = getChildCount();
442 int childLeft = 0;
443 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700444 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 }
446
447 for (int i = 0; i < childCount; i++) {
448 final View child = getChildAt(i);
449 if (child.getVisibility() != View.GONE) {
450 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700451 final int childHeight = child.getMeasuredHeight();
452 int childTop = mPaddingTop;
453 if (mCenterPagesVertically) {
454 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
455 }
456 child.layout(childLeft, childTop,
457 childLeft + childWidth, childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700458 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 }
460 }
461 }
462
Winson Chunge3193b92010-09-10 11:44:42 -0700463 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700464 if (mFadeInAdjacentScreens) {
465 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700466 int halfScreenSize = getMeasuredWidth() / 2;
467 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700468 final int childCount = getChildCount();
469 for (int i = 0; i < childCount; ++i) {
470 View layout = (View) getChildAt(i);
471 int childWidth = layout.getMeasuredWidth();
472 int halfChildWidth = (childWidth / 2);
473 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700474
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700475 // On the first layout, we may not have a width nor a proper offset, so for now
476 // we should just assume full page width (and calculate the offset according to
477 // that).
478 if (childWidth <= 0) {
479 childWidth = getMeasuredWidth();
480 childCenter = (i * childWidth) + (childWidth / 2);
481 }
482
Winson Chunge8878e32010-09-15 20:37:09 -0700483 int d = halfChildWidth;
484 int distanceFromScreenCenter = childCenter - screenCenter;
485 if (distanceFromScreenCenter > 0) {
486 if (i > 0) {
487 d += getChildAt(i - 1).getMeasuredWidth() / 2;
488 }
Michael Jurka0142d492010-08-25 17:46:15 -0700489 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700490 if (i < childCount - 1) {
491 d += getChildAt(i + 1).getMeasuredWidth() / 2;
492 }
Michael Jurka0142d492010-08-25 17:46:15 -0700493 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700494 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700495
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700496 // Preventing potential divide-by-zero
497 d = Math.max(1, d);
498
Winson Chunge8878e32010-09-15 20:37:09 -0700499 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
500 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
501 float alpha = 1.0f - dimAlpha;
502
Adam Cohenf34bab52010-09-30 14:11:56 -0700503 if (alpha < ALPHA_QUANTIZE_LEVEL) {
504 alpha = 0.0f;
505 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
506 alpha = 1.0f;
507 }
508
Michael Jurka0142d492010-08-25 17:46:15 -0700509 if (Float.compare(alpha, layout.getAlpha()) != 0) {
510 layout.setAlpha(alpha);
511 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 }
Michael Jurka0142d492010-08-25 17:46:15 -0700513 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 }
515 }
Winson Chunge3193b92010-09-10 11:44:42 -0700516 }
517
Adam Cohenf34bab52010-09-30 14:11:56 -0700518 protected void screenScrolled(int screenCenter) {
519 }
520
Winson Chunge3193b92010-09-10 11:44:42 -0700521 @Override
522 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700523 int halfScreenSize = getMeasuredWidth() / 2;
524 int screenCenter = mScrollX + halfScreenSize;
525
526 if (screenCenter != mLastScreenCenter) {
527 screenScrolled(screenCenter);
528 updateAdjacentPagesAlpha();
529 mLastScreenCenter = screenCenter;
530 }
Michael Jurka0142d492010-08-25 17:46:15 -0700531
532 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700533 // As an optimization, this code assumes that all pages have the same width as the 0th
534 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700535 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700536 if (pageCount > 0) {
537 final int pageWidth = getChildAt(0).getMeasuredWidth();
538 final int screenWidth = getMeasuredWidth();
539 int x = getRelativeChildOffset(0) + pageWidth;
540 int leftScreen = 0;
541 int rightScreen = 0;
542 while (x <= mScrollX) {
543 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700544 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700545 // replace above line with this if you don't assume all pages have same width as 0th
546 // page:
547 // x += getChildAt(leftScreen).getMeasuredWidth();
548 }
549 rightScreen = leftScreen;
550 while (x < mScrollX + screenWidth) {
551 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700552 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700553 // replace above line with this if you don't assume all pages have same width as 0th
554 // page:
555 //if (rightScreen < pageCount) {
556 // x += getChildAt(rightScreen).getMeasuredWidth();
557 //}
558 }
559 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700560
Michael Jurkac4fb9172010-09-02 17:19:20 -0700561 final long drawingTime = getDrawingTime();
562 for (int i = leftScreen; i <= rightScreen; i++) {
563 drawChild(canvas, getChildAt(i), drawingTime);
564 }
Michael Jurka0142d492010-08-25 17:46:15 -0700565 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 }
567
568 @Override
569 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700570 int page = indexOfChild(child);
571 if (page != mCurrentPage || !mScroller.isFinished()) {
572 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 return true;
574 }
575 return false;
576 }
577
578 @Override
579 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700580 int focusablePage;
581 if (mNextPage != INVALID_PAGE) {
582 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700584 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700585 }
Winson Chung86f77532010-08-24 11:08:22 -0700586 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700587 if (v != null) {
588 v.requestFocus(direction, previouslyFocusedRect);
589 }
590 return false;
591 }
592
593 @Override
594 public boolean dispatchUnhandledMove(View focused, int direction) {
595 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700596 if (getCurrentPage() > 0) {
597 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 return true;
599 }
600 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700601 if (getCurrentPage() < getPageCount() - 1) {
602 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 return true;
604 }
605 }
606 return super.dispatchUnhandledMove(focused, direction);
607 }
608
609 @Override
610 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700611 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
612 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
614 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700615 if (mCurrentPage > 0) {
616 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 }
618 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700619 if (mCurrentPage < getPageCount() - 1) {
620 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 }
622 }
623 }
624
625 /**
626 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700627 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 *
Winson Chung86f77532010-08-24 11:08:22 -0700629 * This happens when live folders requery, and if they're off page, they
630 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700631 */
632 @Override
633 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 View v = focused;
636 while (true) {
637 if (v == current) {
638 super.focusableViewAvailable(focused);
639 return;
640 }
641 if (v == this) {
642 return;
643 }
644 ViewParent parent = v.getParent();
645 if (parent instanceof View) {
646 v = (View)v.getParent();
647 } else {
648 return;
649 }
650 }
651 }
652
653 /**
654 * {@inheritDoc}
655 */
656 @Override
657 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
658 if (disallowIntercept) {
659 // We need to make sure to cancel our long press if
660 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700661 final View currentPage = getChildAt(mCurrentPage);
662 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700663 }
664 super.requestDisallowInterceptTouchEvent(disallowIntercept);
665 }
666
667 @Override
668 public boolean onInterceptTouchEvent(MotionEvent ev) {
669 /*
670 * This method JUST determines whether we want to intercept the motion.
671 * If we return true, onTouchEvent will be called and we do the actual
672 * scrolling there.
673 */
674
Winson Chung45e1d6e2010-11-09 17:19:49 -0800675 // Skip touch handling if there are no pages to swipe
676 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
677
Winson Chung321e9ee2010-08-09 13:37:56 -0700678 /*
679 * Shortcut the most recurring case: the user is in the dragging
680 * state and he is moving his finger. We want to intercept this
681 * motion.
682 */
683 final int action = ev.getAction();
684 if ((action == MotionEvent.ACTION_MOVE) &&
685 (mTouchState == TOUCH_STATE_SCROLLING)) {
686 return true;
687 }
688
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 switch (action & MotionEvent.ACTION_MASK) {
690 case MotionEvent.ACTION_MOVE: {
691 /*
692 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
693 * whether the user has moved far enough from his original down touch.
694 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700695 if (mActivePointerId != INVALID_POINTER) {
696 determineScrollingStart(ev);
697 break;
698 }
699 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
700 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
701 // i.e. fall through to the next case (don't break)
702 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
703 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700704 }
705
706 case MotionEvent.ACTION_DOWN: {
707 final float x = ev.getX();
708 final float y = ev.getY();
709 // Remember location of down touch
710 mDownMotionX = x;
711 mLastMotionX = x;
712 mLastMotionY = y;
713 mActivePointerId = ev.getPointerId(0);
714 mAllowLongPress = true;
715
716 /*
717 * If being flinged and user touches the screen, initiate drag;
718 * otherwise don't. mScroller.isFinished should be false when
719 * being flinged.
720 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700721 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700722 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
723 if (finishedScrolling) {
724 mTouchState = TOUCH_STATE_REST;
725 mScroller.abortAnimation();
726 } else {
727 mTouchState = TOUCH_STATE_SCROLLING;
728 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700729
Winson Chung86f77532010-08-24 11:08:22 -0700730 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700731 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700732 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700733 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
734 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700735 int width = getMeasuredWidth();
736 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700737 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700739 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 mTouchState = TOUCH_STATE_NEXT_PAGE;
741 }
742 }
743 }
744 break;
745 }
746
747 case MotionEvent.ACTION_CANCEL:
748 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700749 mTouchState = TOUCH_STATE_REST;
750 mAllowLongPress = false;
751 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700752 break;
753
754 case MotionEvent.ACTION_POINTER_UP:
755 onSecondaryPointerUp(ev);
756 break;
757 }
758
759 /*
760 * The only time we want to intercept motion events is if we are in the
761 * drag mode.
762 */
763 return mTouchState != TOUCH_STATE_REST;
764 }
765
Winson Chung80baf5a2010-08-09 16:03:15 -0700766 protected void animateClickFeedback(View v, final Runnable r) {
767 // animate the view slightly to show click feedback running some logic after it is "pressed"
768 Animation anim = AnimationUtils.loadAnimation(getContext(),
769 R.anim.paged_view_click_feedback);
770 anim.setAnimationListener(new AnimationListener() {
771 @Override
772 public void onAnimationStart(Animation animation) {}
773 @Override
774 public void onAnimationRepeat(Animation animation) {
775 r.run();
776 }
777 @Override
778 public void onAnimationEnd(Animation animation) {}
779 });
780 v.startAnimation(anim);
781 }
782
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 /*
784 * Determines if we should change the touch state to start scrolling after the
785 * user moves their touch point too far.
786 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700787 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 /*
789 * Locally do absolute value. mLastMotionX is set to the y value
790 * of the down event.
791 */
792 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
793 final float x = ev.getX(pointerIndex);
794 final float y = ev.getY(pointerIndex);
795 final int xDiff = (int) Math.abs(x - mLastMotionX);
796 final int yDiff = (int) Math.abs(y - mLastMotionY);
797
798 final int touchSlop = mTouchSlop;
799 boolean xPaged = xDiff > mPagingTouchSlop;
800 boolean xMoved = xDiff > touchSlop;
801 boolean yMoved = yDiff > touchSlop;
802
803 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700804 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 // Scroll if the user moved far enough along the X axis
806 mTouchState = TOUCH_STATE_SCROLLING;
807 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700808 mTouchX = mScrollX;
809 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
810 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 }
812 // Either way, cancel any pending longpress
813 if (mAllowLongPress) {
814 mAllowLongPress = false;
815 // Try canceling the long press. It could also have been scheduled
816 // by a distant descendant, so use the mAllowLongPress flag to block
817 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700818 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700819 if (currentPage != null) {
820 currentPage.cancelLongPress();
821 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 }
823 }
824 }
825
Adam Cohen21f12b52010-10-07 17:15:37 -0700826 protected boolean handlePagingClicks() {
827 return false;
828 }
829
Adam Cohen68d73932010-11-15 10:50:58 -0800830 protected void overScroll(float amount) {
831 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * amount);
832 if (amount < 0) {
833 mScrollX = overScrollAmount;
834 } else {
835 mScrollX = mMaxScrollX + overScrollAmount;
836 }
837 invalidate();
838 }
839
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 @Override
841 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800842 // Skip touch handling if there are no pages to swipe
843 if (getChildCount() <= 0) return super.onTouchEvent(ev);
844
Michael Jurkab8f06722010-10-10 15:58:46 -0700845 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700846
847 final int action = ev.getAction();
848
849 switch (action & MotionEvent.ACTION_MASK) {
850 case MotionEvent.ACTION_DOWN:
851 /*
852 * If being flinged and user touches, stop the fling. isFinished
853 * will be false if being flinged.
854 */
855 if (!mScroller.isFinished()) {
856 mScroller.abortAnimation();
857 }
858
859 // Remember where the motion event started
860 mDownMotionX = mLastMotionX = ev.getX();
861 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700862 if (mTouchState == TOUCH_STATE_SCROLLING) {
863 pageBeginMoving();
864 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 break;
866
867 case MotionEvent.ACTION_MOVE:
868 if (mTouchState == TOUCH_STATE_SCROLLING) {
869 // Scroll to follow the motion event
870 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
871 final float x = ev.getX(pointerIndex);
872 final int deltaX = (int) (mLastMotionX - x);
873 mLastMotionX = x;
874
Adam Cohen68d73932010-11-15 10:50:58 -0800875 if (deltaX != 0) {
876 mTouchX += deltaX;
877 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
878 if (!mDeferScrollUpdate) {
879 scrollBy(deltaX, 0);
880 } else {
881 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 }
883 } else {
884 awakenScrollBars();
885 }
Adam Cohen564976a2010-10-13 18:52:07 -0700886 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 determineScrollingStart(ev);
888 }
889 break;
890
891 case MotionEvent.ACTION_UP:
892 if (mTouchState == TOUCH_STATE_SCROLLING) {
893 final int activePointerId = mActivePointerId;
894 final int pointerIndex = ev.findPointerIndex(activePointerId);
895 final float x = ev.getX(pointerIndex);
896 final VelocityTracker velocityTracker = mVelocityTracker;
897 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
898 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700899 final int deltaX = (int) (x - mDownMotionX);
900 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
901 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700902
Michael Jurka0142d492010-08-25 17:46:15 -0700903 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700904 if ((isSignificantMove && deltaX > 0 ||
905 (isfling && velocityX > snapVelocity)) &&
906 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700907 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700908 } else if ((isSignificantMove && deltaX < 0 ||
909 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700910 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700911 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 } else {
913 snapToDestination();
914 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700915 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 // at this point we have not moved beyond the touch slop
917 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
918 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700919 int nextPage = Math.max(0, mCurrentPage - 1);
920 if (nextPage != mCurrentPage) {
921 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 } else {
923 snapToDestination();
924 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700925 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700926 // at this point we have not moved beyond the touch slop
927 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
928 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700929 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
930 if (nextPage != mCurrentPage) {
931 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 } else {
933 snapToDestination();
934 }
935 }
936 mTouchState = TOUCH_STATE_REST;
937 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700938 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 break;
940
941 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700942 if (mTouchState == TOUCH_STATE_SCROLLING) {
943 snapToDestination();
944 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 mTouchState = TOUCH_STATE_REST;
946 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700947 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 break;
949
950 case MotionEvent.ACTION_POINTER_UP:
951 onSecondaryPointerUp(ev);
952 break;
953 }
954
955 return true;
956 }
957
Michael Jurkab8f06722010-10-10 15:58:46 -0700958 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
959 if (mVelocityTracker == null) {
960 mVelocityTracker = VelocityTracker.obtain();
961 }
962 mVelocityTracker.addMovement(ev);
963 }
964
965 private void releaseVelocityTracker() {
966 if (mVelocityTracker != null) {
967 mVelocityTracker.recycle();
968 mVelocityTracker = null;
969 }
970 }
971
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 private void onSecondaryPointerUp(MotionEvent ev) {
973 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
974 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
975 final int pointerId = ev.getPointerId(pointerIndex);
976 if (pointerId == mActivePointerId) {
977 // This was our active pointer going up. Choose a new
978 // active pointer and adjust accordingly.
979 // TODO: Make this decision more intelligent.
980 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
981 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
982 mLastMotionY = ev.getY(newPointerIndex);
983 mActivePointerId = ev.getPointerId(newPointerIndex);
984 if (mVelocityTracker != null) {
985 mVelocityTracker.clear();
986 }
987 }
988 }
989
990 @Override
991 public void requestChildFocus(View child, View focused) {
992 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700993 int page = indexOfChild(child);
994 if (page >= 0 && !isInTouchMode()) {
995 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 }
997 }
998
Winson Chunge3193b92010-09-10 11:44:42 -0700999 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1000 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001001 int left;
1002 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001003 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001004 left = getRelativeChildOffset(i);
1005 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -07001006 if (left <= relativeOffset && relativeOffset <= right) {
1007 return i;
1008 }
Winson Chunge3193b92010-09-10 11:44:42 -07001009 }
1010 return -1;
1011 }
1012
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 protected int getRelativeChildOffset(int index) {
1014 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1015 }
1016
1017 protected int getChildOffset(int index) {
1018 if (getChildCount() == 0)
1019 return 0;
1020
1021 int offset = getRelativeChildOffset(0);
1022 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001023 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 }
1025 return offset;
1026 }
1027
Adam Cohend19d3ca2010-09-15 14:43:42 -07001028 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 int minDistanceFromScreenCenter = getMeasuredWidth();
1030 int minDistanceFromScreenCenterIndex = -1;
1031 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1032 final int childCount = getChildCount();
1033 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001034 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 int childWidth = layout.getMeasuredWidth();
1036 int halfChildWidth = (childWidth / 2);
1037 int childCenter = getChildOffset(i) + halfChildWidth;
1038 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1039 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1040 minDistanceFromScreenCenter = distanceFromScreenCenter;
1041 minDistanceFromScreenCenterIndex = i;
1042 }
1043 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001044 return minDistanceFromScreenCenterIndex;
1045 }
1046
1047 protected void snapToDestination() {
1048 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 }
1050
Michael Jurka0142d492010-08-25 17:46:15 -07001051 protected void snapToPageWithVelocity(int whichPage, int velocity) {
1052 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
1053 // can use it
1054 snapToPage(whichPage);
1055 }
1056
1057 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001058 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 }
1060
Michael Jurka0142d492010-08-25 17:46:15 -07001061 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001062 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001063
Winson Chung86f77532010-08-24 11:08:22 -07001064 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001065 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001067 snapToPage(whichPage, delta, duration);
1068 }
1069
1070 protected void snapToPage(int whichPage, int delta, int duration) {
1071 mNextPage = whichPage;
1072
1073 View focusedChild = getFocusedChild();
1074 if (focusedChild != null && whichPage != mCurrentPage &&
1075 focusedChild == getChildAt(mCurrentPage)) {
1076 focusedChild.clearFocus();
1077 }
1078
1079 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 awakenScrollBars(duration);
1081 if (duration == 0) {
1082 duration = Math.abs(delta);
1083 }
1084
1085 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001086 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001087
1088 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001089 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001090 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001091 invalidate();
1092 }
1093
1094 @Override
1095 protected Parcelable onSaveInstanceState() {
1096 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001097 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001098 return state;
1099 }
1100
1101 @Override
1102 protected void onRestoreInstanceState(Parcelable state) {
1103 SavedState savedState = (SavedState) state;
1104 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001105 if (savedState.currentPage != -1) {
1106 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 }
1108 }
1109
1110 public void scrollLeft() {
1111 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001112 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001113 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001114 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 }
1116 }
1117
1118 public void scrollRight() {
1119 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001120 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001122 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001123 }
1124 }
1125
Winson Chung86f77532010-08-24 11:08:22 -07001126 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 int result = -1;
1128 if (v != null) {
1129 ViewParent vp = v.getParent();
1130 int count = getChildCount();
1131 for (int i = 0; i < count; i++) {
1132 if (vp == getChildAt(i)) {
1133 return i;
1134 }
1135 }
1136 }
1137 return result;
1138 }
1139
1140 /**
1141 * @return True is long presses are still allowed for the current touch
1142 */
1143 public boolean allowLongPress() {
1144 return mAllowLongPress;
1145 }
1146
Michael Jurka0142d492010-08-25 17:46:15 -07001147 /**
1148 * Set true to allow long-press events to be triggered, usually checked by
1149 * {@link Launcher} to accept or block dpad-initiated long-presses.
1150 */
1151 public void setAllowLongPress(boolean allowLongPress) {
1152 mAllowLongPress = allowLongPress;
1153 }
1154
Winson Chung321e9ee2010-08-09 13:37:56 -07001155 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001156 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001157
1158 SavedState(Parcelable superState) {
1159 super(superState);
1160 }
1161
1162 private SavedState(Parcel in) {
1163 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001164 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 }
1166
1167 @Override
1168 public void writeToParcel(Parcel out, int flags) {
1169 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001170 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 }
1172
1173 public static final Parcelable.Creator<SavedState> CREATOR =
1174 new Parcelable.Creator<SavedState>() {
1175 public SavedState createFromParcel(Parcel in) {
1176 return new SavedState(in);
1177 }
1178
1179 public SavedState[] newArray(int size) {
1180 return new SavedState[size];
1181 }
1182 };
1183 }
1184
Winson Chung86f77532010-08-24 11:08:22 -07001185 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001186 if (mContentIsRefreshable) {
1187 final int count = getChildCount();
1188 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001189 int lowerPageBound = getAssociatedLowerPageBound(page);
1190 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001191 for (int i = 0; i < count; ++i) {
1192 final ViewGroup layout = (ViewGroup) getChildAt(i);
1193 final int childCount = layout.getChildCount();
1194 if (lowerPageBound <= i && i <= upperPageBound) {
1195 if (mDirtyPageContent.get(i)) {
1196 syncPageItems(i);
1197 mDirtyPageContent.set(i, false);
1198 }
1199 } else {
1200 if (childCount > 0) {
1201 layout.removeAllViews();
1202 }
1203 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001204 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001205 }
1206 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001207 }
1208 }
1209
Winson Chunge3193b92010-09-10 11:44:42 -07001210 protected int getAssociatedLowerPageBound(int page) {
1211 return Math.max(0, page - 1);
1212 }
1213 protected int getAssociatedUpperPageBound(int page) {
1214 final int count = getChildCount();
1215 return Math.min(page + 1, count - 1);
1216 }
1217
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001218 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001219 if (isChoiceMode(CHOICE_MODE_NONE)) {
1220 mChoiceMode = mode;
1221 mActionMode = startActionMode(callback);
1222 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001223 }
1224
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001225 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001226 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001227 mChoiceMode = CHOICE_MODE_NONE;
1228 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001229 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001230 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001231 }
1232 }
1233
1234 protected boolean isChoiceMode(int mode) {
1235 return mChoiceMode == mode;
1236 }
1237
1238 protected ArrayList<Checkable> getCheckedGrandchildren() {
1239 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1240 final int childCount = getChildCount();
1241 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001242 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001243 final int grandChildCount = layout.getChildCount();
1244 for (int j = 0; j < grandChildCount; ++j) {
1245 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001246 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001247 checked.add((Checkable) v);
1248 }
1249 }
1250 }
1251 return checked;
1252 }
1253
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001254 /**
1255 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1256 * Otherwise, returns null.
1257 */
1258 protected Checkable getSingleCheckedGrandchild() {
1259 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1260 final int childCount = getChildCount();
1261 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001262 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001263 final int grandChildCount = layout.getChildCount();
1264 for (int j = 0; j < grandChildCount; ++j) {
1265 final View v = layout.getChildAt(j);
1266 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1267 return (Checkable) v;
1268 }
1269 }
1270 }
1271 }
1272 return null;
1273 }
1274
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001275 public Object getChosenItem() {
1276 View checkedView = (View) getSingleCheckedGrandchild();
1277 if (checkedView != null) {
1278 return checkedView.getTag();
1279 }
1280 return null;
1281 }
1282
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001283 protected void resetCheckedGrandchildren() {
1284 // loop through children, and set all of their children to _not_ be checked
1285 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1286 for (int i = 0; i < checked.size(); ++i) {
1287 final Checkable c = checked.get(i);
1288 c.setChecked(false);
1289 }
1290 }
1291
Winson Chung86f77532010-08-24 11:08:22 -07001292 /**
1293 * This method is called ONLY to synchronize the number of pages that the paged view has.
1294 * To actually fill the pages with information, implement syncPageItems() below. It is
1295 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1296 * and therefore, individual page items do not need to be updated in this method.
1297 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001299
1300 /**
1301 * This method is called to synchronize the items that are on a particular page. If views on
1302 * the page can be reused, then they should be updated within this method.
1303 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001305
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001307 if (mContentIsRefreshable) {
1308 // Update all the pages
1309 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001310
Michael Jurka0142d492010-08-25 17:46:15 -07001311 // Mark each of the pages as dirty
1312 final int count = getChildCount();
1313 mDirtyPageContent.clear();
1314 for (int i = 0; i < count; ++i) {
1315 mDirtyPageContent.add(true);
1316 }
1317
1318 // Load any pages that are necessary for the current window of views
1319 loadAssociatedPages(mCurrentPage);
1320 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001321 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001322 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001323 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 }
1325}