blob: 1180106c9717970ca2b5f1cc7363be8175598684 [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 Chung785d2eb2011-04-14 16:08:02 -070032import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070033import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080034import android.view.InputDevice;
35import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.view.MotionEvent;
37import android.view.VelocityTracker;
38import android.view.View;
39import android.view.ViewConfiguration;
40import android.view.ViewGroup;
41import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070043import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.widget.Scroller;
45
Winson Chung04998342011-01-05 13:54:43 -080046import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070047
Winson Chung321e9ee2010-08-09 13:37:56 -070048/**
49 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070050 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070051 */
52public abstract class PagedView extends ViewGroup {
53 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070054 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070055 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070056
Winson Chung86f77532010-08-24 11:08:22 -070057 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070058 private static final int MIN_LENGTH_FOR_FLING = 25;
59 // The min drag distance to trigger a page shift (regardless of velocity)
60 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Adam Cohene0f66b52010-11-23 15:06:07 -080062 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohene0f66b52010-11-23 15:06:07 -080065 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
66 private static final int MINIMUM_SNAP_VELOCITY = 2200;
67 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080068 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080069
Michael Jurka0142d492010-08-25 17:46:15 -070070 // the velocity at which a fling gesture will cause us to snap to the next page
71 protected int mSnapVelocity = 500;
72
73 protected float mSmoothingTime;
74 protected float mTouchX;
75
76 protected boolean mFirstLayout = true;
77
78 protected int mCurrentPage;
79 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080080 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080081 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070082 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070083 private VelocityTracker mVelocityTracker;
84
85 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080086 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080087 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080088 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080089 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070090 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected final static int TOUCH_STATE_REST = 0;
93 protected final static int TOUCH_STATE_SCROLLING = 1;
94 protected final static int TOUCH_STATE_PREV_PAGE = 2;
95 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070096 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka7426c422010-11-11 15:23:47 -0800102 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka7426c422010-11-11 15:23:47 -0800104 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105 private int mPagingTouchSlop;
106 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800107 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700108 protected int mPageSpacing;
109 protected int mPageLayoutPaddingTop;
110 protected int mPageLayoutPaddingBottom;
111 protected int mPageLayoutPaddingLeft;
112 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700113 protected int mPageLayoutWidthGap;
114 protected int mPageLayoutHeightGap;
Michael Jurka36fcb742011-04-06 17:08:58 -0700115 protected int mPageLayoutMaxHeight;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700116 protected int mCellCountX;
117 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700118 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800119 protected boolean mAllowOverScroll = true;
120 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka8c920dd2011-01-20 14:16:56 -0800122 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800123 protected float mLayoutScale = 1.0f;
124
Michael Jurka5f1c5092010-09-03 14:15:02 -0700125 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Michael Jurka5f1c5092010-09-03 14:15:02 -0700127 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Winson Chung86f77532010-08-24 11:08:22 -0700129 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Winson Chung86f77532010-08-24 11:08:22 -0700131 private ArrayList<Boolean> mDirtyPageContent;
132 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700134 // choice modes
135 protected static final int CHOICE_MODE_NONE = 0;
136 protected static final int CHOICE_MODE_SINGLE = 1;
137 // Multiple selection mode is not supported by all Launcher actions atm
138 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700139
Michael Jurkae17e19c2010-09-28 11:01:39 -0700140 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700141 private ActionMode mActionMode;
142
Winson Chung04998342011-01-05 13:54:43 -0800143 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
144 // AllApps and Customize, and allows them to share holographic icons for the application view
145 // (which is in both).
146 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700147
Michael Jurka0142d492010-08-25 17:46:15 -0700148 // If true, syncPages and syncPageItems will be called to refresh pages
149 protected boolean mContentIsRefreshable = true;
150
151 // If true, modify alpha of neighboring pages as user scrolls left/right
152 protected boolean mFadeInAdjacentScreens = true;
153
154 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
155 // to switch to a new page
156 protected boolean mUsePagingTouchSlop = true;
157
158 // If true, the subclass should directly update mScrollX itself in its computeScroll method
159 // (SmoothPagedView does this)
160 protected boolean mDeferScrollUpdate = false;
161
Patrick Dubroy1262e362010-10-06 15:49:50 -0700162 protected boolean mIsPageMoving = false;
163
Winson Chung86f77532010-08-24 11:08:22 -0700164 public interface PageSwitchListener {
165 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 }
167
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 public PagedView(Context context) {
169 this(context, null);
170 }
171
172 public PagedView(Context context, AttributeSet attrs) {
173 this(context, attrs, 0);
174 }
175
176 public PagedView(Context context, AttributeSet attrs, int defStyle) {
177 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700178 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700179
Adam Cohen9c4949e2010-10-05 12:27:22 -0700180 TypedArray a = context.obtainStyledAttributes(attrs,
181 R.styleable.PagedView, defStyle, 0);
182 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
183 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800184 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700185 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800186 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800188 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700189 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800190 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700191 mPageLayoutWidthGap = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutWidthGap, -1);
193 mPageLayoutHeightGap = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutHeightGap, -1);
Michael Jurka36fcb742011-04-06 17:08:58 -0700195 mPageLayoutMaxHeight = a.getDimensionPixelSize(
196 R.styleable.PagedView_pageLayoutMaxHeight, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700197 a.recycle();
198
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700200 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202
203 /**
204 * Initializes various states for this workspace.
205 */
Michael Jurka0142d492010-08-25 17:46:15 -0700206 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700207 mDirtyPageContent = new ArrayList<Boolean>();
208 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800209 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700210 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700211 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
217 }
218
Winson Chung86f77532010-08-24 11:08:22 -0700219 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
220 mPageSwitchListener = pageSwitchListener;
221 if (mPageSwitchListener != null) {
222 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224 }
225
226 /**
Winson Chung86f77532010-08-24 11:08:22 -0700227 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 *
Winson Chung86f77532010-08-24 11:08:22 -0700229 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 */
Winson Chung86f77532010-08-24 11:08:22 -0700231 int getCurrentPage() {
232 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 }
234
Winson Chung86f77532010-08-24 11:08:22 -0700235 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Winson Chung86f77532010-08-24 11:08:22 -0700239 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
243 int getScrollWidth() {
244 return getWidth();
245 }
246
Adam Cohenbe909342011-01-28 17:02:58 -0800247 public int getTouchState() {
248 return mTouchState;
249 }
250
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800252 * Updates the scroll of the current page immediately to its final scroll position. We use this
253 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
254 * the previous tab page.
255 */
256 protected void updateCurrentPageScroll() {
257 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
258 scrollTo(newX, 0);
259 mScroller.setFinalX(newX);
260 }
261
262 /**
Winson Chung86f77532010-08-24 11:08:22 -0700263 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 */
Winson Chung86f77532010-08-24 11:08:22 -0700265 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800266 if (!mScroller.isFinished()) {
267 mScroller.abortAnimation();
268 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800269 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
270 // the default
271 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800272 return;
273 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700274
Winson Chung86f77532010-08-24 11:08:22 -0700275 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800276 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700277 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800278 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 }
280
Michael Jurka0142d492010-08-25 17:46:15 -0700281 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700282 if (mPageSwitchListener != null) {
283 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700284 }
285 }
286
Michael Jurkace7e05f2011-02-01 22:02:35 -0800287 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700288 mIsPageMoving = true;
289 onPageBeginMoving();
290 }
291
Michael Jurkace7e05f2011-02-01 22:02:35 -0800292 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 onPageEndMoving();
294 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700295 }
296
Adam Cohen26976d92011-03-22 15:33:33 -0700297 protected boolean isPageMoving() {
298 return mIsPageMoving;
299 }
300
Michael Jurka0142d492010-08-25 17:46:15 -0700301 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700302 protected void onPageBeginMoving() {
303 }
304
305 // a method that subclasses can override to add behavior
306 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700307 }
308
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 /**
Winson Chung86f77532010-08-24 11:08:22 -0700310 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 *
312 * @param l The listener used to respond to long clicks.
313 */
314 @Override
315 public void setOnLongClickListener(OnLongClickListener l) {
316 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700317 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700319 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
321 }
322
323 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800324 public void scrollBy(int x, int y) {
325 scrollTo(mUnboundedScrollX + x, mScrollY + y);
326 }
327
328 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700329 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800330 mUnboundedScrollX = x;
331
332 if (x < 0) {
333 super.scrollTo(0, y);
334 if (mAllowOverScroll) {
335 overScroll(x);
336 }
337 } else if (x > mMaxScrollX) {
338 super.scrollTo(mMaxScrollX, y);
339 if (mAllowOverScroll) {
340 overScroll(x - mMaxScrollX);
341 }
342 } else {
343 super.scrollTo(x, y);
344 }
345
Michael Jurka0142d492010-08-25 17:46:15 -0700346 mTouchX = x;
347 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
348 }
349
350 // we moved this functionality to a helper function so SmoothPagedView can reuse it
351 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700353 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700355 invalidate();
356 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700357 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700358 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700359 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700360 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700361 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800362 // We don't want to trigger a page end moving unless the page has settled
363 // and the user has stopped scrolling
364 if (mTouchState == TOUCH_STATE_REST) {
365 pageEndMoving();
366 }
Michael Jurka0142d492010-08-25 17:46:15 -0700367 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700368 }
Michael Jurka0142d492010-08-25 17:46:15 -0700369 return false;
370 }
371
372 @Override
373 public void computeScroll() {
374 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 }
376
377 @Override
378 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
379 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
380 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
381 if (widthMode != MeasureSpec.EXACTLY) {
382 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
383 }
384
Adam Lesinski6b879f02010-11-04 16:15:23 -0700385 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
386 * of the All apps view on XLarge displays to not take up more space then it needs. Width
387 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
388 * each page to have the same width.
389 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700391 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
392 int maxChildHeight = 0;
393
394 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700395
Michael Jurka36fcb742011-04-06 17:08:58 -0700396 if (mPageLayoutMaxHeight != -1) {
397 heightSize = Math.min(mPageLayoutMaxHeight, heightSize);
398 }
399
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700401 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700402 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 final int childCount = getChildCount();
404 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700405 // disallowing padding in paged view (just pass 0)
406 final View child = getChildAt(i);
407 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
408
409 int childWidthMode;
410 if (lp.width == LayoutParams.WRAP_CONTENT) {
411 childWidthMode = MeasureSpec.AT_MOST;
412 } else {
413 childWidthMode = MeasureSpec.EXACTLY;
414 }
415
416 int childHeightMode;
417 if (lp.height == LayoutParams.WRAP_CONTENT) {
418 childHeightMode = MeasureSpec.AT_MOST;
419 } else {
420 childHeightMode = MeasureSpec.EXACTLY;
421 }
422
423 final int childWidthMeasureSpec =
424 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
425 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700426 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700427
428 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700429 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700430 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
431 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700432 }
433
434 if (heightMode == MeasureSpec.AT_MOST) {
435 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800437 if (childCount > 0) {
438 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
439 } else {
440 mMaxScrollX = 0;
441 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700442
443 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 }
445
Michael Jurka8c920dd2011-01-20 14:16:56 -0800446 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800447 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
448 int delta = newX - mScrollX;
449
Michael Jurka8c920dd2011-01-20 14:16:56 -0800450 final int pageCount = getChildCount();
451 for (int i = 0; i < pageCount; i++) {
452 View page = (View) getChildAt(i);
453 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800454 }
455 setCurrentPage(newCurrentPage);
456 }
457
Michael Jurka8c920dd2011-01-20 14:16:56 -0800458 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
459 // 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 -0800460 // tightens the layout accordingly
461 public void setLayoutScale(float childrenScale) {
462 mLayoutScale = childrenScale;
463
464 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
465 int childCount = getChildCount();
466 float childrenX[] = new float[childCount];
467 float childrenY[] = new float[childCount];
468 for (int i = 0; i < childCount; i++) {
469 final View child = getChildAt(i);
470 childrenX[i] = child.getX();
471 childrenY[i] = child.getY();
472 }
473 onLayout(false, mLeft, mTop, mRight, mBottom);
474 for (int i = 0; i < childCount; i++) {
475 final View child = getChildAt(i);
476 child.setX(childrenX[i]);
477 child.setY(childrenY[i]);
478 }
479 // Also, the page offset has changed (since the pages are now smaller);
480 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800481 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800482 }
483
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700485 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700486 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700487 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
488 setHorizontalScrollBarEnabled(false);
489 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
490 scrollTo(newX, 0);
491 mScroller.setFinalX(newX);
492 setHorizontalScrollBarEnabled(true);
493 mFirstLayout = false;
494 }
495
Adam Lesinski6b879f02010-11-04 16:15:23 -0700496 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 final int childCount = getChildCount();
498 int childLeft = 0;
499 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700500 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
501 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700502 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 }
504
505 for (int i = 0; i < childCount; i++) {
506 final View child = getChildAt(i);
507 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700509 final int childHeight = child.getMeasuredHeight();
510 int childTop = mPaddingTop;
511 if (mCenterPagesVertically) {
512 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
513 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800514
Winson Chung785d2eb2011-04-14 16:08:02 -0700515 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700516 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800517 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700518 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
520 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800521 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
522 mFirstLayout = false;
523 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525
Winson Chung03929772011-02-23 17:07:10 -0800526 protected void forceUpdateAdjacentPagesAlpha() {
527 mDirtyPageAlpha = true;
528 updateAdjacentPagesAlpha();
529 }
530
Winson Chunge3193b92010-09-10 11:44:42 -0700531 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700532 if (mFadeInAdjacentScreens) {
533 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700534 int screenWidth = getMeasuredWidth();
535 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700536 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700537 final int childCount = getChildCount();
538 for (int i = 0; i < childCount; ++i) {
539 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800540 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700541 int halfChildWidth = (childWidth / 2);
542 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700543
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700544 // On the first layout, we may not have a width nor a proper offset, so for now
545 // we should just assume full page width (and calculate the offset according to
546 // that).
547 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700548 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700549 childCenter = (i * childWidth) + (childWidth / 2);
550 }
551
Winson Chunge8878e32010-09-15 20:37:09 -0700552 int d = halfChildWidth;
553 int distanceFromScreenCenter = childCenter - screenCenter;
554 if (distanceFromScreenCenter > 0) {
555 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800556 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700557 }
Michael Jurka0142d492010-08-25 17:46:15 -0700558 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700559 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800560 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700561 }
Michael Jurka0142d492010-08-25 17:46:15 -0700562 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700563 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700564
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700565 // Preventing potential divide-by-zero
566 d = Math.max(1, d);
567
Winson Chunge8878e32010-09-15 20:37:09 -0700568 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
569 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
570 float alpha = 1.0f - dimAlpha;
571
Adam Cohenf34bab52010-09-30 14:11:56 -0700572 if (alpha < ALPHA_QUANTIZE_LEVEL) {
573 alpha = 0.0f;
574 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
575 alpha = 1.0f;
576 }
577
Michael Jurkaa40829a2011-02-16 18:02:45 -0800578 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
579 // this optimization causes alpha to not be properly updated sometimes (repro
580 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
581 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
582 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800583 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700584 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800585 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700586 }
Michael Jurka0142d492010-08-25 17:46:15 -0700587 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700588 }
589 }
Winson Chunge3193b92010-09-10 11:44:42 -0700590 }
591
Adam Cohenf34bab52010-09-30 14:11:56 -0700592 protected void screenScrolled(int screenCenter) {
593 }
594
Winson Chunge3193b92010-09-10 11:44:42 -0700595 @Override
596 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700597 int halfScreenSize = getMeasuredWidth() / 2;
598 int screenCenter = mScrollX + halfScreenSize;
599
600 if (screenCenter != mLastScreenCenter) {
601 screenScrolled(screenCenter);
602 updateAdjacentPagesAlpha();
603 mLastScreenCenter = screenCenter;
604 }
Michael Jurka0142d492010-08-25 17:46:15 -0700605
606 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700607 // As an optimization, this code assumes that all pages have the same width as the 0th
608 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700609 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700610 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800611 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700612 final int screenWidth = getMeasuredWidth();
613 int x = getRelativeChildOffset(0) + pageWidth;
614 int leftScreen = 0;
615 int rightScreen = 0;
616 while (x <= mScrollX) {
617 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800618 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700619 }
620 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800621 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700622 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800623 if (rightScreen < pageCount) {
624 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
625 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700626 }
627 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700628
Michael Jurkac4fb9172010-09-02 17:19:20 -0700629 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800630 // Clip to the bounds
631 canvas.save();
632 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
633 mScrollY + mBottom - mTop);
634
Michael Jurkac4fb9172010-09-02 17:19:20 -0700635 for (int i = leftScreen; i <= rightScreen; i++) {
636 drawChild(canvas, getChildAt(i), drawingTime);
637 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800638 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700639 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 }
641
642 @Override
643 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700644 int page = indexOfChild(child);
645 if (page != mCurrentPage || !mScroller.isFinished()) {
646 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 return true;
648 }
649 return false;
650 }
651
652 @Override
653 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700654 int focusablePage;
655 if (mNextPage != INVALID_PAGE) {
656 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700657 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700658 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 }
Winson Chung86f77532010-08-24 11:08:22 -0700660 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700661 if (v != null) {
662 v.requestFocus(direction, previouslyFocusedRect);
663 }
664 return false;
665 }
666
667 @Override
668 public boolean dispatchUnhandledMove(View focused, int direction) {
669 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700670 if (getCurrentPage() > 0) {
671 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 return true;
673 }
674 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700675 if (getCurrentPage() < getPageCount() - 1) {
676 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 return true;
678 }
679 }
680 return super.dispatchUnhandledMove(focused, direction);
681 }
682
683 @Override
684 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700685 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
686 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700687 }
688 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700689 if (mCurrentPage > 0) {
690 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 }
692 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700693 if (mCurrentPage < getPageCount() - 1) {
694 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 }
696 }
697 }
698
699 /**
700 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700701 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700702 *
Winson Chung86f77532010-08-24 11:08:22 -0700703 * This happens when live folders requery, and if they're off page, they
704 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 */
706 @Override
707 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700708 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700709 View v = focused;
710 while (true) {
711 if (v == current) {
712 super.focusableViewAvailable(focused);
713 return;
714 }
715 if (v == this) {
716 return;
717 }
718 ViewParent parent = v.getParent();
719 if (parent instanceof View) {
720 v = (View)v.getParent();
721 } else {
722 return;
723 }
724 }
725 }
726
727 /**
728 * {@inheritDoc}
729 */
730 @Override
731 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
732 if (disallowIntercept) {
733 // We need to make sure to cancel our long press if
734 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700735 final View currentPage = getChildAt(mCurrentPage);
736 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700737 }
738 super.requestDisallowInterceptTouchEvent(disallowIntercept);
739 }
740
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800741 /**
742 * Return true if a tap at (x, y) should trigger a flip to the previous page.
743 */
744 protected boolean hitsPreviousPage(float x, float y) {
745 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
746 }
747
748 /**
749 * Return true if a tap at (x, y) should trigger a flip to the next page.
750 */
751 protected boolean hitsNextPage(float x, float y) {
752 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
753 }
754
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 @Override
756 public boolean onInterceptTouchEvent(MotionEvent ev) {
757 /*
758 * This method JUST determines whether we want to intercept the motion.
759 * If we return true, onTouchEvent will be called and we do the actual
760 * scrolling there.
761 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800762 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700763
Winson Chung45e1d6e2010-11-09 17:19:49 -0800764 // Skip touch handling if there are no pages to swipe
765 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
766
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 /*
768 * Shortcut the most recurring case: the user is in the dragging
769 * state and he is moving his finger. We want to intercept this
770 * motion.
771 */
772 final int action = ev.getAction();
773 if ((action == MotionEvent.ACTION_MOVE) &&
774 (mTouchState == TOUCH_STATE_SCROLLING)) {
775 return true;
776 }
777
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 switch (action & MotionEvent.ACTION_MASK) {
779 case MotionEvent.ACTION_MOVE: {
780 /*
781 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
782 * whether the user has moved far enough from his original down touch.
783 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700784 if (mActivePointerId != INVALID_POINTER) {
785 determineScrollingStart(ev);
786 break;
787 }
788 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
789 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
790 // i.e. fall through to the next case (don't break)
791 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
792 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 }
794
795 case MotionEvent.ACTION_DOWN: {
796 final float x = ev.getX();
797 final float y = ev.getY();
798 // Remember location of down touch
799 mDownMotionX = x;
800 mLastMotionX = x;
801 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800802 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800803 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 mActivePointerId = ev.getPointerId(0);
805 mAllowLongPress = true;
806
807 /*
808 * If being flinged and user touches the screen, initiate drag;
809 * otherwise don't. mScroller.isFinished should be false when
810 * being flinged.
811 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700812 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700813 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
814 if (finishedScrolling) {
815 mTouchState = TOUCH_STATE_REST;
816 mScroller.abortAnimation();
817 } else {
818 mTouchState = TOUCH_STATE_SCROLLING;
819 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700820
Winson Chung86f77532010-08-24 11:08:22 -0700821 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800823 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800825 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800827 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 mTouchState = TOUCH_STATE_NEXT_PAGE;
829 }
830 }
831 }
832 break;
833 }
834
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800836 onWallpaperTap(ev);
837 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 mTouchState = TOUCH_STATE_REST;
839 mAllowLongPress = false;
840 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800841 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 break;
843
844 case MotionEvent.ACTION_POINTER_UP:
845 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800846 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 break;
848 }
849
850 /*
851 * The only time we want to intercept motion events is if we are in the
852 * drag mode.
853 */
854 return mTouchState != TOUCH_STATE_REST;
855 }
856
Winson Chung80baf5a2010-08-09 16:03:15 -0700857 protected void animateClickFeedback(View v, final Runnable r) {
858 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800859 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
860 loadAnimator(mContext, R.anim.paged_view_click_feedback);
861 anim.setTarget(v);
862 anim.addListener(new AnimatorListenerAdapter() {
863 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700864 r.run();
865 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700866 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800867 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700868 }
869
Adam Cohenf8d28232011-02-01 21:47:00 -0800870 protected void determineScrollingStart(MotionEvent ev) {
871 determineScrollingStart(ev, 1.0f);
872 }
873
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 /*
875 * Determines if we should change the touch state to start scrolling after the
876 * user moves their touch point too far.
877 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800878 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 /*
880 * Locally do absolute value. mLastMotionX is set to the y value
881 * of the down event.
882 */
883 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
884 final float x = ev.getX(pointerIndex);
885 final float y = ev.getY(pointerIndex);
886 final int xDiff = (int) Math.abs(x - mLastMotionX);
887 final int yDiff = (int) Math.abs(y - mLastMotionY);
888
Adam Cohenf8d28232011-02-01 21:47:00 -0800889 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 boolean xPaged = xDiff > mPagingTouchSlop;
891 boolean xMoved = xDiff > touchSlop;
892 boolean yMoved = yDiff > touchSlop;
893
Adam Cohenf8d28232011-02-01 21:47:00 -0800894 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700895 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 // Scroll if the user moved far enough along the X axis
897 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800898 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800900 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700901 mTouchX = mScrollX;
902 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
903 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 }
905 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800906 cancelCurrentPageLongPress();
907 }
908 }
909
910 protected void cancelCurrentPageLongPress() {
911 if (mAllowLongPress) {
912 mAllowLongPress = false;
913 // Try canceling the long press. It could also have been scheduled
914 // by a distant descendant, so use the mAllowLongPress flag to block
915 // everything
916 final View currentPage = getPageAt(mCurrentPage);
917 if (currentPage != null) {
918 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 }
920 }
921 }
922
Adam Cohene0f66b52010-11-23 15:06:07 -0800923 // This curve determines how the effect of scrolling over the limits of the page dimishes
924 // as the user pulls further and further from the bounds
925 private float overScrollInfluenceCurve(float f) {
926 f -= 1.0f;
927 return f * f * f + 1.0f;
928 }
929
Adam Cohen68d73932010-11-15 10:50:58 -0800930 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800931 int screenSize = getMeasuredWidth();
932
933 float f = (amount / screenSize);
934
935 if (f == 0) return;
936 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
937
Adam Cohen7bfc9792011-01-28 13:52:37 -0800938 // Clamp this factor, f, to -1 < f < 1
939 if (Math.abs(f) >= 1) {
940 f /= Math.abs(f);
941 }
942
Adam Cohene0f66b52010-11-23 15:06:07 -0800943 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800944 if (amount < 0) {
945 mScrollX = overScrollAmount;
946 } else {
947 mScrollX = mMaxScrollX + overScrollAmount;
948 }
949 invalidate();
950 }
951
Michael Jurkac5b262c2011-01-12 20:24:50 -0800952 protected float maxOverScroll() {
953 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
954 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
955 float f = 1.0f;
956 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
957 return OVERSCROLL_DAMP_FACTOR * f;
958 }
959
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 @Override
961 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800962 // Skip touch handling if there are no pages to swipe
963 if (getChildCount() <= 0) return super.onTouchEvent(ev);
964
Michael Jurkab8f06722010-10-10 15:58:46 -0700965 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700966
967 final int action = ev.getAction();
968
969 switch (action & MotionEvent.ACTION_MASK) {
970 case MotionEvent.ACTION_DOWN:
971 /*
972 * If being flinged and user touches, stop the fling. isFinished
973 * will be false if being flinged.
974 */
975 if (!mScroller.isFinished()) {
976 mScroller.abortAnimation();
977 }
978
979 // Remember where the motion event started
980 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800981 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800982 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700984 if (mTouchState == TOUCH_STATE_SCROLLING) {
985 pageBeginMoving();
986 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 break;
988
989 case MotionEvent.ACTION_MOVE:
990 if (mTouchState == TOUCH_STATE_SCROLLING) {
991 // Scroll to follow the motion event
992 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
993 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800994 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700995
Adam Cohenaefd4e12011-02-14 16:39:38 -0800996 mTotalMotionX += Math.abs(deltaX);
997
Winson Chungc0844aa2011-02-02 15:25:58 -0800998 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
999 // keep the remainder because we are actually testing if we've moved from the last
1000 // scrolled position (which is discrete).
1001 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001002 mTouchX += deltaX;
1003 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1004 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001005 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001006 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001007 } else {
1008 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001010 mLastMotionX = x;
1011 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 } else {
1013 awakenScrollBars();
1014 }
Adam Cohen564976a2010-10-13 18:52:07 -07001015 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 determineScrollingStart(ev);
1017 }
1018 break;
1019
1020 case MotionEvent.ACTION_UP:
1021 if (mTouchState == TOUCH_STATE_SCROLLING) {
1022 final int activePointerId = mActivePointerId;
1023 final int pointerIndex = ev.findPointerIndex(activePointerId);
1024 final float x = ev.getX(pointerIndex);
1025 final VelocityTracker velocityTracker = mVelocityTracker;
1026 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1027 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001028 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001029 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001030 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001031
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001032 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1033
Adam Cohenaefd4e12011-02-14 16:39:38 -08001034 // In the case that the page is moved far to one direction and then is flung
1035 // in the opposite direction, we use a threshold to determine whether we should
1036 // just return to the starting page, or if we should skip one further.
1037 boolean returnToOriginalPage = false;
1038 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001039 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001040 Math.signum(velocityX) != Math.signum(deltaX)) {
1041 returnToOriginalPage = true;
1042 }
1043
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001044 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001045 Math.abs(velocityX) > snapVelocity;
1046
1047 int finalPage;
1048 // We give flings precedence over large moves, which is why we short-circuit our
1049 // test for a large move if a fling has been registered. That is, a large
1050 // move to the left and fling to the right will register as a fling to the right.
1051 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1052 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1053 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1054 snapToPageWithVelocity(finalPage, velocityX);
1055 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1056 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001057 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001058 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1059 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 } else {
1061 snapToDestination();
1062 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001063 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 // at this point we have not moved beyond the touch slop
1065 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1066 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001067 int nextPage = Math.max(0, mCurrentPage - 1);
1068 if (nextPage != mCurrentPage) {
1069 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 } else {
1071 snapToDestination();
1072 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001073 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 // at this point we have not moved beyond the touch slop
1075 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1076 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001077 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1078 if (nextPage != mCurrentPage) {
1079 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 } else {
1081 snapToDestination();
1082 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001083 } else {
1084 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001085 }
1086 mTouchState = TOUCH_STATE_REST;
1087 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001088 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 break;
1090
1091 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001092 if (mTouchState == TOUCH_STATE_SCROLLING) {
1093 snapToDestination();
1094 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 mTouchState = TOUCH_STATE_REST;
1096 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001097 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001098 break;
1099
1100 case MotionEvent.ACTION_POINTER_UP:
1101 onSecondaryPointerUp(ev);
1102 break;
1103 }
1104
1105 return true;
1106 }
1107
Winson Chung185d7162011-02-28 13:47:29 -08001108 @Override
1109 public boolean onGenericMotionEvent(MotionEvent event) {
1110 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1111 switch (event.getAction()) {
1112 case MotionEvent.ACTION_SCROLL: {
1113 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1114 final float vscroll;
1115 final float hscroll;
1116 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1117 vscroll = 0;
1118 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1119 } else {
1120 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1121 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1122 }
1123 if (hscroll != 0 || vscroll != 0) {
1124 if (hscroll > 0 || vscroll > 0) {
1125 scrollRight();
1126 } else {
1127 scrollLeft();
1128 }
1129 return true;
1130 }
1131 }
1132 }
1133 }
1134 return super.onGenericMotionEvent(event);
1135 }
1136
Michael Jurkab8f06722010-10-10 15:58:46 -07001137 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1138 if (mVelocityTracker == null) {
1139 mVelocityTracker = VelocityTracker.obtain();
1140 }
1141 mVelocityTracker.addMovement(ev);
1142 }
1143
1144 private void releaseVelocityTracker() {
1145 if (mVelocityTracker != null) {
1146 mVelocityTracker.recycle();
1147 mVelocityTracker = null;
1148 }
1149 }
1150
Winson Chung321e9ee2010-08-09 13:37:56 -07001151 private void onSecondaryPointerUp(MotionEvent ev) {
1152 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1153 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1154 final int pointerId = ev.getPointerId(pointerIndex);
1155 if (pointerId == mActivePointerId) {
1156 // This was our active pointer going up. Choose a new
1157 // active pointer and adjust accordingly.
1158 // TODO: Make this decision more intelligent.
1159 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1160 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1161 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001162 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 mActivePointerId = ev.getPointerId(newPointerIndex);
1164 if (mVelocityTracker != null) {
1165 mVelocityTracker.clear();
1166 }
1167 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001168 if (mTouchState == TOUCH_STATE_REST) {
1169 onWallpaperTap(ev);
1170 }
1171 }
1172
1173 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 }
1175
1176 @Override
1177 public void requestChildFocus(View child, View focused) {
1178 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001179 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001180 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001181 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 }
1183 }
1184
Winson Chunge3193b92010-09-10 11:44:42 -07001185 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1186 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001187 int left;
1188 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001189 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001190 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001191 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001192 if (left <= relativeOffset && relativeOffset <= right) {
1193 return i;
1194 }
Winson Chunge3193b92010-09-10 11:44:42 -07001195 }
1196 return -1;
1197 }
1198
Winson Chung1908d072011-02-24 18:09:44 -08001199 protected void setMinimumWidthOverride(int minimumWidth) {
1200 mMinimumWidth = minimumWidth;
1201 }
Winson Chung34b23d52011-03-18 11:29:34 -07001202 protected void resetMinimumWidthOverride() {
1203 mMinimumWidth = 0;
1204 }
Winson Chung1908d072011-02-24 18:09:44 -08001205
1206 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001207 // This functions are called enough times that it actually makes a difference in the
1208 // profiler -- so just inline the max() here
1209 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1210 final int minWidth = mMinimumWidth;
1211 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001212 }
1213
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001215 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 }
1217
1218 protected int getChildOffset(int index) {
1219 if (getChildCount() == 0)
1220 return 0;
1221
1222 int offset = getRelativeChildOffset(0);
1223 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001224 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 }
1226 return offset;
1227 }
1228
Michael Jurkad3ef3062010-11-23 16:23:58 -08001229 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001230 // This functions are called enough times that it actually makes a difference in the
1231 // profiler -- so just inline the max() here
1232 final int measuredWidth = child.getMeasuredWidth();
1233 final int minWidth = mMinimumWidth;
1234 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1235 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001236 }
1237
Adam Cohend19d3ca2010-09-15 14:43:42 -07001238 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 int minDistanceFromScreenCenter = getMeasuredWidth();
1240 int minDistanceFromScreenCenterIndex = -1;
1241 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1242 final int childCount = getChildCount();
1243 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001244 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001245 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 int halfChildWidth = (childWidth / 2);
1247 int childCenter = getChildOffset(i) + halfChildWidth;
1248 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1249 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1250 minDistanceFromScreenCenter = distanceFromScreenCenter;
1251 minDistanceFromScreenCenterIndex = i;
1252 }
1253 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001254 return minDistanceFromScreenCenterIndex;
1255 }
1256
1257 protected void snapToDestination() {
1258 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 }
1260
Adam Cohene0f66b52010-11-23 15:06:07 -08001261 private static class ScrollInterpolator implements Interpolator {
1262 public ScrollInterpolator() {
1263 }
1264
1265 public float getInterpolation(float t) {
1266 t -= 1.0f;
1267 return t*t*t*t*t + 1;
1268 }
1269 }
1270
1271 // We want the duration of the page snap animation to be influenced by the distance that
1272 // the screen has to travel, however, we don't want this duration to be effected in a
1273 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1274 // of travel has on the overall snap duration.
1275 float distanceInfluenceForSnapDuration(float f) {
1276 f -= 0.5f; // center the values about 0.
1277 f *= 0.3f * Math.PI / 2.0f;
1278 return (float) Math.sin(f);
1279 }
1280
Michael Jurka0142d492010-08-25 17:46:15 -07001281 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001282 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1283 int halfScreenSize = getMeasuredWidth() / 2;
1284
Winson Chung785d2eb2011-04-14 16:08:02 -07001285 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1286 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1287 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001288 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1289 int delta = newX - mUnboundedScrollX;
1290 int duration = 0;
1291
1292 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1293 // If the velocity is low enough, then treat this more as an automatic page advance
1294 // as opposed to an apparent physical response to flinging
1295 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1296 return;
1297 }
1298
1299 // Here we compute a "distance" that will be used in the computation of the overall
1300 // snap duration. This is a function of the actual distance that needs to be traveled;
1301 // we keep this value close to half screen size in order to reduce the variance in snap
1302 // duration as a function of the distance the page needs to travel.
1303 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1304 float distance = halfScreenSize + halfScreenSize *
1305 distanceInfluenceForSnapDuration(distanceRatio);
1306
1307 velocity = Math.abs(velocity);
1308 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1309
1310 // we want the page's snap velocity to approximately match the velocity at which the
1311 // user flings, so we scale the duration by a value near to the derivative of the scroll
1312 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1313 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1314
1315 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001316 }
1317
1318 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001319 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 }
1321
Michael Jurka0142d492010-08-25 17:46:15 -07001322 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001323 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001324
Winson Chung785d2eb2011-04-14 16:08:02 -07001325 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1326 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1327 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001328 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001329 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001331 snapToPage(whichPage, delta, duration);
1332 }
1333
1334 protected void snapToPage(int whichPage, int delta, int duration) {
1335 mNextPage = whichPage;
1336
1337 View focusedChild = getFocusedChild();
1338 if (focusedChild != null && whichPage != mCurrentPage &&
1339 focusedChild == getChildAt(mCurrentPage)) {
1340 focusedChild.clearFocus();
1341 }
1342
1343 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 awakenScrollBars(duration);
1345 if (duration == 0) {
1346 duration = Math.abs(delta);
1347 }
1348
1349 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001350 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001351
1352 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001353 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001354 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 invalidate();
1356 }
1357
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 public void scrollLeft() {
1359 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001360 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001362 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 }
1364 }
1365
1366 public void scrollRight() {
1367 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001368 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001370 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 }
1372 }
1373
Winson Chung86f77532010-08-24 11:08:22 -07001374 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001375 int result = -1;
1376 if (v != null) {
1377 ViewParent vp = v.getParent();
1378 int count = getChildCount();
1379 for (int i = 0; i < count; i++) {
1380 if (vp == getChildAt(i)) {
1381 return i;
1382 }
1383 }
1384 }
1385 return result;
1386 }
1387
1388 /**
1389 * @return True is long presses are still allowed for the current touch
1390 */
1391 public boolean allowLongPress() {
1392 return mAllowLongPress;
1393 }
1394
Michael Jurka0142d492010-08-25 17:46:15 -07001395 /**
1396 * Set true to allow long-press events to be triggered, usually checked by
1397 * {@link Launcher} to accept or block dpad-initiated long-presses.
1398 */
1399 public void setAllowLongPress(boolean allowLongPress) {
1400 mAllowLongPress = allowLongPress;
1401 }
1402
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001404 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001405
1406 SavedState(Parcelable superState) {
1407 super(superState);
1408 }
1409
1410 private SavedState(Parcel in) {
1411 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001412 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 }
1414
1415 @Override
1416 public void writeToParcel(Parcel out, int flags) {
1417 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001418 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 }
1420
1421 public static final Parcelable.Creator<SavedState> CREATOR =
1422 new Parcelable.Creator<SavedState>() {
1423 public SavedState createFromParcel(Parcel in) {
1424 return new SavedState(in);
1425 }
1426
1427 public SavedState[] newArray(int size) {
1428 return new SavedState[size];
1429 }
1430 };
1431 }
1432
Winson Chung86f77532010-08-24 11:08:22 -07001433 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001434 if (mContentIsRefreshable) {
1435 final int count = getChildCount();
1436 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001437 int lowerPageBound = getAssociatedLowerPageBound(page);
1438 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001439 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1440 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001441 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001442 Page layout = (Page) getChildAt(i);
1443 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001444 if (lowerPageBound <= i && i <= upperPageBound) {
1445 if (mDirtyPageContent.get(i)) {
1446 syncPageItems(i);
1447 mDirtyPageContent.set(i, false);
1448 }
1449 } else {
1450 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001451 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001452 }
1453 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001454 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001455 }
1456 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001457 }
1458 }
1459
Winson Chunge3193b92010-09-10 11:44:42 -07001460 protected int getAssociatedLowerPageBound(int page) {
1461 return Math.max(0, page - 1);
1462 }
1463 protected int getAssociatedUpperPageBound(int page) {
1464 final int count = getChildCount();
1465 return Math.min(page + 1, count - 1);
1466 }
1467
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001468 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001469 if (isChoiceMode(CHOICE_MODE_NONE)) {
1470 mChoiceMode = mode;
1471 mActionMode = startActionMode(callback);
1472 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001473 }
1474
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001475 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001476 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001477 mChoiceMode = CHOICE_MODE_NONE;
1478 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001479 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001480 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001481 }
1482 }
1483
1484 protected boolean isChoiceMode(int mode) {
1485 return mChoiceMode == mode;
1486 }
1487
1488 protected ArrayList<Checkable> getCheckedGrandchildren() {
1489 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1490 final int childCount = getChildCount();
1491 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001492 Page layout = (Page) getChildAt(i);
1493 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001494 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001495 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001496 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001497 checked.add((Checkable) v);
1498 }
1499 }
1500 }
1501 return checked;
1502 }
1503
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001504 /**
1505 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1506 * Otherwise, returns null.
1507 */
1508 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001509 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001510 final int childCount = getChildCount();
1511 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001512 Page layout = (Page) getChildAt(i);
1513 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001514 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001515 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001516 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1517 return (Checkable) v;
1518 }
1519 }
1520 }
1521 }
1522 return null;
1523 }
1524
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001525 protected void resetCheckedGrandchildren() {
1526 // loop through children, and set all of their children to _not_ be checked
1527 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1528 for (int i = 0; i < checked.size(); ++i) {
1529 final Checkable c = checked.get(i);
1530 c.setChecked(false);
1531 }
1532 }
1533
Winson Chunga12a2502010-12-20 14:41:35 -08001534 public void setRestorePage(int restorePage) {
1535 mRestorePage = restorePage;
1536 }
1537
Winson Chung86f77532010-08-24 11:08:22 -07001538 /**
1539 * This method is called ONLY to synchronize the number of pages that the paged view has.
1540 * To actually fill the pages with information, implement syncPageItems() below. It is
1541 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1542 * and therefore, individual page items do not need to be updated in this method.
1543 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001545
1546 /**
1547 * This method is called to synchronize the items that are on a particular page. If views on
1548 * the page can be reused, then they should be updated within this method.
1549 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001551
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001553 if (mContentIsRefreshable) {
1554 // Update all the pages
1555 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001556
Michael Jurka0142d492010-08-25 17:46:15 -07001557 // Mark each of the pages as dirty
1558 final int count = getChildCount();
1559 mDirtyPageContent.clear();
1560 for (int i = 0; i < count; ++i) {
1561 mDirtyPageContent.add(true);
1562 }
1563
Winson Chunga12a2502010-12-20 14:41:35 -08001564 // Use the restore page if necessary
1565 if (mRestorePage > -1) {
1566 mCurrentPage = mRestorePage;
1567 mRestorePage = -1;
1568 }
1569
Michael Jurka0142d492010-08-25 17:46:15 -07001570 // Load any pages that are necessary for the current window of views
1571 loadAssociatedPages(mCurrentPage);
1572 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001573 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001574 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001575 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001576 }
1577}