blob: 3bef69e66460a7a4eccd091407c06bca4c9f81d0 [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 Chunge3193b92010-09-10 11:44:42 -0700534 int halfScreenSize = getMeasuredWidth() / 2;
535 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700536 final int childCount = getChildCount();
537 for (int i = 0; i < childCount; ++i) {
538 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700540 int halfChildWidth = (childWidth / 2);
541 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700542
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700543 // On the first layout, we may not have a width nor a proper offset, so for now
544 // we should just assume full page width (and calculate the offset according to
545 // that).
546 if (childWidth <= 0) {
547 childWidth = getMeasuredWidth();
548 childCenter = (i * childWidth) + (childWidth / 2);
549 }
550
Winson Chunge8878e32010-09-15 20:37:09 -0700551 int d = halfChildWidth;
552 int distanceFromScreenCenter = childCenter - screenCenter;
553 if (distanceFromScreenCenter > 0) {
554 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700556 }
Michael Jurka0142d492010-08-25 17:46:15 -0700557 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700558 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700560 }
Michael Jurka0142d492010-08-25 17:46:15 -0700561 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700562 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700563
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700564 // Preventing potential divide-by-zero
565 d = Math.max(1, d);
566
Winson Chunge8878e32010-09-15 20:37:09 -0700567 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
568 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
569 float alpha = 1.0f - dimAlpha;
570
Adam Cohenf34bab52010-09-30 14:11:56 -0700571 if (alpha < ALPHA_QUANTIZE_LEVEL) {
572 alpha = 0.0f;
573 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
574 alpha = 1.0f;
575 }
576
Michael Jurkaa40829a2011-02-16 18:02:45 -0800577 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
578 // this optimization causes alpha to not be properly updated sometimes (repro
579 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
580 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
581 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800582 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700583 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800584 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700585 }
Michael Jurka0142d492010-08-25 17:46:15 -0700586 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700587 }
588 }
Winson Chunge3193b92010-09-10 11:44:42 -0700589 }
590
Adam Cohenf34bab52010-09-30 14:11:56 -0700591 protected void screenScrolled(int screenCenter) {
592 }
593
Winson Chunge3193b92010-09-10 11:44:42 -0700594 @Override
595 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700596 int halfScreenSize = getMeasuredWidth() / 2;
597 int screenCenter = mScrollX + halfScreenSize;
598
599 if (screenCenter != mLastScreenCenter) {
600 screenScrolled(screenCenter);
601 updateAdjacentPagesAlpha();
602 mLastScreenCenter = screenCenter;
603 }
Michael Jurka0142d492010-08-25 17:46:15 -0700604
605 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700606 // As an optimization, this code assumes that all pages have the same width as the 0th
607 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700608 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700609 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800610 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700611 final int screenWidth = getMeasuredWidth();
612 int x = getRelativeChildOffset(0) + pageWidth;
613 int leftScreen = 0;
614 int rightScreen = 0;
615 while (x <= mScrollX) {
616 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800617 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700618 }
619 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800620 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700621 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800622 if (rightScreen < pageCount) {
623 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
624 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700625 }
626 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700627
Michael Jurkac4fb9172010-09-02 17:19:20 -0700628 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800629 // Clip to the bounds
630 canvas.save();
631 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
632 mScrollY + mBottom - mTop);
633
Michael Jurkac4fb9172010-09-02 17:19:20 -0700634 for (int i = leftScreen; i <= rightScreen; i++) {
635 drawChild(canvas, getChildAt(i), drawingTime);
636 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800637 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700638 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700639 }
640
641 @Override
642 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700643 int page = indexOfChild(child);
644 if (page != mCurrentPage || !mScroller.isFinished()) {
645 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 return true;
647 }
648 return false;
649 }
650
651 @Override
652 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700653 int focusablePage;
654 if (mNextPage != INVALID_PAGE) {
655 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700657 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 }
Winson Chung86f77532010-08-24 11:08:22 -0700659 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 if (v != null) {
661 v.requestFocus(direction, previouslyFocusedRect);
662 }
663 return false;
664 }
665
666 @Override
667 public boolean dispatchUnhandledMove(View focused, int direction) {
668 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700669 if (getCurrentPage() > 0) {
670 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700671 return true;
672 }
673 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700674 if (getCurrentPage() < getPageCount() - 1) {
675 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 return true;
677 }
678 }
679 return super.dispatchUnhandledMove(focused, direction);
680 }
681
682 @Override
683 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700684 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
685 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 }
687 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700688 if (mCurrentPage > 0) {
689 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700690 }
691 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700692 if (mCurrentPage < getPageCount() - 1) {
693 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 }
695 }
696 }
697
698 /**
699 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700700 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 *
Winson Chung86f77532010-08-24 11:08:22 -0700702 * This happens when live folders requery, and if they're off page, they
703 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700704 */
705 @Override
706 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700707 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 View v = focused;
709 while (true) {
710 if (v == current) {
711 super.focusableViewAvailable(focused);
712 return;
713 }
714 if (v == this) {
715 return;
716 }
717 ViewParent parent = v.getParent();
718 if (parent instanceof View) {
719 v = (View)v.getParent();
720 } else {
721 return;
722 }
723 }
724 }
725
726 /**
727 * {@inheritDoc}
728 */
729 @Override
730 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
731 if (disallowIntercept) {
732 // We need to make sure to cancel our long press if
733 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700734 final View currentPage = getChildAt(mCurrentPage);
735 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 }
737 super.requestDisallowInterceptTouchEvent(disallowIntercept);
738 }
739
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800740 /**
741 * Return true if a tap at (x, y) should trigger a flip to the previous page.
742 */
743 protected boolean hitsPreviousPage(float x, float y) {
744 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
745 }
746
747 /**
748 * Return true if a tap at (x, y) should trigger a flip to the next page.
749 */
750 protected boolean hitsNextPage(float x, float y) {
751 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
752 }
753
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 @Override
755 public boolean onInterceptTouchEvent(MotionEvent ev) {
756 /*
757 * This method JUST determines whether we want to intercept the motion.
758 * If we return true, onTouchEvent will be called and we do the actual
759 * scrolling there.
760 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800761 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700762
Winson Chung45e1d6e2010-11-09 17:19:49 -0800763 // Skip touch handling if there are no pages to swipe
764 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
765
Winson Chung321e9ee2010-08-09 13:37:56 -0700766 /*
767 * Shortcut the most recurring case: the user is in the dragging
768 * state and he is moving his finger. We want to intercept this
769 * motion.
770 */
771 final int action = ev.getAction();
772 if ((action == MotionEvent.ACTION_MOVE) &&
773 (mTouchState == TOUCH_STATE_SCROLLING)) {
774 return true;
775 }
776
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 switch (action & MotionEvent.ACTION_MASK) {
778 case MotionEvent.ACTION_MOVE: {
779 /*
780 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
781 * whether the user has moved far enough from his original down touch.
782 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700783 if (mActivePointerId != INVALID_POINTER) {
784 determineScrollingStart(ev);
785 break;
786 }
787 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
788 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
789 // i.e. fall through to the next case (don't break)
790 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
791 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 }
793
794 case MotionEvent.ACTION_DOWN: {
795 final float x = ev.getX();
796 final float y = ev.getY();
797 // Remember location of down touch
798 mDownMotionX = x;
799 mLastMotionX = x;
800 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800801 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800802 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 mActivePointerId = ev.getPointerId(0);
804 mAllowLongPress = true;
805
806 /*
807 * If being flinged and user touches the screen, initiate drag;
808 * otherwise don't. mScroller.isFinished should be false when
809 * being flinged.
810 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700811 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700812 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
813 if (finishedScrolling) {
814 mTouchState = TOUCH_STATE_REST;
815 mScroller.abortAnimation();
816 } else {
817 mTouchState = TOUCH_STATE_SCROLLING;
818 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700819
Winson Chung86f77532010-08-24 11:08:22 -0700820 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800822 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800824 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700825 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800826 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 mTouchState = TOUCH_STATE_NEXT_PAGE;
828 }
829 }
830 }
831 break;
832 }
833
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800835 onWallpaperTap(ev);
836 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 mTouchState = TOUCH_STATE_REST;
838 mAllowLongPress = false;
839 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800840 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 break;
842
843 case MotionEvent.ACTION_POINTER_UP:
844 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800845 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 break;
847 }
848
849 /*
850 * The only time we want to intercept motion events is if we are in the
851 * drag mode.
852 */
853 return mTouchState != TOUCH_STATE_REST;
854 }
855
Winson Chung80baf5a2010-08-09 16:03:15 -0700856 protected void animateClickFeedback(View v, final Runnable r) {
857 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800858 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
859 loadAnimator(mContext, R.anim.paged_view_click_feedback);
860 anim.setTarget(v);
861 anim.addListener(new AnimatorListenerAdapter() {
862 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700863 r.run();
864 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700865 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800866 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700867 }
868
Adam Cohenf8d28232011-02-01 21:47:00 -0800869 protected void determineScrollingStart(MotionEvent ev) {
870 determineScrollingStart(ev, 1.0f);
871 }
872
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 /*
874 * Determines if we should change the touch state to start scrolling after the
875 * user moves their touch point too far.
876 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800877 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 /*
879 * Locally do absolute value. mLastMotionX is set to the y value
880 * of the down event.
881 */
882 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
883 final float x = ev.getX(pointerIndex);
884 final float y = ev.getY(pointerIndex);
885 final int xDiff = (int) Math.abs(x - mLastMotionX);
886 final int yDiff = (int) Math.abs(y - mLastMotionY);
887
Adam Cohenf8d28232011-02-01 21:47:00 -0800888 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 boolean xPaged = xDiff > mPagingTouchSlop;
890 boolean xMoved = xDiff > touchSlop;
891 boolean yMoved = yDiff > touchSlop;
892
Adam Cohenf8d28232011-02-01 21:47:00 -0800893 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700894 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 // Scroll if the user moved far enough along the X axis
896 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800897 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800899 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700900 mTouchX = mScrollX;
901 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
902 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 }
904 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800905 cancelCurrentPageLongPress();
906 }
907 }
908
909 protected void cancelCurrentPageLongPress() {
910 if (mAllowLongPress) {
911 mAllowLongPress = false;
912 // Try canceling the long press. It could also have been scheduled
913 // by a distant descendant, so use the mAllowLongPress flag to block
914 // everything
915 final View currentPage = getPageAt(mCurrentPage);
916 if (currentPage != null) {
917 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 }
919 }
920 }
921
Adam Cohene0f66b52010-11-23 15:06:07 -0800922 // This curve determines how the effect of scrolling over the limits of the page dimishes
923 // as the user pulls further and further from the bounds
924 private float overScrollInfluenceCurve(float f) {
925 f -= 1.0f;
926 return f * f * f + 1.0f;
927 }
928
Adam Cohen68d73932010-11-15 10:50:58 -0800929 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800930 int screenSize = getMeasuredWidth();
931
932 float f = (amount / screenSize);
933
934 if (f == 0) return;
935 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
936
Adam Cohen7bfc9792011-01-28 13:52:37 -0800937 // Clamp this factor, f, to -1 < f < 1
938 if (Math.abs(f) >= 1) {
939 f /= Math.abs(f);
940 }
941
Adam Cohene0f66b52010-11-23 15:06:07 -0800942 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800943 if (amount < 0) {
944 mScrollX = overScrollAmount;
945 } else {
946 mScrollX = mMaxScrollX + overScrollAmount;
947 }
948 invalidate();
949 }
950
Michael Jurkac5b262c2011-01-12 20:24:50 -0800951 protected float maxOverScroll() {
952 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
953 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
954 float f = 1.0f;
955 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
956 return OVERSCROLL_DAMP_FACTOR * f;
957 }
958
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 @Override
960 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800961 // Skip touch handling if there are no pages to swipe
962 if (getChildCount() <= 0) return super.onTouchEvent(ev);
963
Michael Jurkab8f06722010-10-10 15:58:46 -0700964 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700965
966 final int action = ev.getAction();
967
968 switch (action & MotionEvent.ACTION_MASK) {
969 case MotionEvent.ACTION_DOWN:
970 /*
971 * If being flinged and user touches, stop the fling. isFinished
972 * will be false if being flinged.
973 */
974 if (!mScroller.isFinished()) {
975 mScroller.abortAnimation();
976 }
977
978 // Remember where the motion event started
979 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800980 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800981 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700983 if (mTouchState == TOUCH_STATE_SCROLLING) {
984 pageBeginMoving();
985 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 break;
987
988 case MotionEvent.ACTION_MOVE:
989 if (mTouchState == TOUCH_STATE_SCROLLING) {
990 // Scroll to follow the motion event
991 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
992 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800993 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700994
Adam Cohenaefd4e12011-02-14 16:39:38 -0800995 mTotalMotionX += Math.abs(deltaX);
996
Winson Chungc0844aa2011-02-02 15:25:58 -0800997 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
998 // keep the remainder because we are actually testing if we've moved from the last
999 // scrolled position (which is discrete).
1000 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001001 mTouchX += deltaX;
1002 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1003 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001004 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001005 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001006 } else {
1007 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001009 mLastMotionX = x;
1010 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 } else {
1012 awakenScrollBars();
1013 }
Adam Cohen564976a2010-10-13 18:52:07 -07001014 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 determineScrollingStart(ev);
1016 }
1017 break;
1018
1019 case MotionEvent.ACTION_UP:
1020 if (mTouchState == TOUCH_STATE_SCROLLING) {
1021 final int activePointerId = mActivePointerId;
1022 final int pointerIndex = ev.findPointerIndex(activePointerId);
1023 final float x = ev.getX(pointerIndex);
1024 final VelocityTracker velocityTracker = mVelocityTracker;
1025 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1026 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001027 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001028 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001029 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001030
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001031 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1032
Adam Cohenaefd4e12011-02-14 16:39:38 -08001033 // In the case that the page is moved far to one direction and then is flung
1034 // in the opposite direction, we use a threshold to determine whether we should
1035 // just return to the starting page, or if we should skip one further.
1036 boolean returnToOriginalPage = false;
1037 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001038 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001039 Math.signum(velocityX) != Math.signum(deltaX)) {
1040 returnToOriginalPage = true;
1041 }
1042
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001043 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001044 Math.abs(velocityX) > snapVelocity;
1045
1046 int finalPage;
1047 // We give flings precedence over large moves, which is why we short-circuit our
1048 // test for a large move if a fling has been registered. That is, a large
1049 // move to the left and fling to the right will register as a fling to the right.
1050 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1051 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1052 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1053 snapToPageWithVelocity(finalPage, velocityX);
1054 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1055 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001056 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001057 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1058 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 } else {
1060 snapToDestination();
1061 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001062 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 // at this point we have not moved beyond the touch slop
1064 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1065 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001066 int nextPage = Math.max(0, mCurrentPage - 1);
1067 if (nextPage != mCurrentPage) {
1068 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 } else {
1070 snapToDestination();
1071 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001072 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 // at this point we have not moved beyond the touch slop
1074 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1075 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001076 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1077 if (nextPage != mCurrentPage) {
1078 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 } else {
1080 snapToDestination();
1081 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001082 } else {
1083 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 }
1085 mTouchState = TOUCH_STATE_REST;
1086 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001087 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001088 break;
1089
1090 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001091 if (mTouchState == TOUCH_STATE_SCROLLING) {
1092 snapToDestination();
1093 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 mTouchState = TOUCH_STATE_REST;
1095 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001096 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001097 break;
1098
1099 case MotionEvent.ACTION_POINTER_UP:
1100 onSecondaryPointerUp(ev);
1101 break;
1102 }
1103
1104 return true;
1105 }
1106
Winson Chung185d7162011-02-28 13:47:29 -08001107 @Override
1108 public boolean onGenericMotionEvent(MotionEvent event) {
1109 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1110 switch (event.getAction()) {
1111 case MotionEvent.ACTION_SCROLL: {
1112 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1113 final float vscroll;
1114 final float hscroll;
1115 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1116 vscroll = 0;
1117 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1118 } else {
1119 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1120 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1121 }
1122 if (hscroll != 0 || vscroll != 0) {
1123 if (hscroll > 0 || vscroll > 0) {
1124 scrollRight();
1125 } else {
1126 scrollLeft();
1127 }
1128 return true;
1129 }
1130 }
1131 }
1132 }
1133 return super.onGenericMotionEvent(event);
1134 }
1135
Michael Jurkab8f06722010-10-10 15:58:46 -07001136 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1137 if (mVelocityTracker == null) {
1138 mVelocityTracker = VelocityTracker.obtain();
1139 }
1140 mVelocityTracker.addMovement(ev);
1141 }
1142
1143 private void releaseVelocityTracker() {
1144 if (mVelocityTracker != null) {
1145 mVelocityTracker.recycle();
1146 mVelocityTracker = null;
1147 }
1148 }
1149
Winson Chung321e9ee2010-08-09 13:37:56 -07001150 private void onSecondaryPointerUp(MotionEvent ev) {
1151 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1152 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1153 final int pointerId = ev.getPointerId(pointerIndex);
1154 if (pointerId == mActivePointerId) {
1155 // This was our active pointer going up. Choose a new
1156 // active pointer and adjust accordingly.
1157 // TODO: Make this decision more intelligent.
1158 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1159 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1160 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001161 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 mActivePointerId = ev.getPointerId(newPointerIndex);
1163 if (mVelocityTracker != null) {
1164 mVelocityTracker.clear();
1165 }
1166 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001167 if (mTouchState == TOUCH_STATE_REST) {
1168 onWallpaperTap(ev);
1169 }
1170 }
1171
1172 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 }
1174
1175 @Override
1176 public void requestChildFocus(View child, View focused) {
1177 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001178 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001179 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001180 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 }
1182 }
1183
Winson Chunge3193b92010-09-10 11:44:42 -07001184 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1185 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001186 int left;
1187 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001188 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001189 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001190 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001191 if (left <= relativeOffset && relativeOffset <= right) {
1192 return i;
1193 }
Winson Chunge3193b92010-09-10 11:44:42 -07001194 }
1195 return -1;
1196 }
1197
Winson Chung1908d072011-02-24 18:09:44 -08001198 protected void setMinimumWidthOverride(int minimumWidth) {
1199 mMinimumWidth = minimumWidth;
1200 }
Winson Chung34b23d52011-03-18 11:29:34 -07001201 protected void resetMinimumWidthOverride() {
1202 mMinimumWidth = 0;
1203 }
Winson Chung1908d072011-02-24 18:09:44 -08001204
1205 protected int getChildWidth(int index) {
1206 return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth());
1207 }
1208
Winson Chung321e9ee2010-08-09 13:37:56 -07001209 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001210 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001211 }
1212
1213 protected int getChildOffset(int index) {
1214 if (getChildCount() == 0)
1215 return 0;
1216
1217 int offset = getRelativeChildOffset(0);
1218 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001219 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221 return offset;
1222 }
1223
Michael Jurkad3ef3062010-11-23 16:23:58 -08001224 protected int getScaledMeasuredWidth(View child) {
Winson Chung1908d072011-02-24 18:09:44 -08001225 return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001226 }
1227
Adam Cohend19d3ca2010-09-15 14:43:42 -07001228 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 int minDistanceFromScreenCenter = getMeasuredWidth();
1230 int minDistanceFromScreenCenterIndex = -1;
1231 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1232 final int childCount = getChildCount();
1233 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001234 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001235 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 int halfChildWidth = (childWidth / 2);
1237 int childCenter = getChildOffset(i) + halfChildWidth;
1238 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1239 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1240 minDistanceFromScreenCenter = distanceFromScreenCenter;
1241 minDistanceFromScreenCenterIndex = i;
1242 }
1243 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001244 return minDistanceFromScreenCenterIndex;
1245 }
1246
1247 protected void snapToDestination() {
1248 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 }
1250
Adam Cohene0f66b52010-11-23 15:06:07 -08001251 private static class ScrollInterpolator implements Interpolator {
1252 public ScrollInterpolator() {
1253 }
1254
1255 public float getInterpolation(float t) {
1256 t -= 1.0f;
1257 return t*t*t*t*t + 1;
1258 }
1259 }
1260
1261 // We want the duration of the page snap animation to be influenced by the distance that
1262 // the screen has to travel, however, we don't want this duration to be effected in a
1263 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1264 // of travel has on the overall snap duration.
1265 float distanceInfluenceForSnapDuration(float f) {
1266 f -= 0.5f; // center the values about 0.
1267 f *= 0.3f * Math.PI / 2.0f;
1268 return (float) Math.sin(f);
1269 }
1270
Michael Jurka0142d492010-08-25 17:46:15 -07001271 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001272 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1273 int halfScreenSize = getMeasuredWidth() / 2;
1274
Winson Chung785d2eb2011-04-14 16:08:02 -07001275 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1276 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1277 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001278 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1279 int delta = newX - mUnboundedScrollX;
1280 int duration = 0;
1281
1282 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1283 // If the velocity is low enough, then treat this more as an automatic page advance
1284 // as opposed to an apparent physical response to flinging
1285 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1286 return;
1287 }
1288
1289 // Here we compute a "distance" that will be used in the computation of the overall
1290 // snap duration. This is a function of the actual distance that needs to be traveled;
1291 // we keep this value close to half screen size in order to reduce the variance in snap
1292 // duration as a function of the distance the page needs to travel.
1293 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1294 float distance = halfScreenSize + halfScreenSize *
1295 distanceInfluenceForSnapDuration(distanceRatio);
1296
1297 velocity = Math.abs(velocity);
1298 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1299
1300 // we want the page's snap velocity to approximately match the velocity at which the
1301 // user flings, so we scale the duration by a value near to the derivative of the scroll
1302 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1303 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1304
1305 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001306 }
1307
1308 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001309 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001310 }
1311
Michael Jurka0142d492010-08-25 17:46:15 -07001312 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001313 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001314
Winson Chung785d2eb2011-04-14 16:08:02 -07001315 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1316 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1317 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001318 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001319 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001320 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001321 snapToPage(whichPage, delta, duration);
1322 }
1323
1324 protected void snapToPage(int whichPage, int delta, int duration) {
1325 mNextPage = whichPage;
1326
1327 View focusedChild = getFocusedChild();
1328 if (focusedChild != null && whichPage != mCurrentPage &&
1329 focusedChild == getChildAt(mCurrentPage)) {
1330 focusedChild.clearFocus();
1331 }
1332
1333 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001334 awakenScrollBars(duration);
1335 if (duration == 0) {
1336 duration = Math.abs(delta);
1337 }
1338
1339 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001340 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001341
1342 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001343 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001344 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 invalidate();
1346 }
1347
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 public void scrollLeft() {
1349 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001350 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001352 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 }
1354 }
1355
1356 public void scrollRight() {
1357 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001358 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001360 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 }
1362 }
1363
Winson Chung86f77532010-08-24 11:08:22 -07001364 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 int result = -1;
1366 if (v != null) {
1367 ViewParent vp = v.getParent();
1368 int count = getChildCount();
1369 for (int i = 0; i < count; i++) {
1370 if (vp == getChildAt(i)) {
1371 return i;
1372 }
1373 }
1374 }
1375 return result;
1376 }
1377
1378 /**
1379 * @return True is long presses are still allowed for the current touch
1380 */
1381 public boolean allowLongPress() {
1382 return mAllowLongPress;
1383 }
1384
Michael Jurka0142d492010-08-25 17:46:15 -07001385 /**
1386 * Set true to allow long-press events to be triggered, usually checked by
1387 * {@link Launcher} to accept or block dpad-initiated long-presses.
1388 */
1389 public void setAllowLongPress(boolean allowLongPress) {
1390 mAllowLongPress = allowLongPress;
1391 }
1392
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001394 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001395
1396 SavedState(Parcelable superState) {
1397 super(superState);
1398 }
1399
1400 private SavedState(Parcel in) {
1401 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001402 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 }
1404
1405 @Override
1406 public void writeToParcel(Parcel out, int flags) {
1407 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001408 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 }
1410
1411 public static final Parcelable.Creator<SavedState> CREATOR =
1412 new Parcelable.Creator<SavedState>() {
1413 public SavedState createFromParcel(Parcel in) {
1414 return new SavedState(in);
1415 }
1416
1417 public SavedState[] newArray(int size) {
1418 return new SavedState[size];
1419 }
1420 };
1421 }
1422
Winson Chung86f77532010-08-24 11:08:22 -07001423 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001424 if (mContentIsRefreshable) {
1425 final int count = getChildCount();
1426 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001427 int lowerPageBound = getAssociatedLowerPageBound(page);
1428 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001429 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1430 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001431 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001432 Page layout = (Page) getChildAt(i);
1433 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001434 if (lowerPageBound <= i && i <= upperPageBound) {
1435 if (mDirtyPageContent.get(i)) {
1436 syncPageItems(i);
1437 mDirtyPageContent.set(i, false);
1438 }
1439 } else {
1440 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001441 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001442 }
1443 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001444 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001445 }
1446 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001447 }
1448 }
1449
Winson Chunge3193b92010-09-10 11:44:42 -07001450 protected int getAssociatedLowerPageBound(int page) {
1451 return Math.max(0, page - 1);
1452 }
1453 protected int getAssociatedUpperPageBound(int page) {
1454 final int count = getChildCount();
1455 return Math.min(page + 1, count - 1);
1456 }
1457
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001458 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001459 if (isChoiceMode(CHOICE_MODE_NONE)) {
1460 mChoiceMode = mode;
1461 mActionMode = startActionMode(callback);
1462 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001463 }
1464
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001465 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001466 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001467 mChoiceMode = CHOICE_MODE_NONE;
1468 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001469 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001470 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001471 }
1472 }
1473
1474 protected boolean isChoiceMode(int mode) {
1475 return mChoiceMode == mode;
1476 }
1477
1478 protected ArrayList<Checkable> getCheckedGrandchildren() {
1479 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1480 final int childCount = getChildCount();
1481 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001482 Page layout = (Page) getChildAt(i);
1483 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001484 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001485 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001486 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001487 checked.add((Checkable) v);
1488 }
1489 }
1490 }
1491 return checked;
1492 }
1493
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001494 /**
1495 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1496 * Otherwise, returns null.
1497 */
1498 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001499 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001500 final int childCount = getChildCount();
1501 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001502 Page layout = (Page) getChildAt(i);
1503 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001504 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001505 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001506 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1507 return (Checkable) v;
1508 }
1509 }
1510 }
1511 }
1512 return null;
1513 }
1514
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001515 protected void resetCheckedGrandchildren() {
1516 // loop through children, and set all of their children to _not_ be checked
1517 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1518 for (int i = 0; i < checked.size(); ++i) {
1519 final Checkable c = checked.get(i);
1520 c.setChecked(false);
1521 }
1522 }
1523
Winson Chunga12a2502010-12-20 14:41:35 -08001524 public void setRestorePage(int restorePage) {
1525 mRestorePage = restorePage;
1526 }
1527
Winson Chung86f77532010-08-24 11:08:22 -07001528 /**
1529 * This method is called ONLY to synchronize the number of pages that the paged view has.
1530 * To actually fill the pages with information, implement syncPageItems() below. It is
1531 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1532 * and therefore, individual page items do not need to be updated in this method.
1533 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001535
1536 /**
1537 * This method is called to synchronize the items that are on a particular page. If views on
1538 * the page can be reused, then they should be updated within this method.
1539 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001541
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001543 if (mContentIsRefreshable) {
1544 // Update all the pages
1545 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001546
Michael Jurka0142d492010-08-25 17:46:15 -07001547 // Mark each of the pages as dirty
1548 final int count = getChildCount();
1549 mDirtyPageContent.clear();
1550 for (int i = 0; i < count; ++i) {
1551 mDirtyPageContent.add(true);
1552 }
1553
Winson Chunga12a2502010-12-20 14:41:35 -08001554 // Use the restore page if necessary
1555 if (mRestorePage > -1) {
1556 mCurrentPage = mRestorePage;
1557 mRestorePage = -1;
1558 }
1559
Michael Jurka0142d492010-08-25 17:46:15 -07001560 // Load any pages that are necessary for the current window of views
1561 loadAssociatedPages(mCurrentPage);
1562 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001563 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001564 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001565 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001566 }
1567}