blob: 2fc788b83bcc1e6c9f3c9c1cd511be8ba97e502e [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;
Winson Chung007c6982011-06-14 13:27:53 -0700185
Winson Chungb44b5242011-06-13 11:32:14 -0700186 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700187 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public interface PageSwitchListener {
190 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 public PagedView(Context context) {
194 this(context, null);
195 }
196
197 public PagedView(Context context, AttributeSet attrs) {
198 this(context, attrs, 0);
199 }
200
201 public PagedView(Context context, AttributeSet attrs, int defStyle) {
202 super(context, attrs, defStyle);
203
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 TypedArray a = context.obtainStyledAttributes(attrs,
205 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800206 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800212 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800214 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700215 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700216 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700217 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700218 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700219 mScrollIndicatorPaddingLeft =
220 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
221 mScrollIndicatorPaddingRight =
222 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700223 a.recycle();
224
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700226 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
228
229 /**
230 * Initializes various states for this workspace.
231 */
Michael Jurka0142d492010-08-25 17:46:15 -0700232 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700233 mDirtyPageContent = new ArrayList<Boolean>();
234 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800235 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700236 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700237 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700238
239 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
240 mTouchSlop = configuration.getScaledTouchSlop();
241 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
242 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700243 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800244
245 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
246 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
247 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700248 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 }
250
Winson Chung86f77532010-08-24 11:08:22 -0700251 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
252 mPageSwitchListener = pageSwitchListener;
253 if (mPageSwitchListener != null) {
254 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700255 }
256 }
257
258 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700259 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
260 * out pages.
261 */
262 protected void setDataIsReady() {
263 mIsDataReady = true;
264 }
265 protected boolean isDataReady() {
266 return mIsDataReady;
267 }
268
269 /**
Winson Chung86f77532010-08-24 11:08:22 -0700270 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 *
Winson Chung86f77532010-08-24 11:08:22 -0700272 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 */
Winson Chung86f77532010-08-24 11:08:22 -0700274 int getCurrentPage() {
275 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700276 }
Winson Chung360e63f2012-04-27 13:48:05 -0700277 int getNextPage() {
278 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
279 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700280
Winson Chung86f77532010-08-24 11:08:22 -0700281 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 return getChildCount();
283 }
284
Winson Chung86f77532010-08-24 11:08:22 -0700285 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 return getChildAt(index);
287 }
288
Adam Cohenae4f1552011-10-20 00:15:42 -0700289 protected int indexToPage(int index) {
290 return index;
291 }
292
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800294 * Updates the scroll of the current page immediately to its final scroll position. We use this
295 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
296 * the previous tab page.
297 */
298 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700299 int offset = getChildOffset(mCurrentPage);
300 int relOffset = getRelativeChildOffset(mCurrentPage);
301 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800302 scrollTo(newX, 0);
303 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800304 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800305 }
306
307 /**
Winson Chung86f77532010-08-24 11:08:22 -0700308 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 */
Winson Chung86f77532010-08-24 11:08:22 -0700310 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800311 if (!mScroller.isFinished()) {
312 mScroller.abortAnimation();
313 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800314 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
315 // the default
316 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800317 return;
318 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700319
Winson Chungbfeac062012-06-06 15:56:08 -0700320
Winson Chung86f77532010-08-24 11:08:22 -0700321 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800322 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700323 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700324 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800325 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 }
327
Michael Jurka0142d492010-08-25 17:46:15 -0700328 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700329 if (mPageSwitchListener != null) {
330 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 }
332 }
333
Michael Jurkace7e05f2011-02-01 22:02:35 -0800334 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700335 if (!mIsPageMoving) {
336 mIsPageMoving = true;
337 onPageBeginMoving();
338 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700339 }
340
Michael Jurkace7e05f2011-02-01 22:02:35 -0800341 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700342 if (mIsPageMoving) {
343 mIsPageMoving = false;
344 onPageEndMoving();
345 }
Michael Jurka0142d492010-08-25 17:46:15 -0700346 }
347
Adam Cohen26976d92011-03-22 15:33:33 -0700348 protected boolean isPageMoving() {
349 return mIsPageMoving;
350 }
351
Michael Jurka0142d492010-08-25 17:46:15 -0700352 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700353 protected void onPageBeginMoving() {
354 }
355
356 // a method that subclasses can override to add behavior
357 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700358 }
359
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 /**
Winson Chung86f77532010-08-24 11:08:22 -0700361 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 *
363 * @param l The listener used to respond to long clicks.
364 */
365 @Override
366 public void setOnLongClickListener(OnLongClickListener l) {
367 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700368 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700370 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372 }
373
374 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800375 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700376 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800377 }
378
379 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700380 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800381 mUnboundedScrollX = x;
382
383 if (x < 0) {
384 super.scrollTo(0, y);
385 if (mAllowOverScroll) {
386 overScroll(x);
387 }
388 } else if (x > mMaxScrollX) {
389 super.scrollTo(mMaxScrollX, y);
390 if (mAllowOverScroll) {
391 overScroll(x - mMaxScrollX);
392 }
393 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800394 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800395 super.scrollTo(x, y);
396 }
397
Michael Jurka0142d492010-08-25 17:46:15 -0700398 mTouchX = x;
399 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
400 }
401
402 // we moved this functionality to a helper function so SmoothPagedView can reuse it
403 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700405 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700406 if (getScrollX() != mScroller.getCurrX()
407 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700408 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700409 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
410 }
Michael Jurka0142d492010-08-25 17:46:15 -0700411 invalidate();
412 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700413 } else if (mNextPage != INVALID_PAGE) {
414 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700415 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700416 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700417
418 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700419 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700420 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700421 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700422 }
423
Adam Cohen73aa9752010-11-24 16:26:58 -0800424 // We don't want to trigger a page end moving unless the page has settled
425 // and the user has stopped scrolling
426 if (mTouchState == TOUCH_STATE_REST) {
427 pageEndMoving();
428 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700429
430 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700431 AccessibilityManager accessibilityManager = (AccessibilityManager)
432 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
433 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700434 AccessibilityEvent ev =
435 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
436 ev.getText().add(getCurrentPageDescription());
437 sendAccessibilityEventUnchecked(ev);
438 }
Michael Jurka0142d492010-08-25 17:46:15 -0700439 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700440 }
Michael Jurka0142d492010-08-25 17:46:15 -0700441 return false;
442 }
443
444 @Override
445 public void computeScroll() {
446 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 }
448
449 @Override
450 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 if (!mIsDataReady) {
452 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
453 return;
454 }
455
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
457 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700458 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
459 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 if (widthMode != MeasureSpec.EXACTLY) {
461 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
462 }
463
Winson Chung8aad6102012-05-11 16:27:49 -0700464 // Return early if we aren't given a proper dimension
465 if (widthSize <= 0 || heightSize <= 0) {
466 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
467 return;
468 }
469
Adam Lesinski6b879f02010-11-04 16:15:23 -0700470 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
471 * of the All apps view on XLarge displays to not take up more space then it needs. Width
472 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
473 * each page to have the same width.
474 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700475 int maxChildHeight = 0;
476
Michael Jurka8b805b12012-04-18 14:23:14 -0700477 final int verticalPadding = getPaddingTop() + getPaddingBottom();
478 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700479
Michael Jurka36fcb742011-04-06 17:08:58 -0700480
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700482 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700483 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 final int childCount = getChildCount();
485 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700486 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700487 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700488 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
489
490 int childWidthMode;
491 if (lp.width == LayoutParams.WRAP_CONTENT) {
492 childWidthMode = MeasureSpec.AT_MOST;
493 } else {
494 childWidthMode = MeasureSpec.EXACTLY;
495 }
496
497 int childHeightMode;
498 if (lp.height == LayoutParams.WRAP_CONTENT) {
499 childHeightMode = MeasureSpec.AT_MOST;
500 } else {
501 childHeightMode = MeasureSpec.EXACTLY;
502 }
503
504 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700505 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700506 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700507 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700508
509 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700510 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700511 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
512 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700513 }
514
515 if (heightMode == MeasureSpec.AT_MOST) {
516 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 }
Winson Chungae890b82011-09-13 18:08:54 -0700518
Winson Chungae890b82011-09-13 18:08:54 -0700519 setMeasuredDimension(widthSize, heightSize);
520
Winson Chung8aad6102012-05-11 16:27:49 -0700521 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
522 // We also wait until we set the measured dimensions before flushing the cache as well, to
523 // ensure that the cache is filled with good values.
524 invalidateCachedOffsets();
525
Winson Chunga128a7b2012-04-30 15:23:15 -0700526 if (childCount > 0) {
527 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
528 + getChildWidth(0));
529
530 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700531 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700532 // The gap between pages in the PagedView should be equal to the gap from the page
533 // to the edge of the screen (so it is not visible in the current screen). To
534 // account for unequal padding on each side of the paged view, we take the maximum
535 // of the left/right gap and use that as the gap between each page.
536 int offset = getRelativeChildOffset(0);
537 int spacing = Math.max(offset, widthSize - offset -
538 getChildAt(0).getMeasuredWidth());
539 setPageSpacing(spacing);
540 }
541 }
542
Adam Cohen25b29952011-11-02 14:49:06 -0700543 updateScrollingIndicatorPosition();
544
Adam Cohenfaa28302010-11-19 12:02:18 -0800545 if (childCount > 0) {
546 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
547 } else {
548 mMaxScrollX = 0;
549 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 }
551
Michael Jurka8c920dd2011-01-20 14:16:56 -0800552 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800553 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700554 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800555
Michael Jurka8c920dd2011-01-20 14:16:56 -0800556 final int pageCount = getChildCount();
557 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700558 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800559 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800560 }
561 setCurrentPage(newCurrentPage);
562 }
563
Michael Jurka8c920dd2011-01-20 14:16:56 -0800564 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
565 // 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 -0800566 // tightens the layout accordingly
567 public void setLayoutScale(float childrenScale) {
568 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700569 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800570
571 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
572 int childCount = getChildCount();
573 float childrenX[] = new float[childCount];
574 float childrenY[] = new float[childCount];
575 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700576 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800577 childrenX[i] = child.getX();
578 childrenY[i] = child.getY();
579 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700580 // Trigger a full re-layout (never just call onLayout directly!)
581 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
582 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
583 requestLayout();
584 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800586 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700587 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800588 child.setX(childrenX[i]);
589 child.setY(childrenY[i]);
590 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700591
Michael Jurkad3ef3062010-11-23 16:23:58 -0800592 // Also, the page offset has changed (since the pages are now smaller);
593 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800594 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595 }
596
Adam Cohen60b07122011-11-14 17:26:06 -0800597 public void setPageSpacing(int pageSpacing) {
598 mPageSpacing = pageSpacing;
599 invalidateCachedOffsets();
600 }
601
Winson Chung321e9ee2010-08-09 13:37:56 -0700602 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700603 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700604 if (!mIsDataReady) {
605 return;
606 }
607
Winson Chung785d2eb2011-04-14 16:08:02 -0700608 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700609 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700611 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700612
613 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700614 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800616 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700617 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700619 if (mCenterPagesVertically) {
620 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
621 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800622
Winson Chung785d2eb2011-04-14 16:08:02 -0700623 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700624 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800625 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700626 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 }
628 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700629
630 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
631 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800632 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700633 setHorizontalScrollBarEnabled(true);
634 mFirstLayout = false;
635 }
Winson Chunge3193b92010-09-10 11:44:42 -0700636 }
637
Adam Cohenf34bab52010-09-30 14:11:56 -0700638 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700639 if (isScrollingIndicatorEnabled()) {
640 updateScrollingIndicator();
641 }
Michael Jurka869390b2012-05-06 15:55:19 -0700642 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
643
644 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700645 for (int i = 0; i < getChildCount(); i++) {
646 View child = getChildAt(i);
647 if (child != null) {
648 float scrollProgress = getScrollProgress(screenCenter, child, i);
649 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800650 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700651 }
652 }
653 invalidate();
654 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700655 }
656
Winson Chunge3193b92010-09-10 11:44:42 -0700657 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700658 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700659 // This ensures that when children are added, they get the correct transforms / alphas
660 // in accordance with any scroll effects.
661 mForceScreenScrolled = true;
662 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700663 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700664 }
665
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 @Override
667 public void onChildViewRemoved(View parent, View child) {
668 }
669
Adam Cohen73894962011-10-31 13:17:17 -0700670 protected void invalidateCachedOffsets() {
671 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700672 if (count == 0) {
673 mChildOffsets = null;
674 mChildRelativeOffsets = null;
675 mChildOffsetsWithLayoutScale = null;
676 return;
677 }
Adam Cohen73894962011-10-31 13:17:17 -0700678
679 mChildOffsets = new int[count];
680 mChildRelativeOffsets = new int[count];
681 mChildOffsetsWithLayoutScale = new int[count];
682 for (int i = 0; i < count; i++) {
683 mChildOffsets[i] = -1;
684 mChildRelativeOffsets[i] = -1;
685 mChildOffsetsWithLayoutScale[i] = -1;
686 }
687 }
688
689 protected int getChildOffset(int index) {
690 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
691 mChildOffsets : mChildOffsetsWithLayoutScale;
692
693 if (childOffsets != null && childOffsets[index] != -1) {
694 return childOffsets[index];
695 } else {
696 if (getChildCount() == 0)
697 return 0;
698
699 int offset = getRelativeChildOffset(0);
700 for (int i = 0; i < index; ++i) {
701 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
702 }
703 if (childOffsets != null) {
704 childOffsets[index] = offset;
705 }
706 return offset;
707 }
708 }
709
710 protected int getRelativeChildOffset(int index) {
711 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
712 return mChildRelativeOffsets[index];
713 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int padding = getPaddingLeft() + getPaddingRight();
715 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700716 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
717 if (mChildRelativeOffsets != null) {
718 mChildRelativeOffsets[index] = offset;
719 }
720 return offset;
721 }
722 }
723
Adam Cohen73894962011-10-31 13:17:17 -0700724 protected int getScaledMeasuredWidth(View child) {
725 // This functions are called enough times that it actually makes a difference in the
726 // profiler -- so just inline the max() here
727 final int measuredWidth = child.getMeasuredWidth();
728 final int minWidth = mMinimumWidth;
729 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
730 return (int) (maxWidth * mLayoutScale + 0.5f);
731 }
732
Michael Jurkadde558b2011-11-09 22:09:06 -0800733 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700734 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700735
Michael Jurkac4fb9172010-09-02 17:19:20 -0700736 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700737 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700738 int leftScreen = 0;
739 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800740 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800741 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 currPage.getX() + currPage.getWidth() -
743 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700744 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800745 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700746 }
747 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800748 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800749 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700751 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800752 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700753 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800754 range[0] = leftScreen;
755 range[1] = rightScreen;
756 } else {
757 range[0] = -1;
758 range[1] = -1;
759 }
760 }
761
Michael Jurka920d7f42012-05-14 16:29:55 -0700762 protected boolean shouldDrawChild(View child) {
763 return child.getAlpha() > 0;
764 }
765
Michael Jurkadde558b2011-11-09 22:09:06 -0800766 @Override
767 protected void dispatchDraw(Canvas canvas) {
768 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
770 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800771 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800772
773 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700774 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
775 // set it for the next frame
776 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800777 screenScrolled(screenCenter);
778 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800779 }
780
781 // Find out which screens are visible; as an optimization we only call draw on them
782 final int pageCount = getChildCount();
783 if (pageCount > 0) {
784 getVisiblePages(mTempVisiblePagesRange);
785 final int leftScreen = mTempVisiblePagesRange[0];
786 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800787 if (leftScreen != -1 && rightScreen != -1) {
788 final long drawingTime = getDrawingTime();
789 // Clip to the bounds
790 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
792 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700793
Michael Jurka80c69852011-12-16 14:16:32 -0800794 for (int i = getChildCount() - 1; i >= 0; i--) {
795 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700796 if (mForceDrawAllChildrenNextFrame ||
797 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800798 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -0800799 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800800 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700801 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800802 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700803 }
Michael Jurka0142d492010-08-25 17:46:15 -0700804 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 }
806
807 @Override
808 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700809 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700810 if (page != mCurrentPage || !mScroller.isFinished()) {
811 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 return true;
813 }
814 return false;
815 }
816
817 @Override
818 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700819 int focusablePage;
820 if (mNextPage != INVALID_PAGE) {
821 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700823 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
Winson Chung86f77532010-08-24 11:08:22 -0700825 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700827 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 }
829 return false;
830 }
831
832 @Override
833 public boolean dispatchUnhandledMove(View focused, int direction) {
834 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700835 if (getCurrentPage() > 0) {
836 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 return true;
838 }
839 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (getCurrentPage() < getPageCount() - 1) {
841 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 return true;
843 }
844 }
845 return super.dispatchUnhandledMove(focused, direction);
846 }
847
848 @Override
849 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700850 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700851 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 }
853 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700854 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700855 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 }
857 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700858 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700859 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 }
861 }
862 }
863
864 /**
865 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700866 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 *
Winson Chung86f77532010-08-24 11:08:22 -0700868 * This happens when live folders requery, and if they're off page, they
869 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 */
871 @Override
872 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700873 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 View v = focused;
875 while (true) {
876 if (v == current) {
877 super.focusableViewAvailable(focused);
878 return;
879 }
880 if (v == this) {
881 return;
882 }
883 ViewParent parent = v.getParent();
884 if (parent instanceof View) {
885 v = (View)v.getParent();
886 } else {
887 return;
888 }
889 }
890 }
891
892 /**
893 * {@inheritDoc}
894 */
895 @Override
896 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
897 if (disallowIntercept) {
898 // We need to make sure to cancel our long press if
899 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700900 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700901 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 }
903 super.requestDisallowInterceptTouchEvent(disallowIntercept);
904 }
905
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800906 /**
907 * Return true if a tap at (x, y) should trigger a flip to the previous page.
908 */
909 protected boolean hitsPreviousPage(float x, float y) {
910 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
911 }
912
913 /**
914 * Return true if a tap at (x, y) should trigger a flip to the next page.
915 */
916 protected boolean hitsNextPage(float x, float y) {
917 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
918 }
919
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 @Override
921 public boolean onInterceptTouchEvent(MotionEvent ev) {
922 /*
923 * This method JUST determines whether we want to intercept the motion.
924 * If we return true, onTouchEvent will be called and we do the actual
925 * scrolling there.
926 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800927 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700928
Winson Chung45e1d6e2010-11-09 17:19:49 -0800929 // Skip touch handling if there are no pages to swipe
930 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
931
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 /*
933 * Shortcut the most recurring case: the user is in the dragging
934 * state and he is moving his finger. We want to intercept this
935 * motion.
936 */
937 final int action = ev.getAction();
938 if ((action == MotionEvent.ACTION_MOVE) &&
939 (mTouchState == TOUCH_STATE_SCROLLING)) {
940 return true;
941 }
942
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 switch (action & MotionEvent.ACTION_MASK) {
944 case MotionEvent.ACTION_MOVE: {
945 /*
946 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
947 * whether the user has moved far enough from his original down touch.
948 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700949 if (mActivePointerId != INVALID_POINTER) {
950 determineScrollingStart(ev);
951 break;
952 }
953 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
954 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
955 // i.e. fall through to the next case (don't break)
956 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
957 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 }
959
960 case MotionEvent.ACTION_DOWN: {
961 final float x = ev.getX();
962 final float y = ev.getY();
963 // Remember location of down touch
964 mDownMotionX = x;
965 mLastMotionX = x;
966 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800967 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800968 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 mActivePointerId = ev.getPointerId(0);
970 mAllowLongPress = true;
971
972 /*
973 * If being flinged and user touches the screen, initiate drag;
974 * otherwise don't. mScroller.isFinished should be false when
975 * being flinged.
976 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700977 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700978 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
979 if (finishedScrolling) {
980 mTouchState = TOUCH_STATE_REST;
981 mScroller.abortAnimation();
982 } else {
983 mTouchState = TOUCH_STATE_SCROLLING;
984 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700985
Winson Chung86f77532010-08-24 11:08:22 -0700986 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800988 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800990 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800992 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 mTouchState = TOUCH_STATE_NEXT_PAGE;
994 }
995 }
996 }
997 break;
998 }
999
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001001 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 mTouchState = TOUCH_STATE_REST;
1003 mAllowLongPress = false;
1004 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001005 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 break;
1007
1008 case MotionEvent.ACTION_POINTER_UP:
1009 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001010 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 break;
1012 }
1013
1014 /*
1015 * The only time we want to intercept motion events is if we are in the
1016 * drag mode.
1017 */
1018 return mTouchState != TOUCH_STATE_REST;
1019 }
1020
Adam Cohenf8d28232011-02-01 21:47:00 -08001021 protected void determineScrollingStart(MotionEvent ev) {
1022 determineScrollingStart(ev, 1.0f);
1023 }
1024
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 /*
1026 * Determines if we should change the touch state to start scrolling after the
1027 * user moves their touch point too far.
1028 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001029 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 /*
1031 * Locally do absolute value. mLastMotionX is set to the y value
1032 * of the down event.
1033 */
1034 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001035 if (pointerIndex == -1) {
1036 return;
1037 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 final float x = ev.getX(pointerIndex);
1039 final float y = ev.getY(pointerIndex);
1040 final int xDiff = (int) Math.abs(x - mLastMotionX);
1041 final int yDiff = (int) Math.abs(y - mLastMotionY);
1042
Adam Cohenf8d28232011-02-01 21:47:00 -08001043 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 boolean xPaged = xDiff > mPagingTouchSlop;
1045 boolean xMoved = xDiff > touchSlop;
1046 boolean yMoved = yDiff > touchSlop;
1047
Adam Cohenf8d28232011-02-01 21:47:00 -08001048 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001049 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001050 // Scroll if the user moved far enough along the X axis
1051 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001052 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001054 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001055 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001056 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1057 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 }
1059 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001060 cancelCurrentPageLongPress();
1061 }
1062 }
1063
1064 protected void cancelCurrentPageLongPress() {
1065 if (mAllowLongPress) {
1066 mAllowLongPress = false;
1067 // Try canceling the long press. It could also have been scheduled
1068 // by a distant descendant, so use the mAllowLongPress flag to block
1069 // everything
1070 final View currentPage = getPageAt(mCurrentPage);
1071 if (currentPage != null) {
1072 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074 }
1075 }
1076
Adam Cohenb5ba0972011-09-07 18:02:31 -07001077 protected float getScrollProgress(int screenCenter, View v, int page) {
1078 final int halfScreenSize = getMeasuredWidth() / 2;
1079
1080 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1081 int delta = screenCenter - (getChildOffset(page) -
1082 getRelativeChildOffset(page) + halfScreenSize);
1083
1084 float scrollProgress = delta / (totalDistance * 1.0f);
1085 scrollProgress = Math.min(scrollProgress, 1.0f);
1086 scrollProgress = Math.max(scrollProgress, -1.0f);
1087 return scrollProgress;
1088 }
1089
Adam Cohene0f66b52010-11-23 15:06:07 -08001090 // This curve determines how the effect of scrolling over the limits of the page dimishes
1091 // as the user pulls further and further from the bounds
1092 private float overScrollInfluenceCurve(float f) {
1093 f -= 1.0f;
1094 return f * f * f + 1.0f;
1095 }
1096
Adam Cohenb5ba0972011-09-07 18:02:31 -07001097 protected void acceleratedOverScroll(float amount) {
1098 int screenSize = getMeasuredWidth();
1099
1100 // We want to reach the max over scroll effect when the user has
1101 // over scrolled half the size of the screen
1102 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1103
1104 if (f == 0) return;
1105
1106 // Clamp this factor, f, to -1 < f < 1
1107 if (Math.abs(f) >= 1) {
1108 f /= Math.abs(f);
1109 }
1110
1111 int overScrollAmount = (int) Math.round(f * screenSize);
1112 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001113 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001114 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001115 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001116 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001117 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001118 }
1119 invalidate();
1120 }
1121
1122 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001123 int screenSize = getMeasuredWidth();
1124
1125 float f = (amount / screenSize);
1126
1127 if (f == 0) return;
1128 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1129
Adam Cohen7bfc9792011-01-28 13:52:37 -08001130 // Clamp this factor, f, to -1 < f < 1
1131 if (Math.abs(f) >= 1) {
1132 f /= Math.abs(f);
1133 }
1134
Adam Cohene0f66b52010-11-23 15:06:07 -08001135 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001136 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001137 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001138 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001139 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001140 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001141 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001142 }
1143 invalidate();
1144 }
1145
Adam Cohenb5ba0972011-09-07 18:02:31 -07001146 protected void overScroll(float amount) {
1147 dampedOverScroll(amount);
1148 }
1149
Michael Jurkac5b262c2011-01-12 20:24:50 -08001150 protected float maxOverScroll() {
1151 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001152 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001153 float f = 1.0f;
1154 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1155 return OVERSCROLL_DAMP_FACTOR * f;
1156 }
1157
Winson Chung321e9ee2010-08-09 13:37:56 -07001158 @Override
1159 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001160 // Skip touch handling if there are no pages to swipe
1161 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1162
Michael Jurkab8f06722010-10-10 15:58:46 -07001163 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001164
1165 final int action = ev.getAction();
1166
1167 switch (action & MotionEvent.ACTION_MASK) {
1168 case MotionEvent.ACTION_DOWN:
1169 /*
1170 * If being flinged and user touches, stop the fling. isFinished
1171 * will be false if being flinged.
1172 */
1173 if (!mScroller.isFinished()) {
1174 mScroller.abortAnimation();
1175 }
1176
1177 // Remember where the motion event started
1178 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001179 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001180 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001182 if (mTouchState == TOUCH_STATE_SCROLLING) {
1183 pageBeginMoving();
1184 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 break;
1186
1187 case MotionEvent.ACTION_MOVE:
1188 if (mTouchState == TOUCH_STATE_SCROLLING) {
1189 // Scroll to follow the motion event
1190 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1191 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001192 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001193
Adam Cohenaefd4e12011-02-14 16:39:38 -08001194 mTotalMotionX += Math.abs(deltaX);
1195
Winson Chungc0844aa2011-02-02 15:25:58 -08001196 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1197 // keep the remainder because we are actually testing if we've moved from the last
1198 // scrolled position (which is discrete).
1199 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001200 mTouchX += deltaX;
1201 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1202 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001203 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001204 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001205 } else {
1206 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001208 mLastMotionX = x;
1209 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 } else {
1211 awakenScrollBars();
1212 }
Adam Cohen564976a2010-10-13 18:52:07 -07001213 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 determineScrollingStart(ev);
1215 }
1216 break;
1217
1218 case MotionEvent.ACTION_UP:
1219 if (mTouchState == TOUCH_STATE_SCROLLING) {
1220 final int activePointerId = mActivePointerId;
1221 final int pointerIndex = ev.findPointerIndex(activePointerId);
1222 final float x = ev.getX(pointerIndex);
1223 final VelocityTracker velocityTracker = mVelocityTracker;
1224 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1225 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001226 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001227 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1228 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1229 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001230
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001231 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1232
Adam Cohen00481b32011-11-18 12:03:48 -08001233 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001234 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001235
Adam Cohenaefd4e12011-02-14 16:39:38 -08001236 // In the case that the page is moved far to one direction and then is flung
1237 // in the opposite direction, we use a threshold to determine whether we should
1238 // just return to the starting page, or if we should skip one further.
1239 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001240 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001241 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001242 returnToOriginalPage = true;
1243 }
1244
Adam Cohenaefd4e12011-02-14 16:39:38 -08001245 int finalPage;
1246 // We give flings precedence over large moves, which is why we short-circuit our
1247 // test for a large move if a fling has been registered. That is, a large
1248 // move to the left and fling to the right will register as a fling to the right.
1249 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1250 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1251 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1252 snapToPageWithVelocity(finalPage, velocityX);
1253 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1254 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001255 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001256 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1257 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 } else {
1259 snapToDestination();
1260 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001261 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 // at this point we have not moved beyond the touch slop
1263 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1264 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001265 int nextPage = Math.max(0, mCurrentPage - 1);
1266 if (nextPage != mCurrentPage) {
1267 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 } else {
1269 snapToDestination();
1270 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001271 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 // at this point we have not moved beyond the touch slop
1273 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1274 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001275 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1276 if (nextPage != mCurrentPage) {
1277 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 } else {
1279 snapToDestination();
1280 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001281 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001282 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 }
1284 mTouchState = TOUCH_STATE_REST;
1285 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001286 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001287 break;
1288
1289 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001290 if (mTouchState == TOUCH_STATE_SCROLLING) {
1291 snapToDestination();
1292 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 mTouchState = TOUCH_STATE_REST;
1294 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001295 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 break;
1297
1298 case MotionEvent.ACTION_POINTER_UP:
1299 onSecondaryPointerUp(ev);
1300 break;
1301 }
1302
1303 return true;
1304 }
1305
Winson Chung185d7162011-02-28 13:47:29 -08001306 @Override
1307 public boolean onGenericMotionEvent(MotionEvent event) {
1308 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1309 switch (event.getAction()) {
1310 case MotionEvent.ACTION_SCROLL: {
1311 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1312 final float vscroll;
1313 final float hscroll;
1314 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1315 vscroll = 0;
1316 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1317 } else {
1318 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1319 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1320 }
1321 if (hscroll != 0 || vscroll != 0) {
1322 if (hscroll > 0 || vscroll > 0) {
1323 scrollRight();
1324 } else {
1325 scrollLeft();
1326 }
1327 return true;
1328 }
1329 }
1330 }
1331 }
1332 return super.onGenericMotionEvent(event);
1333 }
1334
Michael Jurkab8f06722010-10-10 15:58:46 -07001335 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1336 if (mVelocityTracker == null) {
1337 mVelocityTracker = VelocityTracker.obtain();
1338 }
1339 mVelocityTracker.addMovement(ev);
1340 }
1341
1342 private void releaseVelocityTracker() {
1343 if (mVelocityTracker != null) {
1344 mVelocityTracker.recycle();
1345 mVelocityTracker = null;
1346 }
1347 }
1348
Winson Chung321e9ee2010-08-09 13:37:56 -07001349 private void onSecondaryPointerUp(MotionEvent ev) {
1350 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1351 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1352 final int pointerId = ev.getPointerId(pointerIndex);
1353 if (pointerId == mActivePointerId) {
1354 // This was our active pointer going up. Choose a new
1355 // active pointer and adjust accordingly.
1356 // TODO: Make this decision more intelligent.
1357 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1358 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1359 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001360 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 mActivePointerId = ev.getPointerId(newPointerIndex);
1362 if (mVelocityTracker != null) {
1363 mVelocityTracker.clear();
1364 }
1365 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001366 }
1367
Michael Jurkad771c962011-08-09 15:00:48 -07001368 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001369
1370 @Override
1371 public void requestChildFocus(View child, View focused) {
1372 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001373 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001374 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001375 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 }
1377 }
1378
Winson Chunge3193b92010-09-10 11:44:42 -07001379 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1380 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001381 int left;
1382 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001383 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001384 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001385 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001386 if (left <= relativeOffset && relativeOffset <= right) {
1387 return i;
1388 }
Winson Chunge3193b92010-09-10 11:44:42 -07001389 }
1390 return -1;
1391 }
1392
Winson Chung1908d072011-02-24 18:09:44 -08001393 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001394 // This functions are called enough times that it actually makes a difference in the
1395 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001396 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001397 final int minWidth = mMinimumWidth;
1398 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001399 }
1400
Adam Cohend19d3ca2010-09-15 14:43:42 -07001401 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001402 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001404 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 final int childCount = getChildCount();
1406 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001407 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001408 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 int halfChildWidth = (childWidth / 2);
1410 int childCenter = getChildOffset(i) + halfChildWidth;
1411 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1412 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1413 minDistanceFromScreenCenter = distanceFromScreenCenter;
1414 minDistanceFromScreenCenterIndex = i;
1415 }
1416 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001417 return minDistanceFromScreenCenterIndex;
1418 }
1419
1420 protected void snapToDestination() {
1421 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 }
1423
Adam Cohene0f66b52010-11-23 15:06:07 -08001424 private static class ScrollInterpolator implements Interpolator {
1425 public ScrollInterpolator() {
1426 }
1427
1428 public float getInterpolation(float t) {
1429 t -= 1.0f;
1430 return t*t*t*t*t + 1;
1431 }
1432 }
1433
1434 // We want the duration of the page snap animation to be influenced by the distance that
1435 // the screen has to travel, however, we don't want this duration to be effected in a
1436 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1437 // of travel has on the overall snap duration.
1438 float distanceInfluenceForSnapDuration(float f) {
1439 f -= 0.5f; // center the values about 0.
1440 f *= 0.3f * Math.PI / 2.0f;
1441 return (float) Math.sin(f);
1442 }
1443
Michael Jurka0142d492010-08-25 17:46:15 -07001444 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001445 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1446 int halfScreenSize = getMeasuredWidth() / 2;
1447
Winson Chung785d2eb2011-04-14 16:08:02 -07001448 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1449 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1450 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001451 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1452 int delta = newX - mUnboundedScrollX;
1453 int duration = 0;
1454
Adam Cohen265b9a62011-12-07 14:37:18 -08001455 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001456 // If the velocity is low enough, then treat this more as an automatic page advance
1457 // as opposed to an apparent physical response to flinging
1458 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1459 return;
1460 }
1461
1462 // Here we compute a "distance" that will be used in the computation of the overall
1463 // snap duration. This is a function of the actual distance that needs to be traveled;
1464 // we keep this value close to half screen size in order to reduce the variance in snap
1465 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001466 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001467 float distance = halfScreenSize + halfScreenSize *
1468 distanceInfluenceForSnapDuration(distanceRatio);
1469
1470 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001471 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001472
1473 // we want the page's snap velocity to approximately match the velocity at which the
1474 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001475 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1476 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Chet Haase97687ef2012-10-25 16:01:11 -07001477 duration = Math.min(duration, MAX_PAGE_SNAP_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001478
1479 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001480 }
1481
1482 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001483 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 }
1485
Michael Jurka0142d492010-08-25 17:46:15 -07001486 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001487 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001488
Winson Chung785d2eb2011-04-14 16:08:02 -07001489 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1490 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1491 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001492 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001493 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001495 snapToPage(whichPage, delta, duration);
1496 }
1497
1498 protected void snapToPage(int whichPage, int delta, int duration) {
1499 mNextPage = whichPage;
1500
1501 View focusedChild = getFocusedChild();
1502 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001503 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001504 focusedChild.clearFocus();
1505 }
1506
1507 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 awakenScrollBars(duration);
1509 if (duration == 0) {
1510 duration = Math.abs(delta);
1511 }
1512
1513 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001514 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001515
Winson Chungb44b5242011-06-13 11:32:14 -07001516 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1517 // loading associated pages until the scroll settles
1518 if (mDeferScrollUpdate) {
1519 loadAssociatedPages(mNextPage);
1520 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001521 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001522 }
Michael Jurka0142d492010-08-25 17:46:15 -07001523 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 invalidate();
1525 }
1526
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 public void scrollLeft() {
1528 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001529 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001531 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001532 }
1533 }
1534
1535 public void scrollRight() {
1536 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001537 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001538 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001539 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 }
1541 }
1542
Winson Chung86f77532010-08-24 11:08:22 -07001543 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 int result = -1;
1545 if (v != null) {
1546 ViewParent vp = v.getParent();
1547 int count = getChildCount();
1548 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001549 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 return i;
1551 }
1552 }
1553 }
1554 return result;
1555 }
1556
1557 /**
1558 * @return True is long presses are still allowed for the current touch
1559 */
1560 public boolean allowLongPress() {
1561 return mAllowLongPress;
1562 }
1563
Michael Jurka0142d492010-08-25 17:46:15 -07001564 /**
1565 * Set true to allow long-press events to be triggered, usually checked by
1566 * {@link Launcher} to accept or block dpad-initiated long-presses.
1567 */
1568 public void setAllowLongPress(boolean allowLongPress) {
1569 mAllowLongPress = allowLongPress;
1570 }
1571
Winson Chung321e9ee2010-08-09 13:37:56 -07001572 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001573 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001574
1575 SavedState(Parcelable superState) {
1576 super(superState);
1577 }
1578
1579 private SavedState(Parcel in) {
1580 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001581 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001582 }
1583
1584 @Override
1585 public void writeToParcel(Parcel out, int flags) {
1586 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001587 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001588 }
1589
1590 public static final Parcelable.Creator<SavedState> CREATOR =
1591 new Parcelable.Creator<SavedState>() {
1592 public SavedState createFromParcel(Parcel in) {
1593 return new SavedState(in);
1594 }
1595
1596 public SavedState[] newArray(int size) {
1597 return new SavedState[size];
1598 }
1599 };
1600 }
1601
Winson Chungf314b0e2011-08-16 11:54:27 -07001602 protected void loadAssociatedPages(int page) {
1603 loadAssociatedPages(page, false);
1604 }
1605 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001606 if (mContentIsRefreshable) {
1607 final int count = getChildCount();
1608 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001609 int lowerPageBound = getAssociatedLowerPageBound(page);
1610 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001611 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1612 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001613 // First, clear any pages that should no longer be loaded
1614 for (int i = 0; i < count; ++i) {
1615 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001616 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001617 if (layout.getPageChildCount() > 0) {
1618 layout.removeAllViewsOnPage();
1619 }
1620 mDirtyPageContent.set(i, true);
1621 }
1622 }
1623 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001624 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001625 if ((i != page) && immediateAndOnly) {
1626 continue;
1627 }
Michael Jurka0142d492010-08-25 17:46:15 -07001628 if (lowerPageBound <= i && i <= upperPageBound) {
1629 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001630 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001631 mDirtyPageContent.set(i, false);
1632 }
Winson Chung86f77532010-08-24 11:08:22 -07001633 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001634 }
1635 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001636 }
1637 }
1638
Winson Chunge3193b92010-09-10 11:44:42 -07001639 protected int getAssociatedLowerPageBound(int page) {
1640 return Math.max(0, page - 1);
1641 }
1642 protected int getAssociatedUpperPageBound(int page) {
1643 final int count = getChildCount();
1644 return Math.min(page + 1, count - 1);
1645 }
1646
Winson Chung86f77532010-08-24 11:08:22 -07001647 /**
1648 * This method is called ONLY to synchronize the number of pages that the paged view has.
1649 * To actually fill the pages with information, implement syncPageItems() below. It is
1650 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1651 * and therefore, individual page items do not need to be updated in this method.
1652 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001653 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001654
1655 /**
1656 * This method is called to synchronize the items that are on a particular page. If views on
1657 * the page can be reused, then they should be updated within this method.
1658 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001659 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001660
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001661 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001662 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001663 }
1664 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001665 invalidatePageData(currentPage, false);
1666 }
1667 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001668 if (!mIsDataReady) {
1669 return;
1670 }
1671
Michael Jurka0142d492010-08-25 17:46:15 -07001672 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001673 // Force all scrolling-related behavior to end
1674 mScroller.forceFinished(true);
1675 mNextPage = INVALID_PAGE;
1676
Michael Jurka0142d492010-08-25 17:46:15 -07001677 // Update all the pages
1678 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001679
Winson Chung5a808352011-06-27 19:08:49 -07001680 // We must force a measure after we've loaded the pages to update the content width and
1681 // to determine the full scroll width
1682 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1683 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1684
1685 // Set a new page as the current page if necessary
1686 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001687 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001688 }
1689
Michael Jurka0142d492010-08-25 17:46:15 -07001690 // Mark each of the pages as dirty
1691 final int count = getChildCount();
1692 mDirtyPageContent.clear();
1693 for (int i = 0; i < count; ++i) {
1694 mDirtyPageContent.add(true);
1695 }
1696
1697 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001698 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001699 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001700 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001701 }
Winson Chung007c6982011-06-14 13:27:53 -07001702
Michael Jurkaafaa0502011-12-13 18:22:50 -08001703 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001704 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1705 // found
1706 if (mHasScrollIndicator && mScrollIndicator == null) {
1707 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001708 if (parent != null) {
1709 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1710 mHasScrollIndicator = mScrollIndicator != null;
1711 if (mHasScrollIndicator) {
1712 mScrollIndicator.setVisibility(View.VISIBLE);
1713 }
Winson Chung007c6982011-06-14 13:27:53 -07001714 }
1715 }
1716 return mScrollIndicator;
1717 }
1718
1719 protected boolean isScrollingIndicatorEnabled() {
Michael Jurkab1dfe252012-09-26 10:53:55 -07001720 return true;
Winson Chung007c6982011-06-14 13:27:53 -07001721 }
1722
Winson Chung5a808352011-06-27 19:08:49 -07001723 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1724 @Override
1725 public void run() {
1726 hideScrollingIndicator(false);
1727 }
1728 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001729 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001730 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001731 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001732 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001733 }
1734
Michael Jurka430e8a52011-08-08 15:52:14 -07001735 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001736 mShouldShowScrollIndicator = true;
1737 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001738 if (getChildCount() <= 1) return;
1739 if (!isScrollingIndicatorEnabled()) return;
1740
Michael Jurkabed61d22012-02-14 22:51:29 -08001741 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001742 getScrollingIndicator();
1743 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001744 // Fade the indicator in
1745 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001746 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001747 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001748 if (immediately) {
1749 mScrollIndicator.setAlpha(1f);
1750 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001751 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f);
Michael Jurka430e8a52011-08-08 15:52:14 -07001752 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1753 mScrollIndicatorAnimator.start();
1754 }
Winson Chung007c6982011-06-14 13:27:53 -07001755 }
1756 }
1757
Adam Cohen21b41102011-11-01 17:29:52 -07001758 protected void cancelScrollingIndicatorAnimations() {
1759 if (mScrollIndicatorAnimator != null) {
1760 mScrollIndicatorAnimator.cancel();
1761 }
1762 }
1763
Winson Chung007c6982011-06-14 13:27:53 -07001764 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001765 if (getChildCount() <= 1) return;
1766 if (!isScrollingIndicatorEnabled()) return;
1767
1768 getScrollingIndicator();
1769 if (mScrollIndicator != null) {
1770 // Fade the indicator out
1771 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001772 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001773 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001774 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001775 mScrollIndicator.setAlpha(0f);
1776 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001777 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f);
Winson Chung32174c82011-07-19 15:47:55 -07001778 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1779 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1780 private boolean cancelled = false;
1781 @Override
1782 public void onAnimationCancel(android.animation.Animator animation) {
1783 cancelled = true;
1784 }
1785 @Override
1786 public void onAnimationEnd(Animator animation) {
1787 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001788 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001789 }
1790 }
1791 });
1792 mScrollIndicatorAnimator.start();
1793 }
Winson Chung007c6982011-06-14 13:27:53 -07001794 }
1795 }
1796
Winson Chung32174c82011-07-19 15:47:55 -07001797 /**
1798 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1799 * fill its space on the track or not.
1800 */
1801 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001802 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001803 }
1804
Winson Chung007c6982011-06-14 13:27:53 -07001805 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001806 if (getChildCount() <= 1) return;
1807 if (!isScrollingIndicatorEnabled()) return;
1808
1809 getScrollingIndicator();
1810 if (mScrollIndicator != null) {
1811 updateScrollingIndicatorPosition();
1812 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001813 if (mShouldShowScrollIndicator) {
1814 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1815 }
Winson Chung007c6982011-06-14 13:27:53 -07001816 }
1817
Winson Chung007c6982011-06-14 13:27:53 -07001818 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001819 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001820 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001821 int numPages = getChildCount();
1822 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001823 int lastChildIndex = Math.max(0, getChildCount() - 1);
1824 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001825 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001826 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1827 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001828
Winson Chungae890b82011-09-13 18:08:54 -07001829 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001830 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001831 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001832 if (hasElasticScrollIndicator()) {
1833 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1834 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1835 mScrollIndicator.requestLayout();
1836 }
1837 } else {
1838 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1839 indicatorPos += indicatorCenterOffset;
1840 }
Winson Chung007c6982011-06-14 13:27:53 -07001841 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001842 }
1843
Winson Chung3ac74c52011-06-30 17:39:37 -07001844 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001845 }
1846
1847 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001848 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001849
1850 /* Accessibility */
1851 @Override
1852 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1853 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001854 info.setScrollable(getPageCount() > 1);
1855 if (getCurrentPage() < getPageCount() - 1) {
1856 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1857 }
1858 if (getCurrentPage() > 0) {
1859 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1860 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001861 }
1862
1863 @Override
1864 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1865 super.onInitializeAccessibilityEvent(event);
1866 event.setScrollable(true);
1867 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1868 event.setFromIndex(mCurrentPage);
1869 event.setToIndex(mCurrentPage);
1870 event.setItemCount(getChildCount());
1871 }
1872 }
1873
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001874 @Override
1875 public boolean performAccessibilityAction(int action, Bundle arguments) {
1876 if (super.performAccessibilityAction(action, arguments)) {
1877 return true;
1878 }
1879 switch (action) {
1880 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1881 if (getCurrentPage() < getPageCount() - 1) {
1882 scrollRight();
1883 return true;
1884 }
1885 } break;
1886 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1887 if (getCurrentPage() > 0) {
1888 scrollLeft();
1889 return true;
1890 }
1891 } break;
1892 }
1893 return false;
1894 }
1895
Winson Chung6a0f57d2011-06-29 20:10:49 -07001896 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001897 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001898 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001899 }
Winson Chungd11265e2011-08-30 13:37:23 -07001900
1901 @Override
1902 public boolean onHoverEvent(android.view.MotionEvent event) {
1903 return true;
1904 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001905}