blob: 9b3a339dff859159bed6fa8cf23e214772853e6f [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 Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
20import android.animation.AnimatorInflater;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070023import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070025import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.graphics.Canvas;
27import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070042import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070043import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070045import android.widget.Checkable;
Winson Chung007c6982011-06-14 13:27:53 -070046import android.widget.ImageView;
Winson Chung321e9ee2010-08-09 13:37:56 -070047import android.widget.Scroller;
48
Winson Chung04998342011-01-05 13:54:43 -080049import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070050
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
57public abstract class PagedView extends ViewGroup {
58 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chung86f77532010-08-24 11:08:22 -070062 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070063 private static final int MIN_LENGTH_FOR_FLING = 25;
64 // The min drag distance to trigger a page shift (regardless of velocity)
65 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Adam Cohene0f66b52010-11-23 15:06:07 -080067 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070068 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb5ba0972011-09-07 18:02:31 -070070 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070071 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080072 private static final int MINIMUM_SNAP_VELOCITY = 2200;
73 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Michael Jurka0142d492010-08-25 17:46:15 -070076 // the velocity at which a fling gesture will cause us to snap to the next page
77 protected int mSnapVelocity = 500;
78
Adam Cohenb5ba0972011-09-07 18:02:31 -070079 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected float mSmoothingTime;
81 protected float mTouchX;
82
83 protected boolean mFirstLayout = true;
84
85 protected int mCurrentPage;
86 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080087 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070089 private VelocityTracker mVelocityTracker;
90
91 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080092 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080093 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080094 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080095 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070096 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected final static int TOUCH_STATE_REST = 0;
99 protected final static int TOUCH_STATE_SCROLLING = 1;
100 protected final static int TOUCH_STATE_PREV_PAGE = 2;
101 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700102 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700105 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Michael Jurka7426c422010-11-11 15:23:47 -0800109 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Michael Jurka7426c422010-11-11 15:23:47 -0800111 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112 private int mPagingTouchSlop;
113 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800114 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700115 protected int mPageSpacing;
116 protected int mPageLayoutPaddingTop;
117 protected int mPageLayoutPaddingBottom;
118 protected int mPageLayoutPaddingLeft;
119 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700120 protected int mPageLayoutWidthGap;
121 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700122 protected int mCellCountX = 0;
123 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700124 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800125 protected boolean mAllowOverScroll = true;
126 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka8c920dd2011-01-20 14:16:56 -0800128 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800129 protected float mLayoutScale = 1.0f;
130
Michael Jurka5f1c5092010-09-03 14:15:02 -0700131 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Michael Jurka5f1c5092010-09-03 14:15:02 -0700133 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Winson Chung86f77532010-08-24 11:08:22 -0700135 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Winson Chung86f77532010-08-24 11:08:22 -0700137 private ArrayList<Boolean> mDirtyPageContent;
Michael Jurka86c119a2011-08-05 20:35:36 -0700138 private boolean mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700140 // choice modes
141 protected static final int CHOICE_MODE_NONE = 0;
142 protected static final int CHOICE_MODE_SINGLE = 1;
143 // Multiple selection mode is not supported by all Launcher actions atm
144 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700145
Michael Jurkae17e19c2010-09-28 11:01:39 -0700146 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700147 private ActionMode mActionMode;
148
Michael Jurka0142d492010-08-25 17:46:15 -0700149 // If true, syncPages and syncPageItems will be called to refresh pages
150 protected boolean mContentIsRefreshable = true;
151
152 // If true, modify alpha of neighboring pages as user scrolls left/right
153 protected boolean mFadeInAdjacentScreens = true;
154
155 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
156 // to switch to a new page
157 protected boolean mUsePagingTouchSlop = true;
158
159 // If true, the subclass should directly update mScrollX itself in its computeScroll method
160 // (SmoothPagedView does this)
161 protected boolean mDeferScrollUpdate = false;
162
Patrick Dubroy1262e362010-10-06 15:49:50 -0700163 protected boolean mIsPageMoving = false;
164
Winson Chungf0ea4d32011-06-06 14:27:16 -0700165 // All syncs and layout passes are deferred until data is ready.
166 protected boolean mIsDataReady = false;
167
Winson Chung007c6982011-06-14 13:27:53 -0700168 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700169 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700170 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700171 private int mScrollIndicatorPaddingLeft;
172 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700173 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700174 protected static final int sScrollIndicatorFadeInDuration = 150;
175 protected static final int sScrollIndicatorFadeOutDuration = 650;
176 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700177
Winson Chungb44b5242011-06-13 11:32:14 -0700178 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700179 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700180
Winson Chung86f77532010-08-24 11:08:22 -0700181 public interface PageSwitchListener {
182 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700183 }
184
Winson Chung321e9ee2010-08-09 13:37:56 -0700185 public PagedView(Context context) {
186 this(context, null);
187 }
188
189 public PagedView(Context context, AttributeSet attrs) {
190 this(context, attrs, 0);
191 }
192
193 public PagedView(Context context, AttributeSet attrs, int defStyle) {
194 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700195 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700196
Adam Cohen9c4949e2010-10-05 12:27:22 -0700197 TypedArray a = context.obtainStyledAttributes(attrs,
198 R.styleable.PagedView, defStyle, 0);
199 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
200 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800201 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800203 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800205 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800207 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700208 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700209 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700210 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700211 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700212 mScrollIndicatorPaddingLeft =
213 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
214 mScrollIndicatorPaddingRight =
215 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700216 a.recycle();
217
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700219 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 }
221
222 /**
223 * Initializes various states for this workspace.
224 */
Michael Jurka0142d492010-08-25 17:46:15 -0700225 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700226 mDirtyPageContent = new ArrayList<Boolean>();
227 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800228 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700229 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700230 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700231
232 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
233 mTouchSlop = configuration.getScaledTouchSlop();
234 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
235 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700236 mDensity = getResources().getDisplayMetrics().density;
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 }
238
Winson Chung86f77532010-08-24 11:08:22 -0700239 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
240 mPageSwitchListener = pageSwitchListener;
241 if (mPageSwitchListener != null) {
242 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 }
244 }
245
246 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700247 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
248 * out pages.
249 */
250 protected void setDataIsReady() {
251 mIsDataReady = true;
252 }
253 protected boolean isDataReady() {
254 return mIsDataReady;
255 }
256
257 /**
Winson Chung86f77532010-08-24 11:08:22 -0700258 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 *
Winson Chung86f77532010-08-24 11:08:22 -0700260 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700261 */
Winson Chung86f77532010-08-24 11:08:22 -0700262 int getCurrentPage() {
263 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 }
265
Winson Chung86f77532010-08-24 11:08:22 -0700266 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 return getChildCount();
268 }
269
Winson Chung86f77532010-08-24 11:08:22 -0700270 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 return getChildAt(index);
272 }
273
Adam Cohenae4f1552011-10-20 00:15:42 -0700274 protected int indexToPage(int index) {
275 return index;
276 }
277
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800279 * Updates the scroll of the current page immediately to its final scroll position. We use this
280 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
281 * the previous tab page.
282 */
283 protected void updateCurrentPageScroll() {
284 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
285 scrollTo(newX, 0);
286 mScroller.setFinalX(newX);
287 }
288
289 /**
Winson Chung86f77532010-08-24 11:08:22 -0700290 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 */
Winson Chung86f77532010-08-24 11:08:22 -0700292 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800293 if (!mScroller.isFinished()) {
294 mScroller.abortAnimation();
295 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800296 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
297 // the default
298 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800299 return;
300 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700301
Winson Chung86f77532010-08-24 11:08:22 -0700302 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800303 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700304 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700305 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800306 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 }
308
Michael Jurka0142d492010-08-25 17:46:15 -0700309 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700310 if (mPageSwitchListener != null) {
311 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 }
313 }
314
Michael Jurkace7e05f2011-02-01 22:02:35 -0800315 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700316 if (!mIsPageMoving) {
317 mIsPageMoving = true;
318 onPageBeginMoving();
319 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700320 }
321
Michael Jurkace7e05f2011-02-01 22:02:35 -0800322 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700323 if (mIsPageMoving) {
324 mIsPageMoving = false;
325 onPageEndMoving();
326 }
Michael Jurka0142d492010-08-25 17:46:15 -0700327 }
328
Adam Cohen26976d92011-03-22 15:33:33 -0700329 protected boolean isPageMoving() {
330 return mIsPageMoving;
331 }
332
Michael Jurka0142d492010-08-25 17:46:15 -0700333 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700334 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700335 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700336 }
337
338 // a method that subclasses can override to add behavior
339 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700340 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700341 }
342
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 /**
Winson Chung86f77532010-08-24 11:08:22 -0700344 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 *
346 * @param l The listener used to respond to long clicks.
347 */
348 @Override
349 public void setOnLongClickListener(OnLongClickListener l) {
350 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700351 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700353 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 }
355 }
356
357 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800358 public void scrollBy(int x, int y) {
359 scrollTo(mUnboundedScrollX + x, mScrollY + y);
360 }
361
362 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700363 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800364 mUnboundedScrollX = x;
365
366 if (x < 0) {
367 super.scrollTo(0, y);
368 if (mAllowOverScroll) {
369 overScroll(x);
370 }
371 } else if (x > mMaxScrollX) {
372 super.scrollTo(mMaxScrollX, y);
373 if (mAllowOverScroll) {
374 overScroll(x - mMaxScrollX);
375 }
376 } else {
377 super.scrollTo(x, y);
378 }
379
Michael Jurka0142d492010-08-25 17:46:15 -0700380 mTouchX = x;
381 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
382 }
383
384 // we moved this functionality to a helper function so SmoothPagedView can reuse it
385 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700387 // Don't bother scrolling if the page does not need to be moved
388 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
389 mDirtyPageAlpha = true;
390 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
391 }
Michael Jurka0142d492010-08-25 17:46:15 -0700392 invalidate();
393 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700394 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700395 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700396 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700397 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700398 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700399
400 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700401 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700402 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700403 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700404 }
405
Adam Cohen73aa9752010-11-24 16:26:58 -0800406 // We don't want to trigger a page end moving unless the page has settled
407 // and the user has stopped scrolling
408 if (mTouchState == TOUCH_STATE_REST) {
409 pageEndMoving();
410 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700411
412 // Notify the user when the page changes
413 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
414 AccessibilityEvent ev =
415 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
416 ev.getText().add(getCurrentPageDescription());
417 sendAccessibilityEventUnchecked(ev);
418 }
Michael Jurka0142d492010-08-25 17:46:15 -0700419 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700420 }
Michael Jurka0142d492010-08-25 17:46:15 -0700421 return false;
422 }
423
424 @Override
425 public void computeScroll() {
426 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
428
429 @Override
430 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700431 if (!mIsDataReady) {
432 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
433 return;
434 }
435
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
437 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
438 if (widthMode != MeasureSpec.EXACTLY) {
439 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
440 }
441
Adam Lesinski6b879f02010-11-04 16:15:23 -0700442 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
443 * of the All apps view on XLarge displays to not take up more space then it needs. Width
444 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
445 * each page to have the same width.
446 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700448 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
449 int maxChildHeight = 0;
450
451 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700452 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700453
Michael Jurka36fcb742011-04-06 17:08:58 -0700454
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700456 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700457 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 final int childCount = getChildCount();
459 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700460 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700461 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700462 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
463
464 int childWidthMode;
465 if (lp.width == LayoutParams.WRAP_CONTENT) {
466 childWidthMode = MeasureSpec.AT_MOST;
467 } else {
468 childWidthMode = MeasureSpec.EXACTLY;
469 }
470
471 int childHeightMode;
472 if (lp.height == LayoutParams.WRAP_CONTENT) {
473 childHeightMode = MeasureSpec.AT_MOST;
474 } else {
475 childHeightMode = MeasureSpec.EXACTLY;
476 }
477
478 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700479 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700480 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700481 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700482
483 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700484 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700485 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
486 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700487 }
488
489 if (heightMode == MeasureSpec.AT_MOST) {
490 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 }
Winson Chungae890b82011-09-13 18:08:54 -0700492
493 updateScrollingIndicatorPosition();
494
495 setMeasuredDimension(widthSize, heightSize);
496
497 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions
Adam Cohenfaa28302010-11-19 12:02:18 -0800498 if (childCount > 0) {
499 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
500 } else {
501 mMaxScrollX = 0;
502 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 }
504
Michael Jurka8c920dd2011-01-20 14:16:56 -0800505 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800506 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
507 int delta = newX - mScrollX;
508
Michael Jurka8c920dd2011-01-20 14:16:56 -0800509 final int pageCount = getChildCount();
510 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700511 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800512 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800513 }
514 setCurrentPage(newCurrentPage);
515 }
516
Michael Jurka8c920dd2011-01-20 14:16:56 -0800517 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
518 // 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 -0800519 // tightens the layout accordingly
520 public void setLayoutScale(float childrenScale) {
521 mLayoutScale = childrenScale;
522
523 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
524 int childCount = getChildCount();
525 float childrenX[] = new float[childCount];
526 float childrenY[] = new float[childCount];
527 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700528 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800529 childrenX[i] = child.getX();
530 childrenY[i] = child.getY();
531 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700532 // Trigger a full re-layout (never just call onLayout directly!)
533 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
534 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
535 requestLayout();
536 measure(widthSpec, heightSpec);
537 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800538 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700539 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800540 child.setX(childrenX[i]);
541 child.setY(childrenY[i]);
542 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700543
Michael Jurkad3ef3062010-11-23 16:23:58 -0800544 // Also, the page offset has changed (since the pages are now smaller);
545 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800546 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800547 }
548
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700550 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700551 if (!mIsDataReady) {
552 return;
553 }
554
Winson Chung785d2eb2011-04-14 16:08:02 -0700555 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700556 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 final int childCount = getChildCount();
558 int childLeft = 0;
559 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700560 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
561 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700562 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700563
564 // Calculate the variable page spacing if necessary
565 if (mPageSpacing < 0) {
566 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
567 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569
570 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700571 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800573 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700574 final int childHeight = child.getMeasuredHeight();
575 int childTop = mPaddingTop;
576 if (mCenterPagesVertically) {
577 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
578 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800579
Winson Chung785d2eb2011-04-14 16:08:02 -0700580 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700581 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800582 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700583 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 }
585 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700586
587 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
588 setHorizontalScrollBarEnabled(false);
589 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
590 scrollTo(newX, 0);
591 mScroller.setFinalX(newX);
592 setHorizontalScrollBarEnabled(true);
593 mFirstLayout = false;
594 }
595
Michael Jurkad3ef3062010-11-23 16:23:58 -0800596 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
597 mFirstLayout = false;
598 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
600
Winson Chung03929772011-02-23 17:07:10 -0800601 protected void forceUpdateAdjacentPagesAlpha() {
602 mDirtyPageAlpha = true;
603 updateAdjacentPagesAlpha();
604 }
605
Winson Chunge3193b92010-09-10 11:44:42 -0700606 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700607 if (mFadeInAdjacentScreens) {
608 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700609 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700610 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700611 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700612 final int childCount = getChildCount();
613 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700614 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800615 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700616 int halfChildWidth = (childWidth / 2);
617 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700618
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700619 // On the first layout, we may not have a width nor a proper offset, so for now
620 // we should just assume full page width (and calculate the offset according to
621 // that).
622 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700623 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700624 childCenter = (i * childWidth) + (childWidth / 2);
625 }
626
Winson Chunge8878e32010-09-15 20:37:09 -0700627 int d = halfChildWidth;
628 int distanceFromScreenCenter = childCenter - screenCenter;
629 if (distanceFromScreenCenter > 0) {
630 if (i > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700631 d += getScaledMeasuredWidth(getPageAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700632 } else {
633 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700634 }
Michael Jurka0142d492010-08-25 17:46:15 -0700635 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700636 if (i < childCount - 1) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700637 d += getScaledMeasuredWidth(getPageAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700638 } else {
639 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700640 }
Michael Jurka0142d492010-08-25 17:46:15 -0700641 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700642 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700643
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700644 // Preventing potential divide-by-zero
645 d = Math.max(1, d);
646
Winson Chunge8878e32010-09-15 20:37:09 -0700647 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
648 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
649 float alpha = 1.0f - dimAlpha;
650
Adam Cohenf34bab52010-09-30 14:11:56 -0700651 if (alpha < ALPHA_QUANTIZE_LEVEL) {
652 alpha = 0.0f;
653 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
654 alpha = 1.0f;
655 }
656
Michael Jurkaa40829a2011-02-16 18:02:45 -0800657 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
658 // this optimization causes alpha to not be properly updated sometimes (repro
659 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
660 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
661 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800662 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700663 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800664 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 }
Michael Jurka0142d492010-08-25 17:46:15 -0700666 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 }
668 }
Winson Chunge3193b92010-09-10 11:44:42 -0700669 }
670
Adam Cohenf34bab52010-09-30 14:11:56 -0700671 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700672 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700673 }
674
Winson Chunge3193b92010-09-10 11:44:42 -0700675 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700676 protected void onViewAdded(View child) {
677 super.onViewAdded(child);
678
679 // This ensures that when children are added, they get the correct transforms / alphas
680 // in accordance with any scroll effects.
681 mForceScreenScrolled = true;
682 invalidate();
683 }
684
685 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700686 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700687 int halfScreenSize = getMeasuredWidth() / 2;
688 int screenCenter = mScrollX + halfScreenSize;
689
Adam Cohen2591f6a2011-10-25 14:36:40 -0700690 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700691 screenScrolled(screenCenter);
692 updateAdjacentPagesAlpha();
693 mLastScreenCenter = screenCenter;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700694 mForceScreenScrolled = false;
Adam Cohenf34bab52010-09-30 14:11:56 -0700695 }
Michael Jurka0142d492010-08-25 17:46:15 -0700696
697 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700698 // As an optimization, this code assumes that all pages have the same width as the 0th
699 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700700 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700701 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700702 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700703 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700704 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700705 int leftScreen = 0;
706 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700707 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700708 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700709 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700710 }
711 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700712 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700713 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700714 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700715 }
Michael Jurka0142d492010-08-25 17:46:15 -0700716
Michael Jurkac4fb9172010-09-02 17:19:20 -0700717 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800718 // Clip to the bounds
719 canvas.save();
720 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
721 mScrollY + mBottom - mTop);
722
Adam Cohen22f823d2011-09-01 17:22:18 -0700723 for (int i = rightScreen; i >= leftScreen; i--) {
724 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800726 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700727 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700728 }
729
730 @Override
731 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700732 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700733 if (page != mCurrentPage || !mScroller.isFinished()) {
734 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 return true;
736 }
737 return false;
738 }
739
740 @Override
741 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700742 int focusablePage;
743 if (mNextPage != INVALID_PAGE) {
744 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700746 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 }
Winson Chung86f77532010-08-24 11:08:22 -0700748 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700749 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700750 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752 return false;
753 }
754
755 @Override
756 public boolean dispatchUnhandledMove(View focused, int direction) {
757 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700758 if (getCurrentPage() > 0) {
759 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 return true;
761 }
762 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700763 if (getCurrentPage() < getPageCount() - 1) {
764 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 return true;
766 }
767 }
768 return super.dispatchUnhandledMove(focused, direction);
769 }
770
771 @Override
772 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700773 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
774 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 }
776 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700777 if (mCurrentPage > 0) {
778 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 }
780 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700781 if (mCurrentPage < getPageCount() - 1) {
782 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 }
784 }
785 }
786
787 /**
788 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700789 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 *
Winson Chung86f77532010-08-24 11:08:22 -0700791 * This happens when live folders requery, and if they're off page, they
792 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 */
794 @Override
795 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700796 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 View v = focused;
798 while (true) {
799 if (v == current) {
800 super.focusableViewAvailable(focused);
801 return;
802 }
803 if (v == this) {
804 return;
805 }
806 ViewParent parent = v.getParent();
807 if (parent instanceof View) {
808 v = (View)v.getParent();
809 } else {
810 return;
811 }
812 }
813 }
814
815 /**
816 * {@inheritDoc}
817 */
818 @Override
819 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
820 if (disallowIntercept) {
821 // We need to make sure to cancel our long press if
822 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700823 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700824 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700825 }
826 super.requestDisallowInterceptTouchEvent(disallowIntercept);
827 }
828
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800829 /**
830 * Return true if a tap at (x, y) should trigger a flip to the previous page.
831 */
832 protected boolean hitsPreviousPage(float x, float y) {
833 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
834 }
835
836 /**
837 * Return true if a tap at (x, y) should trigger a flip to the next page.
838 */
839 protected boolean hitsNextPage(float x, float y) {
840 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
841 }
842
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 @Override
844 public boolean onInterceptTouchEvent(MotionEvent ev) {
845 /*
846 * This method JUST determines whether we want to intercept the motion.
847 * If we return true, onTouchEvent will be called and we do the actual
848 * scrolling there.
849 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800850 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851
Winson Chung45e1d6e2010-11-09 17:19:49 -0800852 // Skip touch handling if there are no pages to swipe
853 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
854
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 /*
856 * Shortcut the most recurring case: the user is in the dragging
857 * state and he is moving his finger. We want to intercept this
858 * motion.
859 */
860 final int action = ev.getAction();
861 if ((action == MotionEvent.ACTION_MOVE) &&
862 (mTouchState == TOUCH_STATE_SCROLLING)) {
863 return true;
864 }
865
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 switch (action & MotionEvent.ACTION_MASK) {
867 case MotionEvent.ACTION_MOVE: {
868 /*
869 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
870 * whether the user has moved far enough from his original down touch.
871 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700872 if (mActivePointerId != INVALID_POINTER) {
873 determineScrollingStart(ev);
874 break;
875 }
876 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
877 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
878 // i.e. fall through to the next case (don't break)
879 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
880 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 }
882
883 case MotionEvent.ACTION_DOWN: {
884 final float x = ev.getX();
885 final float y = ev.getY();
886 // Remember location of down touch
887 mDownMotionX = x;
888 mLastMotionX = x;
889 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800890 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800891 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 mActivePointerId = ev.getPointerId(0);
893 mAllowLongPress = true;
894
895 /*
896 * If being flinged and user touches the screen, initiate drag;
897 * otherwise don't. mScroller.isFinished should be false when
898 * being flinged.
899 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700900 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700901 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
902 if (finishedScrolling) {
903 mTouchState = TOUCH_STATE_REST;
904 mScroller.abortAnimation();
905 } else {
906 mTouchState = TOUCH_STATE_SCROLLING;
907 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700908
Winson Chung86f77532010-08-24 11:08:22 -0700909 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800911 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800913 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800915 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 mTouchState = TOUCH_STATE_NEXT_PAGE;
917 }
918 }
919 }
920 break;
921 }
922
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800924 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 mTouchState = TOUCH_STATE_REST;
926 mAllowLongPress = false;
927 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800928 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 break;
930
931 case MotionEvent.ACTION_POINTER_UP:
932 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800933 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 break;
935 }
936
937 /*
938 * The only time we want to intercept motion events is if we are in the
939 * drag mode.
940 */
941 return mTouchState != TOUCH_STATE_REST;
942 }
943
Winson Chung80baf5a2010-08-09 16:03:15 -0700944 protected void animateClickFeedback(View v, final Runnable r) {
945 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800946 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
947 loadAnimator(mContext, R.anim.paged_view_click_feedback);
948 anim.setTarget(v);
949 anim.addListener(new AnimatorListenerAdapter() {
950 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700951 r.run();
952 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700953 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800954 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700955 }
956
Adam Cohenf8d28232011-02-01 21:47:00 -0800957 protected void determineScrollingStart(MotionEvent ev) {
958 determineScrollingStart(ev, 1.0f);
959 }
960
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 /*
962 * Determines if we should change the touch state to start scrolling after the
963 * user moves their touch point too far.
964 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800965 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 /*
967 * Locally do absolute value. mLastMotionX is set to the y value
968 * of the down event.
969 */
970 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700971 if (pointerIndex == -1) {
972 return;
973 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 final float x = ev.getX(pointerIndex);
975 final float y = ev.getY(pointerIndex);
976 final int xDiff = (int) Math.abs(x - mLastMotionX);
977 final int yDiff = (int) Math.abs(y - mLastMotionY);
978
Adam Cohenf8d28232011-02-01 21:47:00 -0800979 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 boolean xPaged = xDiff > mPagingTouchSlop;
981 boolean xMoved = xDiff > touchSlop;
982 boolean yMoved = yDiff > touchSlop;
983
Adam Cohenf8d28232011-02-01 21:47:00 -0800984 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700985 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 // Scroll if the user moved far enough along the X axis
987 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800988 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800990 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700991 mTouchX = mScrollX;
992 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
993 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 }
995 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800996 cancelCurrentPageLongPress();
997 }
998 }
999
1000 protected void cancelCurrentPageLongPress() {
1001 if (mAllowLongPress) {
1002 mAllowLongPress = false;
1003 // Try canceling the long press. It could also have been scheduled
1004 // by a distant descendant, so use the mAllowLongPress flag to block
1005 // everything
1006 final View currentPage = getPageAt(mCurrentPage);
1007 if (currentPage != null) {
1008 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 }
1010 }
1011 }
1012
Adam Cohenb5ba0972011-09-07 18:02:31 -07001013 protected float getScrollProgress(int screenCenter, View v, int page) {
1014 final int halfScreenSize = getMeasuredWidth() / 2;
1015
1016 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1017 int delta = screenCenter - (getChildOffset(page) -
1018 getRelativeChildOffset(page) + halfScreenSize);
1019
1020 float scrollProgress = delta / (totalDistance * 1.0f);
1021 scrollProgress = Math.min(scrollProgress, 1.0f);
1022 scrollProgress = Math.max(scrollProgress, -1.0f);
1023 return scrollProgress;
1024 }
1025
Adam Cohene0f66b52010-11-23 15:06:07 -08001026 // This curve determines how the effect of scrolling over the limits of the page dimishes
1027 // as the user pulls further and further from the bounds
1028 private float overScrollInfluenceCurve(float f) {
1029 f -= 1.0f;
1030 return f * f * f + 1.0f;
1031 }
1032
Adam Cohenb5ba0972011-09-07 18:02:31 -07001033 protected void acceleratedOverScroll(float amount) {
1034 int screenSize = getMeasuredWidth();
1035
1036 // We want to reach the max over scroll effect when the user has
1037 // over scrolled half the size of the screen
1038 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1039
1040 if (f == 0) return;
1041
1042 // Clamp this factor, f, to -1 < f < 1
1043 if (Math.abs(f) >= 1) {
1044 f /= Math.abs(f);
1045 }
1046
1047 int overScrollAmount = (int) Math.round(f * screenSize);
1048 if (amount < 0) {
1049 mScrollX = overScrollAmount;
1050 } else {
1051 mScrollX = mMaxScrollX + overScrollAmount;
1052 }
1053 invalidate();
1054 }
1055
1056 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001057 int screenSize = getMeasuredWidth();
1058
1059 float f = (amount / screenSize);
1060
1061 if (f == 0) return;
1062 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1063
Adam Cohen7bfc9792011-01-28 13:52:37 -08001064 // Clamp this factor, f, to -1 < f < 1
1065 if (Math.abs(f) >= 1) {
1066 f /= Math.abs(f);
1067 }
1068
Adam Cohene0f66b52010-11-23 15:06:07 -08001069 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001070 if (amount < 0) {
1071 mScrollX = overScrollAmount;
1072 } else {
1073 mScrollX = mMaxScrollX + overScrollAmount;
1074 }
1075 invalidate();
1076 }
1077
Adam Cohenb5ba0972011-09-07 18:02:31 -07001078 protected void overScroll(float amount) {
1079 dampedOverScroll(amount);
1080 }
1081
Michael Jurkac5b262c2011-01-12 20:24:50 -08001082 protected float maxOverScroll() {
1083 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001084 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001085 float f = 1.0f;
1086 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1087 return OVERSCROLL_DAMP_FACTOR * f;
1088 }
1089
Winson Chung321e9ee2010-08-09 13:37:56 -07001090 @Override
1091 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001092 // Skip touch handling if there are no pages to swipe
1093 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1094
Michael Jurkab8f06722010-10-10 15:58:46 -07001095 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001096
1097 final int action = ev.getAction();
1098
1099 switch (action & MotionEvent.ACTION_MASK) {
1100 case MotionEvent.ACTION_DOWN:
1101 /*
1102 * If being flinged and user touches, stop the fling. isFinished
1103 * will be false if being flinged.
1104 */
1105 if (!mScroller.isFinished()) {
1106 mScroller.abortAnimation();
1107 }
1108
1109 // Remember where the motion event started
1110 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001111 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001112 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001113 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001114 if (mTouchState == TOUCH_STATE_SCROLLING) {
1115 pageBeginMoving();
1116 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 break;
1118
1119 case MotionEvent.ACTION_MOVE:
1120 if (mTouchState == TOUCH_STATE_SCROLLING) {
1121 // Scroll to follow the motion event
1122 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1123 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001124 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001125
Adam Cohenaefd4e12011-02-14 16:39:38 -08001126 mTotalMotionX += Math.abs(deltaX);
1127
Winson Chungc0844aa2011-02-02 15:25:58 -08001128 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1129 // keep the remainder because we are actually testing if we've moved from the last
1130 // scrolled position (which is discrete).
1131 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001132 mTouchX += deltaX;
1133 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1134 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001135 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001137 } else {
1138 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001139 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001140 mLastMotionX = x;
1141 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 } else {
1143 awakenScrollBars();
1144 }
Adam Cohen564976a2010-10-13 18:52:07 -07001145 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001146 determineScrollingStart(ev);
1147 }
1148 break;
1149
1150 case MotionEvent.ACTION_UP:
1151 if (mTouchState == TOUCH_STATE_SCROLLING) {
1152 final int activePointerId = mActivePointerId;
1153 final int pointerIndex = ev.findPointerIndex(activePointerId);
1154 final float x = ev.getX(pointerIndex);
1155 final VelocityTracker velocityTracker = mVelocityTracker;
1156 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1157 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001158 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001159 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001160 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001161
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001162 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1163
Adam Cohenaefd4e12011-02-14 16:39:38 -08001164 // In the case that the page is moved far to one direction and then is flung
1165 // in the opposite direction, we use a threshold to determine whether we should
1166 // just return to the starting page, or if we should skip one further.
1167 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001168 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001169 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001170 Math.signum(velocityX) != Math.signum(deltaX)) {
1171 returnToOriginalPage = true;
1172 }
1173
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001174 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001175 Math.abs(velocityX) > snapVelocity;
1176
1177 int finalPage;
1178 // We give flings precedence over large moves, which is why we short-circuit our
1179 // test for a large move if a fling has been registered. That is, a large
1180 // move to the left and fling to the right will register as a fling to the right.
1181 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1182 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1183 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1184 snapToPageWithVelocity(finalPage, velocityX);
1185 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1186 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001187 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001188 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1189 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 } else {
1191 snapToDestination();
1192 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001193 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 // at this point we have not moved beyond the touch slop
1195 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1196 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001197 int nextPage = Math.max(0, mCurrentPage - 1);
1198 if (nextPage != mCurrentPage) {
1199 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 } else {
1201 snapToDestination();
1202 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001203 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 // at this point we have not moved beyond the touch slop
1205 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1206 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001207 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1208 if (nextPage != mCurrentPage) {
1209 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 } else {
1211 snapToDestination();
1212 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001213 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001214 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
1216 mTouchState = TOUCH_STATE_REST;
1217 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001218 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 break;
1220
1221 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001222 if (mTouchState == TOUCH_STATE_SCROLLING) {
1223 snapToDestination();
1224 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 mTouchState = TOUCH_STATE_REST;
1226 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001227 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 break;
1229
1230 case MotionEvent.ACTION_POINTER_UP:
1231 onSecondaryPointerUp(ev);
1232 break;
1233 }
1234
1235 return true;
1236 }
1237
Winson Chung185d7162011-02-28 13:47:29 -08001238 @Override
1239 public boolean onGenericMotionEvent(MotionEvent event) {
1240 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1241 switch (event.getAction()) {
1242 case MotionEvent.ACTION_SCROLL: {
1243 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1244 final float vscroll;
1245 final float hscroll;
1246 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1247 vscroll = 0;
1248 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1249 } else {
1250 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1251 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1252 }
1253 if (hscroll != 0 || vscroll != 0) {
1254 if (hscroll > 0 || vscroll > 0) {
1255 scrollRight();
1256 } else {
1257 scrollLeft();
1258 }
1259 return true;
1260 }
1261 }
1262 }
1263 }
1264 return super.onGenericMotionEvent(event);
1265 }
1266
Michael Jurkab8f06722010-10-10 15:58:46 -07001267 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1268 if (mVelocityTracker == null) {
1269 mVelocityTracker = VelocityTracker.obtain();
1270 }
1271 mVelocityTracker.addMovement(ev);
1272 }
1273
1274 private void releaseVelocityTracker() {
1275 if (mVelocityTracker != null) {
1276 mVelocityTracker.recycle();
1277 mVelocityTracker = null;
1278 }
1279 }
1280
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 private void onSecondaryPointerUp(MotionEvent ev) {
1282 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1283 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1284 final int pointerId = ev.getPointerId(pointerIndex);
1285 if (pointerId == mActivePointerId) {
1286 // This was our active pointer going up. Choose a new
1287 // active pointer and adjust accordingly.
1288 // TODO: Make this decision more intelligent.
1289 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1290 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1291 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001292 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 mActivePointerId = ev.getPointerId(newPointerIndex);
1294 if (mVelocityTracker != null) {
1295 mVelocityTracker.clear();
1296 }
1297 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001298 }
1299
Michael Jurkad771c962011-08-09 15:00:48 -07001300 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001301
1302 @Override
1303 public void requestChildFocus(View child, View focused) {
1304 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001305 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001306 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001307 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001308 }
1309 }
1310
Winson Chunge3193b92010-09-10 11:44:42 -07001311 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1312 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001313 int left;
1314 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001315 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001316 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001317 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001318 if (left <= relativeOffset && relativeOffset <= right) {
1319 return i;
1320 }
Winson Chunge3193b92010-09-10 11:44:42 -07001321 }
1322 return -1;
1323 }
1324
Winson Chung1908d072011-02-24 18:09:44 -08001325 protected void setMinimumWidthOverride(int minimumWidth) {
1326 mMinimumWidth = minimumWidth;
1327 }
Winson Chung34b23d52011-03-18 11:29:34 -07001328 protected void resetMinimumWidthOverride() {
1329 mMinimumWidth = 0;
1330 }
Winson Chung1908d072011-02-24 18:09:44 -08001331
1332 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001333 // This functions are called enough times that it actually makes a difference in the
1334 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001335 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001336 final int minWidth = mMinimumWidth;
1337 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001338 }
1339
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001341 int padding = mPaddingLeft + mPaddingRight;
1342 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001344 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001345 int padding = mPaddingLeft + mPaddingRight;
1346 return mPaddingLeft + (getMeasuredWidth() - padding -
Adam Cohen22f823d2011-09-01 17:22:18 -07001347 getScaledMeasuredWidth(getPageAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001348 }
1349
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 protected int getChildOffset(int index) {
1351 if (getChildCount() == 0)
1352 return 0;
1353
1354 int offset = getRelativeChildOffset(0);
1355 for (int i = 0; i < index; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001356 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 }
1358 return offset;
1359 }
1360
Michael Jurkad3ef3062010-11-23 16:23:58 -08001361 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001362 // This functions are called enough times that it actually makes a difference in the
1363 // profiler -- so just inline the max() here
1364 final int measuredWidth = child.getMeasuredWidth();
1365 final int minWidth = mMinimumWidth;
1366 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1367 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001368 }
1369
Adam Cohend19d3ca2010-09-15 14:43:42 -07001370 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001371 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 int minDistanceFromScreenCenterIndex = -1;
1373 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1374 final int childCount = getChildCount();
1375 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001376 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001377 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 int halfChildWidth = (childWidth / 2);
1379 int childCenter = getChildOffset(i) + halfChildWidth;
1380 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1381 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1382 minDistanceFromScreenCenter = distanceFromScreenCenter;
1383 minDistanceFromScreenCenterIndex = i;
1384 }
1385 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001386 return minDistanceFromScreenCenterIndex;
1387 }
1388
1389 protected void snapToDestination() {
1390 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 }
1392
Adam Cohene0f66b52010-11-23 15:06:07 -08001393 private static class ScrollInterpolator implements Interpolator {
1394 public ScrollInterpolator() {
1395 }
1396
1397 public float getInterpolation(float t) {
1398 t -= 1.0f;
1399 return t*t*t*t*t + 1;
1400 }
1401 }
1402
1403 // We want the duration of the page snap animation to be influenced by the distance that
1404 // the screen has to travel, however, we don't want this duration to be effected in a
1405 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1406 // of travel has on the overall snap duration.
1407 float distanceInfluenceForSnapDuration(float f) {
1408 f -= 0.5f; // center the values about 0.
1409 f *= 0.3f * Math.PI / 2.0f;
1410 return (float) Math.sin(f);
1411 }
1412
Michael Jurka0142d492010-08-25 17:46:15 -07001413 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001414 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1415 int halfScreenSize = getMeasuredWidth() / 2;
1416
Winson Chung785d2eb2011-04-14 16:08:02 -07001417 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1418 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1419 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001420 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1421 int delta = newX - mUnboundedScrollX;
1422 int duration = 0;
1423
1424 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1425 // If the velocity is low enough, then treat this more as an automatic page advance
1426 // as opposed to an apparent physical response to flinging
1427 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1428 return;
1429 }
1430
1431 // Here we compute a "distance" that will be used in the computation of the overall
1432 // snap duration. This is a function of the actual distance that needs to be traveled;
1433 // we keep this value close to half screen size in order to reduce the variance in snap
1434 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001435 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001436 float distance = halfScreenSize + halfScreenSize *
1437 distanceInfluenceForSnapDuration(distanceRatio);
1438
1439 velocity = Math.abs(velocity);
1440 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1441
1442 // we want the page's snap velocity to approximately match the velocity at which the
1443 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001444 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1445 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001446
1447 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001448 }
1449
1450 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001451 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 }
1453
Michael Jurka0142d492010-08-25 17:46:15 -07001454 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001455 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001456
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1458 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1459 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001460 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001461 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001463 snapToPage(whichPage, delta, duration);
1464 }
1465
1466 protected void snapToPage(int whichPage, int delta, int duration) {
1467 mNextPage = whichPage;
1468
1469 View focusedChild = getFocusedChild();
1470 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001471 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001472 focusedChild.clearFocus();
1473 }
1474
1475 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 awakenScrollBars(duration);
1477 if (duration == 0) {
1478 duration = Math.abs(delta);
1479 }
1480
1481 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001482 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001483
Winson Chungb44b5242011-06-13 11:32:14 -07001484 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1485 // loading associated pages until the scroll settles
1486 if (mDeferScrollUpdate) {
1487 loadAssociatedPages(mNextPage);
1488 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001489 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001490 }
Michael Jurka0142d492010-08-25 17:46:15 -07001491 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001492 invalidate();
1493 }
1494
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 public void scrollLeft() {
1496 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001497 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001499 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 }
1501 }
1502
1503 public void scrollRight() {
1504 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001505 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001507 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 }
1509 }
1510
Winson Chung86f77532010-08-24 11:08:22 -07001511 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 int result = -1;
1513 if (v != null) {
1514 ViewParent vp = v.getParent();
1515 int count = getChildCount();
1516 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001517 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 return i;
1519 }
1520 }
1521 }
1522 return result;
1523 }
1524
1525 /**
1526 * @return True is long presses are still allowed for the current touch
1527 */
1528 public boolean allowLongPress() {
1529 return mAllowLongPress;
1530 }
1531
Michael Jurka0142d492010-08-25 17:46:15 -07001532 /**
1533 * Set true to allow long-press events to be triggered, usually checked by
1534 * {@link Launcher} to accept or block dpad-initiated long-presses.
1535 */
1536 public void setAllowLongPress(boolean allowLongPress) {
1537 mAllowLongPress = allowLongPress;
1538 }
1539
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001541 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001542
1543 SavedState(Parcelable superState) {
1544 super(superState);
1545 }
1546
1547 private SavedState(Parcel in) {
1548 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001549 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 }
1551
1552 @Override
1553 public void writeToParcel(Parcel out, int flags) {
1554 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001555 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001556 }
1557
1558 public static final Parcelable.Creator<SavedState> CREATOR =
1559 new Parcelable.Creator<SavedState>() {
1560 public SavedState createFromParcel(Parcel in) {
1561 return new SavedState(in);
1562 }
1563
1564 public SavedState[] newArray(int size) {
1565 return new SavedState[size];
1566 }
1567 };
1568 }
1569
Winson Chungf314b0e2011-08-16 11:54:27 -07001570 protected void loadAssociatedPages(int page) {
1571 loadAssociatedPages(page, false);
1572 }
1573 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001574 if (mContentIsRefreshable) {
1575 final int count = getChildCount();
1576 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001577 int lowerPageBound = getAssociatedLowerPageBound(page);
1578 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001579 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1580 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001581 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001582 if ((i != page) && immediateAndOnly) {
1583 continue;
1584 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001585 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001586 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001587 if (lowerPageBound <= i && i <= upperPageBound) {
1588 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001589 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001590 mDirtyPageContent.set(i, false);
1591 }
1592 } else {
1593 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001594 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001595 }
1596 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001597 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001598 }
1599 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001600 }
1601 }
1602
Winson Chunge3193b92010-09-10 11:44:42 -07001603 protected int getAssociatedLowerPageBound(int page) {
1604 return Math.max(0, page - 1);
1605 }
1606 protected int getAssociatedUpperPageBound(int page) {
1607 final int count = getChildCount();
1608 return Math.min(page + 1, count - 1);
1609 }
1610
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001611 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001612 if (isChoiceMode(CHOICE_MODE_NONE)) {
1613 mChoiceMode = mode;
1614 mActionMode = startActionMode(callback);
1615 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001616 }
1617
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001618 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001619 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001620 mChoiceMode = CHOICE_MODE_NONE;
1621 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001622 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001623 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001624 }
1625 }
1626
1627 protected boolean isChoiceMode(int mode) {
1628 return mChoiceMode == mode;
1629 }
1630
1631 protected ArrayList<Checkable> getCheckedGrandchildren() {
1632 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1633 final int childCount = getChildCount();
1634 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001635 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001636 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001637 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001638 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001639 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001640 checked.add((Checkable) v);
1641 }
1642 }
1643 }
1644 return checked;
1645 }
1646
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001647 /**
1648 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1649 * Otherwise, returns null.
1650 */
1651 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001652 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001653 final int childCount = getChildCount();
1654 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001655 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001656 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001657 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001658 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001659 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1660 return (Checkable) v;
1661 }
1662 }
1663 }
1664 }
1665 return null;
1666 }
1667
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001668 protected void resetCheckedGrandchildren() {
1669 // loop through children, and set all of their children to _not_ be checked
1670 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1671 for (int i = 0; i < checked.size(); ++i) {
1672 final Checkable c = checked.get(i);
1673 c.setChecked(false);
1674 }
1675 }
1676
Winson Chung86f77532010-08-24 11:08:22 -07001677 /**
1678 * This method is called ONLY to synchronize the number of pages that the paged view has.
1679 * To actually fill the pages with information, implement syncPageItems() below. It is
1680 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1681 * and therefore, individual page items do not need to be updated in this method.
1682 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001683 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001684
1685 /**
1686 * This method is called to synchronize the items that are on a particular page. If views on
1687 * the page can be reused, then they should be updated within this method.
1688 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001689 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001690
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001691 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001692 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001693 }
1694 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001695 invalidatePageData(currentPage, false);
1696 }
1697 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001698 if (!mIsDataReady) {
1699 return;
1700 }
1701
Michael Jurka0142d492010-08-25 17:46:15 -07001702 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001703 // Force all scrolling-related behavior to end
1704 mScroller.forceFinished(true);
1705 mNextPage = INVALID_PAGE;
1706
Michael Jurka0142d492010-08-25 17:46:15 -07001707 // Update all the pages
1708 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001709
Winson Chung5a808352011-06-27 19:08:49 -07001710 // We must force a measure after we've loaded the pages to update the content width and
1711 // to determine the full scroll width
1712 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1713 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1714
1715 // Set a new page as the current page if necessary
1716 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001717 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001718 }
1719
Michael Jurka0142d492010-08-25 17:46:15 -07001720 // Mark each of the pages as dirty
1721 final int count = getChildCount();
1722 mDirtyPageContent.clear();
1723 for (int i = 0; i < count; ++i) {
1724 mDirtyPageContent.add(true);
1725 }
1726
1727 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001728 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001729 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001730 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001731 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001732 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001733 }
Winson Chung007c6982011-06-14 13:27:53 -07001734
1735 private ImageView getScrollingIndicator() {
1736 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1737 // found
1738 if (mHasScrollIndicator && mScrollIndicator == null) {
1739 ViewGroup parent = (ViewGroup) getParent();
1740 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1741 mHasScrollIndicator = mScrollIndicator != null;
1742 if (mHasScrollIndicator) {
1743 mScrollIndicator.setVisibility(View.VISIBLE);
1744 }
1745 }
1746 return mScrollIndicator;
1747 }
1748
1749 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001750 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001751 }
1752
Winson Chung5a808352011-06-27 19:08:49 -07001753 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1754 @Override
1755 public void run() {
1756 hideScrollingIndicator(false);
1757 }
1758 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001759 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001760 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001761 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001762 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001763 }
1764
Michael Jurka430e8a52011-08-08 15:52:14 -07001765 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001766 if (getChildCount() <= 1) return;
1767 if (!isScrollingIndicatorEnabled()) return;
1768
1769 getScrollingIndicator();
1770 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001771 // Fade the indicator in
1772 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001773 mScrollIndicator.setVisibility(View.VISIBLE);
1774 if (mScrollIndicatorAnimator != null) {
1775 mScrollIndicatorAnimator.cancel();
1776 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001777 if (immediately) {
1778 mScrollIndicator.setAlpha(1f);
1779 } else {
1780 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1781 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1782 mScrollIndicatorAnimator.start();
1783 }
Winson Chung007c6982011-06-14 13:27:53 -07001784 }
1785 }
1786
1787 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001788 if (getChildCount() <= 1) return;
1789 if (!isScrollingIndicatorEnabled()) return;
1790
1791 getScrollingIndicator();
1792 if (mScrollIndicator != null) {
1793 // Fade the indicator out
1794 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001795 if (mScrollIndicatorAnimator != null) {
1796 mScrollIndicatorAnimator.cancel();
1797 }
1798 if (immediately) {
1799 mScrollIndicator.setVisibility(View.GONE);
1800 mScrollIndicator.setAlpha(0f);
1801 } else {
1802 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1803 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1804 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1805 private boolean cancelled = false;
1806 @Override
1807 public void onAnimationCancel(android.animation.Animator animation) {
1808 cancelled = true;
1809 }
1810 @Override
1811 public void onAnimationEnd(Animator animation) {
1812 if (!cancelled) {
1813 mScrollIndicator.setVisibility(View.GONE);
1814 }
1815 }
1816 });
1817 mScrollIndicatorAnimator.start();
1818 }
Winson Chung007c6982011-06-14 13:27:53 -07001819 }
1820 }
1821
Winson Chung32174c82011-07-19 15:47:55 -07001822 /**
1823 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1824 * fill its space on the track or not.
1825 */
1826 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001827 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001828 }
1829
Winson Chung007c6982011-06-14 13:27:53 -07001830 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001831 if (getChildCount() <= 1) return;
1832 if (!isScrollingIndicatorEnabled()) return;
1833
1834 getScrollingIndicator();
1835 if (mScrollIndicator != null) {
1836 updateScrollingIndicatorPosition();
1837 }
1838 }
1839
Winson Chung007c6982011-06-14 13:27:53 -07001840 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001841 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001842 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001843 int numPages = getChildCount();
1844 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001845 int lastChildIndex = Math.max(0, getChildCount() - 1);
1846 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001847 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001848 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1849 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001850
Winson Chungae890b82011-09-13 18:08:54 -07001851 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001852 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001853 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001854 if (hasElasticScrollIndicator()) {
1855 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1856 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1857 mScrollIndicator.requestLayout();
1858 }
1859 } else {
1860 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1861 indicatorPos += indicatorCenterOffset;
1862 }
Winson Chung007c6982011-06-14 13:27:53 -07001863 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001864 mScrollIndicator.invalidate();
1865 }
1866
Winson Chung3ac74c52011-06-30 17:39:37 -07001867 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001868 }
1869
1870 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001871 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001872
1873 /* Accessibility */
1874 @Override
1875 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1876 super.onInitializeAccessibilityNodeInfo(info);
1877 info.setScrollable(true);
1878 }
1879
1880 @Override
1881 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1882 super.onInitializeAccessibilityEvent(event);
1883 event.setScrollable(true);
1884 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1885 event.setFromIndex(mCurrentPage);
1886 event.setToIndex(mCurrentPage);
1887 event.setItemCount(getChildCount());
1888 }
1889 }
1890
Winson Chung6a0f57d2011-06-29 20:10:49 -07001891 protected String getCurrentPageDescription() {
1892 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1893 return String.format(mContext.getString(R.string.default_scroll_format),
1894 page + 1, getChildCount());
1895 }
Winson Chungd11265e2011-08-30 13:37:23 -07001896
1897 @Override
1898 public boolean onHoverEvent(android.view.MotionEvent event) {
1899 return true;
1900 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001901}