blob: 3719746c4dfdc8ed516489b5006ff94cab26aed9 [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 Chung04998342011-01-05 13:54:43 -080019import java.util.ArrayList;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070020
Winson Chung228a0fa2011-01-26 22:14:13 -080021import android.animation.Animator;
22import android.animation.AnimatorInflater;
23import android.animation.AnimatorListenerAdapter;
24import android.animation.ObjectAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
28import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.os.Parcel;
30import android.os.Parcelable;
31import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080039import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Winson Chung04998342011-01-05 13:54:43 -080043import 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
Adam Cohene0f66b52010-11-23 15:06:07 -080058 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
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 Cohene0f66b52010-11-23 15:06:07 -080061 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
62 private static final int MINIMUM_SNAP_VELOCITY = 2200;
63 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080064 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080065
Michael Jurka0142d492010-08-25 17:46:15 -070066 // the velocity at which a fling gesture will cause us to snap to the next page
67 protected int mSnapVelocity = 500;
68
69 protected float mSmoothingTime;
70 protected float mTouchX;
71
72 protected boolean mFirstLayout = true;
73
74 protected int mCurrentPage;
75 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080076 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080077 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070079 private VelocityTracker mVelocityTracker;
80
81 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080082 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080083 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080084 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080085 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070086 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070087
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected final static int TOUCH_STATE_REST = 0;
89 protected final static int TOUCH_STATE_SCROLLING = 1;
90 protected final static int TOUCH_STATE_PREV_PAGE = 2;
91 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070092 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101 private int mPagingTouchSlop;
102 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700103 protected int mPageSpacing;
104 protected int mPageLayoutPaddingTop;
105 protected int mPageLayoutPaddingBottom;
106 protected int mPageLayoutPaddingLeft;
107 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700108 protected int mPageLayoutWidthGap;
109 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700110 protected int mCellCountX;
111 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700112 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800113 protected boolean mAllowOverScroll = true;
114 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Michael Jurka8c920dd2011-01-20 14:16:56 -0800116 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800117 protected float mLayoutScale = 1.0f;
118
Michael Jurka5f1c5092010-09-03 14:15:02 -0700119 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka5f1c5092010-09-03 14:15:02 -0700121 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Winson Chung86f77532010-08-24 11:08:22 -0700123 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Winson Chung86f77532010-08-24 11:08:22 -0700125 private ArrayList<Boolean> mDirtyPageContent;
126 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700128 // choice modes
129 protected static final int CHOICE_MODE_NONE = 0;
130 protected static final int CHOICE_MODE_SINGLE = 1;
131 // Multiple selection mode is not supported by all Launcher actions atm
132 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700133
Michael Jurkae17e19c2010-09-28 11:01:39 -0700134 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700135 private ActionMode mActionMode;
136
Winson Chung04998342011-01-05 13:54:43 -0800137 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
138 // AllApps and Customize, and allows them to share holographic icons for the application view
139 // (which is in both).
140 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700141
Michael Jurka0142d492010-08-25 17:46:15 -0700142 // If true, syncPages and syncPageItems will be called to refresh pages
143 protected boolean mContentIsRefreshable = true;
144
145 // If true, modify alpha of neighboring pages as user scrolls left/right
146 protected boolean mFadeInAdjacentScreens = true;
147
148 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
149 // to switch to a new page
150 protected boolean mUsePagingTouchSlop = true;
151
152 // If true, the subclass should directly update mScrollX itself in its computeScroll method
153 // (SmoothPagedView does this)
154 protected boolean mDeferScrollUpdate = false;
155
Patrick Dubroy1262e362010-10-06 15:49:50 -0700156 protected boolean mIsPageMoving = false;
157
Winson Chung86f77532010-08-24 11:08:22 -0700158 public interface PageSwitchListener {
159 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 }
161
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 public PagedView(Context context) {
163 this(context, null);
164 }
165
166 public PagedView(Context context, AttributeSet attrs) {
167 this(context, attrs, 0);
168 }
169
170 public PagedView(Context context, AttributeSet attrs, int defStyle) {
171 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700172 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700173
Adam Cohen9c4949e2010-10-05 12:27:22 -0700174 TypedArray a = context.obtainStyledAttributes(attrs,
175 R.styleable.PagedView, defStyle, 0);
176 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
177 mPageLayoutPaddingTop = a.getDimensionPixelSize(
178 R.styleable.PagedView_pageLayoutPaddingTop, 10);
179 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
180 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
181 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
182 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
183 mPageLayoutPaddingRight = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700185 mPageLayoutWidthGap = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutWidthGap, -1);
187 mPageLayoutHeightGap = a.getDimensionPixelSize(
188 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700189 a.recycle();
190
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700192 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 }
194
195 /**
196 * Initializes various states for this workspace.
197 */
Michael Jurka0142d492010-08-25 17:46:15 -0700198 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700199 mDirtyPageContent = new ArrayList<Boolean>();
200 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800201 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700202 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700203 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700204
205 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
206 mTouchSlop = configuration.getScaledTouchSlop();
207 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
208 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
209 }
210
Winson Chung86f77532010-08-24 11:08:22 -0700211 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
212 mPageSwitchListener = pageSwitchListener;
213 if (mPageSwitchListener != null) {
214 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 }
216 }
217
218 /**
Winson Chung86f77532010-08-24 11:08:22 -0700219 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 *
Winson Chung86f77532010-08-24 11:08:22 -0700221 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 */
Winson Chung86f77532010-08-24 11:08:22 -0700223 int getCurrentPage() {
224 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 }
226
Winson Chung86f77532010-08-24 11:08:22 -0700227 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 return getChildCount();
229 }
230
Winson Chung86f77532010-08-24 11:08:22 -0700231 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 return getChildAt(index);
233 }
234
235 int getScrollWidth() {
236 return getWidth();
237 }
238
Adam Cohenbe909342011-01-28 17:02:58 -0800239 public int getTouchState() {
240 return mTouchState;
241 }
242
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800244 * Updates the scroll of the current page immediately to its final scroll position. We use this
245 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
246 * the previous tab page.
247 */
248 protected void updateCurrentPageScroll() {
249 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
250 scrollTo(newX, 0);
251 mScroller.setFinalX(newX);
252 }
253
254 /**
Winson Chung86f77532010-08-24 11:08:22 -0700255 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 */
Winson Chung86f77532010-08-24 11:08:22 -0700257 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800258 if (!mScroller.isFinished()) {
259 mScroller.abortAnimation();
260 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800261 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
262 // the default
263 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800264 return;
265 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700266
Winson Chung86f77532010-08-24 11:08:22 -0700267 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800268 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700269 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800270 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 }
272
Michael Jurka0142d492010-08-25 17:46:15 -0700273 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700274 if (mPageSwitchListener != null) {
275 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700276 }
277 }
278
Michael Jurkace7e05f2011-02-01 22:02:35 -0800279 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700280 mIsPageMoving = true;
281 onPageBeginMoving();
282 }
283
Michael Jurkace7e05f2011-02-01 22:02:35 -0800284 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700285 onPageEndMoving();
286 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700287 }
288
289 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700290 protected void onPageBeginMoving() {
291 }
292
293 // a method that subclasses can override to add behavior
294 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700295 }
296
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 /**
Winson Chung86f77532010-08-24 11:08:22 -0700298 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 *
300 * @param l The listener used to respond to long clicks.
301 */
302 @Override
303 public void setOnLongClickListener(OnLongClickListener l) {
304 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700305 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700307 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 }
309 }
310
311 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800312 public void scrollBy(int x, int y) {
313 scrollTo(mUnboundedScrollX + x, mScrollY + y);
314 }
315
316 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700317 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800318 mUnboundedScrollX = x;
319
320 if (x < 0) {
321 super.scrollTo(0, y);
322 if (mAllowOverScroll) {
323 overScroll(x);
324 }
325 } else if (x > mMaxScrollX) {
326 super.scrollTo(mMaxScrollX, y);
327 if (mAllowOverScroll) {
328 overScroll(x - mMaxScrollX);
329 }
330 } else {
331 super.scrollTo(x, y);
332 }
333
Michael Jurka0142d492010-08-25 17:46:15 -0700334 mTouchX = x;
335 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
336 }
337
338 // we moved this functionality to a helper function so SmoothPagedView can reuse it
339 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700340 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700341 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700343 invalidate();
344 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700345 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700346 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700347 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700348 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700349 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800350 // We don't want to trigger a page end moving unless the page has settled
351 // and the user has stopped scrolling
352 if (mTouchState == TOUCH_STATE_REST) {
353 pageEndMoving();
354 }
Michael Jurka0142d492010-08-25 17:46:15 -0700355 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
Michael Jurka0142d492010-08-25 17:46:15 -0700357 return false;
358 }
359
360 @Override
361 public void computeScroll() {
362 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
364
365 @Override
366 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
367 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
368 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
369 if (widthMode != MeasureSpec.EXACTLY) {
370 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
371 }
372
Adam Lesinski6b879f02010-11-04 16:15:23 -0700373 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
374 * of the All apps view on XLarge displays to not take up more space then it needs. Width
375 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
376 * each page to have the same width.
377 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700378 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700379 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
380 int maxChildHeight = 0;
381
382 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700383
384 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700385 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 final int childCount = getChildCount();
387 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700388 // disallowing padding in paged view (just pass 0)
389 final View child = getChildAt(i);
390 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
391
392 int childWidthMode;
393 if (lp.width == LayoutParams.WRAP_CONTENT) {
394 childWidthMode = MeasureSpec.AT_MOST;
395 } else {
396 childWidthMode = MeasureSpec.EXACTLY;
397 }
398
399 int childHeightMode;
400 if (lp.height == LayoutParams.WRAP_CONTENT) {
401 childHeightMode = MeasureSpec.AT_MOST;
402 } else {
403 childHeightMode = MeasureSpec.EXACTLY;
404 }
405
406 final int childWidthMeasureSpec =
407 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
408 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700409 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700410
411 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700412 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
413 }
414
415 if (heightMode == MeasureSpec.AT_MOST) {
416 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800418 if (childCount > 0) {
419 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
420 } else {
421 mMaxScrollX = 0;
422 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700423
424 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700425 }
426
Michael Jurka8c920dd2011-01-20 14:16:56 -0800427 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800428 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
429 int delta = newX - mScrollX;
430
Michael Jurka8c920dd2011-01-20 14:16:56 -0800431 final int pageCount = getChildCount();
432 for (int i = 0; i < pageCount; i++) {
433 View page = (View) getChildAt(i);
434 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800435 }
436 setCurrentPage(newCurrentPage);
437 }
438
Michael Jurka8c920dd2011-01-20 14:16:56 -0800439 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
440 // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and
Michael Jurkad3ef3062010-11-23 16:23:58 -0800441 // tightens the layout accordingly
442 public void setLayoutScale(float childrenScale) {
443 mLayoutScale = childrenScale;
444
445 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
446 int childCount = getChildCount();
447 float childrenX[] = new float[childCount];
448 float childrenY[] = new float[childCount];
449 for (int i = 0; i < childCount; i++) {
450 final View child = getChildAt(i);
451 childrenX[i] = child.getX();
452 childrenY[i] = child.getY();
453 }
454 onLayout(false, mLeft, mTop, mRight, mBottom);
455 for (int i = 0; i < childCount; i++) {
456 final View child = getChildAt(i);
457 child.setX(childrenX[i]);
458 child.setY(childrenY[i]);
459 }
460 // Also, the page offset has changed (since the pages are now smaller);
461 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800462 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800463 }
464
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700466 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700467 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
468 setHorizontalScrollBarEnabled(false);
469 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
470 scrollTo(newX, 0);
471 mScroller.setFinalX(newX);
472 setHorizontalScrollBarEnabled(true);
473 mFirstLayout = false;
474 }
475
Adam Lesinski6b879f02010-11-04 16:15:23 -0700476 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 final int childCount = getChildCount();
478 int childLeft = 0;
479 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700480 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 }
482
483 for (int i = 0; i < childCount; i++) {
484 final View child = getChildAt(i);
485 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800486 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700487 final int childHeight = child.getMeasuredHeight();
488 int childTop = mPaddingTop;
489 if (mCenterPagesVertically) {
490 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
491 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800492
Adam Lesinski6b879f02010-11-04 16:15:23 -0700493 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800494 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700495 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 }
497 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800498 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
499 mFirstLayout = false;
500 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 }
502
Winson Chunge3193b92010-09-10 11:44:42 -0700503 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700504 if (mFadeInAdjacentScreens) {
505 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700506 int halfScreenSize = getMeasuredWidth() / 2;
507 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700508 final int childCount = getChildCount();
509 for (int i = 0; i < childCount; ++i) {
510 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800511 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700512 int halfChildWidth = (childWidth / 2);
513 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700514
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700515 // On the first layout, we may not have a width nor a proper offset, so for now
516 // we should just assume full page width (and calculate the offset according to
517 // that).
518 if (childWidth <= 0) {
519 childWidth = getMeasuredWidth();
520 childCenter = (i * childWidth) + (childWidth / 2);
521 }
522
Winson Chunge8878e32010-09-15 20:37:09 -0700523 int d = halfChildWidth;
524 int distanceFromScreenCenter = childCenter - screenCenter;
525 if (distanceFromScreenCenter > 0) {
526 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800527 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700528 }
Michael Jurka0142d492010-08-25 17:46:15 -0700529 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700530 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800531 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700532 }
Michael Jurka0142d492010-08-25 17:46:15 -0700533 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700534 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700535
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700536 // Preventing potential divide-by-zero
537 d = Math.max(1, d);
538
Winson Chunge8878e32010-09-15 20:37:09 -0700539 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
540 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
541 float alpha = 1.0f - dimAlpha;
542
Adam Cohenf34bab52010-09-30 14:11:56 -0700543 if (alpha < ALPHA_QUANTIZE_LEVEL) {
544 alpha = 0.0f;
545 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
546 alpha = 1.0f;
547 }
548
Michael Jurkacfdb0962011-02-04 01:38:00 -0800549 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700550 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800551 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
Michael Jurka0142d492010-08-25 17:46:15 -0700553 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 }
555 }
Winson Chunge3193b92010-09-10 11:44:42 -0700556 }
557
Adam Cohenf34bab52010-09-30 14:11:56 -0700558 protected void screenScrolled(int screenCenter) {
559 }
560
Winson Chunge3193b92010-09-10 11:44:42 -0700561 @Override
562 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700563 int halfScreenSize = getMeasuredWidth() / 2;
564 int screenCenter = mScrollX + halfScreenSize;
565
566 if (screenCenter != mLastScreenCenter) {
567 screenScrolled(screenCenter);
568 updateAdjacentPagesAlpha();
569 mLastScreenCenter = screenCenter;
570 }
Michael Jurka0142d492010-08-25 17:46:15 -0700571
572 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700573 // As an optimization, this code assumes that all pages have the same width as the 0th
574 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700575 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700576 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800577 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700578 final int screenWidth = getMeasuredWidth();
579 int x = getRelativeChildOffset(0) + pageWidth;
580 int leftScreen = 0;
581 int rightScreen = 0;
582 while (x <= mScrollX) {
583 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800584 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700585 }
586 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800587 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700588 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800589 if (rightScreen < pageCount) {
590 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
591 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700592 }
593 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700594
Michael Jurkac4fb9172010-09-02 17:19:20 -0700595 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800596 // Clip to the bounds
597 canvas.save();
598 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
599 mScrollY + mBottom - mTop);
600
Michael Jurkac0759f52011-02-03 16:47:14 -0800601 for (int i = 0; i < pageCount; i++) {
602 View child = getChildAt(i);
603 if (child != null && child instanceof PagedViewCellLayout) {
604 boolean willBeDrawn = i >= leftScreen && i <= rightScreen;
605 if (!willBeDrawn) {
606 ((PagedViewCellLayout)child).destroyHardwareLayers();
607 }
608 }
609 }
610
Michael Jurkac4fb9172010-09-02 17:19:20 -0700611 for (int i = leftScreen; i <= rightScreen; i++) {
Michael Jurkac0759f52011-02-03 16:47:14 -0800612 View child = getChildAt(i);
613 if (child != null && child instanceof PagedViewCellLayout) {
614 ((PagedViewCellLayout)child).createHardwareLayers();
615 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700616 drawChild(canvas, getChildAt(i), drawingTime);
617 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800618 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700619 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 }
621
622 @Override
623 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700624 int page = indexOfChild(child);
625 if (page != mCurrentPage || !mScroller.isFinished()) {
626 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 return true;
628 }
629 return false;
630 }
631
632 @Override
633 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 int focusablePage;
635 if (mNextPage != INVALID_PAGE) {
636 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700638 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 }
Winson Chung86f77532010-08-24 11:08:22 -0700640 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 if (v != null) {
642 v.requestFocus(direction, previouslyFocusedRect);
643 }
644 return false;
645 }
646
647 @Override
648 public boolean dispatchUnhandledMove(View focused, int direction) {
649 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700650 if (getCurrentPage() > 0) {
651 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700652 return true;
653 }
654 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700655 if (getCurrentPage() < getPageCount() - 1) {
656 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700657 return true;
658 }
659 }
660 return super.dispatchUnhandledMove(focused, direction);
661 }
662
663 @Override
664 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700665 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
666 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 }
668 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700669 if (mCurrentPage > 0) {
670 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700671 }
672 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700673 if (mCurrentPage < getPageCount() - 1) {
674 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 }
676 }
677 }
678
679 /**
680 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700681 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 *
Winson Chung86f77532010-08-24 11:08:22 -0700683 * This happens when live folders requery, and if they're off page, they
684 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 */
686 @Override
687 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700688 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 View v = focused;
690 while (true) {
691 if (v == current) {
692 super.focusableViewAvailable(focused);
693 return;
694 }
695 if (v == this) {
696 return;
697 }
698 ViewParent parent = v.getParent();
699 if (parent instanceof View) {
700 v = (View)v.getParent();
701 } else {
702 return;
703 }
704 }
705 }
706
707 /**
708 * {@inheritDoc}
709 */
710 @Override
711 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
712 if (disallowIntercept) {
713 // We need to make sure to cancel our long press if
714 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700715 final View currentPage = getChildAt(mCurrentPage);
716 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 }
718 super.requestDisallowInterceptTouchEvent(disallowIntercept);
719 }
720
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800721 /**
722 * Return true if a tap at (x, y) should trigger a flip to the previous page.
723 */
724 protected boolean hitsPreviousPage(float x, float y) {
725 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
726 }
727
728 /**
729 * Return true if a tap at (x, y) should trigger a flip to the next page.
730 */
731 protected boolean hitsNextPage(float x, float y) {
732 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
733 }
734
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 @Override
736 public boolean onInterceptTouchEvent(MotionEvent ev) {
737 /*
738 * This method JUST determines whether we want to intercept the motion.
739 * If we return true, onTouchEvent will be called and we do the actual
740 * scrolling there.
741 */
742
Winson Chung45e1d6e2010-11-09 17:19:49 -0800743 // Skip touch handling if there are no pages to swipe
744 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
745
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 /*
747 * Shortcut the most recurring case: the user is in the dragging
748 * state and he is moving his finger. We want to intercept this
749 * motion.
750 */
751 final int action = ev.getAction();
752 if ((action == MotionEvent.ACTION_MOVE) &&
753 (mTouchState == TOUCH_STATE_SCROLLING)) {
754 return true;
755 }
756
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 switch (action & MotionEvent.ACTION_MASK) {
758 case MotionEvent.ACTION_MOVE: {
759 /*
760 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
761 * whether the user has moved far enough from his original down touch.
762 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700763 if (mActivePointerId != INVALID_POINTER) {
764 determineScrollingStart(ev);
765 break;
766 }
767 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
768 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
769 // i.e. fall through to the next case (don't break)
770 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
771 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 }
773
774 case MotionEvent.ACTION_DOWN: {
775 final float x = ev.getX();
776 final float y = ev.getY();
777 // Remember location of down touch
778 mDownMotionX = x;
779 mLastMotionX = x;
780 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800781 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800782 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 mActivePointerId = ev.getPointerId(0);
784 mAllowLongPress = true;
785
786 /*
787 * If being flinged and user touches the screen, initiate drag;
788 * otherwise don't. mScroller.isFinished should be false when
789 * being flinged.
790 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700791 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700792 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
793 if (finishedScrolling) {
794 mTouchState = TOUCH_STATE_REST;
795 mScroller.abortAnimation();
796 } else {
797 mTouchState = TOUCH_STATE_SCROLLING;
798 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700799
Winson Chung86f77532010-08-24 11:08:22 -0700800 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800802 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800804 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800806 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 mTouchState = TOUCH_STATE_NEXT_PAGE;
808 }
809 }
810 }
811 break;
812 }
813
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800815 onWallpaperTap(ev);
816 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 mTouchState = TOUCH_STATE_REST;
818 mAllowLongPress = false;
819 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 break;
821
822 case MotionEvent.ACTION_POINTER_UP:
823 onSecondaryPointerUp(ev);
824 break;
825 }
826
827 /*
828 * The only time we want to intercept motion events is if we are in the
829 * drag mode.
830 */
831 return mTouchState != TOUCH_STATE_REST;
832 }
833
Winson Chung80baf5a2010-08-09 16:03:15 -0700834 protected void animateClickFeedback(View v, final Runnable r) {
835 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800836 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
837 loadAnimator(mContext, R.anim.paged_view_click_feedback);
838 anim.setTarget(v);
839 anim.addListener(new AnimatorListenerAdapter() {
840 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700841 r.run();
842 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700843 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800844 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700845 }
846
Adam Cohenf8d28232011-02-01 21:47:00 -0800847 protected void determineScrollingStart(MotionEvent ev) {
848 determineScrollingStart(ev, 1.0f);
849 }
850
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 /*
852 * Determines if we should change the touch state to start scrolling after the
853 * user moves their touch point too far.
854 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800855 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 /*
857 * Locally do absolute value. mLastMotionX is set to the y value
858 * of the down event.
859 */
860 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
861 final float x = ev.getX(pointerIndex);
862 final float y = ev.getY(pointerIndex);
863 final int xDiff = (int) Math.abs(x - mLastMotionX);
864 final int yDiff = (int) Math.abs(y - mLastMotionY);
865
Adam Cohenf8d28232011-02-01 21:47:00 -0800866 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 boolean xPaged = xDiff > mPagingTouchSlop;
868 boolean xMoved = xDiff > touchSlop;
869 boolean yMoved = yDiff > touchSlop;
870
Adam Cohenf8d28232011-02-01 21:47:00 -0800871 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700872 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 // Scroll if the user moved far enough along the X axis
874 mTouchState = TOUCH_STATE_SCROLLING;
875 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800876 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700877 mTouchX = mScrollX;
878 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
879 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 }
881 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800882 cancelCurrentPageLongPress();
883 }
884 }
885
886 protected void cancelCurrentPageLongPress() {
887 if (mAllowLongPress) {
888 mAllowLongPress = false;
889 // Try canceling the long press. It could also have been scheduled
890 // by a distant descendant, so use the mAllowLongPress flag to block
891 // everything
892 final View currentPage = getPageAt(mCurrentPage);
893 if (currentPage != null) {
894 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 }
896 }
897 }
898
Adam Cohene0f66b52010-11-23 15:06:07 -0800899 // This curve determines how the effect of scrolling over the limits of the page dimishes
900 // as the user pulls further and further from the bounds
901 private float overScrollInfluenceCurve(float f) {
902 f -= 1.0f;
903 return f * f * f + 1.0f;
904 }
905
Adam Cohen68d73932010-11-15 10:50:58 -0800906 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800907 int screenSize = getMeasuredWidth();
908
909 float f = (amount / screenSize);
910
911 if (f == 0) return;
912 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
913
Adam Cohen7bfc9792011-01-28 13:52:37 -0800914 // Clamp this factor, f, to -1 < f < 1
915 if (Math.abs(f) >= 1) {
916 f /= Math.abs(f);
917 }
918
Adam Cohene0f66b52010-11-23 15:06:07 -0800919 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800920 if (amount < 0) {
921 mScrollX = overScrollAmount;
922 } else {
923 mScrollX = mMaxScrollX + overScrollAmount;
924 }
925 invalidate();
926 }
927
Michael Jurkac5b262c2011-01-12 20:24:50 -0800928 protected float maxOverScroll() {
929 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
930 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
931 float f = 1.0f;
932 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
933 return OVERSCROLL_DAMP_FACTOR * f;
934 }
935
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 @Override
937 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800938 // Skip touch handling if there are no pages to swipe
939 if (getChildCount() <= 0) return super.onTouchEvent(ev);
940
Michael Jurkab8f06722010-10-10 15:58:46 -0700941 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700942
943 final int action = ev.getAction();
944
945 switch (action & MotionEvent.ACTION_MASK) {
946 case MotionEvent.ACTION_DOWN:
947 /*
948 * If being flinged and user touches, stop the fling. isFinished
949 * will be false if being flinged.
950 */
951 if (!mScroller.isFinished()) {
952 mScroller.abortAnimation();
953 }
954
955 // Remember where the motion event started
956 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800957 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800958 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700960 if (mTouchState == TOUCH_STATE_SCROLLING) {
961 pageBeginMoving();
962 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 break;
964
965 case MotionEvent.ACTION_MOVE:
966 if (mTouchState == TOUCH_STATE_SCROLLING) {
967 // Scroll to follow the motion event
968 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
969 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800970 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700971
Adam Cohenaefd4e12011-02-14 16:39:38 -0800972 mTotalMotionX += Math.abs(deltaX);
973
Winson Chungc0844aa2011-02-02 15:25:58 -0800974 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
975 // keep the remainder because we are actually testing if we've moved from the last
976 // scrolled position (which is discrete).
977 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800978 mTouchX += deltaX;
979 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
980 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800981 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800982 } else {
983 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800985 mLastMotionX = x;
986 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 } else {
988 awakenScrollBars();
989 }
Adam Cohen564976a2010-10-13 18:52:07 -0700990 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 determineScrollingStart(ev);
992 }
993 break;
994
995 case MotionEvent.ACTION_UP:
996 if (mTouchState == TOUCH_STATE_SCROLLING) {
997 final int activePointerId = mActivePointerId;
998 final int pointerIndex = ev.findPointerIndex(activePointerId);
999 final float x = ev.getX(pointerIndex);
1000 final VelocityTracker velocityTracker = mVelocityTracker;
1001 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1002 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001003 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001004 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001005 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001006
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001007 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1008
Adam Cohenaefd4e12011-02-14 16:39:38 -08001009 // In the case that the page is moved far to one direction and then is flung
1010 // in the opposite direction, we use a threshold to determine whether we should
1011 // just return to the starting page, or if we should skip one further.
1012 boolean returnToOriginalPage = false;
1013 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001014 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001015 Math.signum(velocityX) != Math.signum(deltaX)) {
1016 returnToOriginalPage = true;
1017 }
1018
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001019 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001020 Math.abs(velocityX) > snapVelocity;
1021
1022 int finalPage;
1023 // We give flings precedence over large moves, which is why we short-circuit our
1024 // test for a large move if a fling has been registered. That is, a large
1025 // move to the left and fling to the right will register as a fling to the right.
1026 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1027 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1028 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1029 snapToPageWithVelocity(finalPage, velocityX);
1030 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1031 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001032 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001033 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1034 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 } else {
1036 snapToDestination();
1037 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001038 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 // at this point we have not moved beyond the touch slop
1040 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1041 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001042 int nextPage = Math.max(0, mCurrentPage - 1);
1043 if (nextPage != mCurrentPage) {
1044 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 } else {
1046 snapToDestination();
1047 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001048 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 // at this point we have not moved beyond the touch slop
1050 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1051 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001052 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1053 if (nextPage != mCurrentPage) {
1054 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 } else {
1056 snapToDestination();
1057 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001058 } else {
1059 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 }
1061 mTouchState = TOUCH_STATE_REST;
1062 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001063 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 break;
1065
1066 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001067 if (mTouchState == TOUCH_STATE_SCROLLING) {
1068 snapToDestination();
1069 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 mTouchState = TOUCH_STATE_REST;
1071 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001072 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 break;
1074
1075 case MotionEvent.ACTION_POINTER_UP:
1076 onSecondaryPointerUp(ev);
1077 break;
1078 }
1079
1080 return true;
1081 }
1082
Michael Jurkab8f06722010-10-10 15:58:46 -07001083 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1084 if (mVelocityTracker == null) {
1085 mVelocityTracker = VelocityTracker.obtain();
1086 }
1087 mVelocityTracker.addMovement(ev);
1088 }
1089
1090 private void releaseVelocityTracker() {
1091 if (mVelocityTracker != null) {
1092 mVelocityTracker.recycle();
1093 mVelocityTracker = null;
1094 }
1095 }
1096
Winson Chung321e9ee2010-08-09 13:37:56 -07001097 private void onSecondaryPointerUp(MotionEvent ev) {
1098 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1099 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1100 final int pointerId = ev.getPointerId(pointerIndex);
1101 if (pointerId == mActivePointerId) {
1102 // This was our active pointer going up. Choose a new
1103 // active pointer and adjust accordingly.
1104 // TODO: Make this decision more intelligent.
1105 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1106 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1107 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001108 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001109 mActivePointerId = ev.getPointerId(newPointerIndex);
1110 if (mVelocityTracker != null) {
1111 mVelocityTracker.clear();
1112 }
1113 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001114 if (mTouchState == TOUCH_STATE_REST) {
1115 onWallpaperTap(ev);
1116 }
1117 }
1118
1119 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001120 }
1121
1122 @Override
1123 public void requestChildFocus(View child, View focused) {
1124 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001125 int page = indexOfChild(child);
1126 if (page >= 0 && !isInTouchMode()) {
1127 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001128 }
1129 }
1130
Winson Chunge3193b92010-09-10 11:44:42 -07001131 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1132 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001133 int left;
1134 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001135 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001136 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001137 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001138 if (left <= relativeOffset && relativeOffset <= right) {
1139 return i;
1140 }
Winson Chunge3193b92010-09-10 11:44:42 -07001141 }
1142 return -1;
1143 }
1144
Winson Chung321e9ee2010-08-09 13:37:56 -07001145 protected int getRelativeChildOffset(int index) {
1146 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1147 }
1148
1149 protected int getChildOffset(int index) {
1150 if (getChildCount() == 0)
1151 return 0;
1152
1153 int offset = getRelativeChildOffset(0);
1154 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001155 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 }
1157 return offset;
1158 }
1159
Michael Jurkad3ef3062010-11-23 16:23:58 -08001160 protected int getScaledMeasuredWidth(View child) {
1161 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1162 }
1163
Adam Cohend19d3ca2010-09-15 14:43:42 -07001164 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 int minDistanceFromScreenCenter = getMeasuredWidth();
1166 int minDistanceFromScreenCenterIndex = -1;
1167 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1168 final int childCount = getChildCount();
1169 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001170 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001171 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 int halfChildWidth = (childWidth / 2);
1173 int childCenter = getChildOffset(i) + halfChildWidth;
1174 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1175 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1176 minDistanceFromScreenCenter = distanceFromScreenCenter;
1177 minDistanceFromScreenCenterIndex = i;
1178 }
1179 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001180 return minDistanceFromScreenCenterIndex;
1181 }
1182
1183 protected void snapToDestination() {
1184 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 }
1186
Adam Cohene0f66b52010-11-23 15:06:07 -08001187 private static class ScrollInterpolator implements Interpolator {
1188 public ScrollInterpolator() {
1189 }
1190
1191 public float getInterpolation(float t) {
1192 t -= 1.0f;
1193 return t*t*t*t*t + 1;
1194 }
1195 }
1196
1197 // We want the duration of the page snap animation to be influenced by the distance that
1198 // the screen has to travel, however, we don't want this duration to be effected in a
1199 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1200 // of travel has on the overall snap duration.
1201 float distanceInfluenceForSnapDuration(float f) {
1202 f -= 0.5f; // center the values about 0.
1203 f *= 0.3f * Math.PI / 2.0f;
1204 return (float) Math.sin(f);
1205 }
1206
Michael Jurka0142d492010-08-25 17:46:15 -07001207 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001208 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1209 int halfScreenSize = getMeasuredWidth() / 2;
1210
1211 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1212 int delta = newX - mUnboundedScrollX;
1213 int duration = 0;
1214
1215 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1216 // If the velocity is low enough, then treat this more as an automatic page advance
1217 // as opposed to an apparent physical response to flinging
1218 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1219 return;
1220 }
1221
1222 // Here we compute a "distance" that will be used in the computation of the overall
1223 // snap duration. This is a function of the actual distance that needs to be traveled;
1224 // we keep this value close to half screen size in order to reduce the variance in snap
1225 // duration as a function of the distance the page needs to travel.
1226 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1227 float distance = halfScreenSize + halfScreenSize *
1228 distanceInfluenceForSnapDuration(distanceRatio);
1229
1230 velocity = Math.abs(velocity);
1231 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1232
1233 // we want the page's snap velocity to approximately match the velocity at which the
1234 // user flings, so we scale the duration by a value near to the derivative of the scroll
1235 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1236 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1237
1238 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001239 }
1240
1241 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001242 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 }
1244
Michael Jurka0142d492010-08-25 17:46:15 -07001245 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001246 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001247
Winson Chung86f77532010-08-24 11:08:22 -07001248 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001249 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001251 snapToPage(whichPage, delta, duration);
1252 }
1253
1254 protected void snapToPage(int whichPage, int delta, int duration) {
1255 mNextPage = whichPage;
1256
1257 View focusedChild = getFocusedChild();
1258 if (focusedChild != null && whichPage != mCurrentPage &&
1259 focusedChild == getChildAt(mCurrentPage)) {
1260 focusedChild.clearFocus();
1261 }
1262
1263 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 awakenScrollBars(duration);
1265 if (duration == 0) {
1266 duration = Math.abs(delta);
1267 }
1268
1269 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001270 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001271
1272 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001273 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001274 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 invalidate();
1276 }
1277
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 public void scrollLeft() {
1279 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001280 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001282 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 }
1284 }
1285
1286 public void scrollRight() {
1287 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001288 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001289 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001290 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 }
1292 }
1293
Winson Chung86f77532010-08-24 11:08:22 -07001294 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 int result = -1;
1296 if (v != null) {
1297 ViewParent vp = v.getParent();
1298 int count = getChildCount();
1299 for (int i = 0; i < count; i++) {
1300 if (vp == getChildAt(i)) {
1301 return i;
1302 }
1303 }
1304 }
1305 return result;
1306 }
1307
1308 /**
1309 * @return True is long presses are still allowed for the current touch
1310 */
1311 public boolean allowLongPress() {
1312 return mAllowLongPress;
1313 }
1314
Michael Jurka0142d492010-08-25 17:46:15 -07001315 /**
1316 * Set true to allow long-press events to be triggered, usually checked by
1317 * {@link Launcher} to accept or block dpad-initiated long-presses.
1318 */
1319 public void setAllowLongPress(boolean allowLongPress) {
1320 mAllowLongPress = allowLongPress;
1321 }
1322
Winson Chung321e9ee2010-08-09 13:37:56 -07001323 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001324 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001325
1326 SavedState(Parcelable superState) {
1327 super(superState);
1328 }
1329
1330 private SavedState(Parcel in) {
1331 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001332 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001333 }
1334
1335 @Override
1336 public void writeToParcel(Parcel out, int flags) {
1337 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001338 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 }
1340
1341 public static final Parcelable.Creator<SavedState> CREATOR =
1342 new Parcelable.Creator<SavedState>() {
1343 public SavedState createFromParcel(Parcel in) {
1344 return new SavedState(in);
1345 }
1346
1347 public SavedState[] newArray(int size) {
1348 return new SavedState[size];
1349 }
1350 };
1351 }
1352
Winson Chung86f77532010-08-24 11:08:22 -07001353 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001354 if (mContentIsRefreshable) {
1355 final int count = getChildCount();
1356 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001357 int lowerPageBound = getAssociatedLowerPageBound(page);
1358 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001359 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001360 Page layout = (Page) getChildAt(i);
1361 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001362 if (lowerPageBound <= i && i <= upperPageBound) {
1363 if (mDirtyPageContent.get(i)) {
1364 syncPageItems(i);
1365 mDirtyPageContent.set(i, false);
1366 }
1367 } else {
1368 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001369 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001370 }
1371 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001372 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001373 }
1374 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001375 }
1376 }
1377
Winson Chunge3193b92010-09-10 11:44:42 -07001378 protected int getAssociatedLowerPageBound(int page) {
1379 return Math.max(0, page - 1);
1380 }
1381 protected int getAssociatedUpperPageBound(int page) {
1382 final int count = getChildCount();
1383 return Math.min(page + 1, count - 1);
1384 }
1385
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001386 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001387 if (isChoiceMode(CHOICE_MODE_NONE)) {
1388 mChoiceMode = mode;
1389 mActionMode = startActionMode(callback);
1390 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001391 }
1392
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001393 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001394 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001395 mChoiceMode = CHOICE_MODE_NONE;
1396 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001397 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001398 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001399 }
1400 }
1401
1402 protected boolean isChoiceMode(int mode) {
1403 return mChoiceMode == mode;
1404 }
1405
1406 protected ArrayList<Checkable> getCheckedGrandchildren() {
1407 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1408 final int childCount = getChildCount();
1409 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001410 Page layout = (Page) getChildAt(i);
1411 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001412 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001413 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001414 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001415 checked.add((Checkable) v);
1416 }
1417 }
1418 }
1419 return checked;
1420 }
1421
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001422 /**
1423 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1424 * Otherwise, returns null.
1425 */
1426 protected Checkable getSingleCheckedGrandchild() {
1427 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1428 final int childCount = getChildCount();
1429 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001430 Page layout = (Page) getChildAt(i);
1431 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001432 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001433 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001434 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1435 return (Checkable) v;
1436 }
1437 }
1438 }
1439 }
1440 return null;
1441 }
1442
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001443 public Object getChosenItem() {
1444 View checkedView = (View) getSingleCheckedGrandchild();
1445 if (checkedView != null) {
1446 return checkedView.getTag();
1447 }
1448 return null;
1449 }
1450
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001451 protected void resetCheckedGrandchildren() {
1452 // loop through children, and set all of their children to _not_ be checked
1453 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1454 for (int i = 0; i < checked.size(); ++i) {
1455 final Checkable c = checked.get(i);
1456 c.setChecked(false);
1457 }
1458 }
1459
Winson Chunga12a2502010-12-20 14:41:35 -08001460 public void setRestorePage(int restorePage) {
1461 mRestorePage = restorePage;
1462 }
1463
Winson Chung86f77532010-08-24 11:08:22 -07001464 /**
1465 * This method is called ONLY to synchronize the number of pages that the paged view has.
1466 * To actually fill the pages with information, implement syncPageItems() below. It is
1467 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1468 * and therefore, individual page items do not need to be updated in this method.
1469 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001471
1472 /**
1473 * This method is called to synchronize the items that are on a particular page. If views on
1474 * the page can be reused, then they should be updated within this method.
1475 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001477
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001479 if (mContentIsRefreshable) {
1480 // Update all the pages
1481 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001482
Michael Jurka0142d492010-08-25 17:46:15 -07001483 // Mark each of the pages as dirty
1484 final int count = getChildCount();
1485 mDirtyPageContent.clear();
1486 for (int i = 0; i < count; ++i) {
1487 mDirtyPageContent.add(true);
1488 }
1489
Winson Chunga12a2502010-12-20 14:41:35 -08001490 // Use the restore page if necessary
1491 if (mRestorePage > -1) {
1492 mCurrentPage = mRestorePage;
1493 mRestorePage = -1;
1494 }
1495
Michael Jurka0142d492010-08-25 17:46:15 -07001496 // Load any pages that are necessary for the current window of views
1497 loadAssociatedPages(mCurrentPage);
1498 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001499 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001500 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001501 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 }
1503}