blob: ad0baf44dc85e53143219aec9e3754efa01158e1 [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;
21import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070022import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070024import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070027import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080032import android.view.InputDevice;
33import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.MotionEvent;
35import android.view.VelocityTracker;
36import android.view.View;
37import android.view.ViewConfiguration;
38import android.view.ViewGroup;
39import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070040import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070041import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070042import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080043import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.widget.Scroller;
45
Winson Chung04998342011-01-05 13:54:43 -080046import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070047
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import java.util.ArrayList;
49
Winson Chung321e9ee2010-08-09 13:37:56 -070050/**
51 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070052 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070053 */
Michael Jurka8b805b12012-04-18 14:23:14 -070054public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070055 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070056 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070057 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070058
Winson Chung86f77532010-08-24 11:08:22 -070059 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070060 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chungf0c6ae02012-03-21 16:10:31 -070062 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
63 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Adam Cohenb5ba0972011-09-07 18:02:31 -070066 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070067 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080068
Adam Cohenb64cb5a2011-02-15 13:53:42 -080069 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080070 // The page is moved more than halfway, automatically move to the next page on touch up.
71 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080072
Adam Cohen265b9a62011-12-07 14:37:18 -080073 // The following constants need to be scaled based on density. The scaled versions will be
74 // assigned to the corresponding member variables below.
75 private static final int FLING_THRESHOLD_VELOCITY = 500;
76 private static final int MIN_SNAP_VELOCITY = 1500;
77 private static final int MIN_FLING_VELOCITY = 250;
78
Winson Chung8aad6102012-05-11 16:27:49 -070079 static final int AUTOMATIC_PAGE_SPACING = -1;
80
Adam Cohen265b9a62011-12-07 14:37:18 -080081 protected int mFlingThresholdVelocity;
82 protected int mMinFlingVelocity;
83 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070084
Adam Cohenb5ba0972011-09-07 18:02:31 -070085 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070086 protected float mSmoothingTime;
87 protected float mTouchX;
88
89 protected boolean mFirstLayout = true;
90
91 protected int mCurrentPage;
92 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080093 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070095 private VelocityTracker mVelocityTracker;
96
97 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080099 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800101 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700102 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700103 private int[] mChildOffsets;
104 private int[] mChildRelativeOffsets;
105 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected final static int TOUCH_STATE_REST = 0;
108 protected final static int TOUCH_STATE_SCROLLING = 1;
109 protected final static int TOUCH_STATE_PREV_PAGE = 2;
110 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700111 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700114 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Michael Jurka0142d492010-08-25 17:46:15 -0700116 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka7426c422010-11-11 15:23:47 -0800118 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka7426c422010-11-11 15:23:47 -0800120 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121 private int mPagingTouchSlop;
122 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800123 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700124 protected int mPageSpacing;
125 protected int mPageLayoutPaddingTop;
126 protected int mPageLayoutPaddingBottom;
127 protected int mPageLayoutPaddingLeft;
128 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700129 protected int mPageLayoutWidthGap;
130 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700131 protected int mCellCountX = 0;
132 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700133 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800134 protected boolean mAllowOverScroll = true;
135 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800136 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700137 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800140 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
141 // the screens from continuing to translate beyond the normal bounds.
142 protected int mOverScrollX;
143
Michael Jurka8c920dd2011-01-20 14:16:56 -0800144 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800145 protected float mLayoutScale = 1.0f;
146
Michael Jurka5f1c5092010-09-03 14:15:02 -0700147 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurka5f1c5092010-09-03 14:15:02 -0700149 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Winson Chung86f77532010-08-24 11:08:22 -0700151 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurkae326f182011-11-21 14:05:46 -0800153 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700154
Michael Jurka0142d492010-08-25 17:46:15 -0700155 // If true, syncPages and syncPageItems will be called to refresh pages
156 protected boolean mContentIsRefreshable = true;
157
158 // If true, modify alpha of neighboring pages as user scrolls left/right
159 protected boolean mFadeInAdjacentScreens = true;
160
161 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
162 // to switch to a new page
163 protected boolean mUsePagingTouchSlop = true;
164
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700166 // (SmoothPagedView does this)
167 protected boolean mDeferScrollUpdate = false;
168
Patrick Dubroy1262e362010-10-06 15:49:50 -0700169 protected boolean mIsPageMoving = false;
170
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 // All syncs and layout passes are deferred until data is ready.
172 protected boolean mIsDataReady = false;
173
Winson Chung007c6982011-06-14 13:27:53 -0700174 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700175 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800176 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700177 private int mScrollIndicatorPaddingLeft;
178 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700179 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800180 private boolean mShouldShowScrollIndicator = false;
181 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700182 protected static final int sScrollIndicatorFadeInDuration = 150;
183 protected static final int sScrollIndicatorFadeOutDuration = 650;
184 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700185
Winson Chungb44b5242011-06-13 11:32:14 -0700186 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700187 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public interface PageSwitchListener {
190 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 public PagedView(Context context) {
194 this(context, null);
195 }
196
197 public PagedView(Context context, AttributeSet attrs) {
198 this(context, attrs, 0);
199 }
200
201 public PagedView(Context context, AttributeSet attrs, int defStyle) {
202 super(context, attrs, defStyle);
203
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 TypedArray a = context.obtainStyledAttributes(attrs,
205 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800206 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800212 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800214 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700215 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700216 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700217 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700218 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700219 mScrollIndicatorPaddingLeft =
220 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
221 mScrollIndicatorPaddingRight =
222 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700223 a.recycle();
224
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700226 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
228
229 /**
230 * Initializes various states for this workspace.
231 */
Michael Jurka0142d492010-08-25 17:46:15 -0700232 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700233 mDirtyPageContent = new ArrayList<Boolean>();
234 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800235 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700236 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700237 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700238
239 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
240 mTouchSlop = configuration.getScaledTouchSlop();
241 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
242 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700243 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800244
245 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
246 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
247 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700248 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 }
250
Winson Chung86f77532010-08-24 11:08:22 -0700251 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
252 mPageSwitchListener = pageSwitchListener;
253 if (mPageSwitchListener != null) {
254 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700255 }
256 }
257
258 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700259 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
260 * out pages.
261 */
262 protected void setDataIsReady() {
263 mIsDataReady = true;
264 }
265 protected boolean isDataReady() {
266 return mIsDataReady;
267 }
268
269 /**
Winson Chung86f77532010-08-24 11:08:22 -0700270 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 *
Winson Chung86f77532010-08-24 11:08:22 -0700272 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 */
Winson Chung86f77532010-08-24 11:08:22 -0700274 int getCurrentPage() {
275 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700276 }
Winson Chung360e63f2012-04-27 13:48:05 -0700277 int getNextPage() {
278 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
279 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700280
Winson Chung86f77532010-08-24 11:08:22 -0700281 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 return getChildCount();
283 }
284
Winson Chung86f77532010-08-24 11:08:22 -0700285 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 return getChildAt(index);
287 }
288
Adam Cohenae4f1552011-10-20 00:15:42 -0700289 protected int indexToPage(int index) {
290 return index;
291 }
292
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800294 * Updates the scroll of the current page immediately to its final scroll position. We use this
295 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
296 * the previous tab page.
297 */
298 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700299 int offset = getChildOffset(mCurrentPage);
300 int relOffset = getRelativeChildOffset(mCurrentPage);
301 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800302 scrollTo(newX, 0);
303 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800304 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800305 }
306
307 /**
Winson Chung86f77532010-08-24 11:08:22 -0700308 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 */
Winson Chung86f77532010-08-24 11:08:22 -0700310 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800311 if (!mScroller.isFinished()) {
312 mScroller.abortAnimation();
313 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800314 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
315 // the default
316 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800317 return;
318 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700319
Winson Chungbfeac062012-06-06 15:56:08 -0700320
Winson Chung86f77532010-08-24 11:08:22 -0700321 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800322 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700323 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700324 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800325 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 }
327
Michael Jurka0142d492010-08-25 17:46:15 -0700328 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700329 if (mPageSwitchListener != null) {
330 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 }
332 }
333
Michael Jurkace7e05f2011-02-01 22:02:35 -0800334 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700335 if (!mIsPageMoving) {
336 mIsPageMoving = true;
337 onPageBeginMoving();
338 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700339 }
340
Michael Jurkace7e05f2011-02-01 22:02:35 -0800341 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700342 if (mIsPageMoving) {
343 mIsPageMoving = false;
344 onPageEndMoving();
345 }
Michael Jurka0142d492010-08-25 17:46:15 -0700346 }
347
Adam Cohen26976d92011-03-22 15:33:33 -0700348 protected boolean isPageMoving() {
349 return mIsPageMoving;
350 }
351
Michael Jurka0142d492010-08-25 17:46:15 -0700352 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700353 protected void onPageBeginMoving() {
354 }
355
356 // a method that subclasses can override to add behavior
357 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700358 }
359
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 /**
Winson Chung86f77532010-08-24 11:08:22 -0700361 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 *
363 * @param l The listener used to respond to long clicks.
364 */
365 @Override
366 public void setOnLongClickListener(OnLongClickListener l) {
367 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700368 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700370 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372 }
373
374 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800375 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700376 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800377 }
378
379 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700380 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800381 mUnboundedScrollX = x;
382
383 if (x < 0) {
384 super.scrollTo(0, y);
385 if (mAllowOverScroll) {
386 overScroll(x);
387 }
388 } else if (x > mMaxScrollX) {
389 super.scrollTo(mMaxScrollX, y);
390 if (mAllowOverScroll) {
391 overScroll(x - mMaxScrollX);
392 }
393 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800394 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800395 super.scrollTo(x, y);
396 }
397
Michael Jurka0142d492010-08-25 17:46:15 -0700398 mTouchX = x;
399 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
400 }
401
402 // we moved this functionality to a helper function so SmoothPagedView can reuse it
403 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700405 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700406 if (getScrollX() != mScroller.getCurrX()
407 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700408 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700409 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
410 }
Michael Jurka0142d492010-08-25 17:46:15 -0700411 invalidate();
412 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700413 } else if (mNextPage != INVALID_PAGE) {
414 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700415 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700416 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700417
418 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700419 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700420 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700421 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700422 }
423
Adam Cohen73aa9752010-11-24 16:26:58 -0800424 // We don't want to trigger a page end moving unless the page has settled
425 // and the user has stopped scrolling
426 if (mTouchState == TOUCH_STATE_REST) {
427 pageEndMoving();
428 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700429
430 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700431 AccessibilityManager accessibilityManager = (AccessibilityManager)
432 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
433 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700434 AccessibilityEvent ev =
435 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
436 ev.getText().add(getCurrentPageDescription());
437 sendAccessibilityEventUnchecked(ev);
438 }
Michael Jurka0142d492010-08-25 17:46:15 -0700439 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700440 }
Michael Jurka0142d492010-08-25 17:46:15 -0700441 return false;
442 }
443
444 @Override
445 public void computeScroll() {
446 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 }
448
449 @Override
450 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 if (!mIsDataReady) {
452 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
453 return;
454 }
455
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
457 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700458 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
459 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 if (widthMode != MeasureSpec.EXACTLY) {
461 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
462 }
463
Winson Chung8aad6102012-05-11 16:27:49 -0700464 // Return early if we aren't given a proper dimension
465 if (widthSize <= 0 || heightSize <= 0) {
466 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
467 return;
468 }
469
Adam Lesinski6b879f02010-11-04 16:15:23 -0700470 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
471 * of the All apps view on XLarge displays to not take up more space then it needs. Width
472 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
473 * each page to have the same width.
474 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700475 int maxChildHeight = 0;
476
Michael Jurka8b805b12012-04-18 14:23:14 -0700477 final int verticalPadding = getPaddingTop() + getPaddingBottom();
478 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700479
Michael Jurka36fcb742011-04-06 17:08:58 -0700480
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700482 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700483 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 final int childCount = getChildCount();
485 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700486 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700487 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700488 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
489
490 int childWidthMode;
491 if (lp.width == LayoutParams.WRAP_CONTENT) {
492 childWidthMode = MeasureSpec.AT_MOST;
493 } else {
494 childWidthMode = MeasureSpec.EXACTLY;
495 }
496
497 int childHeightMode;
498 if (lp.height == LayoutParams.WRAP_CONTENT) {
499 childHeightMode = MeasureSpec.AT_MOST;
500 } else {
501 childHeightMode = MeasureSpec.EXACTLY;
502 }
503
504 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700505 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700506 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700507 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700508
509 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700510 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700511 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
512 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700513 }
514
515 if (heightMode == MeasureSpec.AT_MOST) {
516 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 }
Winson Chungae890b82011-09-13 18:08:54 -0700518
Winson Chungae890b82011-09-13 18:08:54 -0700519 setMeasuredDimension(widthSize, heightSize);
520
Winson Chung8aad6102012-05-11 16:27:49 -0700521 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
522 // We also wait until we set the measured dimensions before flushing the cache as well, to
523 // ensure that the cache is filled with good values.
524 invalidateCachedOffsets();
525
Winson Chunga128a7b2012-04-30 15:23:15 -0700526 if (childCount > 0) {
527 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
528 + getChildWidth(0));
529
530 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700531 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700532 // The gap between pages in the PagedView should be equal to the gap from the page
533 // to the edge of the screen (so it is not visible in the current screen). To
534 // account for unequal padding on each side of the paged view, we take the maximum
535 // of the left/right gap and use that as the gap between each page.
536 int offset = getRelativeChildOffset(0);
537 int spacing = Math.max(offset, widthSize - offset -
538 getChildAt(0).getMeasuredWidth());
539 setPageSpacing(spacing);
540 }
541 }
542
Adam Cohen25b29952011-11-02 14:49:06 -0700543 updateScrollingIndicatorPosition();
544
Adam Cohenfaa28302010-11-19 12:02:18 -0800545 if (childCount > 0) {
546 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
547 } else {
548 mMaxScrollX = 0;
549 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 }
551
Michael Jurka8c920dd2011-01-20 14:16:56 -0800552 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800553 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700554 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800555
Michael Jurka8c920dd2011-01-20 14:16:56 -0800556 final int pageCount = getChildCount();
557 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700558 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800559 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800560 }
561 setCurrentPage(newCurrentPage);
562 }
563
Michael Jurka8c920dd2011-01-20 14:16:56 -0800564 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
565 // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and
Michael Jurkad3ef3062010-11-23 16:23:58 -0800566 // tightens the layout accordingly
567 public void setLayoutScale(float childrenScale) {
568 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700569 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800570
571 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
572 int childCount = getChildCount();
573 float childrenX[] = new float[childCount];
574 float childrenY[] = new float[childCount];
575 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700576 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800577 childrenX[i] = child.getX();
578 childrenY[i] = child.getY();
579 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700580 // Trigger a full re-layout (never just call onLayout directly!)
581 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
582 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
583 requestLayout();
584 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700585 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800586 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700587 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800588 child.setX(childrenX[i]);
589 child.setY(childrenY[i]);
590 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700591
Michael Jurkad3ef3062010-11-23 16:23:58 -0800592 // Also, the page offset has changed (since the pages are now smaller);
593 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800594 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595 }
596
Adam Cohen60b07122011-11-14 17:26:06 -0800597 public void setPageSpacing(int pageSpacing) {
598 mPageSpacing = pageSpacing;
599 invalidateCachedOffsets();
600 }
601
Winson Chung321e9ee2010-08-09 13:37:56 -0700602 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700603 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700604 if (!mIsDataReady) {
605 return;
606 }
607
Winson Chung785d2eb2011-04-14 16:08:02 -0700608 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700609 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700611 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700612
613 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700614 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800616 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700617 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700619 if (mCenterPagesVertically) {
620 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
621 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800622
Winson Chung785d2eb2011-04-14 16:08:02 -0700623 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700624 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800625 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700626 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700627 }
628 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700629
630 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
631 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800632 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700633 setHorizontalScrollBarEnabled(true);
634 mFirstLayout = false;
635 }
Winson Chunge3193b92010-09-10 11:44:42 -0700636 }
637
Adam Cohenf34bab52010-09-30 14:11:56 -0700638 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700639 if (isScrollingIndicatorEnabled()) {
640 updateScrollingIndicator();
641 }
Michael Jurka869390b2012-05-06 15:55:19 -0700642 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
643
644 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700645 for (int i = 0; i < getChildCount(); i++) {
646 View child = getChildAt(i);
647 if (child != null) {
648 float scrollProgress = getScrollProgress(screenCenter, child, i);
649 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800650 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700651 }
652 }
653 invalidate();
654 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700655 }
656
Winson Chunge3193b92010-09-10 11:44:42 -0700657 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700658 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700659 // This ensures that when children are added, they get the correct transforms / alphas
660 // in accordance with any scroll effects.
661 mForceScreenScrolled = true;
662 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700663 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700664 }
665
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 @Override
667 public void onChildViewRemoved(View parent, View child) {
668 }
669
Adam Cohen73894962011-10-31 13:17:17 -0700670 protected void invalidateCachedOffsets() {
671 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700672 if (count == 0) {
673 mChildOffsets = null;
674 mChildRelativeOffsets = null;
675 mChildOffsetsWithLayoutScale = null;
676 return;
677 }
Adam Cohen73894962011-10-31 13:17:17 -0700678
679 mChildOffsets = new int[count];
680 mChildRelativeOffsets = new int[count];
681 mChildOffsetsWithLayoutScale = new int[count];
682 for (int i = 0; i < count; i++) {
683 mChildOffsets[i] = -1;
684 mChildRelativeOffsets[i] = -1;
685 mChildOffsetsWithLayoutScale[i] = -1;
686 }
687 }
688
689 protected int getChildOffset(int index) {
690 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
691 mChildOffsets : mChildOffsetsWithLayoutScale;
692
693 if (childOffsets != null && childOffsets[index] != -1) {
694 return childOffsets[index];
695 } else {
696 if (getChildCount() == 0)
697 return 0;
698
699 int offset = getRelativeChildOffset(0);
700 for (int i = 0; i < index; ++i) {
701 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
702 }
703 if (childOffsets != null) {
704 childOffsets[index] = offset;
705 }
706 return offset;
707 }
708 }
709
710 protected int getRelativeChildOffset(int index) {
711 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
712 return mChildRelativeOffsets[index];
713 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int padding = getPaddingLeft() + getPaddingRight();
715 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700716 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
717 if (mChildRelativeOffsets != null) {
718 mChildRelativeOffsets[index] = offset;
719 }
720 return offset;
721 }
722 }
723
Adam Cohen73894962011-10-31 13:17:17 -0700724 protected int getScaledMeasuredWidth(View child) {
725 // This functions are called enough times that it actually makes a difference in the
726 // profiler -- so just inline the max() here
727 final int measuredWidth = child.getMeasuredWidth();
728 final int minWidth = mMinimumWidth;
729 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
730 return (int) (maxWidth * mLayoutScale + 0.5f);
731 }
732
Michael Jurkadde558b2011-11-09 22:09:06 -0800733 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700734 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700735
Michael Jurkac4fb9172010-09-02 17:19:20 -0700736 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700737 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700738 int leftScreen = 0;
739 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800740 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800741 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 currPage.getX() + currPage.getWidth() -
743 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700744 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800745 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700746 }
747 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800748 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800749 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700751 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800752 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700753 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800754 range[0] = leftScreen;
755 range[1] = rightScreen;
756 } else {
757 range[0] = -1;
758 range[1] = -1;
759 }
760 }
761
Michael Jurka920d7f42012-05-14 16:29:55 -0700762 protected boolean shouldDrawChild(View child) {
763 return child.getAlpha() > 0;
764 }
765
Michael Jurkadde558b2011-11-09 22:09:06 -0800766 @Override
767 protected void dispatchDraw(Canvas canvas) {
768 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
770 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800771 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800772
773 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700774 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
775 // set it for the next frame
776 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800777 screenScrolled(screenCenter);
778 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800779 }
780
781 // Find out which screens are visible; as an optimization we only call draw on them
782 final int pageCount = getChildCount();
783 if (pageCount > 0) {
784 getVisiblePages(mTempVisiblePagesRange);
785 final int leftScreen = mTempVisiblePagesRange[0];
786 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800787 if (leftScreen != -1 && rightScreen != -1) {
788 final long drawingTime = getDrawingTime();
789 // Clip to the bounds
790 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
792 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700793
Michael Jurka80c69852011-12-16 14:16:32 -0800794 // On certain graphics drivers, if you draw to a off-screen buffer that's not
795 // used, it can lead to poor performance. We were running into this when
796 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
797 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
798 // As a fix, below we set pages that aren't going to be rendered are to be
799 // View.INVISIBLE, preventing re-drawing of their hardware layer
800 for (int i = getChildCount() - 1; i >= 0; i--) {
801 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700802 if (mForceDrawAllChildrenNextFrame ||
803 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800804 v.setVisibility(VISIBLE);
805 drawChild(canvas, v, drawingTime);
806 } else {
807 v.setVisibility(INVISIBLE);
808 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800809 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700810 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800811 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700812 }
Michael Jurka0142d492010-08-25 17:46:15 -0700813 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 }
815
816 @Override
817 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700818 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700819 if (page != mCurrentPage || !mScroller.isFinished()) {
820 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 return true;
822 }
823 return false;
824 }
825
826 @Override
827 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700828 int focusablePage;
829 if (mNextPage != INVALID_PAGE) {
830 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700832 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 }
Winson Chung86f77532010-08-24 11:08:22 -0700834 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700836 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 }
838 return false;
839 }
840
841 @Override
842 public boolean dispatchUnhandledMove(View focused, int direction) {
843 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700844 if (getCurrentPage() > 0) {
845 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 return true;
847 }
848 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700849 if (getCurrentPage() < getPageCount() - 1) {
850 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 return true;
852 }
853 }
854 return super.dispatchUnhandledMove(focused, direction);
855 }
856
857 @Override
858 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700860 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 }
862 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700863 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700864 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700867 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700868 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 }
870 }
871 }
872
873 /**
874 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700875 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 *
Winson Chung86f77532010-08-24 11:08:22 -0700877 * This happens when live folders requery, and if they're off page, they
878 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 */
880 @Override
881 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700882 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 View v = focused;
884 while (true) {
885 if (v == current) {
886 super.focusableViewAvailable(focused);
887 return;
888 }
889 if (v == this) {
890 return;
891 }
892 ViewParent parent = v.getParent();
893 if (parent instanceof View) {
894 v = (View)v.getParent();
895 } else {
896 return;
897 }
898 }
899 }
900
901 /**
902 * {@inheritDoc}
903 */
904 @Override
905 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
906 if (disallowIntercept) {
907 // We need to make sure to cancel our long press if
908 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700909 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700910 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 }
912 super.requestDisallowInterceptTouchEvent(disallowIntercept);
913 }
914
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800915 /**
916 * Return true if a tap at (x, y) should trigger a flip to the previous page.
917 */
918 protected boolean hitsPreviousPage(float x, float y) {
919 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
920 }
921
922 /**
923 * Return true if a tap at (x, y) should trigger a flip to the next page.
924 */
925 protected boolean hitsNextPage(float x, float y) {
926 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
927 }
928
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 @Override
930 public boolean onInterceptTouchEvent(MotionEvent ev) {
931 /*
932 * This method JUST determines whether we want to intercept the motion.
933 * If we return true, onTouchEvent will be called and we do the actual
934 * scrolling there.
935 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800936 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700937
Winson Chung45e1d6e2010-11-09 17:19:49 -0800938 // Skip touch handling if there are no pages to swipe
939 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
940
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 /*
942 * Shortcut the most recurring case: the user is in the dragging
943 * state and he is moving his finger. We want to intercept this
944 * motion.
945 */
946 final int action = ev.getAction();
947 if ((action == MotionEvent.ACTION_MOVE) &&
948 (mTouchState == TOUCH_STATE_SCROLLING)) {
949 return true;
950 }
951
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 switch (action & MotionEvent.ACTION_MASK) {
953 case MotionEvent.ACTION_MOVE: {
954 /*
955 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
956 * whether the user has moved far enough from his original down touch.
957 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700958 if (mActivePointerId != INVALID_POINTER) {
959 determineScrollingStart(ev);
960 break;
961 }
962 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
963 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
964 // i.e. fall through to the next case (don't break)
965 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
966 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 }
968
969 case MotionEvent.ACTION_DOWN: {
970 final float x = ev.getX();
971 final float y = ev.getY();
972 // Remember location of down touch
973 mDownMotionX = x;
974 mLastMotionX = x;
975 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800976 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800977 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 mActivePointerId = ev.getPointerId(0);
979 mAllowLongPress = true;
980
981 /*
982 * If being flinged and user touches the screen, initiate drag;
983 * otherwise don't. mScroller.isFinished should be false when
984 * being flinged.
985 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700986 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700987 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
988 if (finishedScrolling) {
989 mTouchState = TOUCH_STATE_REST;
990 mScroller.abortAnimation();
991 } else {
992 mTouchState = TOUCH_STATE_SCROLLING;
993 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700994
Winson Chung86f77532010-08-24 11:08:22 -0700995 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800997 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800999 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001001 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 mTouchState = TOUCH_STATE_NEXT_PAGE;
1003 }
1004 }
1005 }
1006 break;
1007 }
1008
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001010 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 mTouchState = TOUCH_STATE_REST;
1012 mAllowLongPress = false;
1013 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001014 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 break;
1016
1017 case MotionEvent.ACTION_POINTER_UP:
1018 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001019 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 break;
1021 }
1022
1023 /*
1024 * The only time we want to intercept motion events is if we are in the
1025 * drag mode.
1026 */
1027 return mTouchState != TOUCH_STATE_REST;
1028 }
1029
Adam Cohenf8d28232011-02-01 21:47:00 -08001030 protected void determineScrollingStart(MotionEvent ev) {
1031 determineScrollingStart(ev, 1.0f);
1032 }
1033
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 /*
1035 * Determines if we should change the touch state to start scrolling after the
1036 * user moves their touch point too far.
1037 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001038 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 /*
1040 * Locally do absolute value. mLastMotionX is set to the y value
1041 * of the down event.
1042 */
1043 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001044 if (pointerIndex == -1) {
1045 return;
1046 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001047 final float x = ev.getX(pointerIndex);
1048 final float y = ev.getY(pointerIndex);
1049 final int xDiff = (int) Math.abs(x - mLastMotionX);
1050 final int yDiff = (int) Math.abs(y - mLastMotionY);
1051
Adam Cohenf8d28232011-02-01 21:47:00 -08001052 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 boolean xPaged = xDiff > mPagingTouchSlop;
1054 boolean xMoved = xDiff > touchSlop;
1055 boolean yMoved = yDiff > touchSlop;
1056
Adam Cohenf8d28232011-02-01 21:47:00 -08001057 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001058 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 // Scroll if the user moved far enough along the X axis
1060 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001061 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001063 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001064 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001065 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1066 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001069 cancelCurrentPageLongPress();
1070 }
1071 }
1072
1073 protected void cancelCurrentPageLongPress() {
1074 if (mAllowLongPress) {
1075 mAllowLongPress = false;
1076 // Try canceling the long press. It could also have been scheduled
1077 // by a distant descendant, so use the mAllowLongPress flag to block
1078 // everything
1079 final View currentPage = getPageAt(mCurrentPage);
1080 if (currentPage != null) {
1081 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 }
1083 }
1084 }
1085
Adam Cohenb5ba0972011-09-07 18:02:31 -07001086 protected float getScrollProgress(int screenCenter, View v, int page) {
1087 final int halfScreenSize = getMeasuredWidth() / 2;
1088
1089 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1090 int delta = screenCenter - (getChildOffset(page) -
1091 getRelativeChildOffset(page) + halfScreenSize);
1092
1093 float scrollProgress = delta / (totalDistance * 1.0f);
1094 scrollProgress = Math.min(scrollProgress, 1.0f);
1095 scrollProgress = Math.max(scrollProgress, -1.0f);
1096 return scrollProgress;
1097 }
1098
Adam Cohene0f66b52010-11-23 15:06:07 -08001099 // This curve determines how the effect of scrolling over the limits of the page dimishes
1100 // as the user pulls further and further from the bounds
1101 private float overScrollInfluenceCurve(float f) {
1102 f -= 1.0f;
1103 return f * f * f + 1.0f;
1104 }
1105
Adam Cohenb5ba0972011-09-07 18:02:31 -07001106 protected void acceleratedOverScroll(float amount) {
1107 int screenSize = getMeasuredWidth();
1108
1109 // We want to reach the max over scroll effect when the user has
1110 // over scrolled half the size of the screen
1111 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1112
1113 if (f == 0) return;
1114
1115 // Clamp this factor, f, to -1 < f < 1
1116 if (Math.abs(f) >= 1) {
1117 f /= Math.abs(f);
1118 }
1119
1120 int overScrollAmount = (int) Math.round(f * screenSize);
1121 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001122 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001123 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001124 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001125 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001126 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001127 }
1128 invalidate();
1129 }
1130
1131 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001132 int screenSize = getMeasuredWidth();
1133
1134 float f = (amount / screenSize);
1135
1136 if (f == 0) return;
1137 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1138
Adam Cohen7bfc9792011-01-28 13:52:37 -08001139 // Clamp this factor, f, to -1 < f < 1
1140 if (Math.abs(f) >= 1) {
1141 f /= Math.abs(f);
1142 }
1143
Adam Cohene0f66b52010-11-23 15:06:07 -08001144 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001145 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001146 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001147 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001148 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001149 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001150 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001151 }
1152 invalidate();
1153 }
1154
Adam Cohenb5ba0972011-09-07 18:02:31 -07001155 protected void overScroll(float amount) {
1156 dampedOverScroll(amount);
1157 }
1158
Michael Jurkac5b262c2011-01-12 20:24:50 -08001159 protected float maxOverScroll() {
1160 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001161 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001162 float f = 1.0f;
1163 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1164 return OVERSCROLL_DAMP_FACTOR * f;
1165 }
1166
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 @Override
1168 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001169 // Skip touch handling if there are no pages to swipe
1170 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1171
Michael Jurkab8f06722010-10-10 15:58:46 -07001172 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001173
1174 final int action = ev.getAction();
1175
1176 switch (action & MotionEvent.ACTION_MASK) {
1177 case MotionEvent.ACTION_DOWN:
1178 /*
1179 * If being flinged and user touches, stop the fling. isFinished
1180 * will be false if being flinged.
1181 */
1182 if (!mScroller.isFinished()) {
1183 mScroller.abortAnimation();
1184 }
1185
1186 // Remember where the motion event started
1187 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001188 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001189 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001191 if (mTouchState == TOUCH_STATE_SCROLLING) {
1192 pageBeginMoving();
1193 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 break;
1195
1196 case MotionEvent.ACTION_MOVE:
1197 if (mTouchState == TOUCH_STATE_SCROLLING) {
1198 // Scroll to follow the motion event
1199 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1200 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001201 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001202
Adam Cohenaefd4e12011-02-14 16:39:38 -08001203 mTotalMotionX += Math.abs(deltaX);
1204
Winson Chungc0844aa2011-02-02 15:25:58 -08001205 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1206 // keep the remainder because we are actually testing if we've moved from the last
1207 // scrolled position (which is discrete).
1208 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001209 mTouchX += deltaX;
1210 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1211 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001212 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001213 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001214 } else {
1215 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001217 mLastMotionX = x;
1218 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 } else {
1220 awakenScrollBars();
1221 }
Adam Cohen564976a2010-10-13 18:52:07 -07001222 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 determineScrollingStart(ev);
1224 }
1225 break;
1226
1227 case MotionEvent.ACTION_UP:
1228 if (mTouchState == TOUCH_STATE_SCROLLING) {
1229 final int activePointerId = mActivePointerId;
1230 final int pointerIndex = ev.findPointerIndex(activePointerId);
1231 final float x = ev.getX(pointerIndex);
1232 final VelocityTracker velocityTracker = mVelocityTracker;
1233 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1234 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001235 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001236 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1237 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1238 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001239
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001240 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1241
Adam Cohen00481b32011-11-18 12:03:48 -08001242 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001243 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001244
Adam Cohenaefd4e12011-02-14 16:39:38 -08001245 // In the case that the page is moved far to one direction and then is flung
1246 // in the opposite direction, we use a threshold to determine whether we should
1247 // just return to the starting page, or if we should skip one further.
1248 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001249 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001250 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001251 returnToOriginalPage = true;
1252 }
1253
Adam Cohenaefd4e12011-02-14 16:39:38 -08001254 int finalPage;
1255 // We give flings precedence over large moves, which is why we short-circuit our
1256 // test for a large move if a fling has been registered. That is, a large
1257 // move to the left and fling to the right will register as a fling to the right.
1258 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1259 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1260 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1261 snapToPageWithVelocity(finalPage, velocityX);
1262 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1263 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001264 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001265 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1266 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 } else {
1268 snapToDestination();
1269 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001270 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 // at this point we have not moved beyond the touch slop
1272 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1273 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001274 int nextPage = Math.max(0, mCurrentPage - 1);
1275 if (nextPage != mCurrentPage) {
1276 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 } else {
1278 snapToDestination();
1279 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001280 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 // at this point we have not moved beyond the touch slop
1282 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1283 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001284 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1285 if (nextPage != mCurrentPage) {
1286 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001287 } else {
1288 snapToDestination();
1289 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001290 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001291 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001292 }
1293 mTouchState = TOUCH_STATE_REST;
1294 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001295 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001296 break;
1297
1298 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001299 if (mTouchState == TOUCH_STATE_SCROLLING) {
1300 snapToDestination();
1301 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 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_POINTER_UP:
1308 onSecondaryPointerUp(ev);
1309 break;
1310 }
1311
1312 return true;
1313 }
1314
Winson Chung185d7162011-02-28 13:47:29 -08001315 @Override
1316 public boolean onGenericMotionEvent(MotionEvent event) {
1317 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1318 switch (event.getAction()) {
1319 case MotionEvent.ACTION_SCROLL: {
1320 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1321 final float vscroll;
1322 final float hscroll;
1323 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1324 vscroll = 0;
1325 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1326 } else {
1327 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1328 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1329 }
1330 if (hscroll != 0 || vscroll != 0) {
1331 if (hscroll > 0 || vscroll > 0) {
1332 scrollRight();
1333 } else {
1334 scrollLeft();
1335 }
1336 return true;
1337 }
1338 }
1339 }
1340 }
1341 return super.onGenericMotionEvent(event);
1342 }
1343
Michael Jurkab8f06722010-10-10 15:58:46 -07001344 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1345 if (mVelocityTracker == null) {
1346 mVelocityTracker = VelocityTracker.obtain();
1347 }
1348 mVelocityTracker.addMovement(ev);
1349 }
1350
1351 private void releaseVelocityTracker() {
1352 if (mVelocityTracker != null) {
1353 mVelocityTracker.recycle();
1354 mVelocityTracker = null;
1355 }
1356 }
1357
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 private void onSecondaryPointerUp(MotionEvent ev) {
1359 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1360 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1361 final int pointerId = ev.getPointerId(pointerIndex);
1362 if (pointerId == mActivePointerId) {
1363 // This was our active pointer going up. Choose a new
1364 // active pointer and adjust accordingly.
1365 // TODO: Make this decision more intelligent.
1366 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1367 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1368 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001369 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 mActivePointerId = ev.getPointerId(newPointerIndex);
1371 if (mVelocityTracker != null) {
1372 mVelocityTracker.clear();
1373 }
1374 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001375 }
1376
Michael Jurkad771c962011-08-09 15:00:48 -07001377 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001378
1379 @Override
1380 public void requestChildFocus(View child, View focused) {
1381 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001382 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001383 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001384 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 }
1386 }
1387
Winson Chunge3193b92010-09-10 11:44:42 -07001388 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1389 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001390 int left;
1391 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001392 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001393 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001394 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001395 if (left <= relativeOffset && relativeOffset <= right) {
1396 return i;
1397 }
Winson Chunge3193b92010-09-10 11:44:42 -07001398 }
1399 return -1;
1400 }
1401
Winson Chung1908d072011-02-24 18:09:44 -08001402 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001403 // This functions are called enough times that it actually makes a difference in the
1404 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001405 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001406 final int minWidth = mMinimumWidth;
1407 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001408 }
1409
Adam Cohend19d3ca2010-09-15 14:43:42 -07001410 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001411 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001413 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 final int childCount = getChildCount();
1415 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001416 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001417 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 int halfChildWidth = (childWidth / 2);
1419 int childCenter = getChildOffset(i) + halfChildWidth;
1420 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1421 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1422 minDistanceFromScreenCenter = distanceFromScreenCenter;
1423 minDistanceFromScreenCenterIndex = i;
1424 }
1425 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001426 return minDistanceFromScreenCenterIndex;
1427 }
1428
1429 protected void snapToDestination() {
1430 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001431 }
1432
Adam Cohene0f66b52010-11-23 15:06:07 -08001433 private static class ScrollInterpolator implements Interpolator {
1434 public ScrollInterpolator() {
1435 }
1436
1437 public float getInterpolation(float t) {
1438 t -= 1.0f;
1439 return t*t*t*t*t + 1;
1440 }
1441 }
1442
1443 // We want the duration of the page snap animation to be influenced by the distance that
1444 // the screen has to travel, however, we don't want this duration to be effected in a
1445 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1446 // of travel has on the overall snap duration.
1447 float distanceInfluenceForSnapDuration(float f) {
1448 f -= 0.5f; // center the values about 0.
1449 f *= 0.3f * Math.PI / 2.0f;
1450 return (float) Math.sin(f);
1451 }
1452
Michael Jurka0142d492010-08-25 17:46:15 -07001453 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001454 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1455 int halfScreenSize = getMeasuredWidth() / 2;
1456
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1458 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1459 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001460 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1461 int delta = newX - mUnboundedScrollX;
1462 int duration = 0;
1463
Adam Cohen265b9a62011-12-07 14:37:18 -08001464 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001465 // If the velocity is low enough, then treat this more as an automatic page advance
1466 // as opposed to an apparent physical response to flinging
1467 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1468 return;
1469 }
1470
1471 // Here we compute a "distance" that will be used in the computation of the overall
1472 // snap duration. This is a function of the actual distance that needs to be traveled;
1473 // we keep this value close to half screen size in order to reduce the variance in snap
1474 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001475 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001476 float distance = halfScreenSize + halfScreenSize *
1477 distanceInfluenceForSnapDuration(distanceRatio);
1478
1479 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001480 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001481
1482 // we want the page's snap velocity to approximately match the velocity at which the
1483 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001484 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1485 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001486
1487 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001488 }
1489
1490 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001491 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001492 }
1493
Michael Jurka0142d492010-08-25 17:46:15 -07001494 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001495 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001496
Winson Chung785d2eb2011-04-14 16:08:02 -07001497 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1498 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1499 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001500 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001501 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001503 snapToPage(whichPage, delta, duration);
1504 }
1505
1506 protected void snapToPage(int whichPage, int delta, int duration) {
1507 mNextPage = whichPage;
1508
1509 View focusedChild = getFocusedChild();
1510 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001511 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001512 focusedChild.clearFocus();
1513 }
1514
1515 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 awakenScrollBars(duration);
1517 if (duration == 0) {
1518 duration = Math.abs(delta);
1519 }
1520
1521 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001522 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001523
Winson Chungb44b5242011-06-13 11:32:14 -07001524 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1525 // loading associated pages until the scroll settles
1526 if (mDeferScrollUpdate) {
1527 loadAssociatedPages(mNextPage);
1528 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001529 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001530 }
Michael Jurka0142d492010-08-25 17:46:15 -07001531 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001532 invalidate();
1533 }
1534
Winson Chung321e9ee2010-08-09 13:37:56 -07001535 public void scrollLeft() {
1536 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001537 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001538 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001539 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 }
1541 }
1542
1543 public void scrollRight() {
1544 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001545 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001546 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001547 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001548 }
1549 }
1550
Winson Chung86f77532010-08-24 11:08:22 -07001551 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001552 int result = -1;
1553 if (v != null) {
1554 ViewParent vp = v.getParent();
1555 int count = getChildCount();
1556 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001557 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001558 return i;
1559 }
1560 }
1561 }
1562 return result;
1563 }
1564
1565 /**
1566 * @return True is long presses are still allowed for the current touch
1567 */
1568 public boolean allowLongPress() {
1569 return mAllowLongPress;
1570 }
1571
Michael Jurka0142d492010-08-25 17:46:15 -07001572 /**
1573 * Set true to allow long-press events to be triggered, usually checked by
1574 * {@link Launcher} to accept or block dpad-initiated long-presses.
1575 */
1576 public void setAllowLongPress(boolean allowLongPress) {
1577 mAllowLongPress = allowLongPress;
1578 }
1579
Winson Chung321e9ee2010-08-09 13:37:56 -07001580 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001581 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001582
1583 SavedState(Parcelable superState) {
1584 super(superState);
1585 }
1586
1587 private SavedState(Parcel in) {
1588 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001589 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001590 }
1591
1592 @Override
1593 public void writeToParcel(Parcel out, int flags) {
1594 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001595 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001596 }
1597
1598 public static final Parcelable.Creator<SavedState> CREATOR =
1599 new Parcelable.Creator<SavedState>() {
1600 public SavedState createFromParcel(Parcel in) {
1601 return new SavedState(in);
1602 }
1603
1604 public SavedState[] newArray(int size) {
1605 return new SavedState[size];
1606 }
1607 };
1608 }
1609
Winson Chungf314b0e2011-08-16 11:54:27 -07001610 protected void loadAssociatedPages(int page) {
1611 loadAssociatedPages(page, false);
1612 }
1613 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001614 if (mContentIsRefreshable) {
1615 final int count = getChildCount();
1616 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001617 int lowerPageBound = getAssociatedLowerPageBound(page);
1618 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001619 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1620 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001621 // First, clear any pages that should no longer be loaded
1622 for (int i = 0; i < count; ++i) {
1623 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001624 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001625 if (layout.getPageChildCount() > 0) {
1626 layout.removeAllViewsOnPage();
1627 }
1628 mDirtyPageContent.set(i, true);
1629 }
1630 }
1631 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001632 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001633 if ((i != page) && immediateAndOnly) {
1634 continue;
1635 }
Michael Jurka0142d492010-08-25 17:46:15 -07001636 if (lowerPageBound <= i && i <= upperPageBound) {
1637 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001638 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001639 mDirtyPageContent.set(i, false);
1640 }
Winson Chung86f77532010-08-24 11:08:22 -07001641 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001642 }
1643 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001644 }
1645 }
1646
Winson Chunge3193b92010-09-10 11:44:42 -07001647 protected int getAssociatedLowerPageBound(int page) {
1648 return Math.max(0, page - 1);
1649 }
1650 protected int getAssociatedUpperPageBound(int page) {
1651 final int count = getChildCount();
1652 return Math.min(page + 1, count - 1);
1653 }
1654
Winson Chung86f77532010-08-24 11:08:22 -07001655 /**
1656 * This method is called ONLY to synchronize the number of pages that the paged view has.
1657 * To actually fill the pages with information, implement syncPageItems() below. It is
1658 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1659 * and therefore, individual page items do not need to be updated in this method.
1660 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001661 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001662
1663 /**
1664 * This method is called to synchronize the items that are on a particular page. If views on
1665 * the page can be reused, then they should be updated within this method.
1666 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001667 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001668
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001669 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001670 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001671 }
1672 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001673 invalidatePageData(currentPage, false);
1674 }
1675 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001676 if (!mIsDataReady) {
1677 return;
1678 }
1679
Michael Jurka0142d492010-08-25 17:46:15 -07001680 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001681 // Force all scrolling-related behavior to end
1682 mScroller.forceFinished(true);
1683 mNextPage = INVALID_PAGE;
1684
Michael Jurka0142d492010-08-25 17:46:15 -07001685 // Update all the pages
1686 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001687
Winson Chung5a808352011-06-27 19:08:49 -07001688 // We must force a measure after we've loaded the pages to update the content width and
1689 // to determine the full scroll width
1690 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1691 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1692
1693 // Set a new page as the current page if necessary
1694 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001695 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001696 }
1697
Michael Jurka0142d492010-08-25 17:46:15 -07001698 // Mark each of the pages as dirty
1699 final int count = getChildCount();
1700 mDirtyPageContent.clear();
1701 for (int i = 0; i < count; ++i) {
1702 mDirtyPageContent.add(true);
1703 }
1704
1705 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001706 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001707 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001708 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001709 }
Winson Chung007c6982011-06-14 13:27:53 -07001710
Michael Jurkaafaa0502011-12-13 18:22:50 -08001711 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001712 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1713 // found
1714 if (mHasScrollIndicator && mScrollIndicator == null) {
1715 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001716 if (parent != null) {
1717 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1718 mHasScrollIndicator = mScrollIndicator != null;
1719 if (mHasScrollIndicator) {
1720 mScrollIndicator.setVisibility(View.VISIBLE);
1721 }
Winson Chung007c6982011-06-14 13:27:53 -07001722 }
1723 }
1724 return mScrollIndicator;
1725 }
1726
1727 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001728 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001729 }
1730
Winson Chung5a808352011-06-27 19:08:49 -07001731 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1732 @Override
1733 public void run() {
1734 hideScrollingIndicator(false);
1735 }
1736 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001737 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001738 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001739 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001740 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001741 }
1742
Michael Jurka430e8a52011-08-08 15:52:14 -07001743 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001744 mShouldShowScrollIndicator = true;
1745 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001746 if (getChildCount() <= 1) return;
1747 if (!isScrollingIndicatorEnabled()) return;
1748
Michael Jurkabed61d22012-02-14 22:51:29 -08001749 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001750 getScrollingIndicator();
1751 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001752 // Fade the indicator in
1753 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001754 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001755 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001756 if (immediately) {
1757 mScrollIndicator.setAlpha(1f);
1758 } else {
1759 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1760 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1761 mScrollIndicatorAnimator.start();
1762 }
Winson Chung007c6982011-06-14 13:27:53 -07001763 }
1764 }
1765
Adam Cohen21b41102011-11-01 17:29:52 -07001766 protected void cancelScrollingIndicatorAnimations() {
1767 if (mScrollIndicatorAnimator != null) {
1768 mScrollIndicatorAnimator.cancel();
1769 }
1770 }
1771
Winson Chung007c6982011-06-14 13:27:53 -07001772 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001773 if (getChildCount() <= 1) return;
1774 if (!isScrollingIndicatorEnabled()) return;
1775
1776 getScrollingIndicator();
1777 if (mScrollIndicator != null) {
1778 // Fade the indicator out
1779 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001780 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001781 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001782 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001783 mScrollIndicator.setAlpha(0f);
1784 } else {
1785 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1786 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1787 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1788 private boolean cancelled = false;
1789 @Override
1790 public void onAnimationCancel(android.animation.Animator animation) {
1791 cancelled = true;
1792 }
1793 @Override
1794 public void onAnimationEnd(Animator animation) {
1795 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001796 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001797 }
1798 }
1799 });
1800 mScrollIndicatorAnimator.start();
1801 }
Winson Chung007c6982011-06-14 13:27:53 -07001802 }
1803 }
1804
Winson Chung32174c82011-07-19 15:47:55 -07001805 /**
1806 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1807 * fill its space on the track or not.
1808 */
1809 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001810 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001811 }
1812
Winson Chung007c6982011-06-14 13:27:53 -07001813 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001814 if (getChildCount() <= 1) return;
1815 if (!isScrollingIndicatorEnabled()) return;
1816
1817 getScrollingIndicator();
1818 if (mScrollIndicator != null) {
1819 updateScrollingIndicatorPosition();
1820 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001821 if (mShouldShowScrollIndicator) {
1822 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1823 }
Winson Chung007c6982011-06-14 13:27:53 -07001824 }
1825
Winson Chung007c6982011-06-14 13:27:53 -07001826 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001827 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001828 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001829 int numPages = getChildCount();
1830 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001831 int lastChildIndex = Math.max(0, getChildCount() - 1);
1832 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001833 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001834 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1835 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001836
Winson Chungae890b82011-09-13 18:08:54 -07001837 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001838 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001839 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001840 if (hasElasticScrollIndicator()) {
1841 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1842 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1843 mScrollIndicator.requestLayout();
1844 }
1845 } else {
1846 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1847 indicatorPos += indicatorCenterOffset;
1848 }
Winson Chung007c6982011-06-14 13:27:53 -07001849 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001850 }
1851
Winson Chung3ac74c52011-06-30 17:39:37 -07001852 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001853 }
1854
1855 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001856 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001857
1858 /* Accessibility */
1859 @Override
1860 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1861 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001862 info.setScrollable(getPageCount() > 1);
1863 if (getCurrentPage() < getPageCount() - 1) {
1864 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1865 }
1866 if (getCurrentPage() > 0) {
1867 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1868 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001869 }
1870
1871 @Override
1872 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1873 super.onInitializeAccessibilityEvent(event);
1874 event.setScrollable(true);
1875 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1876 event.setFromIndex(mCurrentPage);
1877 event.setToIndex(mCurrentPage);
1878 event.setItemCount(getChildCount());
1879 }
1880 }
1881
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001882 @Override
1883 public boolean performAccessibilityAction(int action, Bundle arguments) {
1884 if (super.performAccessibilityAction(action, arguments)) {
1885 return true;
1886 }
1887 switch (action) {
1888 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1889 if (getCurrentPage() < getPageCount() - 1) {
1890 scrollRight();
1891 return true;
1892 }
1893 } break;
1894 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1895 if (getCurrentPage() > 0) {
1896 scrollLeft();
1897 return true;
1898 }
1899 } break;
1900 }
1901 return false;
1902 }
1903
Winson Chung6a0f57d2011-06-29 20:10:49 -07001904 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001905 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001906 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001907 }
Winson Chungd11265e2011-08-30 13:37:23 -07001908
1909 @Override
1910 public boolean onHoverEvent(android.view.MotionEvent event) {
1911 return true;
1912 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001913}