blob: 2cb943d7dc9626ea60604d0f1e430fadd45fbb2f [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;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Winson Chungbb6f6a52011-07-25 17:55:44 -070021import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.graphics.Canvas;
25import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070026import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080031import android.view.InputDevice;
32import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070039import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070040import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import java.util.ArrayList;
48
Winson Chung321e9ee2010-08-09 13:37:56 -070049/**
50 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070051 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070052 */
Michael Jurka8b805b12012-04-18 14:23:14 -070053public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070054 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070055 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chung86f77532010-08-24 11:08:22 -070058 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070059 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Winson Chungf0c6ae02012-03-21 16:10:31 -070061 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Chet Haase97687ef2012-10-25 16:01:11 -070062 protected static final int MAX_PAGE_SNAP_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070063 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Adam Cohenb5ba0972011-09-07 18:02:31 -070066 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070067 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080068
Adam Cohenb64cb5a2011-02-15 13:53:42 -080069 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080070 // The page is moved more than halfway, automatically move to the next page on touch up.
71 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080072
Adam Cohen265b9a62011-12-07 14:37:18 -080073 // The following constants need to be scaled based on density. The scaled versions will be
74 // assigned to the corresponding member variables below.
75 private static final int FLING_THRESHOLD_VELOCITY = 500;
76 private static final int MIN_SNAP_VELOCITY = 1500;
77 private static final int MIN_FLING_VELOCITY = 250;
78
Winson Chung8aad6102012-05-11 16:27:49 -070079 static final int AUTOMATIC_PAGE_SPACING = -1;
80
Adam Cohen265b9a62011-12-07 14:37:18 -080081 protected int mFlingThresholdVelocity;
82 protected int mMinFlingVelocity;
83 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070084
Adam Cohenb5ba0972011-09-07 18:02:31 -070085 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070086 protected float mSmoothingTime;
87 protected float mTouchX;
88
89 protected boolean mFirstLayout = true;
90
91 protected int mCurrentPage;
92 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080093 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070095 private VelocityTracker mVelocityTracker;
96
97 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080099 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800101 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700102 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700103 private int[] mChildOffsets;
104 private int[] mChildRelativeOffsets;
105 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected final static int TOUCH_STATE_REST = 0;
108 protected final static int TOUCH_STATE_SCROLLING = 1;
109 protected final static int TOUCH_STATE_PREV_PAGE = 2;
110 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700111 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700114 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Michael Jurka0142d492010-08-25 17:46:15 -0700116 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka7426c422010-11-11 15:23:47 -0800118 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka7426c422010-11-11 15:23:47 -0800120 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121 private int mPagingTouchSlop;
122 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800123 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700124 protected int mPageSpacing;
125 protected int mPageLayoutPaddingTop;
126 protected int mPageLayoutPaddingBottom;
127 protected int mPageLayoutPaddingLeft;
128 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700129 protected int mPageLayoutWidthGap;
130 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700131 protected int mCellCountX = 0;
132 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700133 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800134 protected boolean mAllowOverScroll = true;
135 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800136 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700137 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800140 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
141 // the screens from continuing to translate beyond the normal bounds.
142 protected int mOverScrollX;
143
Michael Jurka8c920dd2011-01-20 14:16:56 -0800144 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800145 protected float mLayoutScale = 1.0f;
146
Michael Jurka5f1c5092010-09-03 14:15:02 -0700147 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurka5f1c5092010-09-03 14:15:02 -0700149 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Winson Chung86f77532010-08-24 11:08:22 -0700151 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurkae326f182011-11-21 14:05:46 -0800153 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700154
Michael Jurka0142d492010-08-25 17:46:15 -0700155 // If true, syncPages and syncPageItems will be called to refresh pages
156 protected boolean mContentIsRefreshable = true;
157
158 // If true, modify alpha of neighboring pages as user scrolls left/right
159 protected boolean mFadeInAdjacentScreens = true;
160
161 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
162 // to switch to a new page
163 protected boolean mUsePagingTouchSlop = true;
164
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700166 // (SmoothPagedView does this)
167 protected boolean mDeferScrollUpdate = false;
168
Patrick Dubroy1262e362010-10-06 15:49:50 -0700169 protected boolean mIsPageMoving = false;
170
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 // All syncs and layout passes are deferred until data is ready.
172 protected boolean mIsDataReady = false;
173
Winson Chung007c6982011-06-14 13:27:53 -0700174 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700175 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800176 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700177 private int mScrollIndicatorPaddingLeft;
178 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700179 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800180 private boolean mShouldShowScrollIndicator = false;
181 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700182 protected static final int sScrollIndicatorFadeInDuration = 150;
183 protected static final int sScrollIndicatorFadeOutDuration = 650;
184 protected static final int sScrollIndicatorFlashDuration = 650;
Chet Haasebc2f0822012-10-26 17:59:53 -0700185 private boolean mScrollingPaused = false;
Winson Chung007c6982011-06-14 13:27:53 -0700186
Winson Chungb44b5242011-06-13 11:32:14 -0700187 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700188 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700189
Winson Chung86f77532010-08-24 11:08:22 -0700190 public interface PageSwitchListener {
191 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 }
193
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 public PagedView(Context context) {
195 this(context, null);
196 }
197
198 public PagedView(Context context, AttributeSet attrs) {
199 this(context, attrs, 0);
200 }
201
202 public PagedView(Context context, AttributeSet attrs, int defStyle) {
203 super(context, attrs, defStyle);
204
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 TypedArray a = context.obtainStyledAttributes(attrs,
206 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800207 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700214 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800215 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700216 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700218 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700220 mScrollIndicatorPaddingLeft =
221 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
222 mScrollIndicatorPaddingRight =
223 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700224 a.recycle();
225
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700227 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
230 /**
231 * Initializes various states for this workspace.
232 */
Michael Jurka0142d492010-08-25 17:46:15 -0700233 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700234 mDirtyPageContent = new ArrayList<Boolean>();
235 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800236 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700237 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700238 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700239
240 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
241 mTouchSlop = configuration.getScaledTouchSlop();
242 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
243 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700244 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800245
246 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
247 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
248 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700249 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 }
251
Winson Chung86f77532010-08-24 11:08:22 -0700252 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
253 mPageSwitchListener = pageSwitchListener;
254 if (mPageSwitchListener != null) {
255 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 }
257 }
258
259 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700260 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
261 * out pages.
262 */
263 protected void setDataIsReady() {
264 mIsDataReady = true;
265 }
266 protected boolean isDataReady() {
267 return mIsDataReady;
268 }
269
270 /**
Winson Chung86f77532010-08-24 11:08:22 -0700271 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 *
Winson Chung86f77532010-08-24 11:08:22 -0700273 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 */
Winson Chung86f77532010-08-24 11:08:22 -0700275 int getCurrentPage() {
276 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 }
Winson Chung360e63f2012-04-27 13:48:05 -0700278 int getNextPage() {
279 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
280 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700281
Winson Chung86f77532010-08-24 11:08:22 -0700282 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 return getChildCount();
284 }
285
Winson Chung86f77532010-08-24 11:08:22 -0700286 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 return getChildAt(index);
288 }
289
Adam Cohenae4f1552011-10-20 00:15:42 -0700290 protected int indexToPage(int index) {
291 return index;
292 }
293
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800295 * Updates the scroll of the current page immediately to its final scroll position. We use this
296 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
297 * the previous tab page.
298 */
299 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700300 int offset = getChildOffset(mCurrentPage);
301 int relOffset = getRelativeChildOffset(mCurrentPage);
302 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800303 scrollTo(newX, 0);
304 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800305 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800306 }
307
308 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700309 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
310 * ends, {@link #resumeScrolling()} should be called, along with
311 * {@link #updateCurrentPageScroll()} to correctly set the final state and re-enable scrolling.
312 */
313 void pauseScrolling() {
314 mScroller.forceFinished(true);
315 cancelScrollingIndicatorAnimations();
316 mScrollingPaused = true;
317 }
318
319 /**
320 * Enables scrolling again.
321 * @see #pauseScrolling()
322 */
323 void resumeScrolling() {
324 mScrollingPaused = false;
325 }
326 /**
Winson Chung86f77532010-08-24 11:08:22 -0700327 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 */
Winson Chung86f77532010-08-24 11:08:22 -0700329 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800330 if (!mScroller.isFinished()) {
331 mScroller.abortAnimation();
332 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800333 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
334 // the default
335 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800336 return;
337 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700338
Winson Chungbfeac062012-06-06 15:56:08 -0700339
Winson Chung86f77532010-08-24 11:08:22 -0700340 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800341 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700342 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700343 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800344 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 }
346
Michael Jurka0142d492010-08-25 17:46:15 -0700347 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700348 if (mPageSwitchListener != null) {
349 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 }
351 }
352
Michael Jurkace7e05f2011-02-01 22:02:35 -0800353 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700354 if (!mIsPageMoving) {
355 mIsPageMoving = true;
356 onPageBeginMoving();
357 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700358 }
359
Michael Jurkace7e05f2011-02-01 22:02:35 -0800360 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700361 if (mIsPageMoving) {
362 mIsPageMoving = false;
363 onPageEndMoving();
364 }
Michael Jurka0142d492010-08-25 17:46:15 -0700365 }
366
Adam Cohen26976d92011-03-22 15:33:33 -0700367 protected boolean isPageMoving() {
368 return mIsPageMoving;
369 }
370
Michael Jurka0142d492010-08-25 17:46:15 -0700371 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700372 protected void onPageBeginMoving() {
373 }
374
375 // a method that subclasses can override to add behavior
376 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700377 }
378
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 /**
Winson Chung86f77532010-08-24 11:08:22 -0700380 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 *
382 * @param l The listener used to respond to long clicks.
383 */
384 @Override
385 public void setOnLongClickListener(OnLongClickListener l) {
386 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700387 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700388 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700389 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 }
391 }
392
393 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800394 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700395 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800396 }
397
398 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700399 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800400 mUnboundedScrollX = x;
401
402 if (x < 0) {
403 super.scrollTo(0, y);
404 if (mAllowOverScroll) {
405 overScroll(x);
406 }
407 } else if (x > mMaxScrollX) {
408 super.scrollTo(mMaxScrollX, y);
409 if (mAllowOverScroll) {
410 overScroll(x - mMaxScrollX);
411 }
412 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800413 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800414 super.scrollTo(x, y);
415 }
416
Michael Jurka0142d492010-08-25 17:46:15 -0700417 mTouchX = x;
418 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
419 }
420
421 // we moved this functionality to a helper function so SmoothPagedView can reuse it
422 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700424 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700425 if (getScrollX() != mScroller.getCurrX()
426 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700427 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700428 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
429 }
Michael Jurka0142d492010-08-25 17:46:15 -0700430 invalidate();
431 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700432 } else if (mNextPage != INVALID_PAGE) {
433 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700434 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700435 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700436
437 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700438 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700439 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700440 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700441 }
442
Adam Cohen73aa9752010-11-24 16:26:58 -0800443 // We don't want to trigger a page end moving unless the page has settled
444 // and the user has stopped scrolling
445 if (mTouchState == TOUCH_STATE_REST) {
446 pageEndMoving();
447 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700448
449 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700450 AccessibilityManager accessibilityManager = (AccessibilityManager)
451 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
452 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700453 AccessibilityEvent ev =
454 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
455 ev.getText().add(getCurrentPageDescription());
456 sendAccessibilityEventUnchecked(ev);
457 }
Michael Jurka0142d492010-08-25 17:46:15 -0700458 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 }
Michael Jurka0142d492010-08-25 17:46:15 -0700460 return false;
461 }
462
463 @Override
464 public void computeScroll() {
465 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 }
467
468 @Override
469 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 if (!mIsDataReady) {
471 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
472 return;
473 }
474
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
476 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700477 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
478 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 if (widthMode != MeasureSpec.EXACTLY) {
480 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
481 }
482
Winson Chung8aad6102012-05-11 16:27:49 -0700483 // Return early if we aren't given a proper dimension
484 if (widthSize <= 0 || heightSize <= 0) {
485 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
486 return;
487 }
488
Adam Lesinski6b879f02010-11-04 16:15:23 -0700489 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
490 * of the All apps view on XLarge displays to not take up more space then it needs. Width
491 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
492 * each page to have the same width.
493 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700494 int maxChildHeight = 0;
495
Michael Jurka8b805b12012-04-18 14:23:14 -0700496 final int verticalPadding = getPaddingTop() + getPaddingBottom();
497 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700498
Michael Jurka36fcb742011-04-06 17:08:58 -0700499
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700501 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700502 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 final int childCount = getChildCount();
504 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700505 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700506 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700507 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
508
509 int childWidthMode;
510 if (lp.width == LayoutParams.WRAP_CONTENT) {
511 childWidthMode = MeasureSpec.AT_MOST;
512 } else {
513 childWidthMode = MeasureSpec.EXACTLY;
514 }
515
516 int childHeightMode;
517 if (lp.height == LayoutParams.WRAP_CONTENT) {
518 childHeightMode = MeasureSpec.AT_MOST;
519 } else {
520 childHeightMode = MeasureSpec.EXACTLY;
521 }
522
523 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700524 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700525 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700526 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700527
528 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700529 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700530 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
531 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700532 }
533
534 if (heightMode == MeasureSpec.AT_MOST) {
535 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 }
Winson Chungae890b82011-09-13 18:08:54 -0700537
Winson Chungae890b82011-09-13 18:08:54 -0700538 setMeasuredDimension(widthSize, heightSize);
539
Winson Chung8aad6102012-05-11 16:27:49 -0700540 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
541 // We also wait until we set the measured dimensions before flushing the cache as well, to
542 // ensure that the cache is filled with good values.
543 invalidateCachedOffsets();
544
Winson Chunga128a7b2012-04-30 15:23:15 -0700545 if (childCount > 0) {
546 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
547 + getChildWidth(0));
548
549 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700550 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700551 // The gap between pages in the PagedView should be equal to the gap from the page
552 // to the edge of the screen (so it is not visible in the current screen). To
553 // account for unequal padding on each side of the paged view, we take the maximum
554 // of the left/right gap and use that as the gap between each page.
555 int offset = getRelativeChildOffset(0);
556 int spacing = Math.max(offset, widthSize - offset -
557 getChildAt(0).getMeasuredWidth());
558 setPageSpacing(spacing);
559 }
560 }
561
Adam Cohen25b29952011-11-02 14:49:06 -0700562 updateScrollingIndicatorPosition();
563
Adam Cohenfaa28302010-11-19 12:02:18 -0800564 if (childCount > 0) {
565 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
566 } else {
567 mMaxScrollX = 0;
568 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 }
570
Michael Jurka8c920dd2011-01-20 14:16:56 -0800571 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800572 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800574
Michael Jurka8c920dd2011-01-20 14:16:56 -0800575 final int pageCount = getChildCount();
576 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700577 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800578 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800579 }
580 setCurrentPage(newCurrentPage);
581 }
582
Michael Jurka8c920dd2011-01-20 14:16:56 -0800583 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
584 // 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 -0800585 // tightens the layout accordingly
586 public void setLayoutScale(float childrenScale) {
587 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700588 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800589
590 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
591 int childCount = getChildCount();
592 float childrenX[] = new float[childCount];
593 float childrenY[] = new float[childCount];
594 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700595 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800596 childrenX[i] = child.getX();
597 childrenY[i] = child.getY();
598 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700599 // Trigger a full re-layout (never just call onLayout directly!)
600 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
601 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
602 requestLayout();
603 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700604 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800605 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700606 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800607 child.setX(childrenX[i]);
608 child.setY(childrenY[i]);
609 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700610
Michael Jurkad3ef3062010-11-23 16:23:58 -0800611 // Also, the page offset has changed (since the pages are now smaller);
612 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800613 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800614 }
615
Adam Cohen60b07122011-11-14 17:26:06 -0800616 public void setPageSpacing(int pageSpacing) {
617 mPageSpacing = pageSpacing;
618 invalidateCachedOffsets();
619 }
620
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700622 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700623 if (!mIsDataReady) {
624 return;
625 }
626
Winson Chung785d2eb2011-04-14 16:08:02 -0700627 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700628 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700629 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700630 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700631
632 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700633 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800635 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700636 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700638 if (mCenterPagesVertically) {
639 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
640 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800641
Winson Chung785d2eb2011-04-14 16:08:02 -0700642 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700643 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800644 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700645 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
647 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700648
649 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
650 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800651 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700652 setHorizontalScrollBarEnabled(true);
653 mFirstLayout = false;
654 }
Winson Chunge3193b92010-09-10 11:44:42 -0700655 }
656
Adam Cohenf34bab52010-09-30 14:11:56 -0700657 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700658 if (isScrollingIndicatorEnabled()) {
659 updateScrollingIndicator();
660 }
Michael Jurka869390b2012-05-06 15:55:19 -0700661 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
662
663 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700664 for (int i = 0; i < getChildCount(); i++) {
665 View child = getChildAt(i);
666 if (child != null) {
667 float scrollProgress = getScrollProgress(screenCenter, child, i);
668 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800669 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700670 }
671 }
672 invalidate();
673 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700674 }
675
Winson Chunge3193b92010-09-10 11:44:42 -0700676 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700678 // This ensures that when children are added, they get the correct transforms / alphas
679 // in accordance with any scroll effects.
680 mForceScreenScrolled = true;
681 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700682 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700683 }
684
Michael Jurka8b805b12012-04-18 14:23:14 -0700685 @Override
686 public void onChildViewRemoved(View parent, View child) {
687 }
688
Adam Cohen73894962011-10-31 13:17:17 -0700689 protected void invalidateCachedOffsets() {
690 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700691 if (count == 0) {
692 mChildOffsets = null;
693 mChildRelativeOffsets = null;
694 mChildOffsetsWithLayoutScale = null;
695 return;
696 }
Adam Cohen73894962011-10-31 13:17:17 -0700697
698 mChildOffsets = new int[count];
699 mChildRelativeOffsets = new int[count];
700 mChildOffsetsWithLayoutScale = new int[count];
701 for (int i = 0; i < count; i++) {
702 mChildOffsets[i] = -1;
703 mChildRelativeOffsets[i] = -1;
704 mChildOffsetsWithLayoutScale[i] = -1;
705 }
706 }
707
708 protected int getChildOffset(int index) {
709 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
710 mChildOffsets : mChildOffsetsWithLayoutScale;
711
712 if (childOffsets != null && childOffsets[index] != -1) {
713 return childOffsets[index];
714 } else {
715 if (getChildCount() == 0)
716 return 0;
717
718 int offset = getRelativeChildOffset(0);
719 for (int i = 0; i < index; ++i) {
720 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
721 }
722 if (childOffsets != null) {
723 childOffsets[index] = offset;
724 }
725 return offset;
726 }
727 }
728
729 protected int getRelativeChildOffset(int index) {
730 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
731 return mChildRelativeOffsets[index];
732 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 final int padding = getPaddingLeft() + getPaddingRight();
734 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700735 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
736 if (mChildRelativeOffsets != null) {
737 mChildRelativeOffsets[index] = offset;
738 }
739 return offset;
740 }
741 }
742
Adam Cohen73894962011-10-31 13:17:17 -0700743 protected int getScaledMeasuredWidth(View child) {
744 // This functions are called enough times that it actually makes a difference in the
745 // profiler -- so just inline the max() here
746 final int measuredWidth = child.getMeasuredWidth();
747 final int minWidth = mMinimumWidth;
748 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
749 return (int) (maxWidth * mLayoutScale + 0.5f);
750 }
751
Michael Jurkadde558b2011-11-09 22:09:06 -0800752 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700753 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700754
Michael Jurkac4fb9172010-09-02 17:19:20 -0700755 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700756 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700757 int leftScreen = 0;
758 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800759 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800760 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 currPage.getX() + currPage.getWidth() -
762 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700763 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800764 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700765 }
766 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800767 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800768 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700770 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800771 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700772 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800773 range[0] = leftScreen;
774 range[1] = rightScreen;
775 } else {
776 range[0] = -1;
777 range[1] = -1;
778 }
779 }
780
Michael Jurka920d7f42012-05-14 16:29:55 -0700781 protected boolean shouldDrawChild(View child) {
782 return child.getAlpha() > 0;
783 }
784
Michael Jurkadde558b2011-11-09 22:09:06 -0800785 @Override
786 protected void dispatchDraw(Canvas canvas) {
787 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
789 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800790 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800791
792 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700793 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
794 // set it for the next frame
795 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800796 screenScrolled(screenCenter);
797 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800798 }
799
800 // Find out which screens are visible; as an optimization we only call draw on them
801 final int pageCount = getChildCount();
802 if (pageCount > 0) {
803 getVisiblePages(mTempVisiblePagesRange);
804 final int leftScreen = mTempVisiblePagesRange[0];
805 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800806 if (leftScreen != -1 && rightScreen != -1) {
807 final long drawingTime = getDrawingTime();
808 // Clip to the bounds
809 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700810 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
811 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700812
Michael Jurka80c69852011-12-16 14:16:32 -0800813 for (int i = getChildCount() - 1; i >= 0; i--) {
814 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700815 if (mForceDrawAllChildrenNextFrame ||
816 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800817 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -0800818 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800819 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700820 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800821 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700822 }
Michael Jurka0142d492010-08-25 17:46:15 -0700823 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
825
826 @Override
827 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700828 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700829 if (page != mCurrentPage || !mScroller.isFinished()) {
830 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 return true;
832 }
833 return false;
834 }
835
836 @Override
837 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700838 int focusablePage;
839 if (mNextPage != INVALID_PAGE) {
840 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700842 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
Winson Chung86f77532010-08-24 11:08:22 -0700844 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700846 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848 return false;
849 }
850
851 @Override
852 public boolean dispatchUnhandledMove(View focused, int direction) {
853 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700854 if (getCurrentPage() > 0) {
855 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 return true;
857 }
858 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 if (getCurrentPage() < getPageCount() - 1) {
860 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 return true;
862 }
863 }
864 return super.dispatchUnhandledMove(focused, direction);
865 }
866
867 @Override
868 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700869 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700870 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 }
872 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700873 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700874 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 }
876 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700877 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700878 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 }
880 }
881 }
882
883 /**
884 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700885 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 *
Winson Chung86f77532010-08-24 11:08:22 -0700887 * This happens when live folders requery, and if they're off page, they
888 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 */
890 @Override
891 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700892 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700893 View v = focused;
894 while (true) {
895 if (v == current) {
896 super.focusableViewAvailable(focused);
897 return;
898 }
899 if (v == this) {
900 return;
901 }
902 ViewParent parent = v.getParent();
903 if (parent instanceof View) {
904 v = (View)v.getParent();
905 } else {
906 return;
907 }
908 }
909 }
910
911 /**
912 * {@inheritDoc}
913 */
914 @Override
915 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
916 if (disallowIntercept) {
917 // We need to make sure to cancel our long press if
918 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700919 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700920 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 }
922 super.requestDisallowInterceptTouchEvent(disallowIntercept);
923 }
924
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800925 /**
926 * Return true if a tap at (x, y) should trigger a flip to the previous page.
927 */
928 protected boolean hitsPreviousPage(float x, float y) {
929 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
930 }
931
932 /**
933 * Return true if a tap at (x, y) should trigger a flip to the next page.
934 */
935 protected boolean hitsNextPage(float x, float y) {
936 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
937 }
938
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 @Override
940 public boolean onInterceptTouchEvent(MotionEvent ev) {
941 /*
942 * This method JUST determines whether we want to intercept the motion.
943 * If we return true, onTouchEvent will be called and we do the actual
944 * scrolling there.
945 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800946 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947
Winson Chung45e1d6e2010-11-09 17:19:49 -0800948 // Skip touch handling if there are no pages to swipe
949 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
950
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 /*
952 * Shortcut the most recurring case: the user is in the dragging
953 * state and he is moving his finger. We want to intercept this
954 * motion.
955 */
956 final int action = ev.getAction();
957 if ((action == MotionEvent.ACTION_MOVE) &&
958 (mTouchState == TOUCH_STATE_SCROLLING)) {
959 return true;
960 }
961
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 switch (action & MotionEvent.ACTION_MASK) {
963 case MotionEvent.ACTION_MOVE: {
964 /*
965 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
966 * whether the user has moved far enough from his original down touch.
967 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700968 if (mActivePointerId != INVALID_POINTER) {
969 determineScrollingStart(ev);
970 break;
971 }
972 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
973 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
974 // i.e. fall through to the next case (don't break)
975 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
976 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 }
978
979 case MotionEvent.ACTION_DOWN: {
980 final float x = ev.getX();
981 final float y = ev.getY();
982 // Remember location of down touch
983 mDownMotionX = x;
984 mLastMotionX = x;
985 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800986 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800987 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 mActivePointerId = ev.getPointerId(0);
989 mAllowLongPress = true;
990
991 /*
992 * If being flinged and user touches the screen, initiate drag;
993 * otherwise don't. mScroller.isFinished should be false when
994 * being flinged.
995 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700996 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700997 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
998 if (finishedScrolling) {
999 mTouchState = TOUCH_STATE_REST;
1000 mScroller.abortAnimation();
1001 } else {
1002 mTouchState = TOUCH_STATE_SCROLLING;
1003 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001004
Winson Chung86f77532010-08-24 11:08:22 -07001005 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001007 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001009 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001011 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 mTouchState = TOUCH_STATE_NEXT_PAGE;
1013 }
1014 }
1015 }
1016 break;
1017 }
1018
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001020 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 mTouchState = TOUCH_STATE_REST;
1022 mAllowLongPress = false;
1023 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001024 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 break;
1026
1027 case MotionEvent.ACTION_POINTER_UP:
1028 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001029 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 break;
1031 }
1032
1033 /*
1034 * The only time we want to intercept motion events is if we are in the
1035 * drag mode.
1036 */
1037 return mTouchState != TOUCH_STATE_REST;
1038 }
1039
Adam Cohenf8d28232011-02-01 21:47:00 -08001040 protected void determineScrollingStart(MotionEvent ev) {
1041 determineScrollingStart(ev, 1.0f);
1042 }
1043
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 /*
1045 * Determines if we should change the touch state to start scrolling after the
1046 * user moves their touch point too far.
1047 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001048 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 /*
1050 * Locally do absolute value. mLastMotionX is set to the y value
1051 * of the down event.
1052 */
1053 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001054 if (pointerIndex == -1) {
1055 return;
1056 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 final float x = ev.getX(pointerIndex);
1058 final float y = ev.getY(pointerIndex);
1059 final int xDiff = (int) Math.abs(x - mLastMotionX);
1060 final int yDiff = (int) Math.abs(y - mLastMotionY);
1061
Adam Cohenf8d28232011-02-01 21:47:00 -08001062 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 boolean xPaged = xDiff > mPagingTouchSlop;
1064 boolean xMoved = xDiff > touchSlop;
1065 boolean yMoved = yDiff > touchSlop;
1066
Adam Cohenf8d28232011-02-01 21:47:00 -08001067 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001068 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 // Scroll if the user moved far enough along the X axis
1070 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001071 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001073 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001074 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001075 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1076 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001077 }
1078 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001079 cancelCurrentPageLongPress();
1080 }
1081 }
1082
1083 protected void cancelCurrentPageLongPress() {
1084 if (mAllowLongPress) {
1085 mAllowLongPress = false;
1086 // Try canceling the long press. It could also have been scheduled
1087 // by a distant descendant, so use the mAllowLongPress flag to block
1088 // everything
1089 final View currentPage = getPageAt(mCurrentPage);
1090 if (currentPage != null) {
1091 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 }
1093 }
1094 }
1095
Adam Cohenb5ba0972011-09-07 18:02:31 -07001096 protected float getScrollProgress(int screenCenter, View v, int page) {
1097 final int halfScreenSize = getMeasuredWidth() / 2;
1098
1099 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1100 int delta = screenCenter - (getChildOffset(page) -
1101 getRelativeChildOffset(page) + halfScreenSize);
1102
1103 float scrollProgress = delta / (totalDistance * 1.0f);
1104 scrollProgress = Math.min(scrollProgress, 1.0f);
1105 scrollProgress = Math.max(scrollProgress, -1.0f);
1106 return scrollProgress;
1107 }
1108
Adam Cohene0f66b52010-11-23 15:06:07 -08001109 // This curve determines how the effect of scrolling over the limits of the page dimishes
1110 // as the user pulls further and further from the bounds
1111 private float overScrollInfluenceCurve(float f) {
1112 f -= 1.0f;
1113 return f * f * f + 1.0f;
1114 }
1115
Adam Cohenb5ba0972011-09-07 18:02:31 -07001116 protected void acceleratedOverScroll(float amount) {
1117 int screenSize = getMeasuredWidth();
1118
1119 // We want to reach the max over scroll effect when the user has
1120 // over scrolled half the size of the screen
1121 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1122
1123 if (f == 0) return;
1124
1125 // Clamp this factor, f, to -1 < f < 1
1126 if (Math.abs(f) >= 1) {
1127 f /= Math.abs(f);
1128 }
1129
1130 int overScrollAmount = (int) Math.round(f * screenSize);
1131 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001132 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001133 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001134 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001135 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001136 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001137 }
1138 invalidate();
1139 }
1140
1141 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001142 int screenSize = getMeasuredWidth();
1143
1144 float f = (amount / screenSize);
1145
1146 if (f == 0) return;
1147 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1148
Adam Cohen7bfc9792011-01-28 13:52:37 -08001149 // Clamp this factor, f, to -1 < f < 1
1150 if (Math.abs(f) >= 1) {
1151 f /= Math.abs(f);
1152 }
1153
Adam Cohene0f66b52010-11-23 15:06:07 -08001154 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001155 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001156 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001157 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001158 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001159 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001160 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001161 }
1162 invalidate();
1163 }
1164
Adam Cohenb5ba0972011-09-07 18:02:31 -07001165 protected void overScroll(float amount) {
1166 dampedOverScroll(amount);
1167 }
1168
Michael Jurkac5b262c2011-01-12 20:24:50 -08001169 protected float maxOverScroll() {
1170 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001171 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001172 float f = 1.0f;
1173 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1174 return OVERSCROLL_DAMP_FACTOR * f;
1175 }
1176
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 @Override
1178 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001179 // Skip touch handling if there are no pages to swipe
1180 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1181
Michael Jurkab8f06722010-10-10 15:58:46 -07001182 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001183
1184 final int action = ev.getAction();
1185
1186 switch (action & MotionEvent.ACTION_MASK) {
1187 case MotionEvent.ACTION_DOWN:
1188 /*
1189 * If being flinged and user touches, stop the fling. isFinished
1190 * will be false if being flinged.
1191 */
1192 if (!mScroller.isFinished()) {
1193 mScroller.abortAnimation();
1194 }
1195
1196 // Remember where the motion event started
1197 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001198 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001199 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001201 if (mTouchState == TOUCH_STATE_SCROLLING) {
1202 pageBeginMoving();
1203 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 break;
1205
1206 case MotionEvent.ACTION_MOVE:
1207 if (mTouchState == TOUCH_STATE_SCROLLING) {
1208 // Scroll to follow the motion event
1209 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1210 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001211 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001212
Adam Cohenaefd4e12011-02-14 16:39:38 -08001213 mTotalMotionX += Math.abs(deltaX);
1214
Winson Chungc0844aa2011-02-02 15:25:58 -08001215 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1216 // keep the remainder because we are actually testing if we've moved from the last
1217 // scrolled position (which is discrete).
1218 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001219 mTouchX += deltaX;
1220 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1221 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001222 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001223 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001224 } else {
1225 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001227 mLastMotionX = x;
1228 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 } else {
1230 awakenScrollBars();
1231 }
Adam Cohen564976a2010-10-13 18:52:07 -07001232 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 determineScrollingStart(ev);
1234 }
1235 break;
1236
1237 case MotionEvent.ACTION_UP:
1238 if (mTouchState == TOUCH_STATE_SCROLLING) {
1239 final int activePointerId = mActivePointerId;
1240 final int pointerIndex = ev.findPointerIndex(activePointerId);
1241 final float x = ev.getX(pointerIndex);
1242 final VelocityTracker velocityTracker = mVelocityTracker;
1243 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1244 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001245 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001246 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1247 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1248 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001249
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001250 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1251
Adam Cohen00481b32011-11-18 12:03:48 -08001252 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001253 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001254
Adam Cohenaefd4e12011-02-14 16:39:38 -08001255 // In the case that the page is moved far to one direction and then is flung
1256 // in the opposite direction, we use a threshold to determine whether we should
1257 // just return to the starting page, or if we should skip one further.
1258 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001259 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001260 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001261 returnToOriginalPage = true;
1262 }
1263
Adam Cohenaefd4e12011-02-14 16:39:38 -08001264 int finalPage;
1265 // We give flings precedence over large moves, which is why we short-circuit our
1266 // test for a large move if a fling has been registered. That is, a large
1267 // move to the left and fling to the right will register as a fling to the right.
1268 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1269 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1270 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1271 snapToPageWithVelocity(finalPage, velocityX);
1272 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1273 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001274 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001275 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1276 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 } else {
1278 snapToDestination();
1279 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001280 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 // at this point we have not moved beyond the touch slop
1282 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1283 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001284 int nextPage = Math.max(0, mCurrentPage - 1);
1285 if (nextPage != mCurrentPage) {
1286 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001287 } else {
1288 snapToDestination();
1289 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001290 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 // at this point we have not moved beyond the touch slop
1292 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1293 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001294 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1295 if (nextPage != mCurrentPage) {
1296 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 } else {
1298 snapToDestination();
1299 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001300 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001301 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 }
1303 mTouchState = TOUCH_STATE_REST;
1304 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001305 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 break;
1307
1308 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001309 if (mTouchState == TOUCH_STATE_SCROLLING) {
1310 snapToDestination();
1311 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 mTouchState = TOUCH_STATE_REST;
1313 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001314 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 break;
1316
1317 case MotionEvent.ACTION_POINTER_UP:
1318 onSecondaryPointerUp(ev);
1319 break;
1320 }
1321
1322 return true;
1323 }
1324
Winson Chung185d7162011-02-28 13:47:29 -08001325 @Override
1326 public boolean onGenericMotionEvent(MotionEvent event) {
1327 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1328 switch (event.getAction()) {
1329 case MotionEvent.ACTION_SCROLL: {
1330 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1331 final float vscroll;
1332 final float hscroll;
1333 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1334 vscroll = 0;
1335 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1336 } else {
1337 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1338 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1339 }
1340 if (hscroll != 0 || vscroll != 0) {
1341 if (hscroll > 0 || vscroll > 0) {
1342 scrollRight();
1343 } else {
1344 scrollLeft();
1345 }
1346 return true;
1347 }
1348 }
1349 }
1350 }
1351 return super.onGenericMotionEvent(event);
1352 }
1353
Michael Jurkab8f06722010-10-10 15:58:46 -07001354 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1355 if (mVelocityTracker == null) {
1356 mVelocityTracker = VelocityTracker.obtain();
1357 }
1358 mVelocityTracker.addMovement(ev);
1359 }
1360
1361 private void releaseVelocityTracker() {
1362 if (mVelocityTracker != null) {
1363 mVelocityTracker.recycle();
1364 mVelocityTracker = null;
1365 }
1366 }
1367
Winson Chung321e9ee2010-08-09 13:37:56 -07001368 private void onSecondaryPointerUp(MotionEvent ev) {
1369 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1370 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1371 final int pointerId = ev.getPointerId(pointerIndex);
1372 if (pointerId == mActivePointerId) {
1373 // This was our active pointer going up. Choose a new
1374 // active pointer and adjust accordingly.
1375 // TODO: Make this decision more intelligent.
1376 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1377 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1378 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001379 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 mActivePointerId = ev.getPointerId(newPointerIndex);
1381 if (mVelocityTracker != null) {
1382 mVelocityTracker.clear();
1383 }
1384 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001385 }
1386
Michael Jurkad771c962011-08-09 15:00:48 -07001387 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001388
1389 @Override
1390 public void requestChildFocus(View child, View focused) {
1391 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001392 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001393 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001394 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 }
1396 }
1397
Winson Chunge3193b92010-09-10 11:44:42 -07001398 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1399 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001400 int left;
1401 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001402 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001403 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001404 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001405 if (left <= relativeOffset && relativeOffset <= right) {
1406 return i;
1407 }
Winson Chunge3193b92010-09-10 11:44:42 -07001408 }
1409 return -1;
1410 }
1411
Winson Chung1908d072011-02-24 18:09:44 -08001412 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001413 // This functions are called enough times that it actually makes a difference in the
1414 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001415 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001416 final int minWidth = mMinimumWidth;
1417 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001418 }
1419
Adam Cohend19d3ca2010-09-15 14:43:42 -07001420 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001421 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001423 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 final int childCount = getChildCount();
1425 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001426 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001427 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 int halfChildWidth = (childWidth / 2);
1429 int childCenter = getChildOffset(i) + halfChildWidth;
1430 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1431 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1432 minDistanceFromScreenCenter = distanceFromScreenCenter;
1433 minDistanceFromScreenCenterIndex = i;
1434 }
1435 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001436 return minDistanceFromScreenCenterIndex;
1437 }
1438
1439 protected void snapToDestination() {
1440 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 }
1442
Adam Cohene0f66b52010-11-23 15:06:07 -08001443 private static class ScrollInterpolator implements Interpolator {
1444 public ScrollInterpolator() {
1445 }
1446
1447 public float getInterpolation(float t) {
1448 t -= 1.0f;
1449 return t*t*t*t*t + 1;
1450 }
1451 }
1452
1453 // We want the duration of the page snap animation to be influenced by the distance that
1454 // the screen has to travel, however, we don't want this duration to be effected in a
1455 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1456 // of travel has on the overall snap duration.
1457 float distanceInfluenceForSnapDuration(float f) {
1458 f -= 0.5f; // center the values about 0.
1459 f *= 0.3f * Math.PI / 2.0f;
1460 return (float) Math.sin(f);
1461 }
1462
Michael Jurka0142d492010-08-25 17:46:15 -07001463 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001464 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1465 int halfScreenSize = getMeasuredWidth() / 2;
1466
Winson Chung785d2eb2011-04-14 16:08:02 -07001467 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1468 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1469 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001470 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1471 int delta = newX - mUnboundedScrollX;
1472 int duration = 0;
1473
Adam Cohen265b9a62011-12-07 14:37:18 -08001474 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001475 // If the velocity is low enough, then treat this more as an automatic page advance
1476 // as opposed to an apparent physical response to flinging
1477 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1478 return;
1479 }
1480
1481 // Here we compute a "distance" that will be used in the computation of the overall
1482 // snap duration. This is a function of the actual distance that needs to be traveled;
1483 // we keep this value close to half screen size in order to reduce the variance in snap
1484 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001485 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001486 float distance = halfScreenSize + halfScreenSize *
1487 distanceInfluenceForSnapDuration(distanceRatio);
1488
1489 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001490 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001491
1492 // we want the page's snap velocity to approximately match the velocity at which the
1493 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001494 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1495 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Chet Haase97687ef2012-10-25 16:01:11 -07001496 duration = Math.min(duration, MAX_PAGE_SNAP_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001497
1498 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001499 }
1500
1501 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001502 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 }
1504
Michael Jurka0142d492010-08-25 17:46:15 -07001505 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001506 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001507
Winson Chung785d2eb2011-04-14 16:08:02 -07001508 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1509 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1510 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001511 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001512 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001514 snapToPage(whichPage, delta, duration);
1515 }
1516
1517 protected void snapToPage(int whichPage, int delta, int duration) {
1518 mNextPage = whichPage;
1519
1520 View focusedChild = getFocusedChild();
1521 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001522 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001523 focusedChild.clearFocus();
1524 }
1525
1526 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 awakenScrollBars(duration);
1528 if (duration == 0) {
1529 duration = Math.abs(delta);
1530 }
1531
1532 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001533 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001534
Winson Chungb44b5242011-06-13 11:32:14 -07001535 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1536 // loading associated pages until the scroll settles
1537 if (mDeferScrollUpdate) {
1538 loadAssociatedPages(mNextPage);
1539 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001540 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001541 }
Michael Jurka0142d492010-08-25 17:46:15 -07001542 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001543 invalidate();
1544 }
1545
Winson Chung321e9ee2010-08-09 13:37:56 -07001546 public void scrollLeft() {
1547 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001548 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001550 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001551 }
1552 }
1553
1554 public void scrollRight() {
1555 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001556 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001558 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001559 }
1560 }
1561
Winson Chung86f77532010-08-24 11:08:22 -07001562 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001563 int result = -1;
1564 if (v != null) {
1565 ViewParent vp = v.getParent();
1566 int count = getChildCount();
1567 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001568 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 return i;
1570 }
1571 }
1572 }
1573 return result;
1574 }
1575
1576 /**
1577 * @return True is long presses are still allowed for the current touch
1578 */
1579 public boolean allowLongPress() {
1580 return mAllowLongPress;
1581 }
1582
Michael Jurka0142d492010-08-25 17:46:15 -07001583 /**
1584 * Set true to allow long-press events to be triggered, usually checked by
1585 * {@link Launcher} to accept or block dpad-initiated long-presses.
1586 */
1587 public void setAllowLongPress(boolean allowLongPress) {
1588 mAllowLongPress = allowLongPress;
1589 }
1590
Winson Chung321e9ee2010-08-09 13:37:56 -07001591 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001592 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001593
1594 SavedState(Parcelable superState) {
1595 super(superState);
1596 }
1597
1598 private SavedState(Parcel in) {
1599 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001600 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001601 }
1602
1603 @Override
1604 public void writeToParcel(Parcel out, int flags) {
1605 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001606 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 }
1608
1609 public static final Parcelable.Creator<SavedState> CREATOR =
1610 new Parcelable.Creator<SavedState>() {
1611 public SavedState createFromParcel(Parcel in) {
1612 return new SavedState(in);
1613 }
1614
1615 public SavedState[] newArray(int size) {
1616 return new SavedState[size];
1617 }
1618 };
1619 }
1620
Winson Chungf314b0e2011-08-16 11:54:27 -07001621 protected void loadAssociatedPages(int page) {
1622 loadAssociatedPages(page, false);
1623 }
1624 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001625 if (mContentIsRefreshable) {
1626 final int count = getChildCount();
1627 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001628 int lowerPageBound = getAssociatedLowerPageBound(page);
1629 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001630 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1631 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001632 // First, clear any pages that should no longer be loaded
1633 for (int i = 0; i < count; ++i) {
1634 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001635 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001636 if (layout.getPageChildCount() > 0) {
1637 layout.removeAllViewsOnPage();
1638 }
1639 mDirtyPageContent.set(i, true);
1640 }
1641 }
1642 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001643 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001644 if ((i != page) && immediateAndOnly) {
1645 continue;
1646 }
Michael Jurka0142d492010-08-25 17:46:15 -07001647 if (lowerPageBound <= i && i <= upperPageBound) {
1648 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001649 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001650 mDirtyPageContent.set(i, false);
1651 }
Winson Chung86f77532010-08-24 11:08:22 -07001652 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001653 }
1654 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001655 }
1656 }
1657
Winson Chunge3193b92010-09-10 11:44:42 -07001658 protected int getAssociatedLowerPageBound(int page) {
1659 return Math.max(0, page - 1);
1660 }
1661 protected int getAssociatedUpperPageBound(int page) {
1662 final int count = getChildCount();
1663 return Math.min(page + 1, count - 1);
1664 }
1665
Winson Chung86f77532010-08-24 11:08:22 -07001666 /**
1667 * This method is called ONLY to synchronize the number of pages that the paged view has.
1668 * To actually fill the pages with information, implement syncPageItems() below. It is
1669 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1670 * and therefore, individual page items do not need to be updated in this method.
1671 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001672 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001673
1674 /**
1675 * This method is called to synchronize the items that are on a particular page. If views on
1676 * the page can be reused, then they should be updated within this method.
1677 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001678 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001679
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001680 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001681 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001682 }
1683 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001684 invalidatePageData(currentPage, false);
1685 }
1686 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001687 if (!mIsDataReady) {
1688 return;
1689 }
1690
Michael Jurka0142d492010-08-25 17:46:15 -07001691 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001692 // Force all scrolling-related behavior to end
1693 mScroller.forceFinished(true);
1694 mNextPage = INVALID_PAGE;
1695
Michael Jurka0142d492010-08-25 17:46:15 -07001696 // Update all the pages
1697 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001698
Winson Chung5a808352011-06-27 19:08:49 -07001699 // We must force a measure after we've loaded the pages to update the content width and
1700 // to determine the full scroll width
1701 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1702 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1703
1704 // Set a new page as the current page if necessary
1705 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001706 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001707 }
1708
Michael Jurka0142d492010-08-25 17:46:15 -07001709 // Mark each of the pages as dirty
1710 final int count = getChildCount();
1711 mDirtyPageContent.clear();
1712 for (int i = 0; i < count; ++i) {
1713 mDirtyPageContent.add(true);
1714 }
1715
1716 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001717 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001718 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001719 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001720 }
Winson Chung007c6982011-06-14 13:27:53 -07001721
Michael Jurkaafaa0502011-12-13 18:22:50 -08001722 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001723 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1724 // found
1725 if (mHasScrollIndicator && mScrollIndicator == null) {
1726 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001727 if (parent != null) {
1728 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1729 mHasScrollIndicator = mScrollIndicator != null;
1730 if (mHasScrollIndicator) {
1731 mScrollIndicator.setVisibility(View.VISIBLE);
1732 }
Winson Chung007c6982011-06-14 13:27:53 -07001733 }
1734 }
1735 return mScrollIndicator;
1736 }
1737
1738 protected boolean isScrollingIndicatorEnabled() {
Michael Jurkab1dfe252012-09-26 10:53:55 -07001739 return true;
Winson Chung007c6982011-06-14 13:27:53 -07001740 }
1741
Winson Chung5a808352011-06-27 19:08:49 -07001742 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1743 @Override
1744 public void run() {
1745 hideScrollingIndicator(false);
1746 }
1747 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001748 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001749 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001750 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001751 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001752 }
1753
Michael Jurka430e8a52011-08-08 15:52:14 -07001754 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001755 mShouldShowScrollIndicator = true;
1756 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001757 if (getChildCount() <= 1) return;
1758 if (!isScrollingIndicatorEnabled()) return;
1759
Michael Jurkabed61d22012-02-14 22:51:29 -08001760 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001761 getScrollingIndicator();
1762 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001763 // Fade the indicator in
1764 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001765 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001766 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001767 if (immediately || mScrollingPaused) {
Michael Jurka430e8a52011-08-08 15:52:14 -07001768 mScrollIndicator.setAlpha(1f);
1769 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001770 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f);
Michael Jurka430e8a52011-08-08 15:52:14 -07001771 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1772 mScrollIndicatorAnimator.start();
1773 }
Winson Chung007c6982011-06-14 13:27:53 -07001774 }
1775 }
1776
Adam Cohen21b41102011-11-01 17:29:52 -07001777 protected void cancelScrollingIndicatorAnimations() {
1778 if (mScrollIndicatorAnimator != null) {
1779 mScrollIndicatorAnimator.cancel();
1780 }
1781 }
1782
Winson Chung007c6982011-06-14 13:27:53 -07001783 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001784 if (getChildCount() <= 1) return;
1785 if (!isScrollingIndicatorEnabled()) return;
1786
1787 getScrollingIndicator();
1788 if (mScrollIndicator != null) {
1789 // Fade the indicator out
1790 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001791 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001792 if (immediately || mScrollingPaused) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001793 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001794 mScrollIndicator.setAlpha(0f);
1795 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001796 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f);
Winson Chung32174c82011-07-19 15:47:55 -07001797 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1798 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1799 private boolean cancelled = false;
1800 @Override
1801 public void onAnimationCancel(android.animation.Animator animation) {
1802 cancelled = true;
1803 }
1804 @Override
1805 public void onAnimationEnd(Animator animation) {
1806 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001807 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001808 }
1809 }
1810 });
1811 mScrollIndicatorAnimator.start();
1812 }
Winson Chung007c6982011-06-14 13:27:53 -07001813 }
1814 }
1815
Winson Chung32174c82011-07-19 15:47:55 -07001816 /**
1817 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1818 * fill its space on the track or not.
1819 */
1820 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001821 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001822 }
1823
Winson Chung007c6982011-06-14 13:27:53 -07001824 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001825 if (getChildCount() <= 1) return;
1826 if (!isScrollingIndicatorEnabled()) return;
1827
1828 getScrollingIndicator();
1829 if (mScrollIndicator != null) {
1830 updateScrollingIndicatorPosition();
1831 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001832 if (mShouldShowScrollIndicator) {
1833 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1834 }
Winson Chung007c6982011-06-14 13:27:53 -07001835 }
1836
Winson Chung007c6982011-06-14 13:27:53 -07001837 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001838 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001839 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001840 int numPages = getChildCount();
1841 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001842 int lastChildIndex = Math.max(0, getChildCount() - 1);
1843 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001844 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001845 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1846 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001847
Winson Chungae890b82011-09-13 18:08:54 -07001848 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001849 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001850 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001851 if (hasElasticScrollIndicator()) {
1852 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1853 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1854 mScrollIndicator.requestLayout();
1855 }
1856 } else {
1857 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1858 indicatorPos += indicatorCenterOffset;
1859 }
Winson Chung007c6982011-06-14 13:27:53 -07001860 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001861 }
1862
Winson Chung3ac74c52011-06-30 17:39:37 -07001863 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001864 }
1865
1866 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001867 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001868
1869 /* Accessibility */
1870 @Override
1871 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1872 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001873 info.setScrollable(getPageCount() > 1);
1874 if (getCurrentPage() < getPageCount() - 1) {
1875 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1876 }
1877 if (getCurrentPage() > 0) {
1878 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1879 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001880 }
1881
1882 @Override
1883 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1884 super.onInitializeAccessibilityEvent(event);
1885 event.setScrollable(true);
1886 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1887 event.setFromIndex(mCurrentPage);
1888 event.setToIndex(mCurrentPage);
1889 event.setItemCount(getChildCount());
1890 }
1891 }
1892
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001893 @Override
1894 public boolean performAccessibilityAction(int action, Bundle arguments) {
1895 if (super.performAccessibilityAction(action, arguments)) {
1896 return true;
1897 }
1898 switch (action) {
1899 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1900 if (getCurrentPage() < getPageCount() - 1) {
1901 scrollRight();
1902 return true;
1903 }
1904 } break;
1905 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1906 if (getCurrentPage() > 0) {
1907 scrollLeft();
1908 return true;
1909 }
1910 } break;
1911 }
1912 return false;
1913 }
1914
Winson Chung6a0f57d2011-06-29 20:10:49 -07001915 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001916 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001917 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001918 }
Winson Chungd11265e2011-08-30 13:37:23 -07001919
1920 @Override
1921 public boolean onHoverEvent(android.view.MotionEvent event) {
1922 return true;
1923 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001924}