blob: a6ae0e1b984bc5e4675cc59af30e60327f2e670a [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 Jurka54a4ac22011-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 Jurka54a4ac22011-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
295 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700296 protected void onPageBeginMoving() {
297 }
298
299 // a method that subclasses can override to add behavior
300 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700301 }
302
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 /**
Winson Chung86f77532010-08-24 11:08:22 -0700304 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 *
306 * @param l The listener used to respond to long clicks.
307 */
308 @Override
309 public void setOnLongClickListener(OnLongClickListener l) {
310 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700311 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700313 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 }
315 }
316
317 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800318 public void scrollBy(int x, int y) {
319 scrollTo(mUnboundedScrollX + x, mScrollY + y);
320 }
321
322 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700323 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800324 mUnboundedScrollX = x;
325
326 if (x < 0) {
327 super.scrollTo(0, y);
328 if (mAllowOverScroll) {
329 overScroll(x);
330 }
331 } else if (x > mMaxScrollX) {
332 super.scrollTo(mMaxScrollX, y);
333 if (mAllowOverScroll) {
334 overScroll(x - mMaxScrollX);
335 }
336 } else {
337 super.scrollTo(x, y);
338 }
339
Michael Jurka0142d492010-08-25 17:46:15 -0700340 mTouchX = x;
341 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
342 }
343
344 // we moved this functionality to a helper function so SmoothPagedView can reuse it
345 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700346 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700347 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700349 invalidate();
350 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700351 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700352 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700353 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700354 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700355 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800356 // We don't want to trigger a page end moving unless the page has settled
357 // and the user has stopped scrolling
358 if (mTouchState == TOUCH_STATE_REST) {
359 pageEndMoving();
360 }
Michael Jurka0142d492010-08-25 17:46:15 -0700361 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 }
Michael Jurka0142d492010-08-25 17:46:15 -0700363 return false;
364 }
365
366 @Override
367 public void computeScroll() {
368 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 }
370
371 @Override
372 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
373 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
374 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
375 if (widthMode != MeasureSpec.EXACTLY) {
376 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
377 }
378
Adam Lesinski6b879f02010-11-04 16:15:23 -0700379 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
380 * of the All apps view on XLarge displays to not take up more space then it needs. Width
381 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
382 * each page to have the same width.
383 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700385 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
386 int maxChildHeight = 0;
387
388 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700389
Michael Jurka54a4ac22011-04-06 17:08:58 -0700390 if (mPageLayoutMaxHeight != -1) {
391 heightSize = Math.min(mPageLayoutMaxHeight, heightSize);
392 }
393
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700395 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 final int childCount = getChildCount();
397 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700398 // disallowing padding in paged view (just pass 0)
399 final View child = getChildAt(i);
400 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
401
402 int childWidthMode;
403 if (lp.width == LayoutParams.WRAP_CONTENT) {
404 childWidthMode = MeasureSpec.AT_MOST;
405 } else {
406 childWidthMode = MeasureSpec.EXACTLY;
407 }
408
409 int childHeightMode;
410 if (lp.height == LayoutParams.WRAP_CONTENT) {
411 childHeightMode = MeasureSpec.AT_MOST;
412 } else {
413 childHeightMode = MeasureSpec.EXACTLY;
414 }
415
416 final int childWidthMeasureSpec =
417 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
418 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700419 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700420
421 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700422 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
423 }
424
425 if (heightMode == MeasureSpec.AT_MOST) {
426 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800428 if (childCount > 0) {
429 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
430 } else {
431 mMaxScrollX = 0;
432 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700433
434 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
436
Michael Jurka8c920dd2011-01-20 14:16:56 -0800437 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800438 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
439 int delta = newX - mScrollX;
440
Michael Jurka8c920dd2011-01-20 14:16:56 -0800441 final int pageCount = getChildCount();
442 for (int i = 0; i < pageCount; i++) {
443 View page = (View) getChildAt(i);
444 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800445 }
446 setCurrentPage(newCurrentPage);
447 }
448
Michael Jurka8c920dd2011-01-20 14:16:56 -0800449 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
450 // 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 -0800451 // tightens the layout accordingly
452 public void setLayoutScale(float childrenScale) {
453 mLayoutScale = childrenScale;
454
455 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
456 int childCount = getChildCount();
457 float childrenX[] = new float[childCount];
458 float childrenY[] = new float[childCount];
459 for (int i = 0; i < childCount; i++) {
460 final View child = getChildAt(i);
461 childrenX[i] = child.getX();
462 childrenY[i] = child.getY();
463 }
464 onLayout(false, mLeft, mTop, mRight, mBottom);
465 for (int i = 0; i < childCount; i++) {
466 final View child = getChildAt(i);
467 child.setX(childrenX[i]);
468 child.setY(childrenY[i]);
469 }
470 // Also, the page offset has changed (since the pages are now smaller);
471 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800472 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800473 }
474
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700476 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700477 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
478 setHorizontalScrollBarEnabled(false);
479 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
480 scrollTo(newX, 0);
481 mScroller.setFinalX(newX);
482 setHorizontalScrollBarEnabled(true);
483 mFirstLayout = false;
484 }
485
Adam Lesinski6b879f02010-11-04 16:15:23 -0700486 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 final int childCount = getChildCount();
488 int childLeft = 0;
489 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700490 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 }
492
493 for (int i = 0; i < childCount; i++) {
494 final View child = getChildAt(i);
495 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800496 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700497 final int childHeight = child.getMeasuredHeight();
498 int childTop = mPaddingTop;
499 if (mCenterPagesVertically) {
500 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
501 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800502
Adam Lesinski6b879f02010-11-04 16:15:23 -0700503 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800504 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700505 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
507 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
509 mFirstLayout = false;
510 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700511 }
512
Winson Chung03929772011-02-23 17:07:10 -0800513 protected void forceUpdateAdjacentPagesAlpha() {
514 mDirtyPageAlpha = true;
515 updateAdjacentPagesAlpha();
516 }
517
Winson Chunge3193b92010-09-10 11:44:42 -0700518 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700519 if (mFadeInAdjacentScreens) {
520 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700521 int halfScreenSize = getMeasuredWidth() / 2;
522 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700523 final int childCount = getChildCount();
524 for (int i = 0; i < childCount; ++i) {
525 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700527 int halfChildWidth = (childWidth / 2);
528 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700529
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700530 // On the first layout, we may not have a width nor a proper offset, so for now
531 // we should just assume full page width (and calculate the offset according to
532 // that).
533 if (childWidth <= 0) {
534 childWidth = getMeasuredWidth();
535 childCenter = (i * childWidth) + (childWidth / 2);
536 }
537
Winson Chunge8878e32010-09-15 20:37:09 -0700538 int d = halfChildWidth;
539 int distanceFromScreenCenter = childCenter - screenCenter;
540 if (distanceFromScreenCenter > 0) {
541 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700543 }
Michael Jurka0142d492010-08-25 17:46:15 -0700544 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700545 if (i < childCount - 1) {
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 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700549 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700550
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700551 // Preventing potential divide-by-zero
552 d = Math.max(1, d);
553
Winson Chunge8878e32010-09-15 20:37:09 -0700554 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
555 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
556 float alpha = 1.0f - dimAlpha;
557
Adam Cohenf34bab52010-09-30 14:11:56 -0700558 if (alpha < ALPHA_QUANTIZE_LEVEL) {
559 alpha = 0.0f;
560 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
561 alpha = 1.0f;
562 }
563
Michael Jurkaa40829a2011-02-16 18:02:45 -0800564 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
565 // this optimization causes alpha to not be properly updated sometimes (repro
566 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
567 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
568 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800569 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700570 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800571 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 }
Michael Jurka0142d492010-08-25 17:46:15 -0700573 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 }
575 }
Winson Chunge3193b92010-09-10 11:44:42 -0700576 }
577
Adam Cohenf34bab52010-09-30 14:11:56 -0700578 protected void screenScrolled(int screenCenter) {
579 }
580
Winson Chunge3193b92010-09-10 11:44:42 -0700581 @Override
582 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700583 int halfScreenSize = getMeasuredWidth() / 2;
584 int screenCenter = mScrollX + halfScreenSize;
585
586 if (screenCenter != mLastScreenCenter) {
587 screenScrolled(screenCenter);
588 updateAdjacentPagesAlpha();
589 mLastScreenCenter = screenCenter;
590 }
Michael Jurka0142d492010-08-25 17:46:15 -0700591
592 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700593 // As an optimization, this code assumes that all pages have the same width as the 0th
594 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700595 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700596 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800597 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700598 final int screenWidth = getMeasuredWidth();
599 int x = getRelativeChildOffset(0) + pageWidth;
600 int leftScreen = 0;
601 int rightScreen = 0;
602 while (x <= mScrollX) {
603 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800604 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700605 }
606 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800607 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700608 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800609 if (rightScreen < pageCount) {
610 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
611 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700612 }
613 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700614
Michael Jurkac4fb9172010-09-02 17:19:20 -0700615 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800616 // Clip to the bounds
617 canvas.save();
618 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
619 mScrollY + mBottom - mTop);
620
Michael Jurkac4fb9172010-09-02 17:19:20 -0700621 for (int i = leftScreen; i <= rightScreen; i++) {
622 drawChild(canvas, getChildAt(i), drawingTime);
623 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800624 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700625 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 }
627
628 @Override
629 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700630 int page = indexOfChild(child);
631 if (page != mCurrentPage || !mScroller.isFinished()) {
632 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700633 return true;
634 }
635 return false;
636 }
637
638 @Override
639 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700640 int focusablePage;
641 if (mNextPage != INVALID_PAGE) {
642 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700644 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 }
Winson Chung86f77532010-08-24 11:08:22 -0700646 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 if (v != null) {
648 v.requestFocus(direction, previouslyFocusedRect);
649 }
650 return false;
651 }
652
653 @Override
654 public boolean dispatchUnhandledMove(View focused, int direction) {
655 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700656 if (getCurrentPage() > 0) {
657 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 return true;
659 }
660 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700661 if (getCurrentPage() < getPageCount() - 1) {
662 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700663 return true;
664 }
665 }
666 return super.dispatchUnhandledMove(focused, direction);
667 }
668
669 @Override
670 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700671 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
672 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 }
674 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700675 if (mCurrentPage > 0) {
676 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 }
678 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700679 if (mCurrentPage < getPageCount() - 1) {
680 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 }
682 }
683 }
684
685 /**
686 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700687 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 *
Winson Chung86f77532010-08-24 11:08:22 -0700689 * This happens when live folders requery, and if they're off page, they
690 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 */
692 @Override
693 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700694 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 View v = focused;
696 while (true) {
697 if (v == current) {
698 super.focusableViewAvailable(focused);
699 return;
700 }
701 if (v == this) {
702 return;
703 }
704 ViewParent parent = v.getParent();
705 if (parent instanceof View) {
706 v = (View)v.getParent();
707 } else {
708 return;
709 }
710 }
711 }
712
713 /**
714 * {@inheritDoc}
715 */
716 @Override
717 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
718 if (disallowIntercept) {
719 // We need to make sure to cancel our long press if
720 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700721 final View currentPage = getChildAt(mCurrentPage);
722 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 }
724 super.requestDisallowInterceptTouchEvent(disallowIntercept);
725 }
726
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800727 /**
728 * Return true if a tap at (x, y) should trigger a flip to the previous page.
729 */
730 protected boolean hitsPreviousPage(float x, float y) {
731 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
732 }
733
734 /**
735 * Return true if a tap at (x, y) should trigger a flip to the next page.
736 */
737 protected boolean hitsNextPage(float x, float y) {
738 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
739 }
740
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 @Override
742 public boolean onInterceptTouchEvent(MotionEvent ev) {
743 /*
744 * This method JUST determines whether we want to intercept the motion.
745 * If we return true, onTouchEvent will be called and we do the actual
746 * scrolling there.
747 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800748 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700749
Winson Chung45e1d6e2010-11-09 17:19:49 -0800750 // Skip touch handling if there are no pages to swipe
751 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
752
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 /*
754 * Shortcut the most recurring case: the user is in the dragging
755 * state and he is moving his finger. We want to intercept this
756 * motion.
757 */
758 final int action = ev.getAction();
759 if ((action == MotionEvent.ACTION_MOVE) &&
760 (mTouchState == TOUCH_STATE_SCROLLING)) {
761 return true;
762 }
763
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 switch (action & MotionEvent.ACTION_MASK) {
765 case MotionEvent.ACTION_MOVE: {
766 /*
767 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
768 * whether the user has moved far enough from his original down touch.
769 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700770 if (mActivePointerId != INVALID_POINTER) {
771 determineScrollingStart(ev);
772 break;
773 }
774 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
775 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
776 // i.e. fall through to the next case (don't break)
777 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
778 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 }
780
781 case MotionEvent.ACTION_DOWN: {
782 final float x = ev.getX();
783 final float y = ev.getY();
784 // Remember location of down touch
785 mDownMotionX = x;
786 mLastMotionX = x;
787 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800788 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800789 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 mActivePointerId = ev.getPointerId(0);
791 mAllowLongPress = true;
792
793 /*
794 * If being flinged and user touches the screen, initiate drag;
795 * otherwise don't. mScroller.isFinished should be false when
796 * being flinged.
797 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700798 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700799 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
800 if (finishedScrolling) {
801 mTouchState = TOUCH_STATE_REST;
802 mScroller.abortAnimation();
803 } else {
804 mTouchState = TOUCH_STATE_SCROLLING;
805 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700806
Winson Chung86f77532010-08-24 11:08:22 -0700807 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800809 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800811 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800813 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 mTouchState = TOUCH_STATE_NEXT_PAGE;
815 }
816 }
817 }
818 break;
819 }
820
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800822 onWallpaperTap(ev);
823 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 mTouchState = TOUCH_STATE_REST;
825 mAllowLongPress = false;
826 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800827 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 break;
829
830 case MotionEvent.ACTION_POINTER_UP:
831 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800832 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 break;
834 }
835
836 /*
837 * The only time we want to intercept motion events is if we are in the
838 * drag mode.
839 */
840 return mTouchState != TOUCH_STATE_REST;
841 }
842
Winson Chung80baf5a2010-08-09 16:03:15 -0700843 protected void animateClickFeedback(View v, final Runnable r) {
844 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800845 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
846 loadAnimator(mContext, R.anim.paged_view_click_feedback);
847 anim.setTarget(v);
848 anim.addListener(new AnimatorListenerAdapter() {
849 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700850 r.run();
851 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700852 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800853 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700854 }
855
Adam Cohenf8d28232011-02-01 21:47:00 -0800856 protected void determineScrollingStart(MotionEvent ev) {
857 determineScrollingStart(ev, 1.0f);
858 }
859
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 /*
861 * Determines if we should change the touch state to start scrolling after the
862 * user moves their touch point too far.
863 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800864 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 /*
866 * Locally do absolute value. mLastMotionX is set to the y value
867 * of the down event.
868 */
869 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
870 final float x = ev.getX(pointerIndex);
871 final float y = ev.getY(pointerIndex);
872 final int xDiff = (int) Math.abs(x - mLastMotionX);
873 final int yDiff = (int) Math.abs(y - mLastMotionY);
874
Adam Cohenf8d28232011-02-01 21:47:00 -0800875 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 boolean xPaged = xDiff > mPagingTouchSlop;
877 boolean xMoved = xDiff > touchSlop;
878 boolean yMoved = yDiff > touchSlop;
879
Adam Cohenf8d28232011-02-01 21:47:00 -0800880 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700881 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 // Scroll if the user moved far enough along the X axis
883 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800884 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800886 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700887 mTouchX = mScrollX;
888 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
889 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 }
891 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800892 cancelCurrentPageLongPress();
893 }
894 }
895
896 protected void cancelCurrentPageLongPress() {
897 if (mAllowLongPress) {
898 mAllowLongPress = false;
899 // Try canceling the long press. It could also have been scheduled
900 // by a distant descendant, so use the mAllowLongPress flag to block
901 // everything
902 final View currentPage = getPageAt(mCurrentPage);
903 if (currentPage != null) {
904 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 }
906 }
907 }
908
Adam Cohene0f66b52010-11-23 15:06:07 -0800909 // This curve determines how the effect of scrolling over the limits of the page dimishes
910 // as the user pulls further and further from the bounds
911 private float overScrollInfluenceCurve(float f) {
912 f -= 1.0f;
913 return f * f * f + 1.0f;
914 }
915
Adam Cohen68d73932010-11-15 10:50:58 -0800916 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800917 int screenSize = getMeasuredWidth();
918
919 float f = (amount / screenSize);
920
921 if (f == 0) return;
922 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
923
Adam Cohen7bfc9792011-01-28 13:52:37 -0800924 // Clamp this factor, f, to -1 < f < 1
925 if (Math.abs(f) >= 1) {
926 f /= Math.abs(f);
927 }
928
Adam Cohene0f66b52010-11-23 15:06:07 -0800929 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800930 if (amount < 0) {
931 mScrollX = overScrollAmount;
932 } else {
933 mScrollX = mMaxScrollX + overScrollAmount;
934 }
935 invalidate();
936 }
937
Michael Jurkac5b262c2011-01-12 20:24:50 -0800938 protected float maxOverScroll() {
939 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
940 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
941 float f = 1.0f;
942 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
943 return OVERSCROLL_DAMP_FACTOR * f;
944 }
945
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 @Override
947 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800948 // Skip touch handling if there are no pages to swipe
949 if (getChildCount() <= 0) return super.onTouchEvent(ev);
950
Michael Jurkab8f06722010-10-10 15:58:46 -0700951 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700952
953 final int action = ev.getAction();
954
955 switch (action & MotionEvent.ACTION_MASK) {
956 case MotionEvent.ACTION_DOWN:
957 /*
958 * If being flinged and user touches, stop the fling. isFinished
959 * will be false if being flinged.
960 */
961 if (!mScroller.isFinished()) {
962 mScroller.abortAnimation();
963 }
964
965 // Remember where the motion event started
966 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800967 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800968 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700970 if (mTouchState == TOUCH_STATE_SCROLLING) {
971 pageBeginMoving();
972 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 break;
974
975 case MotionEvent.ACTION_MOVE:
976 if (mTouchState == TOUCH_STATE_SCROLLING) {
977 // Scroll to follow the motion event
978 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
979 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800980 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700981
Adam Cohenaefd4e12011-02-14 16:39:38 -0800982 mTotalMotionX += Math.abs(deltaX);
983
Winson Chungc0844aa2011-02-02 15:25:58 -0800984 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
985 // keep the remainder because we are actually testing if we've moved from the last
986 // scrolled position (which is discrete).
987 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800988 mTouchX += deltaX;
989 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
990 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800991 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800992 } else {
993 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800995 mLastMotionX = x;
996 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 } else {
998 awakenScrollBars();
999 }
Adam Cohen564976a2010-10-13 18:52:07 -07001000 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 determineScrollingStart(ev);
1002 }
1003 break;
1004
1005 case MotionEvent.ACTION_UP:
1006 if (mTouchState == TOUCH_STATE_SCROLLING) {
1007 final int activePointerId = mActivePointerId;
1008 final int pointerIndex = ev.findPointerIndex(activePointerId);
1009 final float x = ev.getX(pointerIndex);
1010 final VelocityTracker velocityTracker = mVelocityTracker;
1011 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1012 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001013 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001014 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001015 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001016
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001017 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1018
Adam Cohenaefd4e12011-02-14 16:39:38 -08001019 // In the case that the page is moved far to one direction and then is flung
1020 // in the opposite direction, we use a threshold to determine whether we should
1021 // just return to the starting page, or if we should skip one further.
1022 boolean returnToOriginalPage = false;
1023 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001024 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001025 Math.signum(velocityX) != Math.signum(deltaX)) {
1026 returnToOriginalPage = true;
1027 }
1028
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001029 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001030 Math.abs(velocityX) > snapVelocity;
1031
1032 int finalPage;
1033 // We give flings precedence over large moves, which is why we short-circuit our
1034 // test for a large move if a fling has been registered. That is, a large
1035 // move to the left and fling to the right will register as a fling to the right.
1036 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1037 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1038 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1039 snapToPageWithVelocity(finalPage, velocityX);
1040 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1041 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001042 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001043 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1044 snapToPageWithVelocity(finalPage, velocityX);
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_PREV_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.max(0, mCurrentPage - 1);
1053 if (nextPage != mCurrentPage) {
1054 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 } else {
1056 snapToDestination();
1057 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001058 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 // at this point we have not moved beyond the touch slop
1060 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1061 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001062 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1063 if (nextPage != mCurrentPage) {
1064 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 } else {
1066 snapToDestination();
1067 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001068 } else {
1069 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 }
1071 mTouchState = TOUCH_STATE_REST;
1072 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001073 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 break;
1075
1076 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001077 if (mTouchState == TOUCH_STATE_SCROLLING) {
1078 snapToDestination();
1079 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 mTouchState = TOUCH_STATE_REST;
1081 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001082 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001083 break;
1084
1085 case MotionEvent.ACTION_POINTER_UP:
1086 onSecondaryPointerUp(ev);
1087 break;
1088 }
1089
1090 return true;
1091 }
1092
Winson Chung185d7162011-02-28 13:47:29 -08001093 @Override
1094 public boolean onGenericMotionEvent(MotionEvent event) {
1095 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1096 switch (event.getAction()) {
1097 case MotionEvent.ACTION_SCROLL: {
1098 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1099 final float vscroll;
1100 final float hscroll;
1101 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1102 vscroll = 0;
1103 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1104 } else {
1105 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1106 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1107 }
1108 if (hscroll != 0 || vscroll != 0) {
1109 if (hscroll > 0 || vscroll > 0) {
1110 scrollRight();
1111 } else {
1112 scrollLeft();
1113 }
1114 return true;
1115 }
1116 }
1117 }
1118 }
1119 return super.onGenericMotionEvent(event);
1120 }
1121
Michael Jurkab8f06722010-10-10 15:58:46 -07001122 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1123 if (mVelocityTracker == null) {
1124 mVelocityTracker = VelocityTracker.obtain();
1125 }
1126 mVelocityTracker.addMovement(ev);
1127 }
1128
1129 private void releaseVelocityTracker() {
1130 if (mVelocityTracker != null) {
1131 mVelocityTracker.recycle();
1132 mVelocityTracker = null;
1133 }
1134 }
1135
Winson Chung321e9ee2010-08-09 13:37:56 -07001136 private void onSecondaryPointerUp(MotionEvent ev) {
1137 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1138 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1139 final int pointerId = ev.getPointerId(pointerIndex);
1140 if (pointerId == mActivePointerId) {
1141 // This was our active pointer going up. Choose a new
1142 // active pointer and adjust accordingly.
1143 // TODO: Make this decision more intelligent.
1144 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1145 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1146 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001147 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 mActivePointerId = ev.getPointerId(newPointerIndex);
1149 if (mVelocityTracker != null) {
1150 mVelocityTracker.clear();
1151 }
1152 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001153 if (mTouchState == TOUCH_STATE_REST) {
1154 onWallpaperTap(ev);
1155 }
1156 }
1157
1158 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001159 }
1160
1161 @Override
1162 public void requestChildFocus(View child, View focused) {
1163 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001164 int page = indexOfChild(child);
1165 if (page >= 0 && !isInTouchMode()) {
1166 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 }
1168 }
1169
Winson Chunge3193b92010-09-10 11:44:42 -07001170 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1171 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001172 int left;
1173 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001174 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001175 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001176 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001177 if (left <= relativeOffset && relativeOffset <= right) {
1178 return i;
1179 }
Winson Chunge3193b92010-09-10 11:44:42 -07001180 }
1181 return -1;
1182 }
1183
Winson Chung1908d072011-02-24 18:09:44 -08001184 protected void setMinimumWidthOverride(int minimumWidth) {
1185 mMinimumWidth = minimumWidth;
1186 }
1187
1188 protected int getChildWidth(int index) {
1189 return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth());
1190 }
1191
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001193 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 }
1195
1196 protected int getChildOffset(int index) {
1197 if (getChildCount() == 0)
1198 return 0;
1199
1200 int offset = getRelativeChildOffset(0);
1201 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001202 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 }
1204 return offset;
1205 }
1206
Michael Jurkad3ef3062010-11-23 16:23:58 -08001207 protected int getScaledMeasuredWidth(View child) {
Winson Chung1908d072011-02-24 18:09:44 -08001208 return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001209 }
1210
Adam Cohend19d3ca2010-09-15 14:43:42 -07001211 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 int minDistanceFromScreenCenter = getMeasuredWidth();
1213 int minDistanceFromScreenCenterIndex = -1;
1214 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1215 final int childCount = getChildCount();
1216 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001217 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001218 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 int halfChildWidth = (childWidth / 2);
1220 int childCenter = getChildOffset(i) + halfChildWidth;
1221 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1222 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1223 minDistanceFromScreenCenter = distanceFromScreenCenter;
1224 minDistanceFromScreenCenterIndex = i;
1225 }
1226 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001227 return minDistanceFromScreenCenterIndex;
1228 }
1229
1230 protected void snapToDestination() {
1231 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 }
1233
Adam Cohene0f66b52010-11-23 15:06:07 -08001234 private static class ScrollInterpolator implements Interpolator {
1235 public ScrollInterpolator() {
1236 }
1237
1238 public float getInterpolation(float t) {
1239 t -= 1.0f;
1240 return t*t*t*t*t + 1;
1241 }
1242 }
1243
1244 // We want the duration of the page snap animation to be influenced by the distance that
1245 // the screen has to travel, however, we don't want this duration to be effected in a
1246 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1247 // of travel has on the overall snap duration.
1248 float distanceInfluenceForSnapDuration(float f) {
1249 f -= 0.5f; // center the values about 0.
1250 f *= 0.3f * Math.PI / 2.0f;
1251 return (float) Math.sin(f);
1252 }
1253
Michael Jurka0142d492010-08-25 17:46:15 -07001254 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001255 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1256 int halfScreenSize = getMeasuredWidth() / 2;
1257
1258 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1259 int delta = newX - mUnboundedScrollX;
1260 int duration = 0;
1261
1262 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1263 // If the velocity is low enough, then treat this more as an automatic page advance
1264 // as opposed to an apparent physical response to flinging
1265 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1266 return;
1267 }
1268
1269 // Here we compute a "distance" that will be used in the computation of the overall
1270 // snap duration. This is a function of the actual distance that needs to be traveled;
1271 // we keep this value close to half screen size in order to reduce the variance in snap
1272 // duration as a function of the distance the page needs to travel.
1273 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1274 float distance = halfScreenSize + halfScreenSize *
1275 distanceInfluenceForSnapDuration(distanceRatio);
1276
1277 velocity = Math.abs(velocity);
1278 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1279
1280 // we want the page's snap velocity to approximately match the velocity at which the
1281 // user flings, so we scale the duration by a value near to the derivative of the scroll
1282 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1283 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1284
1285 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001286 }
1287
1288 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001289 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 }
1291
Michael Jurka0142d492010-08-25 17:46:15 -07001292 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001293 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001294
Winson Chung86f77532010-08-24 11:08:22 -07001295 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001296 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001298 snapToPage(whichPage, delta, duration);
1299 }
1300
1301 protected void snapToPage(int whichPage, int delta, int duration) {
1302 mNextPage = whichPage;
1303
1304 View focusedChild = getFocusedChild();
1305 if (focusedChild != null && whichPage != mCurrentPage &&
1306 focusedChild == getChildAt(mCurrentPage)) {
1307 focusedChild.clearFocus();
1308 }
1309
1310 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 awakenScrollBars(duration);
1312 if (duration == 0) {
1313 duration = Math.abs(delta);
1314 }
1315
1316 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001317 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001318
1319 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001320 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001321 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001322 invalidate();
1323 }
1324
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 public void scrollLeft() {
1326 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001327 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001329 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 }
1331 }
1332
1333 public void scrollRight() {
1334 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001335 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001337 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 }
1339 }
1340
Winson Chung86f77532010-08-24 11:08:22 -07001341 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 int result = -1;
1343 if (v != null) {
1344 ViewParent vp = v.getParent();
1345 int count = getChildCount();
1346 for (int i = 0; i < count; i++) {
1347 if (vp == getChildAt(i)) {
1348 return i;
1349 }
1350 }
1351 }
1352 return result;
1353 }
1354
1355 /**
1356 * @return True is long presses are still allowed for the current touch
1357 */
1358 public boolean allowLongPress() {
1359 return mAllowLongPress;
1360 }
1361
Michael Jurka0142d492010-08-25 17:46:15 -07001362 /**
1363 * Set true to allow long-press events to be triggered, usually checked by
1364 * {@link Launcher} to accept or block dpad-initiated long-presses.
1365 */
1366 public void setAllowLongPress(boolean allowLongPress) {
1367 mAllowLongPress = allowLongPress;
1368 }
1369
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001371 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001372
1373 SavedState(Parcelable superState) {
1374 super(superState);
1375 }
1376
1377 private SavedState(Parcel in) {
1378 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001379 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 }
1381
1382 @Override
1383 public void writeToParcel(Parcel out, int flags) {
1384 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001385 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 }
1387
1388 public static final Parcelable.Creator<SavedState> CREATOR =
1389 new Parcelable.Creator<SavedState>() {
1390 public SavedState createFromParcel(Parcel in) {
1391 return new SavedState(in);
1392 }
1393
1394 public SavedState[] newArray(int size) {
1395 return new SavedState[size];
1396 }
1397 };
1398 }
1399
Winson Chung86f77532010-08-24 11:08:22 -07001400 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001401 if (mContentIsRefreshable) {
1402 final int count = getChildCount();
1403 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001404 int lowerPageBound = getAssociatedLowerPageBound(page);
1405 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001406 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001407 Page layout = (Page) getChildAt(i);
1408 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001409 if (lowerPageBound <= i && i <= upperPageBound) {
1410 if (mDirtyPageContent.get(i)) {
1411 syncPageItems(i);
1412 mDirtyPageContent.set(i, false);
1413 }
1414 } else {
1415 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001416 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001417 }
1418 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001419 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001420 }
1421 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001422 }
1423 }
1424
Winson Chunge3193b92010-09-10 11:44:42 -07001425 protected int getAssociatedLowerPageBound(int page) {
1426 return Math.max(0, page - 1);
1427 }
1428 protected int getAssociatedUpperPageBound(int page) {
1429 final int count = getChildCount();
1430 return Math.min(page + 1, count - 1);
1431 }
1432
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001433 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001434 if (isChoiceMode(CHOICE_MODE_NONE)) {
1435 mChoiceMode = mode;
1436 mActionMode = startActionMode(callback);
1437 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001438 }
1439
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001440 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001441 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001442 mChoiceMode = CHOICE_MODE_NONE;
1443 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001444 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001445 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001446 }
1447 }
1448
1449 protected boolean isChoiceMode(int mode) {
1450 return mChoiceMode == mode;
1451 }
1452
1453 protected ArrayList<Checkable> getCheckedGrandchildren() {
1454 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1455 final int childCount = getChildCount();
1456 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001457 Page layout = (Page) getChildAt(i);
1458 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001459 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001460 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001461 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001462 checked.add((Checkable) v);
1463 }
1464 }
1465 }
1466 return checked;
1467 }
1468
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001469 /**
1470 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1471 * Otherwise, returns null.
1472 */
1473 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001474 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001475 final int childCount = getChildCount();
1476 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001477 Page layout = (Page) getChildAt(i);
1478 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001479 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001480 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001481 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1482 return (Checkable) v;
1483 }
1484 }
1485 }
1486 }
1487 return null;
1488 }
1489
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001490 protected void resetCheckedGrandchildren() {
1491 // loop through children, and set all of their children to _not_ be checked
1492 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1493 for (int i = 0; i < checked.size(); ++i) {
1494 final Checkable c = checked.get(i);
1495 c.setChecked(false);
1496 }
1497 }
1498
Winson Chunga12a2502010-12-20 14:41:35 -08001499 public void setRestorePage(int restorePage) {
1500 mRestorePage = restorePage;
1501 }
1502
Winson Chung86f77532010-08-24 11:08:22 -07001503 /**
1504 * This method is called ONLY to synchronize the number of pages that the paged view has.
1505 * To actually fill the pages with information, implement syncPageItems() below. It is
1506 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1507 * and therefore, individual page items do not need to be updated in this method.
1508 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001509 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001510
1511 /**
1512 * This method is called to synchronize the items that are on a particular page. If views on
1513 * the page can be reused, then they should be updated within this method.
1514 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001516
Winson Chung321e9ee2010-08-09 13:37:56 -07001517 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001518 if (mContentIsRefreshable) {
1519 // Update all the pages
1520 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001521
Michael Jurka0142d492010-08-25 17:46:15 -07001522 // Mark each of the pages as dirty
1523 final int count = getChildCount();
1524 mDirtyPageContent.clear();
1525 for (int i = 0; i < count; ++i) {
1526 mDirtyPageContent.add(true);
1527 }
1528
Winson Chunga12a2502010-12-20 14:41:35 -08001529 // Use the restore page if necessary
1530 if (mRestorePage > -1) {
1531 mCurrentPage = mRestorePage;
1532 mRestorePage = -1;
1533 }
1534
Michael Jurka0142d492010-08-25 17:46:15 -07001535 // Load any pages that are necessary for the current window of views
1536 loadAssociatedPages(mCurrentPage);
1537 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001538 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001539 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001540 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001541 }
1542}