blob: e64b0c3a6bd42cdbfe781293761c456af742e9b1 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
20import android.animation.AnimatorInflater;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070023import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070025import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.graphics.Canvas;
27import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070042import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070043import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070045import android.widget.Checkable;
Winson Chung007c6982011-06-14 13:27:53 -070046import android.widget.ImageView;
Winson Chung321e9ee2010-08-09 13:37:56 -070047import android.widget.Scroller;
48
Winson Chung04998342011-01-05 13:54:43 -080049import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070050
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
57public abstract class PagedView extends ViewGroup {
58 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chung86f77532010-08-24 11:08:22 -070062 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070063 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohene0f66b52010-11-23 15:06:07 -080065 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070066 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Adam Cohenb5ba0972011-09-07 18:02:31 -070068 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070069 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080070
Adam Cohenb64cb5a2011-02-15 13:53:42 -080071 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080072 // The page is moved more than halfway, automatically move to the next page on touch up.
73 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080074
Adam Cohen265b9a62011-12-07 14:37:18 -080075 // The following constants need to be scaled based on density. The scaled versions will be
76 // assigned to the corresponding member variables below.
77 private static final int FLING_THRESHOLD_VELOCITY = 500;
78 private static final int MIN_SNAP_VELOCITY = 1500;
79 private static final int MIN_FLING_VELOCITY = 250;
80
81 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];
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Adam Cohenebea84d2011-11-09 17:20:41 -0800138 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
139 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
140 // the screens from continuing to translate beyond the normal bounds.
141 protected int mOverScrollX;
142
Michael Jurka8c920dd2011-01-20 14:16:56 -0800143 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800144 protected float mLayoutScale = 1.0f;
145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka5f1c5092010-09-03 14:15:02 -0700148 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149
Winson Chung86f77532010-08-24 11:08:22 -0700150 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurkae326f182011-11-21 14:05:46 -0800152 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 // If true, syncPages and syncPageItems will be called to refresh pages
155 protected boolean mContentIsRefreshable = true;
156
157 // If true, modify alpha of neighboring pages as user scrolls left/right
158 protected boolean mFadeInAdjacentScreens = true;
159
160 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
161 // to switch to a new page
162 protected boolean mUsePagingTouchSlop = true;
163
164 // If true, the subclass should directly update mScrollX itself in its computeScroll method
165 // (SmoothPagedView does this)
166 protected boolean mDeferScrollUpdate = false;
167
Patrick Dubroy1262e362010-10-06 15:49:50 -0700168 protected boolean mIsPageMoving = false;
169
Winson Chungf0ea4d32011-06-06 14:27:16 -0700170 // All syncs and layout passes are deferred until data is ready.
171 protected boolean mIsDataReady = false;
172
Winson Chung007c6982011-06-14 13:27:53 -0700173 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700174 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800175 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700176 private int mScrollIndicatorPaddingLeft;
177 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700178 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800179 private boolean mShouldShowScrollIndicator = false;
180 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700181 protected static final int sScrollIndicatorFadeInDuration = 150;
182 protected static final int sScrollIndicatorFadeOutDuration = 650;
183 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700184
Winson Chungb44b5242011-06-13 11:32:14 -0700185 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700186 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700187
Winson Chung86f77532010-08-24 11:08:22 -0700188 public interface PageSwitchListener {
189 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 }
191
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 public PagedView(Context context) {
193 this(context, null);
194 }
195
196 public PagedView(Context context, AttributeSet attrs) {
197 this(context, attrs, 0);
198 }
199
200 public PagedView(Context context, AttributeSet attrs, int defStyle) {
201 super(context, attrs, defStyle);
202
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 TypedArray a = context.obtainStyledAttributes(attrs,
204 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800205 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800207 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700214 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700215 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700216 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700218 mScrollIndicatorPaddingLeft =
219 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
220 mScrollIndicatorPaddingRight =
221 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700222 a.recycle();
223
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700225 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227
228 /**
229 * Initializes various states for this workspace.
230 */
Michael Jurka0142d492010-08-25 17:46:15 -0700231 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700232 mDirtyPageContent = new ArrayList<Boolean>();
233 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800234 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700235 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700236 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700237
238 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
239 mTouchSlop = configuration.getScaledTouchSlop();
240 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
241 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700242 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800243
244 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
245 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
246 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 }
248
Winson Chung86f77532010-08-24 11:08:22 -0700249 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
250 mPageSwitchListener = pageSwitchListener;
251 if (mPageSwitchListener != null) {
252 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700253 }
254 }
255
256 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700257 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
258 * out pages.
259 */
260 protected void setDataIsReady() {
261 mIsDataReady = true;
262 }
263 protected boolean isDataReady() {
264 return mIsDataReady;
265 }
266
267 /**
Winson Chung86f77532010-08-24 11:08:22 -0700268 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 *
Winson Chung86f77532010-08-24 11:08:22 -0700270 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 */
Winson Chung86f77532010-08-24 11:08:22 -0700272 int getCurrentPage() {
273 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 }
275
Winson Chung86f77532010-08-24 11:08:22 -0700276 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 return getChildCount();
278 }
279
Winson Chung86f77532010-08-24 11:08:22 -0700280 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 return getChildAt(index);
282 }
283
Adam Cohenae4f1552011-10-20 00:15:42 -0700284 protected int indexToPage(int index) {
285 return index;
286 }
287
Winson Chung321e9ee2010-08-09 13:37:56 -0700288 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800289 * Updates the scroll of the current page immediately to its final scroll position. We use this
290 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
291 * the previous tab page.
292 */
293 protected void updateCurrentPageScroll() {
294 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
295 scrollTo(newX, 0);
296 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800297 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800298 }
299
300 /**
Winson Chung86f77532010-08-24 11:08:22 -0700301 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 */
Winson Chung86f77532010-08-24 11:08:22 -0700303 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800304 if (!mScroller.isFinished()) {
305 mScroller.abortAnimation();
306 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800307 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
308 // the default
309 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800310 return;
311 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700312
Winson Chung86f77532010-08-24 11:08:22 -0700313 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800314 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700315 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700316 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800317 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 }
319
Michael Jurka0142d492010-08-25 17:46:15 -0700320 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700321 if (mPageSwitchListener != null) {
322 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 }
324 }
325
Michael Jurkace7e05f2011-02-01 22:02:35 -0800326 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700327 if (!mIsPageMoving) {
328 mIsPageMoving = true;
329 onPageBeginMoving();
330 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700331 }
332
Michael Jurkace7e05f2011-02-01 22:02:35 -0800333 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700334 if (mIsPageMoving) {
335 mIsPageMoving = false;
336 onPageEndMoving();
337 }
Michael Jurka0142d492010-08-25 17:46:15 -0700338 }
339
Adam Cohen26976d92011-03-22 15:33:33 -0700340 protected boolean isPageMoving() {
341 return mIsPageMoving;
342 }
343
Michael Jurka0142d492010-08-25 17:46:15 -0700344 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700345 protected void onPageBeginMoving() {
346 }
347
348 // a method that subclasses can override to add behavior
349 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700350 }
351
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 /**
Winson Chung86f77532010-08-24 11:08:22 -0700353 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 *
355 * @param l The listener used to respond to long clicks.
356 */
357 @Override
358 public void setOnLongClickListener(OnLongClickListener l) {
359 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700360 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700362 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
364 }
365
366 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800367 public void scrollBy(int x, int y) {
368 scrollTo(mUnboundedScrollX + x, mScrollY + y);
369 }
370
371 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700372 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800373 mUnboundedScrollX = x;
374
375 if (x < 0) {
376 super.scrollTo(0, y);
377 if (mAllowOverScroll) {
378 overScroll(x);
379 }
380 } else if (x > mMaxScrollX) {
381 super.scrollTo(mMaxScrollX, y);
382 if (mAllowOverScroll) {
383 overScroll(x - mMaxScrollX);
384 }
385 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800386 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800387 super.scrollTo(x, y);
388 }
389
Michael Jurka0142d492010-08-25 17:46:15 -0700390 mTouchX = x;
391 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
392 }
393
394 // we moved this functionality to a helper function so SmoothPagedView can reuse it
395 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700397 // Don't bother scrolling if the page does not need to be moved
398 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700399 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
400 }
Michael Jurka0142d492010-08-25 17:46:15 -0700401 invalidate();
402 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700403 } else if (mNextPage != INVALID_PAGE) {
404 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700405 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700406 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700407
408 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700409 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700410 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700411 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700412 }
413
Adam Cohen73aa9752010-11-24 16:26:58 -0800414 // We don't want to trigger a page end moving unless the page has settled
415 // and the user has stopped scrolling
416 if (mTouchState == TOUCH_STATE_REST) {
417 pageEndMoving();
418 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700419
420 // Notify the user when the page changes
421 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
422 AccessibilityEvent ev =
423 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
424 ev.getText().add(getCurrentPageDescription());
425 sendAccessibilityEventUnchecked(ev);
426 }
Michael Jurka0142d492010-08-25 17:46:15 -0700427 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
Michael Jurka0142d492010-08-25 17:46:15 -0700429 return false;
430 }
431
432 @Override
433 public void computeScroll() {
434 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
436
437 @Override
438 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700439 if (!mIsDataReady) {
440 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
441 return;
442 }
443
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
445 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
446 if (widthMode != MeasureSpec.EXACTLY) {
447 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
448 }
449
Adam Lesinski6b879f02010-11-04 16:15:23 -0700450 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
451 * of the All apps view on XLarge displays to not take up more space then it needs. Width
452 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
453 * each page to have the same width.
454 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700456 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
457 int maxChildHeight = 0;
458
459 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700460 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700461
Michael Jurka36fcb742011-04-06 17:08:58 -0700462
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700464 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700465 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 final int childCount = getChildCount();
467 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700468 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700469 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700470 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
471
472 int childWidthMode;
473 if (lp.width == LayoutParams.WRAP_CONTENT) {
474 childWidthMode = MeasureSpec.AT_MOST;
475 } else {
476 childWidthMode = MeasureSpec.EXACTLY;
477 }
478
479 int childHeightMode;
480 if (lp.height == LayoutParams.WRAP_CONTENT) {
481 childHeightMode = MeasureSpec.AT_MOST;
482 } else {
483 childHeightMode = MeasureSpec.EXACTLY;
484 }
485
486 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700487 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700488 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700489 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700490
491 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700492 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700493 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
494 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700495 }
496
497 if (heightMode == MeasureSpec.AT_MOST) {
498 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 }
Winson Chungae890b82011-09-13 18:08:54 -0700500
Winson Chungae890b82011-09-13 18:08:54 -0700501 setMeasuredDimension(widthSize, heightSize);
502
Adam Cohen25b29952011-11-02 14:49:06 -0700503 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
504 // We also wait until we set the measured dimensions before flushing the cache as well, to
505 // ensure that the cache is filled with good values.
506 invalidateCachedOffsets();
507 updateScrollingIndicatorPosition();
508
Adam Cohenfaa28302010-11-19 12:02:18 -0800509 if (childCount > 0) {
510 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
511 } else {
512 mMaxScrollX = 0;
513 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 }
515
Michael Jurka8c920dd2011-01-20 14:16:56 -0800516 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800517 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
518 int delta = newX - mScrollX;
519
Michael Jurka8c920dd2011-01-20 14:16:56 -0800520 final int pageCount = getChildCount();
521 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700522 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800523 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800524 }
525 setCurrentPage(newCurrentPage);
526 }
527
Michael Jurka8c920dd2011-01-20 14:16:56 -0800528 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
529 // 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 -0800530 // tightens the layout accordingly
531 public void setLayoutScale(float childrenScale) {
532 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700533 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800534
535 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
536 int childCount = getChildCount();
537 float childrenX[] = new float[childCount];
538 float childrenY[] = new float[childCount];
539 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700540 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 childrenX[i] = child.getX();
542 childrenY[i] = child.getY();
543 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700544 // Trigger a full re-layout (never just call onLayout directly!)
545 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
546 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
547 requestLayout();
548 measure(widthSpec, heightSpec);
549 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800550 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700551 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800552 child.setX(childrenX[i]);
553 child.setY(childrenY[i]);
554 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700555
Michael Jurkad3ef3062010-11-23 16:23:58 -0800556 // Also, the page offset has changed (since the pages are now smaller);
557 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800558 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 }
560
Adam Cohen60b07122011-11-14 17:26:06 -0800561 public void setPageSpacing(int pageSpacing) {
562 mPageSpacing = pageSpacing;
563 invalidateCachedOffsets();
564 }
565
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700567 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700568 if (!mIsDataReady) {
569 return;
570 }
571
Winson Chung785d2eb2011-04-14 16:08:02 -0700572 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700573 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 final int childCount = getChildCount();
575 int childLeft = 0;
576 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700577 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
578 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700579 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700580
581 // Calculate the variable page spacing if necessary
582 if (mPageSpacing < 0) {
Adam Cohen60b07122011-11-14 17:26:06 -0800583 setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
Winson Chungae890b82011-09-13 18:08:54 -0700584 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700585 }
586
587 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700588 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700591 final int childHeight = child.getMeasuredHeight();
592 int childTop = mPaddingTop;
593 if (mCenterPagesVertically) {
594 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
595 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800596
Winson Chung785d2eb2011-04-14 16:08:02 -0700597 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700598 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800599 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700600 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 }
602 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700603
604 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
605 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800606 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700607 setHorizontalScrollBarEnabled(true);
608 mFirstLayout = false;
609 }
610
Michael Jurkad3ef3062010-11-23 16:23:58 -0800611 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
612 mFirstLayout = false;
613 }
Winson Chunge3193b92010-09-10 11:44:42 -0700614 }
615
Adam Cohenf34bab52010-09-30 14:11:56 -0700616 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700617 if (isScrollingIndicatorEnabled()) {
618 updateScrollingIndicator();
619 }
620 if (mFadeInAdjacentScreens) {
621 for (int i = 0; i < getChildCount(); i++) {
622 View child = getChildAt(i);
623 if (child != null) {
624 float scrollProgress = getScrollProgress(screenCenter, child, i);
625 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800626 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700627 }
628 }
629 invalidate();
630 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700631 }
632
Winson Chunge3193b92010-09-10 11:44:42 -0700633 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700634 protected void onViewAdded(View child) {
635 super.onViewAdded(child);
636
637 // This ensures that when children are added, they get the correct transforms / alphas
638 // in accordance with any scroll effects.
639 mForceScreenScrolled = true;
640 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700641 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700642 }
643
Adam Cohen73894962011-10-31 13:17:17 -0700644 protected void invalidateCachedOffsets() {
645 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700646 if (count == 0) {
647 mChildOffsets = null;
648 mChildRelativeOffsets = null;
649 mChildOffsetsWithLayoutScale = null;
650 return;
651 }
Adam Cohen73894962011-10-31 13:17:17 -0700652
653 mChildOffsets = new int[count];
654 mChildRelativeOffsets = new int[count];
655 mChildOffsetsWithLayoutScale = new int[count];
656 for (int i = 0; i < count; i++) {
657 mChildOffsets[i] = -1;
658 mChildRelativeOffsets[i] = -1;
659 mChildOffsetsWithLayoutScale[i] = -1;
660 }
661 }
662
663 protected int getChildOffset(int index) {
664 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
665 mChildOffsets : mChildOffsetsWithLayoutScale;
666
667 if (childOffsets != null && childOffsets[index] != -1) {
668 return childOffsets[index];
669 } else {
670 if (getChildCount() == 0)
671 return 0;
672
673 int offset = getRelativeChildOffset(0);
674 for (int i = 0; i < index; ++i) {
675 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
676 }
677 if (childOffsets != null) {
678 childOffsets[index] = offset;
679 }
680 return offset;
681 }
682 }
683
684 protected int getRelativeChildOffset(int index) {
685 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
686 return mChildRelativeOffsets[index];
687 } else {
688 final int padding = mPaddingLeft + mPaddingRight;
689 final int offset = mPaddingLeft +
690 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
691 if (mChildRelativeOffsets != null) {
692 mChildRelativeOffsets[index] = offset;
693 }
694 return offset;
695 }
696 }
697
698 protected int getScaledRelativeChildOffset(int index) {
699 final int padding = mPaddingLeft + mPaddingRight;
700 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
701 getScaledMeasuredWidth(getPageAt(index))) / 2;
702 return offset;
703 }
704
705 protected int getScaledMeasuredWidth(View child) {
706 // This functions are called enough times that it actually makes a difference in the
707 // profiler -- so just inline the max() here
708 final int measuredWidth = child.getMeasuredWidth();
709 final int minWidth = mMinimumWidth;
710 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
711 return (int) (maxWidth * mLayoutScale + 0.5f);
712 }
713
Michael Jurkadde558b2011-11-09 22:09:06 -0800714 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700715 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700716 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700717 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700718 int leftScreen = 0;
719 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800720 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800721 while (leftScreen < pageCount - 1 &&
Michael Jurka47f74742012-03-02 13:39:13 -0800722 currPage.getRight() - currPage.getPaddingRight() < mScrollX) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700723 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800724 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 }
726 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800727 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800728 while (rightScreen < pageCount - 1 &&
Michael Jurka47f74742012-03-02 13:39:13 -0800729 currPage.getLeft() + currPage.getPaddingLeft() < mScrollX + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700730 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800731 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700732 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800733 range[0] = leftScreen;
734 range[1] = rightScreen;
735 } else {
736 range[0] = -1;
737 range[1] = -1;
738 }
739 }
740
741 @Override
742 protected void dispatchDraw(Canvas canvas) {
743 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800744 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
745 // it is equal to the scaled overscroll position.
746 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800747
748 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
749 screenScrolled(screenCenter);
750 mLastScreenCenter = screenCenter;
751 mForceScreenScrolled = false;
752 }
753
754 // Find out which screens are visible; as an optimization we only call draw on them
755 final int pageCount = getChildCount();
756 if (pageCount > 0) {
757 getVisiblePages(mTempVisiblePagesRange);
758 final int leftScreen = mTempVisiblePagesRange[0];
759 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800760 if (leftScreen != -1 && rightScreen != -1) {
761 final long drawingTime = getDrawingTime();
762 // Clip to the bounds
763 canvas.save();
764 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
765 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700766
Michael Jurka80c69852011-12-16 14:16:32 -0800767 // On certain graphics drivers, if you draw to a off-screen buffer that's not
768 // used, it can lead to poor performance. We were running into this when
769 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
770 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
771 // As a fix, below we set pages that aren't going to be rendered are to be
772 // View.INVISIBLE, preventing re-drawing of their hardware layer
773 for (int i = getChildCount() - 1; i >= 0; i--) {
774 final View v = getPageAt(i);
Winson Chungb33e05f2012-01-30 12:18:04 -0800775
Michael Jurkabed61d22012-02-14 22:51:29 -0800776 if (leftScreen <= i && i <= rightScreen &&
777 v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
Michael Jurka80c69852011-12-16 14:16:32 -0800778 v.setVisibility(VISIBLE);
779 drawChild(canvas, v, drawingTime);
780 } else {
781 v.setVisibility(INVISIBLE);
782 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800783 }
784 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700785 }
Michael Jurka0142d492010-08-25 17:46:15 -0700786 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 }
788
789 @Override
790 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700791 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700792 if (page != mCurrentPage || !mScroller.isFinished()) {
793 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 return true;
795 }
796 return false;
797 }
798
799 @Override
800 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700801 int focusablePage;
802 if (mNextPage != INVALID_PAGE) {
803 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700805 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 }
Winson Chung86f77532010-08-24 11:08:22 -0700807 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700809 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 }
811 return false;
812 }
813
814 @Override
815 public boolean dispatchUnhandledMove(View focused, int direction) {
816 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700817 if (getCurrentPage() > 0) {
818 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 return true;
820 }
821 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700822 if (getCurrentPage() < getPageCount() - 1) {
823 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 return true;
825 }
826 }
827 return super.dispatchUnhandledMove(focused, direction);
828 }
829
830 @Override
831 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700832 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
833 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 }
835 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700836 if (mCurrentPage > 0) {
837 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (mCurrentPage < getPageCount() - 1) {
841 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843 }
844 }
845
846 /**
847 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700848 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 *
Winson Chung86f77532010-08-24 11:08:22 -0700850 * This happens when live folders requery, and if they're off page, they
851 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 */
853 @Override
854 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700855 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 View v = focused;
857 while (true) {
858 if (v == current) {
859 super.focusableViewAvailable(focused);
860 return;
861 }
862 if (v == this) {
863 return;
864 }
865 ViewParent parent = v.getParent();
866 if (parent instanceof View) {
867 v = (View)v.getParent();
868 } else {
869 return;
870 }
871 }
872 }
873
874 /**
875 * {@inheritDoc}
876 */
877 @Override
878 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
879 if (disallowIntercept) {
880 // We need to make sure to cancel our long press if
881 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700882 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700883 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
885 super.requestDisallowInterceptTouchEvent(disallowIntercept);
886 }
887
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800888 /**
889 * Return true if a tap at (x, y) should trigger a flip to the previous page.
890 */
891 protected boolean hitsPreviousPage(float x, float y) {
892 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
893 }
894
895 /**
896 * Return true if a tap at (x, y) should trigger a flip to the next page.
897 */
898 protected boolean hitsNextPage(float x, float y) {
899 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
900 }
901
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 @Override
903 public boolean onInterceptTouchEvent(MotionEvent ev) {
904 /*
905 * This method JUST determines whether we want to intercept the motion.
906 * If we return true, onTouchEvent will be called and we do the actual
907 * scrolling there.
908 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800909 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700910
Winson Chung45e1d6e2010-11-09 17:19:49 -0800911 // Skip touch handling if there are no pages to swipe
912 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
913
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 /*
915 * Shortcut the most recurring case: the user is in the dragging
916 * state and he is moving his finger. We want to intercept this
917 * motion.
918 */
919 final int action = ev.getAction();
920 if ((action == MotionEvent.ACTION_MOVE) &&
921 (mTouchState == TOUCH_STATE_SCROLLING)) {
922 return true;
923 }
924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 switch (action & MotionEvent.ACTION_MASK) {
926 case MotionEvent.ACTION_MOVE: {
927 /*
928 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
929 * whether the user has moved far enough from his original down touch.
930 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700931 if (mActivePointerId != INVALID_POINTER) {
932 determineScrollingStart(ev);
933 break;
934 }
935 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
936 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
937 // i.e. fall through to the next case (don't break)
938 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
939 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 }
941
942 case MotionEvent.ACTION_DOWN: {
943 final float x = ev.getX();
944 final float y = ev.getY();
945 // Remember location of down touch
946 mDownMotionX = x;
947 mLastMotionX = x;
948 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800949 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800950 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 mActivePointerId = ev.getPointerId(0);
952 mAllowLongPress = true;
953
954 /*
955 * If being flinged and user touches the screen, initiate drag;
956 * otherwise don't. mScroller.isFinished should be false when
957 * being flinged.
958 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700959 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700960 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
961 if (finishedScrolling) {
962 mTouchState = TOUCH_STATE_REST;
963 mScroller.abortAnimation();
964 } else {
965 mTouchState = TOUCH_STATE_SCROLLING;
966 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700967
Winson Chung86f77532010-08-24 11:08:22 -0700968 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800970 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800972 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800974 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 mTouchState = TOUCH_STATE_NEXT_PAGE;
976 }
977 }
978 }
979 break;
980 }
981
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800983 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 mTouchState = TOUCH_STATE_REST;
985 mAllowLongPress = false;
986 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800987 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 break;
989
990 case MotionEvent.ACTION_POINTER_UP:
991 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800992 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 break;
994 }
995
996 /*
997 * The only time we want to intercept motion events is if we are in the
998 * drag mode.
999 */
1000 return mTouchState != TOUCH_STATE_REST;
1001 }
1002
Adam Cohenf8d28232011-02-01 21:47:00 -08001003 protected void determineScrollingStart(MotionEvent ev) {
1004 determineScrollingStart(ev, 1.0f);
1005 }
1006
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 /*
1008 * Determines if we should change the touch state to start scrolling after the
1009 * user moves their touch point too far.
1010 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001011 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 /*
1013 * Locally do absolute value. mLastMotionX is set to the y value
1014 * of the down event.
1015 */
1016 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001017 if (pointerIndex == -1) {
1018 return;
1019 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 final float x = ev.getX(pointerIndex);
1021 final float y = ev.getY(pointerIndex);
1022 final int xDiff = (int) Math.abs(x - mLastMotionX);
1023 final int yDiff = (int) Math.abs(y - mLastMotionY);
1024
Adam Cohenf8d28232011-02-01 21:47:00 -08001025 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001026 boolean xPaged = xDiff > mPagingTouchSlop;
1027 boolean xMoved = xDiff > touchSlop;
1028 boolean yMoved = yDiff > touchSlop;
1029
Adam Cohenf8d28232011-02-01 21:47:00 -08001030 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001031 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 // Scroll if the user moved far enough along the X axis
1033 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001034 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001036 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001037 mTouchX = mScrollX;
1038 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1039 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 }
1041 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001042 cancelCurrentPageLongPress();
1043 }
1044 }
1045
1046 protected void cancelCurrentPageLongPress() {
1047 if (mAllowLongPress) {
1048 mAllowLongPress = false;
1049 // Try canceling the long press. It could also have been scheduled
1050 // by a distant descendant, so use the mAllowLongPress flag to block
1051 // everything
1052 final View currentPage = getPageAt(mCurrentPage);
1053 if (currentPage != null) {
1054 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 }
1056 }
1057 }
1058
Adam Cohenb5ba0972011-09-07 18:02:31 -07001059 protected float getScrollProgress(int screenCenter, View v, int page) {
1060 final int halfScreenSize = getMeasuredWidth() / 2;
1061
1062 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1063 int delta = screenCenter - (getChildOffset(page) -
1064 getRelativeChildOffset(page) + halfScreenSize);
1065
1066 float scrollProgress = delta / (totalDistance * 1.0f);
1067 scrollProgress = Math.min(scrollProgress, 1.0f);
1068 scrollProgress = Math.max(scrollProgress, -1.0f);
1069 return scrollProgress;
1070 }
1071
Adam Cohene0f66b52010-11-23 15:06:07 -08001072 // This curve determines how the effect of scrolling over the limits of the page dimishes
1073 // as the user pulls further and further from the bounds
1074 private float overScrollInfluenceCurve(float f) {
1075 f -= 1.0f;
1076 return f * f * f + 1.0f;
1077 }
1078
Adam Cohenb5ba0972011-09-07 18:02:31 -07001079 protected void acceleratedOverScroll(float amount) {
1080 int screenSize = getMeasuredWidth();
1081
1082 // We want to reach the max over scroll effect when the user has
1083 // over scrolled half the size of the screen
1084 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1085
1086 if (f == 0) return;
1087
1088 // Clamp this factor, f, to -1 < f < 1
1089 if (Math.abs(f) >= 1) {
1090 f /= Math.abs(f);
1091 }
1092
1093 int overScrollAmount = (int) Math.round(f * screenSize);
1094 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001095 mOverScrollX = overScrollAmount;
1096 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001097 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001098 mOverScrollX = mMaxScrollX + overScrollAmount;
1099 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001100 }
1101 invalidate();
1102 }
1103
1104 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001105 int screenSize = getMeasuredWidth();
1106
1107 float f = (amount / screenSize);
1108
1109 if (f == 0) return;
1110 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1111
Adam Cohen7bfc9792011-01-28 13:52:37 -08001112 // Clamp this factor, f, to -1 < f < 1
1113 if (Math.abs(f) >= 1) {
1114 f /= Math.abs(f);
1115 }
1116
Adam Cohene0f66b52010-11-23 15:06:07 -08001117 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001118 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001119 mOverScrollX = overScrollAmount;
1120 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001121 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001122 mOverScrollX = mMaxScrollX + overScrollAmount;
1123 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001124 }
1125 invalidate();
1126 }
1127
Adam Cohenb5ba0972011-09-07 18:02:31 -07001128 protected void overScroll(float amount) {
1129 dampedOverScroll(amount);
1130 }
1131
Michael Jurkac5b262c2011-01-12 20:24:50 -08001132 protected float maxOverScroll() {
1133 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001134 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001135 float f = 1.0f;
1136 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1137 return OVERSCROLL_DAMP_FACTOR * f;
1138 }
1139
Winson Chung321e9ee2010-08-09 13:37:56 -07001140 @Override
1141 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001142 // Skip touch handling if there are no pages to swipe
1143 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1144
Michael Jurkab8f06722010-10-10 15:58:46 -07001145 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001146
1147 final int action = ev.getAction();
1148
1149 switch (action & MotionEvent.ACTION_MASK) {
1150 case MotionEvent.ACTION_DOWN:
1151 /*
1152 * If being flinged and user touches, stop the fling. isFinished
1153 * will be false if being flinged.
1154 */
1155 if (!mScroller.isFinished()) {
1156 mScroller.abortAnimation();
1157 }
1158
1159 // Remember where the motion event started
1160 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001161 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001162 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001164 if (mTouchState == TOUCH_STATE_SCROLLING) {
1165 pageBeginMoving();
1166 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001167 break;
1168
1169 case MotionEvent.ACTION_MOVE:
1170 if (mTouchState == TOUCH_STATE_SCROLLING) {
1171 // Scroll to follow the motion event
1172 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1173 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001174 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001175
Adam Cohenaefd4e12011-02-14 16:39:38 -08001176 mTotalMotionX += Math.abs(deltaX);
1177
Winson Chungc0844aa2011-02-02 15:25:58 -08001178 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1179 // keep the remainder because we are actually testing if we've moved from the last
1180 // scrolled position (which is discrete).
1181 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001182 mTouchX += deltaX;
1183 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1184 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001185 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001186 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001187 } else {
1188 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001190 mLastMotionX = x;
1191 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 } else {
1193 awakenScrollBars();
1194 }
Adam Cohen564976a2010-10-13 18:52:07 -07001195 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 determineScrollingStart(ev);
1197 }
1198 break;
1199
1200 case MotionEvent.ACTION_UP:
1201 if (mTouchState == TOUCH_STATE_SCROLLING) {
1202 final int activePointerId = mActivePointerId;
1203 final int pointerIndex = ev.findPointerIndex(activePointerId);
1204 final float x = ev.getX(pointerIndex);
1205 final VelocityTracker velocityTracker = mVelocityTracker;
1206 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1207 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001208 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001209 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1210 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1211 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001212
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001213 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1214
Adam Cohen00481b32011-11-18 12:03:48 -08001215 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001216 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001217
Adam Cohenaefd4e12011-02-14 16:39:38 -08001218 // In the case that the page is moved far to one direction and then is flung
1219 // in the opposite direction, we use a threshold to determine whether we should
1220 // just return to the starting page, or if we should skip one further.
1221 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001222 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001223 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001224 returnToOriginalPage = true;
1225 }
1226
Adam Cohenaefd4e12011-02-14 16:39:38 -08001227 int finalPage;
1228 // We give flings precedence over large moves, which is why we short-circuit our
1229 // test for a large move if a fling has been registered. That is, a large
1230 // move to the left and fling to the right will register as a fling to the right.
1231 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1232 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1233 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1234 snapToPageWithVelocity(finalPage, velocityX);
1235 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1236 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001237 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001238 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1239 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 } else {
1241 snapToDestination();
1242 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001243 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 // at this point we have not moved beyond the touch slop
1245 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1246 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001247 int nextPage = Math.max(0, mCurrentPage - 1);
1248 if (nextPage != mCurrentPage) {
1249 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 } else {
1251 snapToDestination();
1252 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001253 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001254 // at this point we have not moved beyond the touch slop
1255 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1256 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001257 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1258 if (nextPage != mCurrentPage) {
1259 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 } else {
1261 snapToDestination();
1262 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001263 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001264 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 }
1266 mTouchState = TOUCH_STATE_REST;
1267 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001268 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 break;
1270
1271 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001272 if (mTouchState == TOUCH_STATE_SCROLLING) {
1273 snapToDestination();
1274 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 mTouchState = TOUCH_STATE_REST;
1276 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001277 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 break;
1279
1280 case MotionEvent.ACTION_POINTER_UP:
1281 onSecondaryPointerUp(ev);
1282 break;
1283 }
1284
1285 return true;
1286 }
1287
Winson Chung185d7162011-02-28 13:47:29 -08001288 @Override
1289 public boolean onGenericMotionEvent(MotionEvent event) {
1290 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1291 switch (event.getAction()) {
1292 case MotionEvent.ACTION_SCROLL: {
1293 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1294 final float vscroll;
1295 final float hscroll;
1296 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1297 vscroll = 0;
1298 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1299 } else {
1300 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1301 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1302 }
1303 if (hscroll != 0 || vscroll != 0) {
1304 if (hscroll > 0 || vscroll > 0) {
1305 scrollRight();
1306 } else {
1307 scrollLeft();
1308 }
1309 return true;
1310 }
1311 }
1312 }
1313 }
1314 return super.onGenericMotionEvent(event);
1315 }
1316
Michael Jurkab8f06722010-10-10 15:58:46 -07001317 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1318 if (mVelocityTracker == null) {
1319 mVelocityTracker = VelocityTracker.obtain();
1320 }
1321 mVelocityTracker.addMovement(ev);
1322 }
1323
1324 private void releaseVelocityTracker() {
1325 if (mVelocityTracker != null) {
1326 mVelocityTracker.recycle();
1327 mVelocityTracker = null;
1328 }
1329 }
1330
Winson Chung321e9ee2010-08-09 13:37:56 -07001331 private void onSecondaryPointerUp(MotionEvent ev) {
1332 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1333 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1334 final int pointerId = ev.getPointerId(pointerIndex);
1335 if (pointerId == mActivePointerId) {
1336 // This was our active pointer going up. Choose a new
1337 // active pointer and adjust accordingly.
1338 // TODO: Make this decision more intelligent.
1339 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1340 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1341 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001342 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001343 mActivePointerId = ev.getPointerId(newPointerIndex);
1344 if (mVelocityTracker != null) {
1345 mVelocityTracker.clear();
1346 }
1347 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001348 }
1349
Michael Jurkad771c962011-08-09 15:00:48 -07001350 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001351
1352 @Override
1353 public void requestChildFocus(View child, View focused) {
1354 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001355 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001356 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001357 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001358 }
1359 }
1360
Winson Chunge3193b92010-09-10 11:44:42 -07001361 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1362 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001363 int left;
1364 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001365 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001366 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001367 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001368 if (left <= relativeOffset && relativeOffset <= right) {
1369 return i;
1370 }
Winson Chunge3193b92010-09-10 11:44:42 -07001371 }
1372 return -1;
1373 }
1374
Winson Chung1908d072011-02-24 18:09:44 -08001375 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001376 // This functions are called enough times that it actually makes a difference in the
1377 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001378 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001379 final int minWidth = mMinimumWidth;
1380 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001381 }
1382
Adam Cohend19d3ca2010-09-15 14:43:42 -07001383 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001384 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 int minDistanceFromScreenCenterIndex = -1;
1386 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1387 final int childCount = getChildCount();
1388 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001389 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001390 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 int halfChildWidth = (childWidth / 2);
1392 int childCenter = getChildOffset(i) + halfChildWidth;
1393 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1394 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1395 minDistanceFromScreenCenter = distanceFromScreenCenter;
1396 minDistanceFromScreenCenterIndex = i;
1397 }
1398 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001399 return minDistanceFromScreenCenterIndex;
1400 }
1401
1402 protected void snapToDestination() {
1403 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 }
1405
Adam Cohene0f66b52010-11-23 15:06:07 -08001406 private static class ScrollInterpolator implements Interpolator {
1407 public ScrollInterpolator() {
1408 }
1409
1410 public float getInterpolation(float t) {
1411 t -= 1.0f;
1412 return t*t*t*t*t + 1;
1413 }
1414 }
1415
1416 // We want the duration of the page snap animation to be influenced by the distance that
1417 // the screen has to travel, however, we don't want this duration to be effected in a
1418 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1419 // of travel has on the overall snap duration.
1420 float distanceInfluenceForSnapDuration(float f) {
1421 f -= 0.5f; // center the values about 0.
1422 f *= 0.3f * Math.PI / 2.0f;
1423 return (float) Math.sin(f);
1424 }
1425
Michael Jurka0142d492010-08-25 17:46:15 -07001426 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001427 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1428 int halfScreenSize = getMeasuredWidth() / 2;
1429
Winson Chung785d2eb2011-04-14 16:08:02 -07001430 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1431 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1432 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001433 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1434 int delta = newX - mUnboundedScrollX;
1435 int duration = 0;
1436
Adam Cohen265b9a62011-12-07 14:37:18 -08001437 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001438 // If the velocity is low enough, then treat this more as an automatic page advance
1439 // as opposed to an apparent physical response to flinging
1440 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1441 return;
1442 }
1443
1444 // Here we compute a "distance" that will be used in the computation of the overall
1445 // snap duration. This is a function of the actual distance that needs to be traveled;
1446 // we keep this value close to half screen size in order to reduce the variance in snap
1447 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001448 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001449 float distance = halfScreenSize + halfScreenSize *
1450 distanceInfluenceForSnapDuration(distanceRatio);
1451
1452 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001453 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001454
1455 // we want the page's snap velocity to approximately match the velocity at which the
1456 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001457 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1458 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001459
1460 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001461 }
1462
1463 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001464 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 }
1466
Michael Jurka0142d492010-08-25 17:46:15 -07001467 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001468 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001469
Winson Chung785d2eb2011-04-14 16:08:02 -07001470 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1471 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1472 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001473 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001474 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001476 snapToPage(whichPage, delta, duration);
1477 }
1478
1479 protected void snapToPage(int whichPage, int delta, int duration) {
1480 mNextPage = whichPage;
1481
1482 View focusedChild = getFocusedChild();
1483 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001484 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001485 focusedChild.clearFocus();
1486 }
1487
1488 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 awakenScrollBars(duration);
1490 if (duration == 0) {
1491 duration = Math.abs(delta);
1492 }
1493
1494 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001495 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001496
Winson Chungb44b5242011-06-13 11:32:14 -07001497 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1498 // loading associated pages until the scroll settles
1499 if (mDeferScrollUpdate) {
1500 loadAssociatedPages(mNextPage);
1501 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001502 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001503 }
Michael Jurka0142d492010-08-25 17:46:15 -07001504 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 invalidate();
1506 }
1507
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 public void scrollLeft() {
1509 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001510 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001512 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 }
1514 }
1515
1516 public void scrollRight() {
1517 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001518 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001519 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001520 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 }
1522 }
1523
Winson Chung86f77532010-08-24 11:08:22 -07001524 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 int result = -1;
1526 if (v != null) {
1527 ViewParent vp = v.getParent();
1528 int count = getChildCount();
1529 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001530 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001531 return i;
1532 }
1533 }
1534 }
1535 return result;
1536 }
1537
1538 /**
1539 * @return True is long presses are still allowed for the current touch
1540 */
1541 public boolean allowLongPress() {
1542 return mAllowLongPress;
1543 }
1544
Michael Jurka0142d492010-08-25 17:46:15 -07001545 /**
1546 * Set true to allow long-press events to be triggered, usually checked by
1547 * {@link Launcher} to accept or block dpad-initiated long-presses.
1548 */
1549 public void setAllowLongPress(boolean allowLongPress) {
1550 mAllowLongPress = allowLongPress;
1551 }
1552
Winson Chung321e9ee2010-08-09 13:37:56 -07001553 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001554 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001555
1556 SavedState(Parcelable superState) {
1557 super(superState);
1558 }
1559
1560 private SavedState(Parcel in) {
1561 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001562 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001563 }
1564
1565 @Override
1566 public void writeToParcel(Parcel out, int flags) {
1567 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001568 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 }
1570
1571 public static final Parcelable.Creator<SavedState> CREATOR =
1572 new Parcelable.Creator<SavedState>() {
1573 public SavedState createFromParcel(Parcel in) {
1574 return new SavedState(in);
1575 }
1576
1577 public SavedState[] newArray(int size) {
1578 return new SavedState[size];
1579 }
1580 };
1581 }
1582
Winson Chungf314b0e2011-08-16 11:54:27 -07001583 protected void loadAssociatedPages(int page) {
1584 loadAssociatedPages(page, false);
1585 }
1586 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001587 if (mContentIsRefreshable) {
1588 final int count = getChildCount();
1589 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001590 int lowerPageBound = getAssociatedLowerPageBound(page);
1591 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001592 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1593 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001594 // First, clear any pages that should no longer be loaded
1595 for (int i = 0; i < count; ++i) {
1596 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001597 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001598 if (layout.getPageChildCount() > 0) {
1599 layout.removeAllViewsOnPage();
1600 }
1601 mDirtyPageContent.set(i, true);
1602 }
1603 }
1604 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001605 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001606 if ((i != page) && immediateAndOnly) {
1607 continue;
1608 }
Michael Jurka0142d492010-08-25 17:46:15 -07001609 if (lowerPageBound <= i && i <= upperPageBound) {
1610 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001611 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001612 mDirtyPageContent.set(i, false);
1613 }
Winson Chung86f77532010-08-24 11:08:22 -07001614 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001615 }
1616 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001617 }
1618 }
1619
Winson Chunge3193b92010-09-10 11:44:42 -07001620 protected int getAssociatedLowerPageBound(int page) {
1621 return Math.max(0, page - 1);
1622 }
1623 protected int getAssociatedUpperPageBound(int page) {
1624 final int count = getChildCount();
1625 return Math.min(page + 1, count - 1);
1626 }
1627
Winson Chung86f77532010-08-24 11:08:22 -07001628 /**
1629 * This method is called ONLY to synchronize the number of pages that the paged view has.
1630 * To actually fill the pages with information, implement syncPageItems() below. It is
1631 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1632 * and therefore, individual page items do not need to be updated in this method.
1633 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001635
1636 /**
1637 * This method is called to synchronize the items that are on a particular page. If views on
1638 * the page can be reused, then they should be updated within this method.
1639 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001640 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001641
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001642 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001643 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001644 }
1645 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001646 invalidatePageData(currentPage, false);
1647 }
1648 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001649 if (!mIsDataReady) {
1650 return;
1651 }
1652
Michael Jurka0142d492010-08-25 17:46:15 -07001653 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001654 // Force all scrolling-related behavior to end
1655 mScroller.forceFinished(true);
1656 mNextPage = INVALID_PAGE;
1657
Michael Jurka0142d492010-08-25 17:46:15 -07001658 // Update all the pages
1659 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001660
Winson Chung5a808352011-06-27 19:08:49 -07001661 // We must force a measure after we've loaded the pages to update the content width and
1662 // to determine the full scroll width
1663 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1664 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1665
1666 // Set a new page as the current page if necessary
1667 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001668 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001669 }
1670
Michael Jurka0142d492010-08-25 17:46:15 -07001671 // Mark each of the pages as dirty
1672 final int count = getChildCount();
1673 mDirtyPageContent.clear();
1674 for (int i = 0; i < count; ++i) {
1675 mDirtyPageContent.add(true);
1676 }
1677
1678 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001679 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001680 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001681 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001682 }
Winson Chung007c6982011-06-14 13:27:53 -07001683
Michael Jurkaafaa0502011-12-13 18:22:50 -08001684 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001685 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1686 // found
1687 if (mHasScrollIndicator && mScrollIndicator == null) {
1688 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001689 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001690 mHasScrollIndicator = mScrollIndicator != null;
1691 if (mHasScrollIndicator) {
1692 mScrollIndicator.setVisibility(View.VISIBLE);
1693 }
1694 }
1695 return mScrollIndicator;
1696 }
1697
1698 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001699 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001700 }
1701
Winson Chung5a808352011-06-27 19:08:49 -07001702 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1703 @Override
1704 public void run() {
1705 hideScrollingIndicator(false);
1706 }
1707 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001708 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001709 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001710 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001711 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001712 }
1713
Michael Jurka430e8a52011-08-08 15:52:14 -07001714 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001715 mShouldShowScrollIndicator = true;
1716 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001717 if (getChildCount() <= 1) return;
1718 if (!isScrollingIndicatorEnabled()) return;
1719
Michael Jurkabed61d22012-02-14 22:51:29 -08001720 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001721 getScrollingIndicator();
1722 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001723 // Fade the indicator in
1724 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001725 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001726 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001727 if (immediately) {
1728 mScrollIndicator.setAlpha(1f);
1729 } else {
1730 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1731 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1732 mScrollIndicatorAnimator.start();
1733 }
Winson Chung007c6982011-06-14 13:27:53 -07001734 }
1735 }
1736
Adam Cohen21b41102011-11-01 17:29:52 -07001737 protected void cancelScrollingIndicatorAnimations() {
1738 if (mScrollIndicatorAnimator != null) {
1739 mScrollIndicatorAnimator.cancel();
1740 }
1741 }
1742
Winson Chung007c6982011-06-14 13:27:53 -07001743 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001744 if (getChildCount() <= 1) return;
1745 if (!isScrollingIndicatorEnabled()) return;
1746
1747 getScrollingIndicator();
1748 if (mScrollIndicator != null) {
1749 // Fade the indicator out
1750 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001751 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001752 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001753 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001754 mScrollIndicator.setAlpha(0f);
1755 } else {
1756 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1757 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1758 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1759 private boolean cancelled = false;
1760 @Override
1761 public void onAnimationCancel(android.animation.Animator animation) {
1762 cancelled = true;
1763 }
1764 @Override
1765 public void onAnimationEnd(Animator animation) {
1766 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001767 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001768 }
1769 }
1770 });
1771 mScrollIndicatorAnimator.start();
1772 }
Winson Chung007c6982011-06-14 13:27:53 -07001773 }
1774 }
1775
Winson Chung32174c82011-07-19 15:47:55 -07001776 /**
1777 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1778 * fill its space on the track or not.
1779 */
1780 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001781 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001782 }
1783
Winson Chung007c6982011-06-14 13:27:53 -07001784 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001785 if (getChildCount() <= 1) return;
1786 if (!isScrollingIndicatorEnabled()) return;
1787
1788 getScrollingIndicator();
1789 if (mScrollIndicator != null) {
1790 updateScrollingIndicatorPosition();
1791 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001792 if (mShouldShowScrollIndicator) {
1793 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1794 }
Winson Chung007c6982011-06-14 13:27:53 -07001795 }
1796
Winson Chung007c6982011-06-14 13:27:53 -07001797 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001798 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001799 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001800 int numPages = getChildCount();
1801 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001802 int lastChildIndex = Math.max(0, getChildCount() - 1);
1803 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001804 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001805 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1806 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001807
Winson Chungae890b82011-09-13 18:08:54 -07001808 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001809 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001810 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001811 if (hasElasticScrollIndicator()) {
1812 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1813 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1814 mScrollIndicator.requestLayout();
1815 }
1816 } else {
1817 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1818 indicatorPos += indicatorCenterOffset;
1819 }
Winson Chung007c6982011-06-14 13:27:53 -07001820 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001821 }
1822
Winson Chung3ac74c52011-06-30 17:39:37 -07001823 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001824 }
1825
1826 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001827 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001828
1829 /* Accessibility */
1830 @Override
1831 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1832 super.onInitializeAccessibilityNodeInfo(info);
1833 info.setScrollable(true);
1834 }
1835
1836 @Override
1837 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1838 super.onInitializeAccessibilityEvent(event);
1839 event.setScrollable(true);
1840 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1841 event.setFromIndex(mCurrentPage);
1842 event.setToIndex(mCurrentPage);
1843 event.setItemCount(getChildCount());
1844 }
1845 }
1846
Winson Chung6a0f57d2011-06-29 20:10:49 -07001847 protected String getCurrentPageDescription() {
1848 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1849 return String.format(mContext.getString(R.string.default_scroll_format),
1850 page + 1, getChildCount());
1851 }
Winson Chungd11265e2011-08-30 13:37:23 -07001852
1853 @Override
1854 public boolean onHoverEvent(android.view.MotionEvent event) {
1855 return true;
1856 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001857}