blob: 81ce6ea23679faf98c72ad01e36948f123a80c09 [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;
62 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohenb5ba0972011-09-07 18:02:31 -070065 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070066 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080067
Adam Cohenb64cb5a2011-02-15 13:53:42 -080068 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080069 // The page is moved more than halfway, automatically move to the next page on touch up.
70 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080071
Adam Cohen265b9a62011-12-07 14:37:18 -080072 // The following constants need to be scaled based on density. The scaled versions will be
73 // assigned to the corresponding member variables below.
74 private static final int FLING_THRESHOLD_VELOCITY = 500;
75 private static final int MIN_SNAP_VELOCITY = 1500;
76 private static final int MIN_FLING_VELOCITY = 250;
77
Winson Chung8aad6102012-05-11 16:27:49 -070078 static final int AUTOMATIC_PAGE_SPACING = -1;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 protected int mFlingThresholdVelocity;
81 protected int mMinFlingVelocity;
82 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070083
Adam Cohenb5ba0972011-09-07 18:02:31 -070084 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected float mSmoothingTime;
86 protected float mTouchX;
87
88 protected boolean mFirstLayout = true;
89
90 protected int mCurrentPage;
91 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080092 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070094 private VelocityTracker mVelocityTracker;
95
96 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080098 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080099 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800100 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700101 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700102 private int[] mChildOffsets;
103 private int[] mChildRelativeOffsets;
104 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected final static int TOUCH_STATE_REST = 0;
107 protected final static int TOUCH_STATE_SCROLLING = 1;
108 protected final static int TOUCH_STATE_PREV_PAGE = 2;
109 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700110 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700113 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Michael Jurka7426c422010-11-11 15:23:47 -0800119 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120 private int mPagingTouchSlop;
121 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800122 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700123 protected int mPageSpacing;
124 protected int mPageLayoutPaddingTop;
125 protected int mPageLayoutPaddingBottom;
126 protected int mPageLayoutPaddingLeft;
127 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700128 protected int mPageLayoutWidthGap;
129 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700130 protected int mCellCountX = 0;
131 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700132 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800133 protected boolean mAllowOverScroll = true;
134 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800135 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700136 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka8b805b12012-04-18 14:23:14 -0700138 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800139 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
140 // the screens from continuing to translate beyond the normal bounds.
141 protected int mOverScrollX;
142
Michael Jurka8c920dd2011-01-20 14:16:56 -0800143 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800144 protected float mLayoutScale = 1.0f;
145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka5f1c5092010-09-03 14:15:02 -0700148 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149
Winson Chung86f77532010-08-24 11:08:22 -0700150 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurkae326f182011-11-21 14:05:46 -0800152 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 // If true, syncPages and syncPageItems will be called to refresh pages
155 protected boolean mContentIsRefreshable = true;
156
157 // If true, modify alpha of neighboring pages as user scrolls left/right
158 protected boolean mFadeInAdjacentScreens = true;
159
160 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
161 // to switch to a new page
162 protected boolean mUsePagingTouchSlop = true;
163
Michael Jurka8b805b12012-04-18 14:23:14 -0700164 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700165 // (SmoothPagedView does this)
166 protected boolean mDeferScrollUpdate = false;
167
Patrick Dubroy1262e362010-10-06 15:49:50 -0700168 protected boolean mIsPageMoving = false;
169
Winson Chungf0ea4d32011-06-06 14:27:16 -0700170 // All syncs and layout passes are deferred until data is ready.
171 protected boolean mIsDataReady = false;
172
Winson Chung007c6982011-06-14 13:27:53 -0700173 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700174 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800175 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700176 private int mScrollIndicatorPaddingLeft;
177 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700178 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800179 private boolean mShouldShowScrollIndicator = false;
180 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700181 protected static final int sScrollIndicatorFadeInDuration = 150;
182 protected static final int sScrollIndicatorFadeOutDuration = 650;
183 protected static final int sScrollIndicatorFlashDuration = 650;
Chet Haasebc2f0822012-10-26 17:59:53 -0700184 private boolean mScrollingPaused = false;
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 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700308 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
309 * ends, {@link #resumeScrolling()} should be called, along with
310 * {@link #updateCurrentPageScroll()} to correctly set the final state and re-enable scrolling.
311 */
312 void pauseScrolling() {
313 mScroller.forceFinished(true);
314 cancelScrollingIndicatorAnimations();
315 mScrollingPaused = true;
316 }
317
318 /**
319 * Enables scrolling again.
320 * @see #pauseScrolling()
321 */
322 void resumeScrolling() {
323 mScrollingPaused = false;
324 }
325 /**
Winson Chung86f77532010-08-24 11:08:22 -0700326 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 */
Winson Chung86f77532010-08-24 11:08:22 -0700328 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800329 if (!mScroller.isFinished()) {
330 mScroller.abortAnimation();
331 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800332 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
333 // the default
334 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800335 return;
336 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700337
Winson Chungbfeac062012-06-06 15:56:08 -0700338
Winson Chung86f77532010-08-24 11:08:22 -0700339 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800340 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700341 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700342 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800343 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 }
345
Michael Jurka0142d492010-08-25 17:46:15 -0700346 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700347 if (mPageSwitchListener != null) {
348 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 }
350 }
351
Michael Jurkace7e05f2011-02-01 22:02:35 -0800352 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700353 if (!mIsPageMoving) {
354 mIsPageMoving = true;
355 onPageBeginMoving();
356 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700357 }
358
Michael Jurkace7e05f2011-02-01 22:02:35 -0800359 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700360 if (mIsPageMoving) {
361 mIsPageMoving = false;
362 onPageEndMoving();
363 }
Michael Jurka0142d492010-08-25 17:46:15 -0700364 }
365
Adam Cohen26976d92011-03-22 15:33:33 -0700366 protected boolean isPageMoving() {
367 return mIsPageMoving;
368 }
369
Michael Jurka0142d492010-08-25 17:46:15 -0700370 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700371 protected void onPageBeginMoving() {
372 }
373
374 // a method that subclasses can override to add behavior
375 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700376 }
377
Winson Chung321e9ee2010-08-09 13:37:56 -0700378 /**
Winson Chung86f77532010-08-24 11:08:22 -0700379 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 *
381 * @param l The listener used to respond to long clicks.
382 */
383 @Override
384 public void setOnLongClickListener(OnLongClickListener l) {
385 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700386 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700388 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
390 }
391
392 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800393 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700394 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800395 }
396
397 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700398 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800399 mUnboundedScrollX = x;
400
401 if (x < 0) {
402 super.scrollTo(0, y);
403 if (mAllowOverScroll) {
404 overScroll(x);
405 }
406 } else if (x > mMaxScrollX) {
407 super.scrollTo(mMaxScrollX, y);
408 if (mAllowOverScroll) {
409 overScroll(x - mMaxScrollX);
410 }
411 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800412 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800413 super.scrollTo(x, y);
414 }
415
Michael Jurka0142d492010-08-25 17:46:15 -0700416 mTouchX = x;
417 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
418 }
419
420 // we moved this functionality to a helper function so SmoothPagedView can reuse it
421 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700423 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700424 if (getScrollX() != mScroller.getCurrX()
425 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700426 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700427 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
428 }
Michael Jurka0142d492010-08-25 17:46:15 -0700429 invalidate();
430 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700431 } else if (mNextPage != INVALID_PAGE) {
432 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700433 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700434 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700435
436 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700437 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700438 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700439 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700440 }
441
Adam Cohen73aa9752010-11-24 16:26:58 -0800442 // We don't want to trigger a page end moving unless the page has settled
443 // and the user has stopped scrolling
444 if (mTouchState == TOUCH_STATE_REST) {
445 pageEndMoving();
446 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700447
448 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700449 AccessibilityManager accessibilityManager = (AccessibilityManager)
450 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
451 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700452 AccessibilityEvent ev =
453 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
454 ev.getText().add(getCurrentPageDescription());
455 sendAccessibilityEventUnchecked(ev);
456 }
Michael Jurka0142d492010-08-25 17:46:15 -0700457 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 }
Michael Jurka0142d492010-08-25 17:46:15 -0700459 return false;
460 }
461
462 @Override
463 public void computeScroll() {
464 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 }
466
467 @Override
468 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700469 if (!mIsDataReady) {
470 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
471 return;
472 }
473
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
475 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700476 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
477 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 if (widthMode != MeasureSpec.EXACTLY) {
479 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
480 }
481
Winson Chung8aad6102012-05-11 16:27:49 -0700482 // Return early if we aren't given a proper dimension
483 if (widthSize <= 0 || heightSize <= 0) {
484 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
485 return;
486 }
487
Adam Lesinski6b879f02010-11-04 16:15:23 -0700488 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
489 * of the All apps view on XLarge displays to not take up more space then it needs. Width
490 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
491 * each page to have the same width.
492 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700493 int maxChildHeight = 0;
494
Michael Jurka8b805b12012-04-18 14:23:14 -0700495 final int verticalPadding = getPaddingTop() + getPaddingBottom();
496 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700497
Michael Jurka36fcb742011-04-06 17:08:58 -0700498
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700500 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700501 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 final int childCount = getChildCount();
503 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700504 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700505 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700506 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
507
508 int childWidthMode;
509 if (lp.width == LayoutParams.WRAP_CONTENT) {
510 childWidthMode = MeasureSpec.AT_MOST;
511 } else {
512 childWidthMode = MeasureSpec.EXACTLY;
513 }
514
515 int childHeightMode;
516 if (lp.height == LayoutParams.WRAP_CONTENT) {
517 childHeightMode = MeasureSpec.AT_MOST;
518 } else {
519 childHeightMode = MeasureSpec.EXACTLY;
520 }
521
522 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700523 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700524 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700525 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700526
527 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700528 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700529 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
530 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700531 }
532
533 if (heightMode == MeasureSpec.AT_MOST) {
534 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 }
Winson Chungae890b82011-09-13 18:08:54 -0700536
Winson Chungae890b82011-09-13 18:08:54 -0700537 setMeasuredDimension(widthSize, heightSize);
538
Winson Chung8aad6102012-05-11 16:27:49 -0700539 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
540 // We also wait until we set the measured dimensions before flushing the cache as well, to
541 // ensure that the cache is filled with good values.
542 invalidateCachedOffsets();
543
Winson Chunga128a7b2012-04-30 15:23:15 -0700544 if (childCount > 0) {
545 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
546 + getChildWidth(0));
547
548 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700549 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700550 // The gap between pages in the PagedView should be equal to the gap from the page
551 // to the edge of the screen (so it is not visible in the current screen). To
552 // account for unequal padding on each side of the paged view, we take the maximum
553 // of the left/right gap and use that as the gap between each page.
554 int offset = getRelativeChildOffset(0);
555 int spacing = Math.max(offset, widthSize - offset -
556 getChildAt(0).getMeasuredWidth());
557 setPageSpacing(spacing);
558 }
559 }
560
Adam Cohen25b29952011-11-02 14:49:06 -0700561 updateScrollingIndicatorPosition();
562
Adam Cohenfaa28302010-11-19 12:02:18 -0800563 if (childCount > 0) {
564 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
565 } else {
566 mMaxScrollX = 0;
567 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569
Michael Jurka8c920dd2011-01-20 14:16:56 -0800570 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800571 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800573
Michael Jurka8c920dd2011-01-20 14:16:56 -0800574 final int pageCount = getChildCount();
575 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700576 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800577 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800578 }
579 setCurrentPage(newCurrentPage);
580 }
581
Michael Jurka8c920dd2011-01-20 14:16:56 -0800582 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
583 // 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 -0800584 // tightens the layout accordingly
585 public void setLayoutScale(float childrenScale) {
586 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700587 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800588
589 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
590 int childCount = getChildCount();
591 float childrenX[] = new float[childCount];
592 float childrenY[] = new float[childCount];
593 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700594 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595 childrenX[i] = child.getX();
596 childrenY[i] = child.getY();
597 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700598 // Trigger a full re-layout (never just call onLayout directly!)
599 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
600 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
601 requestLayout();
602 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700603 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800604 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700605 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800606 child.setX(childrenX[i]);
607 child.setY(childrenY[i]);
608 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700609
Michael Jurkad3ef3062010-11-23 16:23:58 -0800610 // Also, the page offset has changed (since the pages are now smaller);
611 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800612 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800613 }
614
Adam Cohen60b07122011-11-14 17:26:06 -0800615 public void setPageSpacing(int pageSpacing) {
616 mPageSpacing = pageSpacing;
617 invalidateCachedOffsets();
618 }
619
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700621 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700622 if (!mIsDataReady) {
623 return;
624 }
625
Winson Chung785d2eb2011-04-14 16:08:02 -0700626 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700629 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700630
631 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700632 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700633 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800634 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700635 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700637 if (mCenterPagesVertically) {
638 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
639 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800640
Winson Chung785d2eb2011-04-14 16:08:02 -0700641 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700642 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800643 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700644 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 }
646 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700647
648 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
649 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800650 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700651 setHorizontalScrollBarEnabled(true);
652 mFirstLayout = false;
653 }
Winson Chunge3193b92010-09-10 11:44:42 -0700654 }
655
Adam Cohenf34bab52010-09-30 14:11:56 -0700656 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700657 if (isScrollingIndicatorEnabled()) {
658 updateScrollingIndicator();
659 }
Michael Jurka869390b2012-05-06 15:55:19 -0700660 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
661
662 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700663 for (int i = 0; i < getChildCount(); i++) {
664 View child = getChildAt(i);
665 if (child != null) {
666 float scrollProgress = getScrollProgress(screenCenter, child, i);
667 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800668 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700669 }
670 }
671 invalidate();
672 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700673 }
674
Winson Chunge3193b92010-09-10 11:44:42 -0700675 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700677 // This ensures that when children are added, they get the correct transforms / alphas
678 // in accordance with any scroll effects.
679 mForceScreenScrolled = true;
680 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700681 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700682 }
683
Michael Jurka8b805b12012-04-18 14:23:14 -0700684 @Override
685 public void onChildViewRemoved(View parent, View child) {
686 }
687
Adam Cohen73894962011-10-31 13:17:17 -0700688 protected void invalidateCachedOffsets() {
689 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700690 if (count == 0) {
691 mChildOffsets = null;
692 mChildRelativeOffsets = null;
693 mChildOffsetsWithLayoutScale = null;
694 return;
695 }
Adam Cohen73894962011-10-31 13:17:17 -0700696
697 mChildOffsets = new int[count];
698 mChildRelativeOffsets = new int[count];
699 mChildOffsetsWithLayoutScale = new int[count];
700 for (int i = 0; i < count; i++) {
701 mChildOffsets[i] = -1;
702 mChildRelativeOffsets[i] = -1;
703 mChildOffsetsWithLayoutScale[i] = -1;
704 }
705 }
706
707 protected int getChildOffset(int index) {
708 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
709 mChildOffsets : mChildOffsetsWithLayoutScale;
710
711 if (childOffsets != null && childOffsets[index] != -1) {
712 return childOffsets[index];
713 } else {
714 if (getChildCount() == 0)
715 return 0;
716
717 int offset = getRelativeChildOffset(0);
718 for (int i = 0; i < index; ++i) {
719 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
720 }
721 if (childOffsets != null) {
722 childOffsets[index] = offset;
723 }
724 return offset;
725 }
726 }
727
728 protected int getRelativeChildOffset(int index) {
729 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
730 return mChildRelativeOffsets[index];
731 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 final int padding = getPaddingLeft() + getPaddingRight();
733 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700734 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
735 if (mChildRelativeOffsets != null) {
736 mChildRelativeOffsets[index] = offset;
737 }
738 return offset;
739 }
740 }
741
Adam Cohen73894962011-10-31 13:17:17 -0700742 protected int getScaledMeasuredWidth(View child) {
743 // This functions are called enough times that it actually makes a difference in the
744 // profiler -- so just inline the max() here
745 final int measuredWidth = child.getMeasuredWidth();
746 final int minWidth = mMinimumWidth;
747 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
748 return (int) (maxWidth * mLayoutScale + 0.5f);
749 }
750
Michael Jurkadde558b2011-11-09 22:09:06 -0800751 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700752 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700753
Michael Jurkac4fb9172010-09-02 17:19:20 -0700754 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700755 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700756 int leftScreen = 0;
757 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800758 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800759 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 currPage.getX() + currPage.getWidth() -
761 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700762 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800763 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700764 }
765 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800766 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800767 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700769 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800770 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700771 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800772 range[0] = leftScreen;
773 range[1] = rightScreen;
774 } else {
775 range[0] = -1;
776 range[1] = -1;
777 }
778 }
779
Michael Jurka920d7f42012-05-14 16:29:55 -0700780 protected boolean shouldDrawChild(View child) {
781 return child.getAlpha() > 0;
782 }
783
Michael Jurkadde558b2011-11-09 22:09:06 -0800784 @Override
785 protected void dispatchDraw(Canvas canvas) {
786 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
788 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800789 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800790
791 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700792 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
793 // set it for the next frame
794 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800795 screenScrolled(screenCenter);
796 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800797 }
798
799 // Find out which screens are visible; as an optimization we only call draw on them
800 final int pageCount = getChildCount();
801 if (pageCount > 0) {
802 getVisiblePages(mTempVisiblePagesRange);
803 final int leftScreen = mTempVisiblePagesRange[0];
804 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800805 if (leftScreen != -1 && rightScreen != -1) {
806 final long drawingTime = getDrawingTime();
807 // Clip to the bounds
808 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700809 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
810 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700811
Michael Jurka80c69852011-12-16 14:16:32 -0800812 for (int i = getChildCount() - 1; i >= 0; i--) {
813 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700814 if (mForceDrawAllChildrenNextFrame ||
815 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800816 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -0800817 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800818 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700819 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800820 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700821 }
Michael Jurka0142d492010-08-25 17:46:15 -0700822 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 }
824
825 @Override
826 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700827 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700828 if (page != mCurrentPage || !mScroller.isFinished()) {
829 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 return true;
831 }
832 return false;
833 }
834
835 @Override
836 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700837 int focusablePage;
838 if (mNextPage != INVALID_PAGE) {
839 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700841 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
Winson Chung86f77532010-08-24 11:08:22 -0700843 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700845 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 }
847 return false;
848 }
849
850 @Override
851 public boolean dispatchUnhandledMove(View focused, int direction) {
852 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700853 if (getCurrentPage() > 0) {
854 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 return true;
856 }
857 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700858 if (getCurrentPage() < getPageCount() - 1) {
859 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 return true;
861 }
862 }
863 return super.dispatchUnhandledMove(focused, direction);
864 }
865
866 @Override
867 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700868 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700869 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 }
871 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700872 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700873 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 }
875 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700876 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700877 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 }
879 }
880 }
881
882 /**
883 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700884 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 *
Winson Chung86f77532010-08-24 11:08:22 -0700886 * This happens when live folders requery, and if they're off page, they
887 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 */
889 @Override
890 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700891 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 View v = focused;
893 while (true) {
894 if (v == current) {
895 super.focusableViewAvailable(focused);
896 return;
897 }
898 if (v == this) {
899 return;
900 }
901 ViewParent parent = v.getParent();
902 if (parent instanceof View) {
903 v = (View)v.getParent();
904 } else {
905 return;
906 }
907 }
908 }
909
910 /**
911 * {@inheritDoc}
912 */
913 @Override
914 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
915 if (disallowIntercept) {
916 // We need to make sure to cancel our long press if
917 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700918 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700919 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 }
921 super.requestDisallowInterceptTouchEvent(disallowIntercept);
922 }
923
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800924 /**
925 * Return true if a tap at (x, y) should trigger a flip to the previous page.
926 */
927 protected boolean hitsPreviousPage(float x, float y) {
928 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
929 }
930
931 /**
932 * Return true if a tap at (x, y) should trigger a flip to the next page.
933 */
934 protected boolean hitsNextPage(float x, float y) {
935 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
936 }
937
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 @Override
939 public boolean onInterceptTouchEvent(MotionEvent ev) {
940 /*
941 * This method JUST determines whether we want to intercept the motion.
942 * If we return true, onTouchEvent will be called and we do the actual
943 * scrolling there.
944 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800945 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700946
Winson Chung45e1d6e2010-11-09 17:19:49 -0800947 // Skip touch handling if there are no pages to swipe
948 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
949
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 /*
951 * Shortcut the most recurring case: the user is in the dragging
952 * state and he is moving his finger. We want to intercept this
953 * motion.
954 */
955 final int action = ev.getAction();
956 if ((action == MotionEvent.ACTION_MOVE) &&
957 (mTouchState == TOUCH_STATE_SCROLLING)) {
958 return true;
959 }
960
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 switch (action & MotionEvent.ACTION_MASK) {
962 case MotionEvent.ACTION_MOVE: {
963 /*
964 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
965 * whether the user has moved far enough from his original down touch.
966 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700967 if (mActivePointerId != INVALID_POINTER) {
968 determineScrollingStart(ev);
969 break;
970 }
971 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
972 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
973 // i.e. fall through to the next case (don't break)
974 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
975 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 }
977
978 case MotionEvent.ACTION_DOWN: {
979 final float x = ev.getX();
980 final float y = ev.getY();
981 // Remember location of down touch
982 mDownMotionX = x;
983 mLastMotionX = x;
984 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800985 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800986 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 mActivePointerId = ev.getPointerId(0);
988 mAllowLongPress = true;
989
990 /*
991 * If being flinged and user touches the screen, initiate drag;
992 * otherwise don't. mScroller.isFinished should be false when
993 * being flinged.
994 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700995 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700996 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
997 if (finishedScrolling) {
998 mTouchState = TOUCH_STATE_REST;
999 mScroller.abortAnimation();
1000 } else {
1001 mTouchState = TOUCH_STATE_SCROLLING;
1002 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001003
Winson Chung86f77532010-08-24 11:08:22 -07001004 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001006 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001008 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001010 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 mTouchState = TOUCH_STATE_NEXT_PAGE;
1012 }
1013 }
1014 }
1015 break;
1016 }
1017
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001019 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 mTouchState = TOUCH_STATE_REST;
1021 mAllowLongPress = false;
1022 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001023 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 break;
1025
1026 case MotionEvent.ACTION_POINTER_UP:
1027 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001028 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 break;
1030 }
1031
1032 /*
1033 * The only time we want to intercept motion events is if we are in the
1034 * drag mode.
1035 */
1036 return mTouchState != TOUCH_STATE_REST;
1037 }
1038
Adam Cohenf8d28232011-02-01 21:47:00 -08001039 protected void determineScrollingStart(MotionEvent ev) {
1040 determineScrollingStart(ev, 1.0f);
1041 }
1042
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 /*
1044 * Determines if we should change the touch state to start scrolling after the
1045 * user moves their touch point too far.
1046 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001047 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 /*
1049 * Locally do absolute value. mLastMotionX is set to the y value
1050 * of the down event.
1051 */
1052 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001053 if (pointerIndex == -1) {
1054 return;
1055 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 final float x = ev.getX(pointerIndex);
1057 final float y = ev.getY(pointerIndex);
1058 final int xDiff = (int) Math.abs(x - mLastMotionX);
1059 final int yDiff = (int) Math.abs(y - mLastMotionY);
1060
Adam Cohenf8d28232011-02-01 21:47:00 -08001061 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 boolean xPaged = xDiff > mPagingTouchSlop;
1063 boolean xMoved = xDiff > touchSlop;
1064 boolean yMoved = yDiff > touchSlop;
1065
Adam Cohenf8d28232011-02-01 21:47:00 -08001066 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001067 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001068 // Scroll if the user moved far enough along the X axis
1069 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001070 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001072 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001073 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001074 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1075 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001076 }
1077 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001078 cancelCurrentPageLongPress();
1079 }
1080 }
1081
1082 protected void cancelCurrentPageLongPress() {
1083 if (mAllowLongPress) {
1084 mAllowLongPress = false;
1085 // Try canceling the long press. It could also have been scheduled
1086 // by a distant descendant, so use the mAllowLongPress flag to block
1087 // everything
1088 final View currentPage = getPageAt(mCurrentPage);
1089 if (currentPage != null) {
1090 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001091 }
1092 }
1093 }
1094
Adam Cohenb5ba0972011-09-07 18:02:31 -07001095 protected float getScrollProgress(int screenCenter, View v, int page) {
1096 final int halfScreenSize = getMeasuredWidth() / 2;
1097
1098 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1099 int delta = screenCenter - (getChildOffset(page) -
1100 getRelativeChildOffset(page) + halfScreenSize);
1101
1102 float scrollProgress = delta / (totalDistance * 1.0f);
1103 scrollProgress = Math.min(scrollProgress, 1.0f);
1104 scrollProgress = Math.max(scrollProgress, -1.0f);
1105 return scrollProgress;
1106 }
1107
Adam Cohene0f66b52010-11-23 15:06:07 -08001108 // This curve determines how the effect of scrolling over the limits of the page dimishes
1109 // as the user pulls further and further from the bounds
1110 private float overScrollInfluenceCurve(float f) {
1111 f -= 1.0f;
1112 return f * f * f + 1.0f;
1113 }
1114
Adam Cohenb5ba0972011-09-07 18:02:31 -07001115 protected void acceleratedOverScroll(float amount) {
1116 int screenSize = getMeasuredWidth();
1117
1118 // We want to reach the max over scroll effect when the user has
1119 // over scrolled half the size of the screen
1120 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1121
1122 if (f == 0) return;
1123
1124 // Clamp this factor, f, to -1 < f < 1
1125 if (Math.abs(f) >= 1) {
1126 f /= Math.abs(f);
1127 }
1128
1129 int overScrollAmount = (int) Math.round(f * screenSize);
1130 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001131 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001132 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001133 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001134 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001135 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001136 }
1137 invalidate();
1138 }
1139
1140 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001141 int screenSize = getMeasuredWidth();
1142
1143 float f = (amount / screenSize);
1144
1145 if (f == 0) return;
1146 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1147
Adam Cohen7bfc9792011-01-28 13:52:37 -08001148 // Clamp this factor, f, to -1 < f < 1
1149 if (Math.abs(f) >= 1) {
1150 f /= Math.abs(f);
1151 }
1152
Adam Cohene0f66b52010-11-23 15:06:07 -08001153 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001154 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001155 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001156 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001157 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001158 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001159 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001160 }
1161 invalidate();
1162 }
1163
Adam Cohenb5ba0972011-09-07 18:02:31 -07001164 protected void overScroll(float amount) {
1165 dampedOverScroll(amount);
1166 }
1167
Michael Jurkac5b262c2011-01-12 20:24:50 -08001168 protected float maxOverScroll() {
1169 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001170 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001171 float f = 1.0f;
1172 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1173 return OVERSCROLL_DAMP_FACTOR * f;
1174 }
1175
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 @Override
1177 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001178 // Skip touch handling if there are no pages to swipe
1179 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1180
Michael Jurkab8f06722010-10-10 15:58:46 -07001181 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001182
1183 final int action = ev.getAction();
1184
1185 switch (action & MotionEvent.ACTION_MASK) {
1186 case MotionEvent.ACTION_DOWN:
1187 /*
1188 * If being flinged and user touches, stop the fling. isFinished
1189 * will be false if being flinged.
1190 */
1191 if (!mScroller.isFinished()) {
1192 mScroller.abortAnimation();
1193 }
1194
1195 // Remember where the motion event started
1196 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001197 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001198 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001200 if (mTouchState == TOUCH_STATE_SCROLLING) {
1201 pageBeginMoving();
1202 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 break;
1204
1205 case MotionEvent.ACTION_MOVE:
1206 if (mTouchState == TOUCH_STATE_SCROLLING) {
1207 // Scroll to follow the motion event
1208 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1209 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001210 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001211
Adam Cohenaefd4e12011-02-14 16:39:38 -08001212 mTotalMotionX += Math.abs(deltaX);
1213
Winson Chungc0844aa2011-02-02 15:25:58 -08001214 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1215 // keep the remainder because we are actually testing if we've moved from the last
1216 // scrolled position (which is discrete).
1217 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001218 mTouchX += deltaX;
1219 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1220 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001221 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001222 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001223 } else {
1224 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001226 mLastMotionX = x;
1227 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 } else {
1229 awakenScrollBars();
1230 }
Adam Cohen564976a2010-10-13 18:52:07 -07001231 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 determineScrollingStart(ev);
1233 }
1234 break;
1235
1236 case MotionEvent.ACTION_UP:
1237 if (mTouchState == TOUCH_STATE_SCROLLING) {
1238 final int activePointerId = mActivePointerId;
1239 final int pointerIndex = ev.findPointerIndex(activePointerId);
1240 final float x = ev.getX(pointerIndex);
1241 final VelocityTracker velocityTracker = mVelocityTracker;
1242 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1243 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001244 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001245 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1246 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1247 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001248
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001249 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1250
Adam Cohen00481b32011-11-18 12:03:48 -08001251 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001252 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001253
Adam Cohenaefd4e12011-02-14 16:39:38 -08001254 // In the case that the page is moved far to one direction and then is flung
1255 // in the opposite direction, we use a threshold to determine whether we should
1256 // just return to the starting page, or if we should skip one further.
1257 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001258 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001259 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001260 returnToOriginalPage = true;
1261 }
1262
Adam Cohenaefd4e12011-02-14 16:39:38 -08001263 int finalPage;
1264 // We give flings precedence over large moves, which is why we short-circuit our
1265 // test for a large move if a fling has been registered. That is, a large
1266 // move to the left and fling to the right will register as a fling to the right.
1267 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1268 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1269 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1270 snapToPageWithVelocity(finalPage, velocityX);
1271 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1272 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001273 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001274 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1275 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 } else {
1277 snapToDestination();
1278 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001279 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 // at this point we have not moved beyond the touch slop
1281 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1282 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001283 int nextPage = Math.max(0, mCurrentPage - 1);
1284 if (nextPage != mCurrentPage) {
1285 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 } else {
1287 snapToDestination();
1288 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001289 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 // at this point we have not moved beyond the touch slop
1291 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1292 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001293 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1294 if (nextPage != mCurrentPage) {
1295 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 } else {
1297 snapToDestination();
1298 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001299 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001300 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 }
1302 mTouchState = TOUCH_STATE_REST;
1303 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001304 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 break;
1306
1307 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001308 if (mTouchState == TOUCH_STATE_SCROLLING) {
1309 snapToDestination();
1310 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 mTouchState = TOUCH_STATE_REST;
1312 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001313 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001314 break;
1315
1316 case MotionEvent.ACTION_POINTER_UP:
1317 onSecondaryPointerUp(ev);
1318 break;
1319 }
1320
1321 return true;
1322 }
1323
Winson Chung185d7162011-02-28 13:47:29 -08001324 @Override
1325 public boolean onGenericMotionEvent(MotionEvent event) {
1326 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1327 switch (event.getAction()) {
1328 case MotionEvent.ACTION_SCROLL: {
1329 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1330 final float vscroll;
1331 final float hscroll;
1332 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1333 vscroll = 0;
1334 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1335 } else {
1336 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1337 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1338 }
1339 if (hscroll != 0 || vscroll != 0) {
1340 if (hscroll > 0 || vscroll > 0) {
1341 scrollRight();
1342 } else {
1343 scrollLeft();
1344 }
1345 return true;
1346 }
1347 }
1348 }
1349 }
1350 return super.onGenericMotionEvent(event);
1351 }
1352
Michael Jurkab8f06722010-10-10 15:58:46 -07001353 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1354 if (mVelocityTracker == null) {
1355 mVelocityTracker = VelocityTracker.obtain();
1356 }
1357 mVelocityTracker.addMovement(ev);
1358 }
1359
1360 private void releaseVelocityTracker() {
1361 if (mVelocityTracker != null) {
1362 mVelocityTracker.recycle();
1363 mVelocityTracker = null;
1364 }
1365 }
1366
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 private void onSecondaryPointerUp(MotionEvent ev) {
1368 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1369 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1370 final int pointerId = ev.getPointerId(pointerIndex);
1371 if (pointerId == mActivePointerId) {
1372 // This was our active pointer going up. Choose a new
1373 // active pointer and adjust accordingly.
1374 // TODO: Make this decision more intelligent.
1375 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1376 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1377 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001378 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 mActivePointerId = ev.getPointerId(newPointerIndex);
1380 if (mVelocityTracker != null) {
1381 mVelocityTracker.clear();
1382 }
1383 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001384 }
1385
Michael Jurkad771c962011-08-09 15:00:48 -07001386 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001387
1388 @Override
1389 public void requestChildFocus(View child, View focused) {
1390 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001391 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001392 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001393 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 }
1395 }
1396
Winson Chunge3193b92010-09-10 11:44:42 -07001397 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1398 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001399 int left;
1400 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001401 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001402 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001403 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001404 if (left <= relativeOffset && relativeOffset <= right) {
1405 return i;
1406 }
Winson Chunge3193b92010-09-10 11:44:42 -07001407 }
1408 return -1;
1409 }
1410
Winson Chung1908d072011-02-24 18:09:44 -08001411 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001412 // This functions are called enough times that it actually makes a difference in the
1413 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001414 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001415 final int minWidth = mMinimumWidth;
1416 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001417 }
1418
Adam Cohend19d3ca2010-09-15 14:43:42 -07001419 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001420 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001422 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001423 final int childCount = getChildCount();
1424 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001425 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001426 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 int halfChildWidth = (childWidth / 2);
1428 int childCenter = getChildOffset(i) + halfChildWidth;
1429 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1430 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1431 minDistanceFromScreenCenter = distanceFromScreenCenter;
1432 minDistanceFromScreenCenterIndex = i;
1433 }
1434 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001435 return minDistanceFromScreenCenterIndex;
1436 }
1437
1438 protected void snapToDestination() {
1439 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 }
1441
Adam Cohene0f66b52010-11-23 15:06:07 -08001442 private static class ScrollInterpolator implements Interpolator {
1443 public ScrollInterpolator() {
1444 }
1445
1446 public float getInterpolation(float t) {
1447 t -= 1.0f;
1448 return t*t*t*t*t + 1;
1449 }
1450 }
1451
1452 // We want the duration of the page snap animation to be influenced by the distance that
1453 // the screen has to travel, however, we don't want this duration to be effected in a
1454 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1455 // of travel has on the overall snap duration.
1456 float distanceInfluenceForSnapDuration(float f) {
1457 f -= 0.5f; // center the values about 0.
1458 f *= 0.3f * Math.PI / 2.0f;
1459 return (float) Math.sin(f);
1460 }
1461
Michael Jurka0142d492010-08-25 17:46:15 -07001462 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001463 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1464 int halfScreenSize = getMeasuredWidth() / 2;
1465
Winson Chung785d2eb2011-04-14 16:08:02 -07001466 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1467 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1468 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001469 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1470 int delta = newX - mUnboundedScrollX;
1471 int duration = 0;
1472
Adam Cohen265b9a62011-12-07 14:37:18 -08001473 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001474 // If the velocity is low enough, then treat this more as an automatic page advance
1475 // as opposed to an apparent physical response to flinging
1476 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1477 return;
1478 }
1479
1480 // Here we compute a "distance" that will be used in the computation of the overall
1481 // snap duration. This is a function of the actual distance that needs to be traveled;
1482 // we keep this value close to half screen size in order to reduce the variance in snap
1483 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001484 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001485 float distance = halfScreenSize + halfScreenSize *
1486 distanceInfluenceForSnapDuration(distanceRatio);
1487
1488 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001489 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001490
1491 // we want the page's snap velocity to approximately match the velocity at which the
1492 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001493 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1494 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001495
1496 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001497 }
1498
1499 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001500 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001501 }
1502
Michael Jurka0142d492010-08-25 17:46:15 -07001503 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001504 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001505
Winson Chung785d2eb2011-04-14 16:08:02 -07001506 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1507 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1508 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001509 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001510 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001512 snapToPage(whichPage, delta, duration);
1513 }
1514
1515 protected void snapToPage(int whichPage, int delta, int duration) {
1516 mNextPage = whichPage;
1517
1518 View focusedChild = getFocusedChild();
1519 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001520 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001521 focusedChild.clearFocus();
1522 }
1523
1524 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 awakenScrollBars(duration);
1526 if (duration == 0) {
1527 duration = Math.abs(delta);
1528 }
1529
1530 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001531 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001532
Winson Chungb44b5242011-06-13 11:32:14 -07001533 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1534 // loading associated pages until the scroll settles
1535 if (mDeferScrollUpdate) {
1536 loadAssociatedPages(mNextPage);
1537 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001538 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001539 }
Michael Jurka0142d492010-08-25 17:46:15 -07001540 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001541 invalidate();
1542 }
1543
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 public void scrollLeft() {
1545 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001546 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001547 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001548 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 }
1550 }
1551
1552 public void scrollRight() {
1553 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001554 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001555 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001556 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 }
1558 }
1559
Winson Chung86f77532010-08-24 11:08:22 -07001560 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001561 int result = -1;
1562 if (v != null) {
1563 ViewParent vp = v.getParent();
1564 int count = getChildCount();
1565 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001566 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001567 return i;
1568 }
1569 }
1570 }
1571 return result;
1572 }
1573
1574 /**
1575 * @return True is long presses are still allowed for the current touch
1576 */
1577 public boolean allowLongPress() {
1578 return mAllowLongPress;
1579 }
1580
Michael Jurka0142d492010-08-25 17:46:15 -07001581 /**
1582 * Set true to allow long-press events to be triggered, usually checked by
1583 * {@link Launcher} to accept or block dpad-initiated long-presses.
1584 */
1585 public void setAllowLongPress(boolean allowLongPress) {
1586 mAllowLongPress = allowLongPress;
1587 }
1588
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001590 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001591
1592 SavedState(Parcelable superState) {
1593 super(superState);
1594 }
1595
1596 private SavedState(Parcel in) {
1597 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001598 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001599 }
1600
1601 @Override
1602 public void writeToParcel(Parcel out, int flags) {
1603 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001604 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001605 }
1606
1607 public static final Parcelable.Creator<SavedState> CREATOR =
1608 new Parcelable.Creator<SavedState>() {
1609 public SavedState createFromParcel(Parcel in) {
1610 return new SavedState(in);
1611 }
1612
1613 public SavedState[] newArray(int size) {
1614 return new SavedState[size];
1615 }
1616 };
1617 }
1618
Winson Chungf314b0e2011-08-16 11:54:27 -07001619 protected void loadAssociatedPages(int page) {
1620 loadAssociatedPages(page, false);
1621 }
1622 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001623 if (mContentIsRefreshable) {
1624 final int count = getChildCount();
1625 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001626 int lowerPageBound = getAssociatedLowerPageBound(page);
1627 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1629 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001630 // First, clear any pages that should no longer be loaded
1631 for (int i = 0; i < count; ++i) {
1632 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001633 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001634 if (layout.getPageChildCount() > 0) {
1635 layout.removeAllViewsOnPage();
1636 }
1637 mDirtyPageContent.set(i, true);
1638 }
1639 }
1640 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001641 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001642 if ((i != page) && immediateAndOnly) {
1643 continue;
1644 }
Michael Jurka0142d492010-08-25 17:46:15 -07001645 if (lowerPageBound <= i && i <= upperPageBound) {
1646 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001647 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001648 mDirtyPageContent.set(i, false);
1649 }
Winson Chung86f77532010-08-24 11:08:22 -07001650 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001651 }
1652 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001653 }
1654 }
1655
Winson Chunge3193b92010-09-10 11:44:42 -07001656 protected int getAssociatedLowerPageBound(int page) {
1657 return Math.max(0, page - 1);
1658 }
1659 protected int getAssociatedUpperPageBound(int page) {
1660 final int count = getChildCount();
1661 return Math.min(page + 1, count - 1);
1662 }
1663
Winson Chung86f77532010-08-24 11:08:22 -07001664 /**
1665 * This method is called ONLY to synchronize the number of pages that the paged view has.
1666 * To actually fill the pages with information, implement syncPageItems() below. It is
1667 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1668 * and therefore, individual page items do not need to be updated in this method.
1669 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001670 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001671
1672 /**
1673 * This method is called to synchronize the items that are on a particular page. If views on
1674 * the page can be reused, then they should be updated within this method.
1675 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001676 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001677
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001678 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001679 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001680 }
1681 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001682 invalidatePageData(currentPage, false);
1683 }
1684 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001685 if (!mIsDataReady) {
1686 return;
1687 }
1688
Michael Jurka0142d492010-08-25 17:46:15 -07001689 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001690 // Force all scrolling-related behavior to end
1691 mScroller.forceFinished(true);
1692 mNextPage = INVALID_PAGE;
1693
Michael Jurka0142d492010-08-25 17:46:15 -07001694 // Update all the pages
1695 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001696
Winson Chung5a808352011-06-27 19:08:49 -07001697 // We must force a measure after we've loaded the pages to update the content width and
1698 // to determine the full scroll width
1699 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1700 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1701
1702 // Set a new page as the current page if necessary
1703 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001704 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001705 }
1706
Michael Jurka0142d492010-08-25 17:46:15 -07001707 // Mark each of the pages as dirty
1708 final int count = getChildCount();
1709 mDirtyPageContent.clear();
1710 for (int i = 0; i < count; ++i) {
1711 mDirtyPageContent.add(true);
1712 }
1713
1714 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001715 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001716 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001717 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001718 }
Winson Chung007c6982011-06-14 13:27:53 -07001719
Michael Jurkaafaa0502011-12-13 18:22:50 -08001720 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001721 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1722 // found
1723 if (mHasScrollIndicator && mScrollIndicator == null) {
1724 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001725 if (parent != null) {
1726 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1727 mHasScrollIndicator = mScrollIndicator != null;
1728 if (mHasScrollIndicator) {
1729 mScrollIndicator.setVisibility(View.VISIBLE);
1730 }
Winson Chung007c6982011-06-14 13:27:53 -07001731 }
1732 }
1733 return mScrollIndicator;
1734 }
1735
1736 protected boolean isScrollingIndicatorEnabled() {
Michael Jurkab1dfe252012-09-26 10:53:55 -07001737 return true;
Winson Chung007c6982011-06-14 13:27:53 -07001738 }
1739
Winson Chung5a808352011-06-27 19:08:49 -07001740 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1741 @Override
1742 public void run() {
1743 hideScrollingIndicator(false);
1744 }
1745 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001746 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001747 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001748 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001749 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001750 }
1751
Michael Jurka430e8a52011-08-08 15:52:14 -07001752 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001753 mShouldShowScrollIndicator = true;
1754 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001755 if (getChildCount() <= 1) return;
1756 if (!isScrollingIndicatorEnabled()) return;
1757
Michael Jurkabed61d22012-02-14 22:51:29 -08001758 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001759 getScrollingIndicator();
1760 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001761 // Fade the indicator in
1762 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001763 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001764 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001765 if (immediately || mScrollingPaused) {
Michael Jurka430e8a52011-08-08 15:52:14 -07001766 mScrollIndicator.setAlpha(1f);
1767 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001768 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f);
Michael Jurka430e8a52011-08-08 15:52:14 -07001769 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1770 mScrollIndicatorAnimator.start();
1771 }
Winson Chung007c6982011-06-14 13:27:53 -07001772 }
1773 }
1774
Adam Cohen21b41102011-11-01 17:29:52 -07001775 protected void cancelScrollingIndicatorAnimations() {
1776 if (mScrollIndicatorAnimator != null) {
1777 mScrollIndicatorAnimator.cancel();
1778 }
1779 }
1780
Winson Chung007c6982011-06-14 13:27:53 -07001781 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001782 if (getChildCount() <= 1) return;
1783 if (!isScrollingIndicatorEnabled()) return;
1784
1785 getScrollingIndicator();
1786 if (mScrollIndicator != null) {
1787 // Fade the indicator out
1788 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001789 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001790 if (immediately || mScrollingPaused) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001791 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001792 mScrollIndicator.setAlpha(0f);
1793 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001794 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f);
Winson Chung32174c82011-07-19 15:47:55 -07001795 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1796 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1797 private boolean cancelled = false;
1798 @Override
1799 public void onAnimationCancel(android.animation.Animator animation) {
1800 cancelled = true;
1801 }
1802 @Override
1803 public void onAnimationEnd(Animator animation) {
1804 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001805 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001806 }
1807 }
1808 });
1809 mScrollIndicatorAnimator.start();
1810 }
Winson Chung007c6982011-06-14 13:27:53 -07001811 }
1812 }
1813
Winson Chung32174c82011-07-19 15:47:55 -07001814 /**
1815 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1816 * fill its space on the track or not.
1817 */
1818 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001819 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001820 }
1821
Winson Chung007c6982011-06-14 13:27:53 -07001822 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001823 if (getChildCount() <= 1) return;
1824 if (!isScrollingIndicatorEnabled()) return;
1825
1826 getScrollingIndicator();
1827 if (mScrollIndicator != null) {
1828 updateScrollingIndicatorPosition();
1829 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001830 if (mShouldShowScrollIndicator) {
1831 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1832 }
Winson Chung007c6982011-06-14 13:27:53 -07001833 }
1834
Winson Chung007c6982011-06-14 13:27:53 -07001835 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001836 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001837 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001838 int numPages = getChildCount();
1839 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001840 int lastChildIndex = Math.max(0, getChildCount() - 1);
1841 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001842 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001843 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1844 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001845
Winson Chungae890b82011-09-13 18:08:54 -07001846 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001847 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001848 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001849 if (hasElasticScrollIndicator()) {
1850 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1851 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1852 mScrollIndicator.requestLayout();
1853 }
1854 } else {
1855 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1856 indicatorPos += indicatorCenterOffset;
1857 }
Winson Chung007c6982011-06-14 13:27:53 -07001858 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001859 }
1860
Winson Chung3ac74c52011-06-30 17:39:37 -07001861 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001862 }
1863
1864 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001865 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001866
1867 /* Accessibility */
1868 @Override
1869 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1870 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001871 info.setScrollable(getPageCount() > 1);
1872 if (getCurrentPage() < getPageCount() - 1) {
1873 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1874 }
1875 if (getCurrentPage() > 0) {
1876 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1877 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001878 }
1879
1880 @Override
1881 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1882 super.onInitializeAccessibilityEvent(event);
1883 event.setScrollable(true);
1884 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1885 event.setFromIndex(mCurrentPage);
1886 event.setToIndex(mCurrentPage);
1887 event.setItemCount(getChildCount());
1888 }
1889 }
1890
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001891 @Override
1892 public boolean performAccessibilityAction(int action, Bundle arguments) {
1893 if (super.performAccessibilityAction(action, arguments)) {
1894 return true;
1895 }
1896 switch (action) {
1897 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1898 if (getCurrentPage() < getPageCount() - 1) {
1899 scrollRight();
1900 return true;
1901 }
1902 } break;
1903 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1904 if (getCurrentPage() > 0) {
1905 scrollLeft();
1906 return true;
1907 }
1908 } break;
1909 }
1910 return false;
1911 }
1912
Winson Chung6a0f57d2011-06-29 20:10:49 -07001913 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001914 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001915 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001916 }
Winson Chungd11265e2011-08-30 13:37:23 -07001917
1918 @Override
1919 public boolean onHoverEvent(android.view.MotionEvent event) {
1920 return true;
1921 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001922}