blob: a2ed9858aafc127559975306e367d1cb8b525da8 [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 }
420
Adam Cohen68d73932010-11-15 10:50:58 -0800421 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 }
424
425 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700426 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700427 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
428 setHorizontalScrollBarEnabled(false);
429 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
430 scrollTo(newX, 0);
431 mScroller.setFinalX(newX);
432 setHorizontalScrollBarEnabled(true);
433 mFirstLayout = false;
434 }
435
Adam Lesinski6b879f02010-11-04 16:15:23 -0700436 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 final int childCount = getChildCount();
438 int childLeft = 0;
439 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700440 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700441 }
442
443 for (int i = 0; i < childCount; i++) {
444 final View child = getChildAt(i);
445 if (child.getVisibility() != View.GONE) {
446 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700447 final int childHeight = child.getMeasuredHeight();
448 int childTop = mPaddingTop;
449 if (mCenterPagesVertically) {
450 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
451 }
452 child.layout(childLeft, childTop,
453 childLeft + childWidth, childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700454 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 }
456 }
457 }
458
Winson Chunge3193b92010-09-10 11:44:42 -0700459 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700460 if (mFadeInAdjacentScreens) {
461 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700462 int halfScreenSize = getMeasuredWidth() / 2;
463 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700464 final int childCount = getChildCount();
465 for (int i = 0; i < childCount; ++i) {
466 View layout = (View) getChildAt(i);
467 int childWidth = layout.getMeasuredWidth();
468 int halfChildWidth = (childWidth / 2);
469 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700470
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700471 // On the first layout, we may not have a width nor a proper offset, so for now
472 // we should just assume full page width (and calculate the offset according to
473 // that).
474 if (childWidth <= 0) {
475 childWidth = getMeasuredWidth();
476 childCenter = (i * childWidth) + (childWidth / 2);
477 }
478
Winson Chunge8878e32010-09-15 20:37:09 -0700479 int d = halfChildWidth;
480 int distanceFromScreenCenter = childCenter - screenCenter;
481 if (distanceFromScreenCenter > 0) {
482 if (i > 0) {
483 d += getChildAt(i - 1).getMeasuredWidth() / 2;
484 }
Michael Jurka0142d492010-08-25 17:46:15 -0700485 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700486 if (i < childCount - 1) {
487 d += getChildAt(i + 1).getMeasuredWidth() / 2;
488 }
Michael Jurka0142d492010-08-25 17:46:15 -0700489 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700490 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700491
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700492 // Preventing potential divide-by-zero
493 d = Math.max(1, d);
494
Winson Chunge8878e32010-09-15 20:37:09 -0700495 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
496 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
497 float alpha = 1.0f - dimAlpha;
498
Adam Cohenf34bab52010-09-30 14:11:56 -0700499 if (alpha < ALPHA_QUANTIZE_LEVEL) {
500 alpha = 0.0f;
501 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
502 alpha = 1.0f;
503 }
504
Michael Jurka0142d492010-08-25 17:46:15 -0700505 if (Float.compare(alpha, layout.getAlpha()) != 0) {
506 layout.setAlpha(alpha);
507 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 }
Michael Jurka0142d492010-08-25 17:46:15 -0700509 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 }
511 }
Winson Chunge3193b92010-09-10 11:44:42 -0700512 }
513
Adam Cohenf34bab52010-09-30 14:11:56 -0700514 protected void screenScrolled(int screenCenter) {
515 }
516
Winson Chunge3193b92010-09-10 11:44:42 -0700517 @Override
518 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700519 int halfScreenSize = getMeasuredWidth() / 2;
520 int screenCenter = mScrollX + halfScreenSize;
521
522 if (screenCenter != mLastScreenCenter) {
523 screenScrolled(screenCenter);
524 updateAdjacentPagesAlpha();
525 mLastScreenCenter = screenCenter;
526 }
Michael Jurka0142d492010-08-25 17:46:15 -0700527
528 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700529 // As an optimization, this code assumes that all pages have the same width as the 0th
530 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700531 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700532 if (pageCount > 0) {
533 final int pageWidth = getChildAt(0).getMeasuredWidth();
534 final int screenWidth = getMeasuredWidth();
535 int x = getRelativeChildOffset(0) + pageWidth;
536 int leftScreen = 0;
537 int rightScreen = 0;
538 while (x <= mScrollX) {
539 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700540 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700541 // replace above line with this if you don't assume all pages have same width as 0th
542 // page:
543 // x += getChildAt(leftScreen).getMeasuredWidth();
544 }
545 rightScreen = leftScreen;
546 while (x < mScrollX + screenWidth) {
547 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700548 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700549 // replace above line with this if you don't assume all pages have same width as 0th
550 // page:
551 //if (rightScreen < pageCount) {
552 // x += getChildAt(rightScreen).getMeasuredWidth();
553 //}
554 }
555 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700556
Michael Jurkac4fb9172010-09-02 17:19:20 -0700557 final long drawingTime = getDrawingTime();
558 for (int i = leftScreen; i <= rightScreen; i++) {
559 drawChild(canvas, getChildAt(i), drawingTime);
560 }
Michael Jurka0142d492010-08-25 17:46:15 -0700561 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
563
564 @Override
565 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700566 int page = indexOfChild(child);
567 if (page != mCurrentPage || !mScroller.isFinished()) {
568 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 return true;
570 }
571 return false;
572 }
573
574 @Override
575 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700576 int focusablePage;
577 if (mNextPage != INVALID_PAGE) {
578 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700580 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700581 }
Winson Chung86f77532010-08-24 11:08:22 -0700582 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 if (v != null) {
584 v.requestFocus(direction, previouslyFocusedRect);
585 }
586 return false;
587 }
588
589 @Override
590 public boolean dispatchUnhandledMove(View focused, int direction) {
591 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700592 if (getCurrentPage() > 0) {
593 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 return true;
595 }
596 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700597 if (getCurrentPage() < getPageCount() - 1) {
598 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 return true;
600 }
601 }
602 return super.dispatchUnhandledMove(focused, direction);
603 }
604
605 @Override
606 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700607 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
608 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 }
610 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700611 if (mCurrentPage > 0) {
612 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
614 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700615 if (mCurrentPage < getPageCount() - 1) {
616 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 }
618 }
619 }
620
621 /**
622 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700623 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 *
Winson Chung86f77532010-08-24 11:08:22 -0700625 * This happens when live folders requery, and if they're off page, they
626 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 */
628 @Override
629 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700630 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700631 View v = focused;
632 while (true) {
633 if (v == current) {
634 super.focusableViewAvailable(focused);
635 return;
636 }
637 if (v == this) {
638 return;
639 }
640 ViewParent parent = v.getParent();
641 if (parent instanceof View) {
642 v = (View)v.getParent();
643 } else {
644 return;
645 }
646 }
647 }
648
649 /**
650 * {@inheritDoc}
651 */
652 @Override
653 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
654 if (disallowIntercept) {
655 // We need to make sure to cancel our long press if
656 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700657 final View currentPage = getChildAt(mCurrentPage);
658 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 }
660 super.requestDisallowInterceptTouchEvent(disallowIntercept);
661 }
662
663 @Override
664 public boolean onInterceptTouchEvent(MotionEvent ev) {
665 /*
666 * This method JUST determines whether we want to intercept the motion.
667 * If we return true, onTouchEvent will be called and we do the actual
668 * scrolling there.
669 */
670
Winson Chung45e1d6e2010-11-09 17:19:49 -0800671 // Skip touch handling if there are no pages to swipe
672 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
673
Winson Chung321e9ee2010-08-09 13:37:56 -0700674 /*
675 * Shortcut the most recurring case: the user is in the dragging
676 * state and he is moving his finger. We want to intercept this
677 * motion.
678 */
679 final int action = ev.getAction();
680 if ((action == MotionEvent.ACTION_MOVE) &&
681 (mTouchState == TOUCH_STATE_SCROLLING)) {
682 return true;
683 }
684
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 switch (action & MotionEvent.ACTION_MASK) {
686 case MotionEvent.ACTION_MOVE: {
687 /*
688 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
689 * whether the user has moved far enough from his original down touch.
690 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700691 if (mActivePointerId != INVALID_POINTER) {
692 determineScrollingStart(ev);
693 break;
694 }
695 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
696 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
697 // i.e. fall through to the next case (don't break)
698 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
699 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700700 }
701
702 case MotionEvent.ACTION_DOWN: {
703 final float x = ev.getX();
704 final float y = ev.getY();
705 // Remember location of down touch
706 mDownMotionX = x;
707 mLastMotionX = x;
708 mLastMotionY = y;
709 mActivePointerId = ev.getPointerId(0);
710 mAllowLongPress = true;
711
712 /*
713 * If being flinged and user touches the screen, initiate drag;
714 * otherwise don't. mScroller.isFinished should be false when
715 * being flinged.
716 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700717 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700718 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
719 if (finishedScrolling) {
720 mTouchState = TOUCH_STATE_REST;
721 mScroller.abortAnimation();
722 } else {
723 mTouchState = TOUCH_STATE_SCROLLING;
724 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700725
Winson Chung86f77532010-08-24 11:08:22 -0700726 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700728 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
730 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700731 int width = getMeasuredWidth();
732 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700733 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700735 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 mTouchState = TOUCH_STATE_NEXT_PAGE;
737 }
738 }
739 }
740 break;
741 }
742
743 case MotionEvent.ACTION_CANCEL:
744 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 mTouchState = TOUCH_STATE_REST;
746 mAllowLongPress = false;
747 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 break;
749
750 case MotionEvent.ACTION_POINTER_UP:
751 onSecondaryPointerUp(ev);
752 break;
753 }
754
755 /*
756 * The only time we want to intercept motion events is if we are in the
757 * drag mode.
758 */
759 return mTouchState != TOUCH_STATE_REST;
760 }
761
Winson Chung80baf5a2010-08-09 16:03:15 -0700762 protected void animateClickFeedback(View v, final Runnable r) {
763 // animate the view slightly to show click feedback running some logic after it is "pressed"
764 Animation anim = AnimationUtils.loadAnimation(getContext(),
765 R.anim.paged_view_click_feedback);
766 anim.setAnimationListener(new AnimationListener() {
767 @Override
768 public void onAnimationStart(Animation animation) {}
769 @Override
770 public void onAnimationRepeat(Animation animation) {
771 r.run();
772 }
773 @Override
774 public void onAnimationEnd(Animation animation) {}
775 });
776 v.startAnimation(anim);
777 }
778
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 /*
780 * Determines if we should change the touch state to start scrolling after the
781 * user moves their touch point too far.
782 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700783 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 /*
785 * Locally do absolute value. mLastMotionX is set to the y value
786 * of the down event.
787 */
788 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
789 final float x = ev.getX(pointerIndex);
790 final float y = ev.getY(pointerIndex);
791 final int xDiff = (int) Math.abs(x - mLastMotionX);
792 final int yDiff = (int) Math.abs(y - mLastMotionY);
793
794 final int touchSlop = mTouchSlop;
795 boolean xPaged = xDiff > mPagingTouchSlop;
796 boolean xMoved = xDiff > touchSlop;
797 boolean yMoved = yDiff > touchSlop;
798
799 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700800 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 // Scroll if the user moved far enough along the X axis
802 mTouchState = TOUCH_STATE_SCROLLING;
803 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700804 mTouchX = mScrollX;
805 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
806 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 }
808 // Either way, cancel any pending longpress
809 if (mAllowLongPress) {
810 mAllowLongPress = false;
811 // Try canceling the long press. It could also have been scheduled
812 // by a distant descendant, so use the mAllowLongPress flag to block
813 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700814 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700815 if (currentPage != null) {
816 currentPage.cancelLongPress();
817 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 }
819 }
820 }
821
Adam Cohen21f12b52010-10-07 17:15:37 -0700822 protected boolean handlePagingClicks() {
823 return false;
824 }
825
Adam Cohen68d73932010-11-15 10:50:58 -0800826 protected void overScroll(float amount) {
827 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * amount);
828 if (amount < 0) {
829 mScrollX = overScrollAmount;
830 } else {
831 mScrollX = mMaxScrollX + overScrollAmount;
832 }
833 invalidate();
834 }
835
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 @Override
837 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800838 // Skip touch handling if there are no pages to swipe
839 if (getChildCount() <= 0) return super.onTouchEvent(ev);
840
Michael Jurkab8f06722010-10-10 15:58:46 -0700841 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842
843 final int action = ev.getAction();
844
845 switch (action & MotionEvent.ACTION_MASK) {
846 case MotionEvent.ACTION_DOWN:
847 /*
848 * If being flinged and user touches, stop the fling. isFinished
849 * will be false if being flinged.
850 */
851 if (!mScroller.isFinished()) {
852 mScroller.abortAnimation();
853 }
854
855 // Remember where the motion event started
856 mDownMotionX = mLastMotionX = ev.getX();
857 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700858 if (mTouchState == TOUCH_STATE_SCROLLING) {
859 pageBeginMoving();
860 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 break;
862
863 case MotionEvent.ACTION_MOVE:
864 if (mTouchState == TOUCH_STATE_SCROLLING) {
865 // Scroll to follow the motion event
866 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
867 final float x = ev.getX(pointerIndex);
868 final int deltaX = (int) (mLastMotionX - x);
869 mLastMotionX = x;
870
Adam Cohen68d73932010-11-15 10:50:58 -0800871 if (deltaX != 0) {
872 mTouchX += deltaX;
873 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
874 if (!mDeferScrollUpdate) {
875 scrollBy(deltaX, 0);
876 } else {
877 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 }
879 } else {
880 awakenScrollBars();
881 }
Adam Cohen564976a2010-10-13 18:52:07 -0700882 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 determineScrollingStart(ev);
884 }
885 break;
886
887 case MotionEvent.ACTION_UP:
888 if (mTouchState == TOUCH_STATE_SCROLLING) {
889 final int activePointerId = mActivePointerId;
890 final int pointerIndex = ev.findPointerIndex(activePointerId);
891 final float x = ev.getX(pointerIndex);
892 final VelocityTracker velocityTracker = mVelocityTracker;
893 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
894 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700895 final int deltaX = (int) (x - mDownMotionX);
896 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
897 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700898
Michael Jurka0142d492010-08-25 17:46:15 -0700899 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700900 if ((isSignificantMove && deltaX > 0 ||
901 (isfling && velocityX > snapVelocity)) &&
902 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700903 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700904 } else if ((isSignificantMove && deltaX < 0 ||
905 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700906 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700907 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700908 } else {
909 snapToDestination();
910 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700911 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 // at this point we have not moved beyond the touch slop
913 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
914 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700915 int nextPage = Math.max(0, mCurrentPage - 1);
916 if (nextPage != mCurrentPage) {
917 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 } else {
919 snapToDestination();
920 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700921 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 // at this point we have not moved beyond the touch slop
923 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
924 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700925 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
926 if (nextPage != mCurrentPage) {
927 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 } else {
929 snapToDestination();
930 }
931 }
932 mTouchState = TOUCH_STATE_REST;
933 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700934 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 break;
936
937 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700938 if (mTouchState == TOUCH_STATE_SCROLLING) {
939 snapToDestination();
940 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 mTouchState = TOUCH_STATE_REST;
942 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700943 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 break;
945
946 case MotionEvent.ACTION_POINTER_UP:
947 onSecondaryPointerUp(ev);
948 break;
949 }
950
951 return true;
952 }
953
Michael Jurkab8f06722010-10-10 15:58:46 -0700954 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
955 if (mVelocityTracker == null) {
956 mVelocityTracker = VelocityTracker.obtain();
957 }
958 mVelocityTracker.addMovement(ev);
959 }
960
961 private void releaseVelocityTracker() {
962 if (mVelocityTracker != null) {
963 mVelocityTracker.recycle();
964 mVelocityTracker = null;
965 }
966 }
967
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 private void onSecondaryPointerUp(MotionEvent ev) {
969 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
970 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
971 final int pointerId = ev.getPointerId(pointerIndex);
972 if (pointerId == mActivePointerId) {
973 // This was our active pointer going up. Choose a new
974 // active pointer and adjust accordingly.
975 // TODO: Make this decision more intelligent.
976 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
977 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
978 mLastMotionY = ev.getY(newPointerIndex);
979 mActivePointerId = ev.getPointerId(newPointerIndex);
980 if (mVelocityTracker != null) {
981 mVelocityTracker.clear();
982 }
983 }
984 }
985
986 @Override
987 public void requestChildFocus(View child, View focused) {
988 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700989 int page = indexOfChild(child);
990 if (page >= 0 && !isInTouchMode()) {
991 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 }
993 }
994
Winson Chunge3193b92010-09-10 11:44:42 -0700995 protected int getChildIndexForRelativeOffset(int relativeOffset) {
996 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700997 int left;
998 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700999 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001000 left = getRelativeChildOffset(i);
1001 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -07001002 if (left <= relativeOffset && relativeOffset <= right) {
1003 return i;
1004 }
Winson Chunge3193b92010-09-10 11:44:42 -07001005 }
1006 return -1;
1007 }
1008
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 protected int getRelativeChildOffset(int index) {
1010 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1011 }
1012
1013 protected int getChildOffset(int index) {
1014 if (getChildCount() == 0)
1015 return 0;
1016
1017 int offset = getRelativeChildOffset(0);
1018 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001019 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 }
1021 return offset;
1022 }
1023
Adam Cohend19d3ca2010-09-15 14:43:42 -07001024 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 int minDistanceFromScreenCenter = getMeasuredWidth();
1026 int minDistanceFromScreenCenterIndex = -1;
1027 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1028 final int childCount = getChildCount();
1029 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001030 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 int childWidth = layout.getMeasuredWidth();
1032 int halfChildWidth = (childWidth / 2);
1033 int childCenter = getChildOffset(i) + halfChildWidth;
1034 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1035 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1036 minDistanceFromScreenCenter = distanceFromScreenCenter;
1037 minDistanceFromScreenCenterIndex = i;
1038 }
1039 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001040 return minDistanceFromScreenCenterIndex;
1041 }
1042
1043 protected void snapToDestination() {
1044 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 }
1046
Michael Jurka0142d492010-08-25 17:46:15 -07001047 protected void snapToPageWithVelocity(int whichPage, int velocity) {
1048 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
1049 // can use it
1050 snapToPage(whichPage);
1051 }
1052
1053 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001054 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 }
1056
Michael Jurka0142d492010-08-25 17:46:15 -07001057 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001058 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001059
Winson Chung86f77532010-08-24 11:08:22 -07001060 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001061 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001063 snapToPage(whichPage, delta, duration);
1064 }
1065
1066 protected void snapToPage(int whichPage, int delta, int duration) {
1067 mNextPage = whichPage;
1068
1069 View focusedChild = getFocusedChild();
1070 if (focusedChild != null && whichPage != mCurrentPage &&
1071 focusedChild == getChildAt(mCurrentPage)) {
1072 focusedChild.clearFocus();
1073 }
1074
1075 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001076 awakenScrollBars(duration);
1077 if (duration == 0) {
1078 duration = Math.abs(delta);
1079 }
1080
1081 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001082 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001083
1084 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001085 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001086 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001087 invalidate();
1088 }
1089
1090 @Override
1091 protected Parcelable onSaveInstanceState() {
1092 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001093 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 return state;
1095 }
1096
1097 @Override
1098 protected void onRestoreInstanceState(Parcelable state) {
1099 SavedState savedState = (SavedState) state;
1100 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001101 if (savedState.currentPage != -1) {
1102 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001103 }
1104 }
1105
1106 public void scrollLeft() {
1107 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001108 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001109 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001110 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 }
1112 }
1113
1114 public void scrollRight() {
1115 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001116 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001118 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 }
1120 }
1121
Winson Chung86f77532010-08-24 11:08:22 -07001122 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001123 int result = -1;
1124 if (v != null) {
1125 ViewParent vp = v.getParent();
1126 int count = getChildCount();
1127 for (int i = 0; i < count; i++) {
1128 if (vp == getChildAt(i)) {
1129 return i;
1130 }
1131 }
1132 }
1133 return result;
1134 }
1135
1136 /**
1137 * @return True is long presses are still allowed for the current touch
1138 */
1139 public boolean allowLongPress() {
1140 return mAllowLongPress;
1141 }
1142
Michael Jurka0142d492010-08-25 17:46:15 -07001143 /**
1144 * Set true to allow long-press events to be triggered, usually checked by
1145 * {@link Launcher} to accept or block dpad-initiated long-presses.
1146 */
1147 public void setAllowLongPress(boolean allowLongPress) {
1148 mAllowLongPress = allowLongPress;
1149 }
1150
Winson Chung321e9ee2010-08-09 13:37:56 -07001151 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001152 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001153
1154 SavedState(Parcelable superState) {
1155 super(superState);
1156 }
1157
1158 private SavedState(Parcel in) {
1159 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001160 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 }
1162
1163 @Override
1164 public void writeToParcel(Parcel out, int flags) {
1165 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001166 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 }
1168
1169 public static final Parcelable.Creator<SavedState> CREATOR =
1170 new Parcelable.Creator<SavedState>() {
1171 public SavedState createFromParcel(Parcel in) {
1172 return new SavedState(in);
1173 }
1174
1175 public SavedState[] newArray(int size) {
1176 return new SavedState[size];
1177 }
1178 };
1179 }
1180
Winson Chung86f77532010-08-24 11:08:22 -07001181 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001182 if (mContentIsRefreshable) {
1183 final int count = getChildCount();
1184 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001185 int lowerPageBound = getAssociatedLowerPageBound(page);
1186 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001187 for (int i = 0; i < count; ++i) {
1188 final ViewGroup layout = (ViewGroup) getChildAt(i);
1189 final int childCount = layout.getChildCount();
1190 if (lowerPageBound <= i && i <= upperPageBound) {
1191 if (mDirtyPageContent.get(i)) {
1192 syncPageItems(i);
1193 mDirtyPageContent.set(i, false);
1194 }
1195 } else {
1196 if (childCount > 0) {
1197 layout.removeAllViews();
1198 }
1199 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001200 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001201 }
1202 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001203 }
1204 }
1205
Winson Chunge3193b92010-09-10 11:44:42 -07001206 protected int getAssociatedLowerPageBound(int page) {
1207 return Math.max(0, page - 1);
1208 }
1209 protected int getAssociatedUpperPageBound(int page) {
1210 final int count = getChildCount();
1211 return Math.min(page + 1, count - 1);
1212 }
1213
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001214 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001215 if (isChoiceMode(CHOICE_MODE_NONE)) {
1216 mChoiceMode = mode;
1217 mActionMode = startActionMode(callback);
1218 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001219 }
1220
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001221 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001222 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001223 mChoiceMode = CHOICE_MODE_NONE;
1224 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001225 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001226 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001227 }
1228 }
1229
1230 protected boolean isChoiceMode(int mode) {
1231 return mChoiceMode == mode;
1232 }
1233
1234 protected ArrayList<Checkable> getCheckedGrandchildren() {
1235 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1236 final int childCount = getChildCount();
1237 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001238 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001239 final int grandChildCount = layout.getChildCount();
1240 for (int j = 0; j < grandChildCount; ++j) {
1241 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001242 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001243 checked.add((Checkable) v);
1244 }
1245 }
1246 }
1247 return checked;
1248 }
1249
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001250 /**
1251 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1252 * Otherwise, returns null.
1253 */
1254 protected Checkable getSingleCheckedGrandchild() {
1255 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1256 final int childCount = getChildCount();
1257 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001258 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001259 final int grandChildCount = layout.getChildCount();
1260 for (int j = 0; j < grandChildCount; ++j) {
1261 final View v = layout.getChildAt(j);
1262 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1263 return (Checkable) v;
1264 }
1265 }
1266 }
1267 }
1268 return null;
1269 }
1270
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001271 public Object getChosenItem() {
1272 View checkedView = (View) getSingleCheckedGrandchild();
1273 if (checkedView != null) {
1274 return checkedView.getTag();
1275 }
1276 return null;
1277 }
1278
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001279 protected void resetCheckedGrandchildren() {
1280 // loop through children, and set all of their children to _not_ be checked
1281 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1282 for (int i = 0; i < checked.size(); ++i) {
1283 final Checkable c = checked.get(i);
1284 c.setChecked(false);
1285 }
1286 }
1287
Winson Chung86f77532010-08-24 11:08:22 -07001288 /**
1289 * This method is called ONLY to synchronize the number of pages that the paged view has.
1290 * To actually fill the pages with information, implement syncPageItems() below. It is
1291 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1292 * and therefore, individual page items do not need to be updated in this method.
1293 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001295
1296 /**
1297 * This method is called to synchronize the items that are on a particular page. If views on
1298 * the page can be reused, then they should be updated within this method.
1299 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001301
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001303 if (mContentIsRefreshable) {
1304 // Update all the pages
1305 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001306
Michael Jurka0142d492010-08-25 17:46:15 -07001307 // Mark each of the pages as dirty
1308 final int count = getChildCount();
1309 mDirtyPageContent.clear();
1310 for (int i = 0; i < count; ++i) {
1311 mDirtyPageContent.add(true);
1312 }
1313
1314 // Load any pages that are necessary for the current window of views
1315 loadAssociatedPages(mCurrentPage);
1316 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001317 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001318 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001319 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 }
1321}