blob: e0978b5de3b62821ab400db3443729cbac0bd6e8 [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 Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080041import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070042import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung321e9ee2010-08-09 13:37:56 -070047/**
48 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070049 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070050 */
51public abstract class PagedView extends ViewGroup {
52 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070053 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Winson Chung86f77532010-08-24 11:08:22 -070055 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070056 private static final int MIN_LENGTH_FOR_FLING = 25;
57 // The min drag distance to trigger a page shift (regardless of velocity)
58 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Adam Cohene0f66b52010-11-23 15:06:07 -080060 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Adam Cohene0f66b52010-11-23 15:06:07 -080063 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
64 private static final int MINIMUM_SNAP_VELOCITY = 2200;
65 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080066 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080067
Michael Jurka0142d492010-08-25 17:46:15 -070068 // the velocity at which a fling gesture will cause us to snap to the next page
69 protected int mSnapVelocity = 500;
70
71 protected float mSmoothingTime;
72 protected float mTouchX;
73
74 protected boolean mFirstLayout = true;
75
76 protected int mCurrentPage;
77 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080078 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080079 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070081 private VelocityTracker mVelocityTracker;
82
83 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080084 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080085 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080086 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080087 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070088 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070089
Michael Jurka0142d492010-08-25 17:46:15 -070090 protected final static int TOUCH_STATE_REST = 0;
91 protected final static int TOUCH_STATE_SCROLLING = 1;
92 protected final static int TOUCH_STATE_PREV_PAGE = 2;
93 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070094 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka7426c422010-11-11 15:23:47 -0800102 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103 private int mPagingTouchSlop;
104 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800105 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700106 protected int mPageSpacing;
107 protected int mPageLayoutPaddingTop;
108 protected int mPageLayoutPaddingBottom;
109 protected int mPageLayoutPaddingLeft;
110 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700111 protected int mPageLayoutWidthGap;
112 protected int mPageLayoutHeightGap;
Michael Jurka36fcb742011-04-06 17:08:58 -0700113 protected int mPageLayoutMaxHeight;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700114 protected int mCellCountX;
115 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700116 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800117 protected boolean mAllowOverScroll = true;
118 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka8c920dd2011-01-20 14:16:56 -0800120 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800121 protected float mLayoutScale = 1.0f;
122
Michael Jurka5f1c5092010-09-03 14:15:02 -0700123 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka5f1c5092010-09-03 14:15:02 -0700125 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Winson Chung86f77532010-08-24 11:08:22 -0700127 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Winson Chung86f77532010-08-24 11:08:22 -0700129 private ArrayList<Boolean> mDirtyPageContent;
130 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700132 // choice modes
133 protected static final int CHOICE_MODE_NONE = 0;
134 protected static final int CHOICE_MODE_SINGLE = 1;
135 // Multiple selection mode is not supported by all Launcher actions atm
136 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700137
Michael Jurkae17e19c2010-09-28 11:01:39 -0700138 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700139 private ActionMode mActionMode;
140
Winson Chung04998342011-01-05 13:54:43 -0800141 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
142 // AllApps and Customize, and allows them to share holographic icons for the application view
143 // (which is in both).
144 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 // If true, syncPages and syncPageItems will be called to refresh pages
147 protected boolean mContentIsRefreshable = true;
148
149 // If true, modify alpha of neighboring pages as user scrolls left/right
150 protected boolean mFadeInAdjacentScreens = true;
151
152 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
153 // to switch to a new page
154 protected boolean mUsePagingTouchSlop = true;
155
156 // If true, the subclass should directly update mScrollX itself in its computeScroll method
157 // (SmoothPagedView does this)
158 protected boolean mDeferScrollUpdate = false;
159
Patrick Dubroy1262e362010-10-06 15:49:50 -0700160 protected boolean mIsPageMoving = false;
161
Winson Chung86f77532010-08-24 11:08:22 -0700162 public interface PageSwitchListener {
163 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 }
165
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 public PagedView(Context context) {
167 this(context, null);
168 }
169
170 public PagedView(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public PagedView(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700176 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700177
Adam Cohen9c4949e2010-10-05 12:27:22 -0700178 TypedArray a = context.obtainStyledAttributes(attrs,
179 R.styleable.PagedView, defStyle, 0);
180 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
181 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800182 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700183 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800184 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700185 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800186 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800188 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700189 mPageLayoutWidthGap = a.getDimensionPixelSize(
190 R.styleable.PagedView_pageLayoutWidthGap, -1);
191 mPageLayoutHeightGap = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutHeightGap, -1);
Michael Jurka36fcb742011-04-06 17:08:58 -0700193 mPageLayoutMaxHeight = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutMaxHeight, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700195 a.recycle();
196
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700198 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 }
200
201 /**
202 * Initializes various states for this workspace.
203 */
Michael Jurka0142d492010-08-25 17:46:15 -0700204 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700205 mDirtyPageContent = new ArrayList<Boolean>();
206 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800207 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700208 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700209 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700210
211 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
212 mTouchSlop = configuration.getScaledTouchSlop();
213 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
214 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
215 }
216
Winson Chung86f77532010-08-24 11:08:22 -0700217 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
218 mPageSwitchListener = pageSwitchListener;
219 if (mPageSwitchListener != null) {
220 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222 }
223
224 /**
Winson Chung86f77532010-08-24 11:08:22 -0700225 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 *
Winson Chung86f77532010-08-24 11:08:22 -0700227 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 */
Winson Chung86f77532010-08-24 11:08:22 -0700229 int getCurrentPage() {
230 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 }
232
Winson Chung86f77532010-08-24 11:08:22 -0700233 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 return getChildCount();
235 }
236
Winson Chung86f77532010-08-24 11:08:22 -0700237 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 return getChildAt(index);
239 }
240
241 int getScrollWidth() {
242 return getWidth();
243 }
244
Adam Cohenbe909342011-01-28 17:02:58 -0800245 public int getTouchState() {
246 return mTouchState;
247 }
248
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800250 * Updates the scroll of the current page immediately to its final scroll position. We use this
251 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
252 * the previous tab page.
253 */
254 protected void updateCurrentPageScroll() {
255 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
256 scrollTo(newX, 0);
257 mScroller.setFinalX(newX);
258 }
259
260 /**
Winson Chung86f77532010-08-24 11:08:22 -0700261 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 */
Winson Chung86f77532010-08-24 11:08:22 -0700263 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800264 if (!mScroller.isFinished()) {
265 mScroller.abortAnimation();
266 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800267 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
268 // the default
269 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800270 return;
271 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700272
Winson Chung86f77532010-08-24 11:08:22 -0700273 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800274 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700275 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800276 invalidate();
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
Michael Jurkace7e05f2011-02-01 22:02:35 -0800285 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700286 mIsPageMoving = true;
287 onPageBeginMoving();
288 }
289
Michael Jurkace7e05f2011-02-01 22:02:35 -0800290 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700291 onPageEndMoving();
292 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700293 }
294
Adam Cohen26976d92011-03-22 15:33:33 -0700295 protected boolean isPageMoving() {
296 return mIsPageMoving;
297 }
298
Michael Jurka0142d492010-08-25 17:46:15 -0700299 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700300 protected void onPageBeginMoving() {
301 }
302
303 // a method that subclasses can override to add behavior
304 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700305 }
306
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 /**
Winson Chung86f77532010-08-24 11:08:22 -0700308 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 *
310 * @param l The listener used to respond to long clicks.
311 */
312 @Override
313 public void setOnLongClickListener(OnLongClickListener l) {
314 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700315 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700316 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700317 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 }
319 }
320
321 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800322 public void scrollBy(int x, int y) {
323 scrollTo(mUnboundedScrollX + x, mScrollY + y);
324 }
325
326 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700327 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800328 mUnboundedScrollX = x;
329
330 if (x < 0) {
331 super.scrollTo(0, y);
332 if (mAllowOverScroll) {
333 overScroll(x);
334 }
335 } else if (x > mMaxScrollX) {
336 super.scrollTo(mMaxScrollX, y);
337 if (mAllowOverScroll) {
338 overScroll(x - mMaxScrollX);
339 }
340 } else {
341 super.scrollTo(x, y);
342 }
343
Michael Jurka0142d492010-08-25 17:46:15 -0700344 mTouchX = x;
345 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
346 }
347
348 // we moved this functionality to a helper function so SmoothPagedView can reuse it
349 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700351 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700353 invalidate();
354 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700355 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700356 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700357 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700358 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700359 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800360 // We don't want to trigger a page end moving unless the page has settled
361 // and the user has stopped scrolling
362 if (mTouchState == TOUCH_STATE_REST) {
363 pageEndMoving();
364 }
Michael Jurka0142d492010-08-25 17:46:15 -0700365 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 }
Michael Jurka0142d492010-08-25 17:46:15 -0700367 return false;
368 }
369
370 @Override
371 public void computeScroll() {
372 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700373 }
374
375 @Override
376 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
377 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
378 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
379 if (widthMode != MeasureSpec.EXACTLY) {
380 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
381 }
382
Adam Lesinski6b879f02010-11-04 16:15:23 -0700383 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
384 * of the All apps view on XLarge displays to not take up more space then it needs. Width
385 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
386 * each page to have the same width.
387 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700388 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700389 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
390 int maxChildHeight = 0;
391
392 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700393
Michael Jurka36fcb742011-04-06 17:08:58 -0700394 if (mPageLayoutMaxHeight != -1) {
395 heightSize = Math.min(mPageLayoutMaxHeight, heightSize);
396 }
397
Winson Chung321e9ee2010-08-09 13:37:56 -0700398 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700399 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 final int childCount = getChildCount();
401 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700402 // disallowing padding in paged view (just pass 0)
403 final View child = getChildAt(i);
404 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
405
406 int childWidthMode;
407 if (lp.width == LayoutParams.WRAP_CONTENT) {
408 childWidthMode = MeasureSpec.AT_MOST;
409 } else {
410 childWidthMode = MeasureSpec.EXACTLY;
411 }
412
413 int childHeightMode;
414 if (lp.height == LayoutParams.WRAP_CONTENT) {
415 childHeightMode = MeasureSpec.AT_MOST;
416 } else {
417 childHeightMode = MeasureSpec.EXACTLY;
418 }
419
420 final int childWidthMeasureSpec =
421 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
422 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700423 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700424
425 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700426 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
427 }
428
429 if (heightMode == MeasureSpec.AT_MOST) {
430 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800432 if (childCount > 0) {
433 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
434 } else {
435 mMaxScrollX = 0;
436 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700437
438 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 }
440
Michael Jurka8c920dd2011-01-20 14:16:56 -0800441 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800442 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
443 int delta = newX - mScrollX;
444
Michael Jurka8c920dd2011-01-20 14:16:56 -0800445 final int pageCount = getChildCount();
446 for (int i = 0; i < pageCount; i++) {
447 View page = (View) getChildAt(i);
448 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800449 }
450 setCurrentPage(newCurrentPage);
451 }
452
Michael Jurka8c920dd2011-01-20 14:16:56 -0800453 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
454 // 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 -0800455 // tightens the layout accordingly
456 public void setLayoutScale(float childrenScale) {
457 mLayoutScale = childrenScale;
458
459 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
460 int childCount = getChildCount();
461 float childrenX[] = new float[childCount];
462 float childrenY[] = new float[childCount];
463 for (int i = 0; i < childCount; i++) {
464 final View child = getChildAt(i);
465 childrenX[i] = child.getX();
466 childrenY[i] = child.getY();
467 }
468 onLayout(false, mLeft, mTop, mRight, mBottom);
469 for (int i = 0; i < childCount; i++) {
470 final View child = getChildAt(i);
471 child.setX(childrenX[i]);
472 child.setY(childrenY[i]);
473 }
474 // Also, the page offset has changed (since the pages are now smaller);
475 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800476 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800477 }
478
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700480 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700481 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
482 setHorizontalScrollBarEnabled(false);
483 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
484 scrollTo(newX, 0);
485 mScroller.setFinalX(newX);
486 setHorizontalScrollBarEnabled(true);
487 mFirstLayout = false;
488 }
489
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 final int childCount = getChildCount();
492 int childLeft = 0;
493 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700494 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 }
496
497 for (int i = 0; i < childCount; i++) {
498 final View child = getChildAt(i);
499 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800500 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700501 final int childHeight = child.getMeasuredHeight();
502 int childTop = mPaddingTop;
503 if (mCenterPagesVertically) {
504 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
505 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800506
Adam Lesinski6b879f02010-11-04 16:15:23 -0700507 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700509 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 }
511 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800512 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
513 mFirstLayout = false;
514 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
516
Winson Chung03929772011-02-23 17:07:10 -0800517 protected void forceUpdateAdjacentPagesAlpha() {
518 mDirtyPageAlpha = true;
519 updateAdjacentPagesAlpha();
520 }
521
Winson Chunge3193b92010-09-10 11:44:42 -0700522 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700523 if (mFadeInAdjacentScreens) {
524 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700525 int halfScreenSize = getMeasuredWidth() / 2;
526 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700527 final int childCount = getChildCount();
528 for (int i = 0; i < childCount; ++i) {
529 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800530 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700531 int halfChildWidth = (childWidth / 2);
532 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700533
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700534 // On the first layout, we may not have a width nor a proper offset, so for now
535 // we should just assume full page width (and calculate the offset according to
536 // that).
537 if (childWidth <= 0) {
538 childWidth = getMeasuredWidth();
539 childCenter = (i * childWidth) + (childWidth / 2);
540 }
541
Winson Chunge8878e32010-09-15 20:37:09 -0700542 int d = halfChildWidth;
543 int distanceFromScreenCenter = childCenter - screenCenter;
544 if (distanceFromScreenCenter > 0) {
545 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800546 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700547 }
Michael Jurka0142d492010-08-25 17:46:15 -0700548 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700549 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800550 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700551 }
Michael Jurka0142d492010-08-25 17:46:15 -0700552 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700553 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700554
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700555 // Preventing potential divide-by-zero
556 d = Math.max(1, d);
557
Winson Chunge8878e32010-09-15 20:37:09 -0700558 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
559 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
560 float alpha = 1.0f - dimAlpha;
561
Adam Cohenf34bab52010-09-30 14:11:56 -0700562 if (alpha < ALPHA_QUANTIZE_LEVEL) {
563 alpha = 0.0f;
564 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
565 alpha = 1.0f;
566 }
567
Michael Jurkaa40829a2011-02-16 18:02:45 -0800568 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
569 // this optimization causes alpha to not be properly updated sometimes (repro
570 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
571 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
572 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800573 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700574 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800575 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700576 }
Michael Jurka0142d492010-08-25 17:46:15 -0700577 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 }
579 }
Winson Chunge3193b92010-09-10 11:44:42 -0700580 }
581
Adam Cohenf34bab52010-09-30 14:11:56 -0700582 protected void screenScrolled(int screenCenter) {
583 }
584
Winson Chunge3193b92010-09-10 11:44:42 -0700585 @Override
586 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700587 int halfScreenSize = getMeasuredWidth() / 2;
588 int screenCenter = mScrollX + halfScreenSize;
589
590 if (screenCenter != mLastScreenCenter) {
591 screenScrolled(screenCenter);
592 updateAdjacentPagesAlpha();
593 mLastScreenCenter = screenCenter;
594 }
Michael Jurka0142d492010-08-25 17:46:15 -0700595
596 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700597 // As an optimization, this code assumes that all pages have the same width as the 0th
598 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700599 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700600 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800601 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700602 final int screenWidth = getMeasuredWidth();
603 int x = getRelativeChildOffset(0) + pageWidth;
604 int leftScreen = 0;
605 int rightScreen = 0;
606 while (x <= mScrollX) {
607 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800608 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700609 }
610 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800611 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700612 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800613 if (rightScreen < pageCount) {
614 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
615 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700616 }
617 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700618
Michael Jurkac4fb9172010-09-02 17:19:20 -0700619 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800620 // Clip to the bounds
621 canvas.save();
622 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
623 mScrollY + mBottom - mTop);
624
Michael Jurkac4fb9172010-09-02 17:19:20 -0700625 for (int i = leftScreen; i <= rightScreen; i++) {
626 drawChild(canvas, getChildAt(i), drawingTime);
627 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800628 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700629 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700630 }
631
632 @Override
633 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 int page = indexOfChild(child);
635 if (page != mCurrentPage || !mScroller.isFinished()) {
636 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 return true;
638 }
639 return false;
640 }
641
642 @Override
643 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700644 int focusablePage;
645 if (mNextPage != INVALID_PAGE) {
646 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700648 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
Winson Chung86f77532010-08-24 11:08:22 -0700650 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 if (v != null) {
652 v.requestFocus(direction, previouslyFocusedRect);
653 }
654 return false;
655 }
656
657 @Override
658 public boolean dispatchUnhandledMove(View focused, int direction) {
659 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700660 if (getCurrentPage() > 0) {
661 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 return true;
663 }
664 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700665 if (getCurrentPage() < getPageCount() - 1) {
666 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 return true;
668 }
669 }
670 return super.dispatchUnhandledMove(focused, direction);
671 }
672
673 @Override
674 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700675 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
676 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 }
678 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700679 if (mCurrentPage > 0) {
680 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 }
682 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700683 if (mCurrentPage < getPageCount() - 1) {
684 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 }
686 }
687 }
688
689 /**
690 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700691 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700692 *
Winson Chung86f77532010-08-24 11:08:22 -0700693 * This happens when live folders requery, and if they're off page, they
694 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 */
696 @Override
697 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700698 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 View v = focused;
700 while (true) {
701 if (v == current) {
702 super.focusableViewAvailable(focused);
703 return;
704 }
705 if (v == this) {
706 return;
707 }
708 ViewParent parent = v.getParent();
709 if (parent instanceof View) {
710 v = (View)v.getParent();
711 } else {
712 return;
713 }
714 }
715 }
716
717 /**
718 * {@inheritDoc}
719 */
720 @Override
721 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
722 if (disallowIntercept) {
723 // We need to make sure to cancel our long press if
724 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700725 final View currentPage = getChildAt(mCurrentPage);
726 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 }
728 super.requestDisallowInterceptTouchEvent(disallowIntercept);
729 }
730
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800731 /**
732 * Return true if a tap at (x, y) should trigger a flip to the previous page.
733 */
734 protected boolean hitsPreviousPage(float x, float y) {
735 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
736 }
737
738 /**
739 * Return true if a tap at (x, y) should trigger a flip to the next page.
740 */
741 protected boolean hitsNextPage(float x, float y) {
742 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
743 }
744
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 @Override
746 public boolean onInterceptTouchEvent(MotionEvent ev) {
747 /*
748 * This method JUST determines whether we want to intercept the motion.
749 * If we return true, onTouchEvent will be called and we do the actual
750 * scrolling there.
751 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800752 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700753
Winson Chung45e1d6e2010-11-09 17:19:49 -0800754 // Skip touch handling if there are no pages to swipe
755 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
756
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 /*
758 * Shortcut the most recurring case: the user is in the dragging
759 * state and he is moving his finger. We want to intercept this
760 * motion.
761 */
762 final int action = ev.getAction();
763 if ((action == MotionEvent.ACTION_MOVE) &&
764 (mTouchState == TOUCH_STATE_SCROLLING)) {
765 return true;
766 }
767
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 switch (action & MotionEvent.ACTION_MASK) {
769 case MotionEvent.ACTION_MOVE: {
770 /*
771 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
772 * whether the user has moved far enough from his original down touch.
773 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700774 if (mActivePointerId != INVALID_POINTER) {
775 determineScrollingStart(ev);
776 break;
777 }
778 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
779 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
780 // i.e. fall through to the next case (don't break)
781 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
782 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 }
784
785 case MotionEvent.ACTION_DOWN: {
786 final float x = ev.getX();
787 final float y = ev.getY();
788 // Remember location of down touch
789 mDownMotionX = x;
790 mLastMotionX = x;
791 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800792 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800793 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 mActivePointerId = ev.getPointerId(0);
795 mAllowLongPress = true;
796
797 /*
798 * If being flinged and user touches the screen, initiate drag;
799 * otherwise don't. mScroller.isFinished should be false when
800 * being flinged.
801 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700802 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700803 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
804 if (finishedScrolling) {
805 mTouchState = TOUCH_STATE_REST;
806 mScroller.abortAnimation();
807 } else {
808 mTouchState = TOUCH_STATE_SCROLLING;
809 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700810
Winson Chung86f77532010-08-24 11:08:22 -0700811 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800813 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800815 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800817 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 mTouchState = TOUCH_STATE_NEXT_PAGE;
819 }
820 }
821 }
822 break;
823 }
824
Winson Chung321e9ee2010-08-09 13:37:56 -0700825 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800826 onWallpaperTap(ev);
827 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 mTouchState = TOUCH_STATE_REST;
829 mAllowLongPress = false;
830 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800831 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 break;
833
834 case MotionEvent.ACTION_POINTER_UP:
835 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800836 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 break;
838 }
839
840 /*
841 * The only time we want to intercept motion events is if we are in the
842 * drag mode.
843 */
844 return mTouchState != TOUCH_STATE_REST;
845 }
846
Winson Chung80baf5a2010-08-09 16:03:15 -0700847 protected void animateClickFeedback(View v, final Runnable r) {
848 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800849 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
850 loadAnimator(mContext, R.anim.paged_view_click_feedback);
851 anim.setTarget(v);
852 anim.addListener(new AnimatorListenerAdapter() {
853 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700854 r.run();
855 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700856 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800857 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700858 }
859
Adam Cohenf8d28232011-02-01 21:47:00 -0800860 protected void determineScrollingStart(MotionEvent ev) {
861 determineScrollingStart(ev, 1.0f);
862 }
863
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 /*
865 * Determines if we should change the touch state to start scrolling after the
866 * user moves their touch point too far.
867 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800868 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 /*
870 * Locally do absolute value. mLastMotionX is set to the y value
871 * of the down event.
872 */
873 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
874 final float x = ev.getX(pointerIndex);
875 final float y = ev.getY(pointerIndex);
876 final int xDiff = (int) Math.abs(x - mLastMotionX);
877 final int yDiff = (int) Math.abs(y - mLastMotionY);
878
Adam Cohenf8d28232011-02-01 21:47:00 -0800879 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 boolean xPaged = xDiff > mPagingTouchSlop;
881 boolean xMoved = xDiff > touchSlop;
882 boolean yMoved = yDiff > touchSlop;
883
Adam Cohenf8d28232011-02-01 21:47:00 -0800884 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700885 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 // Scroll if the user moved far enough along the X axis
887 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800888 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800890 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700891 mTouchX = mScrollX;
892 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
893 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700894 }
895 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800896 cancelCurrentPageLongPress();
897 }
898 }
899
900 protected void cancelCurrentPageLongPress() {
901 if (mAllowLongPress) {
902 mAllowLongPress = false;
903 // Try canceling the long press. It could also have been scheduled
904 // by a distant descendant, so use the mAllowLongPress flag to block
905 // everything
906 final View currentPage = getPageAt(mCurrentPage);
907 if (currentPage != null) {
908 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 }
910 }
911 }
912
Adam Cohene0f66b52010-11-23 15:06:07 -0800913 // This curve determines how the effect of scrolling over the limits of the page dimishes
914 // as the user pulls further and further from the bounds
915 private float overScrollInfluenceCurve(float f) {
916 f -= 1.0f;
917 return f * f * f + 1.0f;
918 }
919
Adam Cohen68d73932010-11-15 10:50:58 -0800920 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800921 int screenSize = getMeasuredWidth();
922
923 float f = (amount / screenSize);
924
925 if (f == 0) return;
926 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
927
Adam Cohen7bfc9792011-01-28 13:52:37 -0800928 // Clamp this factor, f, to -1 < f < 1
929 if (Math.abs(f) >= 1) {
930 f /= Math.abs(f);
931 }
932
Adam Cohene0f66b52010-11-23 15:06:07 -0800933 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800934 if (amount < 0) {
935 mScrollX = overScrollAmount;
936 } else {
937 mScrollX = mMaxScrollX + overScrollAmount;
938 }
939 invalidate();
940 }
941
Michael Jurkac5b262c2011-01-12 20:24:50 -0800942 protected float maxOverScroll() {
943 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
944 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
945 float f = 1.0f;
946 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
947 return OVERSCROLL_DAMP_FACTOR * f;
948 }
949
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 @Override
951 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800952 // Skip touch handling if there are no pages to swipe
953 if (getChildCount() <= 0) return super.onTouchEvent(ev);
954
Michael Jurkab8f06722010-10-10 15:58:46 -0700955 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700956
957 final int action = ev.getAction();
958
959 switch (action & MotionEvent.ACTION_MASK) {
960 case MotionEvent.ACTION_DOWN:
961 /*
962 * If being flinged and user touches, stop the fling. isFinished
963 * will be false if being flinged.
964 */
965 if (!mScroller.isFinished()) {
966 mScroller.abortAnimation();
967 }
968
969 // Remember where the motion event started
970 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800971 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800972 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700974 if (mTouchState == TOUCH_STATE_SCROLLING) {
975 pageBeginMoving();
976 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 break;
978
979 case MotionEvent.ACTION_MOVE:
980 if (mTouchState == TOUCH_STATE_SCROLLING) {
981 // Scroll to follow the motion event
982 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
983 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800984 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700985
Adam Cohenaefd4e12011-02-14 16:39:38 -0800986 mTotalMotionX += Math.abs(deltaX);
987
Winson Chungc0844aa2011-02-02 15:25:58 -0800988 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
989 // keep the remainder because we are actually testing if we've moved from the last
990 // scrolled position (which is discrete).
991 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800992 mTouchX += deltaX;
993 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
994 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800995 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800996 } else {
997 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800999 mLastMotionX = x;
1000 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 } else {
1002 awakenScrollBars();
1003 }
Adam Cohen564976a2010-10-13 18:52:07 -07001004 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 determineScrollingStart(ev);
1006 }
1007 break;
1008
1009 case MotionEvent.ACTION_UP:
1010 if (mTouchState == TOUCH_STATE_SCROLLING) {
1011 final int activePointerId = mActivePointerId;
1012 final int pointerIndex = ev.findPointerIndex(activePointerId);
1013 final float x = ev.getX(pointerIndex);
1014 final VelocityTracker velocityTracker = mVelocityTracker;
1015 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1016 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001017 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001018 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001019 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001020
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001021 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1022
Adam Cohenaefd4e12011-02-14 16:39:38 -08001023 // In the case that the page is moved far to one direction and then is flung
1024 // in the opposite direction, we use a threshold to determine whether we should
1025 // just return to the starting page, or if we should skip one further.
1026 boolean returnToOriginalPage = false;
1027 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001028 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001029 Math.signum(velocityX) != Math.signum(deltaX)) {
1030 returnToOriginalPage = true;
1031 }
1032
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001033 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001034 Math.abs(velocityX) > snapVelocity;
1035
1036 int finalPage;
1037 // We give flings precedence over large moves, which is why we short-circuit our
1038 // test for a large move if a fling has been registered. That is, a large
1039 // move to the left and fling to the right will register as a fling to the right.
1040 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1041 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1042 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1043 snapToPageWithVelocity(finalPage, velocityX);
1044 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1045 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001046 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001047 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1048 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 } else {
1050 snapToDestination();
1051 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001052 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 // at this point we have not moved beyond the touch slop
1054 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1055 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001056 int nextPage = Math.max(0, mCurrentPage - 1);
1057 if (nextPage != mCurrentPage) {
1058 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 } else {
1060 snapToDestination();
1061 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001062 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 // at this point we have not moved beyond the touch slop
1064 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1065 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001066 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1067 if (nextPage != mCurrentPage) {
1068 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 } else {
1070 snapToDestination();
1071 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001072 } else {
1073 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 }
1075 mTouchState = TOUCH_STATE_REST;
1076 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001077 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 break;
1079
1080 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001081 if (mTouchState == TOUCH_STATE_SCROLLING) {
1082 snapToDestination();
1083 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 mTouchState = TOUCH_STATE_REST;
1085 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001086 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001087 break;
1088
1089 case MotionEvent.ACTION_POINTER_UP:
1090 onSecondaryPointerUp(ev);
1091 break;
1092 }
1093
1094 return true;
1095 }
1096
Winson Chung185d7162011-02-28 13:47:29 -08001097 @Override
1098 public boolean onGenericMotionEvent(MotionEvent event) {
1099 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1100 switch (event.getAction()) {
1101 case MotionEvent.ACTION_SCROLL: {
1102 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1103 final float vscroll;
1104 final float hscroll;
1105 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1106 vscroll = 0;
1107 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1108 } else {
1109 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1110 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1111 }
1112 if (hscroll != 0 || vscroll != 0) {
1113 if (hscroll > 0 || vscroll > 0) {
1114 scrollRight();
1115 } else {
1116 scrollLeft();
1117 }
1118 return true;
1119 }
1120 }
1121 }
1122 }
1123 return super.onGenericMotionEvent(event);
1124 }
1125
Michael Jurkab8f06722010-10-10 15:58:46 -07001126 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1127 if (mVelocityTracker == null) {
1128 mVelocityTracker = VelocityTracker.obtain();
1129 }
1130 mVelocityTracker.addMovement(ev);
1131 }
1132
1133 private void releaseVelocityTracker() {
1134 if (mVelocityTracker != null) {
1135 mVelocityTracker.recycle();
1136 mVelocityTracker = null;
1137 }
1138 }
1139
Winson Chung321e9ee2010-08-09 13:37:56 -07001140 private void onSecondaryPointerUp(MotionEvent ev) {
1141 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1142 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1143 final int pointerId = ev.getPointerId(pointerIndex);
1144 if (pointerId == mActivePointerId) {
1145 // This was our active pointer going up. Choose a new
1146 // active pointer and adjust accordingly.
1147 // TODO: Make this decision more intelligent.
1148 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1149 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1150 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001151 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 mActivePointerId = ev.getPointerId(newPointerIndex);
1153 if (mVelocityTracker != null) {
1154 mVelocityTracker.clear();
1155 }
1156 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001157 if (mTouchState == TOUCH_STATE_REST) {
1158 onWallpaperTap(ev);
1159 }
1160 }
1161
1162 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 }
1164
1165 @Override
1166 public void requestChildFocus(View child, View focused) {
1167 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001168 int page = indexOfChild(child);
1169 if (page >= 0 && !isInTouchMode()) {
1170 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 }
1172 }
1173
Winson Chunge3193b92010-09-10 11:44:42 -07001174 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1175 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001176 int left;
1177 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001178 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001179 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001180 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001181 if (left <= relativeOffset && relativeOffset <= right) {
1182 return i;
1183 }
Winson Chunge3193b92010-09-10 11:44:42 -07001184 }
1185 return -1;
1186 }
1187
Winson Chung1908d072011-02-24 18:09:44 -08001188 protected void setMinimumWidthOverride(int minimumWidth) {
1189 mMinimumWidth = minimumWidth;
1190 }
Winson Chung34b23d52011-03-18 11:29:34 -07001191 protected void resetMinimumWidthOverride() {
1192 mMinimumWidth = 0;
1193 }
Winson Chung1908d072011-02-24 18:09:44 -08001194
1195 protected int getChildWidth(int index) {
1196 return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth());
1197 }
1198
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001200 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 }
1202
1203 protected int getChildOffset(int index) {
1204 if (getChildCount() == 0)
1205 return 0;
1206
1207 int offset = getRelativeChildOffset(0);
1208 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001209 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 }
1211 return offset;
1212 }
1213
Michael Jurkad3ef3062010-11-23 16:23:58 -08001214 protected int getScaledMeasuredWidth(View child) {
Winson Chung1908d072011-02-24 18:09:44 -08001215 return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001216 }
1217
Adam Cohend19d3ca2010-09-15 14:43:42 -07001218 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 int minDistanceFromScreenCenter = getMeasuredWidth();
1220 int minDistanceFromScreenCenterIndex = -1;
1221 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1222 final int childCount = getChildCount();
1223 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001224 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001225 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 int halfChildWidth = (childWidth / 2);
1227 int childCenter = getChildOffset(i) + halfChildWidth;
1228 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1229 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1230 minDistanceFromScreenCenter = distanceFromScreenCenter;
1231 minDistanceFromScreenCenterIndex = i;
1232 }
1233 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001234 return minDistanceFromScreenCenterIndex;
1235 }
1236
1237 protected void snapToDestination() {
1238 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 }
1240
Adam Cohene0f66b52010-11-23 15:06:07 -08001241 private static class ScrollInterpolator implements Interpolator {
1242 public ScrollInterpolator() {
1243 }
1244
1245 public float getInterpolation(float t) {
1246 t -= 1.0f;
1247 return t*t*t*t*t + 1;
1248 }
1249 }
1250
1251 // We want the duration of the page snap animation to be influenced by the distance that
1252 // the screen has to travel, however, we don't want this duration to be effected in a
1253 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1254 // of travel has on the overall snap duration.
1255 float distanceInfluenceForSnapDuration(float f) {
1256 f -= 0.5f; // center the values about 0.
1257 f *= 0.3f * Math.PI / 2.0f;
1258 return (float) Math.sin(f);
1259 }
1260
Michael Jurka0142d492010-08-25 17:46:15 -07001261 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001262 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1263 int halfScreenSize = getMeasuredWidth() / 2;
1264
1265 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1266 int delta = newX - mUnboundedScrollX;
1267 int duration = 0;
1268
1269 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1270 // If the velocity is low enough, then treat this more as an automatic page advance
1271 // as opposed to an apparent physical response to flinging
1272 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1273 return;
1274 }
1275
1276 // Here we compute a "distance" that will be used in the computation of the overall
1277 // snap duration. This is a function of the actual distance that needs to be traveled;
1278 // we keep this value close to half screen size in order to reduce the variance in snap
1279 // duration as a function of the distance the page needs to travel.
1280 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1281 float distance = halfScreenSize + halfScreenSize *
1282 distanceInfluenceForSnapDuration(distanceRatio);
1283
1284 velocity = Math.abs(velocity);
1285 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1286
1287 // we want the page's snap velocity to approximately match the velocity at which the
1288 // user flings, so we scale the duration by a value near to the derivative of the scroll
1289 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1290 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1291
1292 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001293 }
1294
1295 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001296 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 }
1298
Michael Jurka0142d492010-08-25 17:46:15 -07001299 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001300 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001301
Winson Chung86f77532010-08-24 11:08:22 -07001302 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001303 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001305 snapToPage(whichPage, delta, duration);
1306 }
1307
1308 protected void snapToPage(int whichPage, int delta, int duration) {
1309 mNextPage = whichPage;
1310
1311 View focusedChild = getFocusedChild();
1312 if (focusedChild != null && whichPage != mCurrentPage &&
1313 focusedChild == getChildAt(mCurrentPage)) {
1314 focusedChild.clearFocus();
1315 }
1316
1317 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 awakenScrollBars(duration);
1319 if (duration == 0) {
1320 duration = Math.abs(delta);
1321 }
1322
1323 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001324 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001325
1326 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001327 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001328 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 invalidate();
1330 }
1331
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 public void scrollLeft() {
1333 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001334 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001335 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001336 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 }
1338 }
1339
1340 public void scrollRight() {
1341 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001342 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001344 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 }
1346 }
1347
Winson Chung86f77532010-08-24 11:08:22 -07001348 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 int result = -1;
1350 if (v != null) {
1351 ViewParent vp = v.getParent();
1352 int count = getChildCount();
1353 for (int i = 0; i < count; i++) {
1354 if (vp == getChildAt(i)) {
1355 return i;
1356 }
1357 }
1358 }
1359 return result;
1360 }
1361
1362 /**
1363 * @return True is long presses are still allowed for the current touch
1364 */
1365 public boolean allowLongPress() {
1366 return mAllowLongPress;
1367 }
1368
Michael Jurka0142d492010-08-25 17:46:15 -07001369 /**
1370 * Set true to allow long-press events to be triggered, usually checked by
1371 * {@link Launcher} to accept or block dpad-initiated long-presses.
1372 */
1373 public void setAllowLongPress(boolean allowLongPress) {
1374 mAllowLongPress = allowLongPress;
1375 }
1376
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001378 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001379
1380 SavedState(Parcelable superState) {
1381 super(superState);
1382 }
1383
1384 private SavedState(Parcel in) {
1385 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001386 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 }
1388
1389 @Override
1390 public void writeToParcel(Parcel out, int flags) {
1391 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001392 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 }
1394
1395 public static final Parcelable.Creator<SavedState> CREATOR =
1396 new Parcelable.Creator<SavedState>() {
1397 public SavedState createFromParcel(Parcel in) {
1398 return new SavedState(in);
1399 }
1400
1401 public SavedState[] newArray(int size) {
1402 return new SavedState[size];
1403 }
1404 };
1405 }
1406
Winson Chung86f77532010-08-24 11:08:22 -07001407 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001408 if (mContentIsRefreshable) {
1409 final int count = getChildCount();
1410 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001411 int lowerPageBound = getAssociatedLowerPageBound(page);
1412 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001413 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001414 Page layout = (Page) getChildAt(i);
1415 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001416 if (lowerPageBound <= i && i <= upperPageBound) {
1417 if (mDirtyPageContent.get(i)) {
1418 syncPageItems(i);
1419 mDirtyPageContent.set(i, false);
1420 }
1421 } else {
1422 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001423 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001424 }
1425 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001426 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001427 }
1428 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001429 }
1430 }
1431
Winson Chunge3193b92010-09-10 11:44:42 -07001432 protected int getAssociatedLowerPageBound(int page) {
1433 return Math.max(0, page - 1);
1434 }
1435 protected int getAssociatedUpperPageBound(int page) {
1436 final int count = getChildCount();
1437 return Math.min(page + 1, count - 1);
1438 }
1439
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001440 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001441 if (isChoiceMode(CHOICE_MODE_NONE)) {
1442 mChoiceMode = mode;
1443 mActionMode = startActionMode(callback);
1444 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001445 }
1446
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001447 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001448 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001449 mChoiceMode = CHOICE_MODE_NONE;
1450 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001451 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001452 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001453 }
1454 }
1455
1456 protected boolean isChoiceMode(int mode) {
1457 return mChoiceMode == mode;
1458 }
1459
1460 protected ArrayList<Checkable> getCheckedGrandchildren() {
1461 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1462 final int childCount = getChildCount();
1463 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001464 Page layout = (Page) getChildAt(i);
1465 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001466 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001467 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001468 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001469 checked.add((Checkable) v);
1470 }
1471 }
1472 }
1473 return checked;
1474 }
1475
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001476 /**
1477 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1478 * Otherwise, returns null.
1479 */
1480 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001481 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001482 final int childCount = getChildCount();
1483 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001484 Page layout = (Page) getChildAt(i);
1485 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001486 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001487 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001488 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1489 return (Checkable) v;
1490 }
1491 }
1492 }
1493 }
1494 return null;
1495 }
1496
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001497 protected void resetCheckedGrandchildren() {
1498 // loop through children, and set all of their children to _not_ be checked
1499 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1500 for (int i = 0; i < checked.size(); ++i) {
1501 final Checkable c = checked.get(i);
1502 c.setChecked(false);
1503 }
1504 }
1505
Winson Chunga12a2502010-12-20 14:41:35 -08001506 public void setRestorePage(int restorePage) {
1507 mRestorePage = restorePage;
1508 }
1509
Winson Chung86f77532010-08-24 11:08:22 -07001510 /**
1511 * This method is called ONLY to synchronize the number of pages that the paged view has.
1512 * To actually fill the pages with information, implement syncPageItems() below. It is
1513 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1514 * and therefore, individual page items do not need to be updated in this method.
1515 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001517
1518 /**
1519 * This method is called to synchronize the items that are on a particular page. If views on
1520 * the page can be reused, then they should be updated within this method.
1521 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001522 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001523
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001525 if (mContentIsRefreshable) {
1526 // Update all the pages
1527 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001528
Michael Jurka0142d492010-08-25 17:46:15 -07001529 // Mark each of the pages as dirty
1530 final int count = getChildCount();
1531 mDirtyPageContent.clear();
1532 for (int i = 0; i < count; ++i) {
1533 mDirtyPageContent.add(true);
1534 }
1535
Winson Chunga12a2502010-12-20 14:41:35 -08001536 // Use the restore page if necessary
1537 if (mRestorePage > -1) {
1538 mCurrentPage = mRestorePage;
1539 mRestorePage = -1;
1540 }
1541
Michael Jurka0142d492010-08-25 17:46:15 -07001542 // Load any pages that are necessary for the current window of views
1543 loadAssociatedPages(mCurrentPage);
1544 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001545 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001546 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001547 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001548 }
1549}