blob: c72fdccd8c5418cd85e0a064a7f5ad510fae7ab3 [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;
Adam Cohen265b9a62011-12-07 14:37:18 -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;
Winson Chunga6427b12011-07-27 10:53:39 -0700179 protected static final int sScrollIndicatorFadeInDuration = 150;
180 protected static final int sScrollIndicatorFadeOutDuration = 650;
181 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700182
Winson Chungb44b5242011-06-13 11:32:14 -0700183 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700184 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700185
Winson Chung86f77532010-08-24 11:08:22 -0700186 public interface PageSwitchListener {
187 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700188 }
189
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 public PagedView(Context context) {
191 this(context, null);
192 }
193
194 public PagedView(Context context, AttributeSet attrs) {
195 this(context, attrs, 0);
196 }
197
198 public PagedView(Context context, AttributeSet attrs, int defStyle) {
199 super(context, attrs, defStyle);
200
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 TypedArray a = context.obtainStyledAttributes(attrs,
202 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800203 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800205 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800207 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700212 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700213 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700214 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700215 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700216 mScrollIndicatorPaddingLeft =
217 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
218 mScrollIndicatorPaddingRight =
219 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700220 a.recycle();
221
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700223 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 }
225
226 /**
227 * Initializes various states for this workspace.
228 */
Michael Jurka0142d492010-08-25 17:46:15 -0700229 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700230 mDirtyPageContent = new ArrayList<Boolean>();
231 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800232 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700233 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700234 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700235
236 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
237 mTouchSlop = configuration.getScaledTouchSlop();
238 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
239 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700240 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800241
242 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
243 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
244 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246
Winson Chung86f77532010-08-24 11:08:22 -0700247 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
248 mPageSwitchListener = pageSwitchListener;
249 if (mPageSwitchListener != null) {
250 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 }
252 }
253
254 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700255 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
256 * out pages.
257 */
258 protected void setDataIsReady() {
259 mIsDataReady = true;
260 }
261 protected boolean isDataReady() {
262 return mIsDataReady;
263 }
264
265 /**
Winson Chung86f77532010-08-24 11:08:22 -0700266 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 *
Winson Chung86f77532010-08-24 11:08:22 -0700268 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 */
Winson Chung86f77532010-08-24 11:08:22 -0700270 int getCurrentPage() {
271 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 }
273
Winson Chung86f77532010-08-24 11:08:22 -0700274 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 return getChildCount();
276 }
277
Winson Chung86f77532010-08-24 11:08:22 -0700278 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 return getChildAt(index);
280 }
281
Adam Cohenae4f1552011-10-20 00:15:42 -0700282 protected int indexToPage(int index) {
283 return index;
284 }
285
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800287 * Updates the scroll of the current page immediately to its final scroll position. We use this
288 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
289 * the previous tab page.
290 */
291 protected void updateCurrentPageScroll() {
292 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
293 scrollTo(newX, 0);
294 mScroller.setFinalX(newX);
295 }
296
297 /**
Winson Chung86f77532010-08-24 11:08:22 -0700298 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 */
Winson Chung86f77532010-08-24 11:08:22 -0700300 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800301 if (!mScroller.isFinished()) {
302 mScroller.abortAnimation();
303 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800304 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
305 // the default
306 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800307 return;
308 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700309
Winson Chung86f77532010-08-24 11:08:22 -0700310 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800311 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700312 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700313 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800314 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 }
316
Michael Jurka0142d492010-08-25 17:46:15 -0700317 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700318 if (mPageSwitchListener != null) {
319 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
321 }
322
Michael Jurkace7e05f2011-02-01 22:02:35 -0800323 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700324 if (!mIsPageMoving) {
325 mIsPageMoving = true;
326 onPageBeginMoving();
327 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700328 }
329
Michael Jurkace7e05f2011-02-01 22:02:35 -0800330 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700331 if (mIsPageMoving) {
332 mIsPageMoving = false;
333 onPageEndMoving();
334 }
Michael Jurka0142d492010-08-25 17:46:15 -0700335 }
336
Adam Cohen26976d92011-03-22 15:33:33 -0700337 protected boolean isPageMoving() {
338 return mIsPageMoving;
339 }
340
Michael Jurka0142d492010-08-25 17:46:15 -0700341 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700342 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700343 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700344 }
345
346 // a method that subclasses can override to add behavior
347 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700348 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700349 }
350
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 /**
Winson Chung86f77532010-08-24 11:08:22 -0700352 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 *
354 * @param l The listener used to respond to long clicks.
355 */
356 @Override
357 public void setOnLongClickListener(OnLongClickListener l) {
358 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700359 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700361 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 }
363 }
364
365 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800366 public void scrollBy(int x, int y) {
367 scrollTo(mUnboundedScrollX + x, mScrollY + y);
368 }
369
370 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700371 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800372 mUnboundedScrollX = x;
373
374 if (x < 0) {
375 super.scrollTo(0, y);
376 if (mAllowOverScroll) {
377 overScroll(x);
378 }
379 } else if (x > mMaxScrollX) {
380 super.scrollTo(mMaxScrollX, y);
381 if (mAllowOverScroll) {
382 overScroll(x - mMaxScrollX);
383 }
384 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800385 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800386 super.scrollTo(x, y);
387 }
388
Michael Jurka0142d492010-08-25 17:46:15 -0700389 mTouchX = x;
390 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
391 }
392
393 // we moved this functionality to a helper function so SmoothPagedView can reuse it
394 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700396 // Don't bother scrolling if the page does not need to be moved
397 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700398 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
399 }
Michael Jurka0142d492010-08-25 17:46:15 -0700400 invalidate();
401 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700402 } else if (mNextPage != INVALID_PAGE) {
403 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700404 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700405 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700406
407 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700408 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700409 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700410 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700411 }
412
Adam Cohen73aa9752010-11-24 16:26:58 -0800413 // We don't want to trigger a page end moving unless the page has settled
414 // and the user has stopped scrolling
415 if (mTouchState == TOUCH_STATE_REST) {
416 pageEndMoving();
417 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700418
419 // Notify the user when the page changes
420 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
421 AccessibilityEvent ev =
422 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
423 ev.getText().add(getCurrentPageDescription());
424 sendAccessibilityEventUnchecked(ev);
425 }
Michael Jurka0142d492010-08-25 17:46:15 -0700426 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
Michael Jurka0142d492010-08-25 17:46:15 -0700428 return false;
429 }
430
431 @Override
432 public void computeScroll() {
433 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
435
436 @Override
437 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700438 if (!mIsDataReady) {
439 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
440 return;
441 }
442
Winson Chung321e9ee2010-08-09 13:37:56 -0700443 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
444 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
445 if (widthMode != MeasureSpec.EXACTLY) {
446 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
447 }
448
Adam Lesinski6b879f02010-11-04 16:15:23 -0700449 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
450 * of the All apps view on XLarge displays to not take up more space then it needs. Width
451 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
452 * each page to have the same width.
453 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700455 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
456 int maxChildHeight = 0;
457
458 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700459 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700460
Michael Jurka36fcb742011-04-06 17:08:58 -0700461
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 final int childCount = getChildCount();
466 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700467 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700468 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700469 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
470
471 int childWidthMode;
472 if (lp.width == LayoutParams.WRAP_CONTENT) {
473 childWidthMode = MeasureSpec.AT_MOST;
474 } else {
475 childWidthMode = MeasureSpec.EXACTLY;
476 }
477
478 int childHeightMode;
479 if (lp.height == LayoutParams.WRAP_CONTENT) {
480 childHeightMode = MeasureSpec.AT_MOST;
481 } else {
482 childHeightMode = MeasureSpec.EXACTLY;
483 }
484
485 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700486 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700487 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700488 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700489
490 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700491 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700492 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
493 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700494 }
495
496 if (heightMode == MeasureSpec.AT_MOST) {
497 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
Winson Chungae890b82011-09-13 18:08:54 -0700499
Winson Chungae890b82011-09-13 18:08:54 -0700500 setMeasuredDimension(widthSize, heightSize);
501
Adam Cohen25b29952011-11-02 14:49:06 -0700502 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
503 // We also wait until we set the measured dimensions before flushing the cache as well, to
504 // ensure that the cache is filled with good values.
505 invalidateCachedOffsets();
506 updateScrollingIndicatorPosition();
507
Adam Cohenfaa28302010-11-19 12:02:18 -0800508 if (childCount > 0) {
509 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
510 } else {
511 mMaxScrollX = 0;
512 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 }
514
Michael Jurka8c920dd2011-01-20 14:16:56 -0800515 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800516 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
517 int delta = newX - mScrollX;
518
Michael Jurka8c920dd2011-01-20 14:16:56 -0800519 final int pageCount = getChildCount();
520 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700521 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800522 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800523 }
524 setCurrentPage(newCurrentPage);
525 }
526
Michael Jurka8c920dd2011-01-20 14:16:56 -0800527 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
528 // 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 -0800529 // tightens the layout accordingly
530 public void setLayoutScale(float childrenScale) {
531 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700532 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533
534 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
535 int childCount = getChildCount();
536 float childrenX[] = new float[childCount];
537 float childrenY[] = new float[childCount];
538 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700539 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800540 childrenX[i] = child.getX();
541 childrenY[i] = child.getY();
542 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700543 // Trigger a full re-layout (never just call onLayout directly!)
544 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
545 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
546 requestLayout();
547 measure(widthSpec, heightSpec);
548 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800549 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700550 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800551 child.setX(childrenX[i]);
552 child.setY(childrenY[i]);
553 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700554
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 // Also, the page offset has changed (since the pages are now smaller);
556 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800557 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558 }
559
Adam Cohen60b07122011-11-14 17:26:06 -0800560 public void setPageSpacing(int pageSpacing) {
561 mPageSpacing = pageSpacing;
562 invalidateCachedOffsets();
563 }
564
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700566 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700567 if (!mIsDataReady) {
568 return;
569 }
570
Winson Chung785d2eb2011-04-14 16:08:02 -0700571 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700572 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 final int childCount = getChildCount();
574 int childLeft = 0;
575 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700576 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
577 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700578 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700579
580 // Calculate the variable page spacing if necessary
581 if (mPageSpacing < 0) {
Adam Cohen60b07122011-11-14 17:26:06 -0800582 setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
Winson Chungae890b82011-09-13 18:08:54 -0700583 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 }
585
586 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700587 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700588 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800589 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700590 final int childHeight = child.getMeasuredHeight();
591 int childTop = mPaddingTop;
592 if (mCenterPagesVertically) {
593 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
594 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595
Winson Chung785d2eb2011-04-14 16:08:02 -0700596 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700597 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800598 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700599 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 }
601 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700602
603 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
604 setHorizontalScrollBarEnabled(false);
605 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
606 scrollTo(newX, 0);
607 mScroller.setFinalX(newX);
608 setHorizontalScrollBarEnabled(true);
609 mFirstLayout = false;
610 }
611
Michael Jurkad3ef3062010-11-23 16:23:58 -0800612 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
613 mFirstLayout = false;
614 }
Winson Chunge3193b92010-09-10 11:44:42 -0700615 }
616
Adam Cohenf34bab52010-09-30 14:11:56 -0700617 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700618 if (isScrollingIndicatorEnabled()) {
619 updateScrollingIndicator();
620 }
621 if (mFadeInAdjacentScreens) {
622 for (int i = 0; i < getChildCount(); i++) {
623 View child = getChildAt(i);
624 if (child != null) {
625 float scrollProgress = getScrollProgress(screenCenter, child, i);
626 float alpha = 1 - Math.abs(scrollProgress);
627 child.setFastAlpha(alpha);
628 child.fastInvalidate();
629 }
630 }
631 invalidate();
632 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700633 }
634
Winson Chunge3193b92010-09-10 11:44:42 -0700635 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700636 protected void onViewAdded(View child) {
637 super.onViewAdded(child);
638
639 // This ensures that when children are added, they get the correct transforms / alphas
640 // in accordance with any scroll effects.
641 mForceScreenScrolled = true;
642 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700643 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700644 }
645
Adam Cohen73894962011-10-31 13:17:17 -0700646 protected void invalidateCachedOffsets() {
647 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700648 if (count == 0) {
649 mChildOffsets = null;
650 mChildRelativeOffsets = null;
651 mChildOffsetsWithLayoutScale = null;
652 return;
653 }
Adam Cohen73894962011-10-31 13:17:17 -0700654
655 mChildOffsets = new int[count];
656 mChildRelativeOffsets = new int[count];
657 mChildOffsetsWithLayoutScale = new int[count];
658 for (int i = 0; i < count; i++) {
659 mChildOffsets[i] = -1;
660 mChildRelativeOffsets[i] = -1;
661 mChildOffsetsWithLayoutScale[i] = -1;
662 }
663 }
664
665 protected int getChildOffset(int index) {
666 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
667 mChildOffsets : mChildOffsetsWithLayoutScale;
668
669 if (childOffsets != null && childOffsets[index] != -1) {
670 return childOffsets[index];
671 } else {
672 if (getChildCount() == 0)
673 return 0;
674
675 int offset = getRelativeChildOffset(0);
676 for (int i = 0; i < index; ++i) {
677 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
678 }
679 if (childOffsets != null) {
680 childOffsets[index] = offset;
681 }
682 return offset;
683 }
684 }
685
686 protected int getRelativeChildOffset(int index) {
687 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
688 return mChildRelativeOffsets[index];
689 } else {
690 final int padding = mPaddingLeft + mPaddingRight;
691 final int offset = mPaddingLeft +
692 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
693 if (mChildRelativeOffsets != null) {
694 mChildRelativeOffsets[index] = offset;
695 }
696 return offset;
697 }
698 }
699
700 protected int getScaledRelativeChildOffset(int index) {
701 final int padding = mPaddingLeft + mPaddingRight;
702 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
703 getScaledMeasuredWidth(getPageAt(index))) / 2;
704 return offset;
705 }
706
707 protected int getScaledMeasuredWidth(View child) {
708 // This functions are called enough times that it actually makes a difference in the
709 // profiler -- so just inline the max() here
710 final int measuredWidth = child.getMeasuredWidth();
711 final int minWidth = mMinimumWidth;
712 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
713 return (int) (maxWidth * mLayoutScale + 0.5f);
714 }
715
Michael Jurkadde558b2011-11-09 22:09:06 -0800716 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700717 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700718 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700719 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700720 int leftScreen = 0;
721 int rightScreen = 0;
Michael Jurka80c69852011-12-16 14:16:32 -0800722 while (leftScreen < pageCount - 1 &&
723 getPageAt(leftScreen).getRight() <= mScrollX) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700724 leftScreen++;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 }
726 rightScreen = leftScreen;
Michael Jurka80c69852011-12-16 14:16:32 -0800727 while (rightScreen < pageCount - 1 &&
728 getPageAt(rightScreen + 1).getLeft() < mScrollX + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700729 rightScreen++;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700730 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800731 range[0] = leftScreen;
732 range[1] = rightScreen;
733 } else {
734 range[0] = -1;
735 range[1] = -1;
736 }
737 }
738
739 @Override
740 protected void dispatchDraw(Canvas canvas) {
741 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800742 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
743 // it is equal to the scaled overscroll position.
744 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800745
746 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
747 screenScrolled(screenCenter);
748 mLastScreenCenter = screenCenter;
749 mForceScreenScrolled = false;
750 }
751
752 // Find out which screens are visible; as an optimization we only call draw on them
753 final int pageCount = getChildCount();
754 if (pageCount > 0) {
755 getVisiblePages(mTempVisiblePagesRange);
756 final int leftScreen = mTempVisiblePagesRange[0];
757 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800758 if (leftScreen != -1 && rightScreen != -1) {
759 final long drawingTime = getDrawingTime();
760 // Clip to the bounds
761 canvas.save();
762 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
763 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700764
Michael Jurka80c69852011-12-16 14:16:32 -0800765 // On certain graphics drivers, if you draw to a off-screen buffer that's not
766 // used, it can lead to poor performance. We were running into this when
767 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
768 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
769 // As a fix, below we set pages that aren't going to be rendered are to be
770 // View.INVISIBLE, preventing re-drawing of their hardware layer
771 for (int i = getChildCount() - 1; i >= 0; i--) {
772 final View v = getPageAt(i);
773 if (leftScreen <= i && i <= rightScreen &&
774 v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
775 v.setVisibility(VISIBLE);
776 drawChild(canvas, v, drawingTime);
777 } else {
778 v.setVisibility(INVISIBLE);
779 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800780 }
781 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700782 }
Michael Jurka0142d492010-08-25 17:46:15 -0700783 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 }
785
786 @Override
787 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700788 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700789 if (page != mCurrentPage || !mScroller.isFinished()) {
790 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 return true;
792 }
793 return false;
794 }
795
796 @Override
797 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700798 int focusablePage;
799 if (mNextPage != INVALID_PAGE) {
800 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700802 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 }
Winson Chung86f77532010-08-24 11:08:22 -0700804 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700806 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 }
808 return false;
809 }
810
811 @Override
812 public boolean dispatchUnhandledMove(View focused, int direction) {
813 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700814 if (getCurrentPage() > 0) {
815 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 return true;
817 }
818 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700819 if (getCurrentPage() < getPageCount() - 1) {
820 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 return true;
822 }
823 }
824 return super.dispatchUnhandledMove(focused, direction);
825 }
826
827 @Override
828 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700829 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
830 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 }
832 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700833 if (mCurrentPage > 0) {
834 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
836 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700837 if (mCurrentPage < getPageCount() - 1) {
838 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 }
840 }
841 }
842
843 /**
844 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700845 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 *
Winson Chung86f77532010-08-24 11:08:22 -0700847 * This happens when live folders requery, and if they're off page, they
848 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 */
850 @Override
851 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700852 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 View v = focused;
854 while (true) {
855 if (v == current) {
856 super.focusableViewAvailable(focused);
857 return;
858 }
859 if (v == this) {
860 return;
861 }
862 ViewParent parent = v.getParent();
863 if (parent instanceof View) {
864 v = (View)v.getParent();
865 } else {
866 return;
867 }
868 }
869 }
870
871 /**
872 * {@inheritDoc}
873 */
874 @Override
875 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
876 if (disallowIntercept) {
877 // We need to make sure to cancel our long press if
878 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700879 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700880 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 }
882 super.requestDisallowInterceptTouchEvent(disallowIntercept);
883 }
884
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800885 /**
886 * Return true if a tap at (x, y) should trigger a flip to the previous page.
887 */
888 protected boolean hitsPreviousPage(float x, float y) {
889 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
890 }
891
892 /**
893 * Return true if a tap at (x, y) should trigger a flip to the next page.
894 */
895 protected boolean hitsNextPage(float x, float y) {
896 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
897 }
898
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 @Override
900 public boolean onInterceptTouchEvent(MotionEvent ev) {
901 /*
902 * This method JUST determines whether we want to intercept the motion.
903 * If we return true, onTouchEvent will be called and we do the actual
904 * scrolling there.
905 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800906 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700907
Winson Chung45e1d6e2010-11-09 17:19:49 -0800908 // Skip touch handling if there are no pages to swipe
909 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
910
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 /*
912 * Shortcut the most recurring case: the user is in the dragging
913 * state and he is moving his finger. We want to intercept this
914 * motion.
915 */
916 final int action = ev.getAction();
917 if ((action == MotionEvent.ACTION_MOVE) &&
918 (mTouchState == TOUCH_STATE_SCROLLING)) {
919 return true;
920 }
921
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 switch (action & MotionEvent.ACTION_MASK) {
923 case MotionEvent.ACTION_MOVE: {
924 /*
925 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
926 * whether the user has moved far enough from his original down touch.
927 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700928 if (mActivePointerId != INVALID_POINTER) {
929 determineScrollingStart(ev);
930 break;
931 }
932 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
933 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
934 // i.e. fall through to the next case (don't break)
935 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
936 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 }
938
939 case MotionEvent.ACTION_DOWN: {
940 final float x = ev.getX();
941 final float y = ev.getY();
942 // Remember location of down touch
943 mDownMotionX = x;
944 mLastMotionX = x;
945 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800946 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800947 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 mActivePointerId = ev.getPointerId(0);
949 mAllowLongPress = true;
950
951 /*
952 * If being flinged and user touches the screen, initiate drag;
953 * otherwise don't. mScroller.isFinished should be false when
954 * being flinged.
955 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700956 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700957 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
958 if (finishedScrolling) {
959 mTouchState = TOUCH_STATE_REST;
960 mScroller.abortAnimation();
961 } else {
962 mTouchState = TOUCH_STATE_SCROLLING;
963 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700964
Winson Chung86f77532010-08-24 11:08:22 -0700965 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800967 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800969 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800971 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 mTouchState = TOUCH_STATE_NEXT_PAGE;
973 }
974 }
975 }
976 break;
977 }
978
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800980 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 mTouchState = TOUCH_STATE_REST;
982 mAllowLongPress = false;
983 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800984 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 break;
986
987 case MotionEvent.ACTION_POINTER_UP:
988 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800989 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 break;
991 }
992
993 /*
994 * The only time we want to intercept motion events is if we are in the
995 * drag mode.
996 */
997 return mTouchState != TOUCH_STATE_REST;
998 }
999
Adam Cohenf8d28232011-02-01 21:47:00 -08001000 protected void determineScrollingStart(MotionEvent ev) {
1001 determineScrollingStart(ev, 1.0f);
1002 }
1003
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 /*
1005 * Determines if we should change the touch state to start scrolling after the
1006 * user moves their touch point too far.
1007 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001008 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 /*
1010 * Locally do absolute value. mLastMotionX is set to the y value
1011 * of the down event.
1012 */
1013 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001014 if (pointerIndex == -1) {
1015 return;
1016 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 final float x = ev.getX(pointerIndex);
1018 final float y = ev.getY(pointerIndex);
1019 final int xDiff = (int) Math.abs(x - mLastMotionX);
1020 final int yDiff = (int) Math.abs(y - mLastMotionY);
1021
Adam Cohenf8d28232011-02-01 21:47:00 -08001022 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 boolean xPaged = xDiff > mPagingTouchSlop;
1024 boolean xMoved = xDiff > touchSlop;
1025 boolean yMoved = yDiff > touchSlop;
1026
Adam Cohenf8d28232011-02-01 21:47:00 -08001027 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001028 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 // Scroll if the user moved far enough along the X axis
1030 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001031 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001033 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001034 mTouchX = mScrollX;
1035 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1036 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 }
1038 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001039 cancelCurrentPageLongPress();
1040 }
1041 }
1042
1043 protected void cancelCurrentPageLongPress() {
1044 if (mAllowLongPress) {
1045 mAllowLongPress = false;
1046 // Try canceling the long press. It could also have been scheduled
1047 // by a distant descendant, so use the mAllowLongPress flag to block
1048 // everything
1049 final View currentPage = getPageAt(mCurrentPage);
1050 if (currentPage != null) {
1051 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 }
1053 }
1054 }
1055
Adam Cohenb5ba0972011-09-07 18:02:31 -07001056 protected float getScrollProgress(int screenCenter, View v, int page) {
1057 final int halfScreenSize = getMeasuredWidth() / 2;
1058
1059 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1060 int delta = screenCenter - (getChildOffset(page) -
1061 getRelativeChildOffset(page) + halfScreenSize);
1062
1063 float scrollProgress = delta / (totalDistance * 1.0f);
1064 scrollProgress = Math.min(scrollProgress, 1.0f);
1065 scrollProgress = Math.max(scrollProgress, -1.0f);
1066 return scrollProgress;
1067 }
1068
Adam Cohene0f66b52010-11-23 15:06:07 -08001069 // This curve determines how the effect of scrolling over the limits of the page dimishes
1070 // as the user pulls further and further from the bounds
1071 private float overScrollInfluenceCurve(float f) {
1072 f -= 1.0f;
1073 return f * f * f + 1.0f;
1074 }
1075
Adam Cohenb5ba0972011-09-07 18:02:31 -07001076 protected void acceleratedOverScroll(float amount) {
1077 int screenSize = getMeasuredWidth();
1078
1079 // We want to reach the max over scroll effect when the user has
1080 // over scrolled half the size of the screen
1081 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1082
1083 if (f == 0) return;
1084
1085 // Clamp this factor, f, to -1 < f < 1
1086 if (Math.abs(f) >= 1) {
1087 f /= Math.abs(f);
1088 }
1089
1090 int overScrollAmount = (int) Math.round(f * screenSize);
1091 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001092 mOverScrollX = overScrollAmount;
1093 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001094 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001095 mOverScrollX = mMaxScrollX + overScrollAmount;
1096 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001097 }
1098 invalidate();
1099 }
1100
1101 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001102 int screenSize = getMeasuredWidth();
1103
1104 float f = (amount / screenSize);
1105
1106 if (f == 0) return;
1107 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1108
Adam Cohen7bfc9792011-01-28 13:52:37 -08001109 // Clamp this factor, f, to -1 < f < 1
1110 if (Math.abs(f) >= 1) {
1111 f /= Math.abs(f);
1112 }
1113
Adam Cohene0f66b52010-11-23 15:06:07 -08001114 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001115 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001116 mOverScrollX = overScrollAmount;
1117 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001118 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001119 mOverScrollX = mMaxScrollX + overScrollAmount;
1120 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001121 }
1122 invalidate();
1123 }
1124
Adam Cohenb5ba0972011-09-07 18:02:31 -07001125 protected void overScroll(float amount) {
1126 dampedOverScroll(amount);
1127 }
1128
Michael Jurkac5b262c2011-01-12 20:24:50 -08001129 protected float maxOverScroll() {
1130 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001131 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001132 float f = 1.0f;
1133 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1134 return OVERSCROLL_DAMP_FACTOR * f;
1135 }
1136
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 @Override
1138 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001139 // Skip touch handling if there are no pages to swipe
1140 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1141
Michael Jurkab8f06722010-10-10 15:58:46 -07001142 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001143
1144 final int action = ev.getAction();
1145
1146 switch (action & MotionEvent.ACTION_MASK) {
1147 case MotionEvent.ACTION_DOWN:
1148 /*
1149 * If being flinged and user touches, stop the fling. isFinished
1150 * will be false if being flinged.
1151 */
1152 if (!mScroller.isFinished()) {
1153 mScroller.abortAnimation();
1154 }
1155
1156 // Remember where the motion event started
1157 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001158 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001159 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001161 if (mTouchState == TOUCH_STATE_SCROLLING) {
1162 pageBeginMoving();
1163 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001164 break;
1165
1166 case MotionEvent.ACTION_MOVE:
1167 if (mTouchState == TOUCH_STATE_SCROLLING) {
1168 // Scroll to follow the motion event
1169 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1170 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001171 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001172
Adam Cohenaefd4e12011-02-14 16:39:38 -08001173 mTotalMotionX += Math.abs(deltaX);
1174
Winson Chungc0844aa2011-02-02 15:25:58 -08001175 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1176 // keep the remainder because we are actually testing if we've moved from the last
1177 // scrolled position (which is discrete).
1178 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001179 mTouchX += deltaX;
1180 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1181 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001182 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001183 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001184 } else {
1185 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001187 mLastMotionX = x;
1188 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 } else {
1190 awakenScrollBars();
1191 }
Adam Cohen564976a2010-10-13 18:52:07 -07001192 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 determineScrollingStart(ev);
1194 }
1195 break;
1196
1197 case MotionEvent.ACTION_UP:
1198 if (mTouchState == TOUCH_STATE_SCROLLING) {
1199 final int activePointerId = mActivePointerId;
1200 final int pointerIndex = ev.findPointerIndex(activePointerId);
1201 final float x = ev.getX(pointerIndex);
1202 final VelocityTracker velocityTracker = mVelocityTracker;
1203 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1204 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001205 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001206 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1207 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1208 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001209
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001210 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1211
Adam Cohen00481b32011-11-18 12:03:48 -08001212 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001213 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001214
Adam Cohenaefd4e12011-02-14 16:39:38 -08001215 // In the case that the page is moved far to one direction and then is flung
1216 // in the opposite direction, we use a threshold to determine whether we should
1217 // just return to the starting page, or if we should skip one further.
1218 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001219 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001220 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001221 returnToOriginalPage = true;
1222 }
1223
Adam Cohenaefd4e12011-02-14 16:39:38 -08001224 int finalPage;
1225 // We give flings precedence over large moves, which is why we short-circuit our
1226 // test for a large move if a fling has been registered. That is, a large
1227 // move to the left and fling to the right will register as a fling to the right.
1228 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1229 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1230 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1231 snapToPageWithVelocity(finalPage, velocityX);
1232 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1233 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001234 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001235 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1236 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 } else {
1238 snapToDestination();
1239 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001240 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 // at this point we have not moved beyond the touch slop
1242 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1243 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001244 int nextPage = Math.max(0, mCurrentPage - 1);
1245 if (nextPage != mCurrentPage) {
1246 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 } else {
1248 snapToDestination();
1249 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001250 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 // at this point we have not moved beyond the touch slop
1252 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1253 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001254 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1255 if (nextPage != mCurrentPage) {
1256 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 } else {
1258 snapToDestination();
1259 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001260 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001261 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 }
1263 mTouchState = TOUCH_STATE_REST;
1264 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001265 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 break;
1267
1268 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001269 if (mTouchState == TOUCH_STATE_SCROLLING) {
1270 snapToDestination();
1271 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 mTouchState = TOUCH_STATE_REST;
1273 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001274 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 break;
1276
1277 case MotionEvent.ACTION_POINTER_UP:
1278 onSecondaryPointerUp(ev);
1279 break;
1280 }
1281
1282 return true;
1283 }
1284
Winson Chung185d7162011-02-28 13:47:29 -08001285 @Override
1286 public boolean onGenericMotionEvent(MotionEvent event) {
1287 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1288 switch (event.getAction()) {
1289 case MotionEvent.ACTION_SCROLL: {
1290 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1291 final float vscroll;
1292 final float hscroll;
1293 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1294 vscroll = 0;
1295 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1296 } else {
1297 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1298 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1299 }
1300 if (hscroll != 0 || vscroll != 0) {
1301 if (hscroll > 0 || vscroll > 0) {
1302 scrollRight();
1303 } else {
1304 scrollLeft();
1305 }
1306 return true;
1307 }
1308 }
1309 }
1310 }
1311 return super.onGenericMotionEvent(event);
1312 }
1313
Michael Jurkab8f06722010-10-10 15:58:46 -07001314 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1315 if (mVelocityTracker == null) {
1316 mVelocityTracker = VelocityTracker.obtain();
1317 }
1318 mVelocityTracker.addMovement(ev);
1319 }
1320
1321 private void releaseVelocityTracker() {
1322 if (mVelocityTracker != null) {
1323 mVelocityTracker.recycle();
1324 mVelocityTracker = null;
1325 }
1326 }
1327
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 private void onSecondaryPointerUp(MotionEvent ev) {
1329 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1330 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1331 final int pointerId = ev.getPointerId(pointerIndex);
1332 if (pointerId == mActivePointerId) {
1333 // This was our active pointer going up. Choose a new
1334 // active pointer and adjust accordingly.
1335 // TODO: Make this decision more intelligent.
1336 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1337 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1338 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001339 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 mActivePointerId = ev.getPointerId(newPointerIndex);
1341 if (mVelocityTracker != null) {
1342 mVelocityTracker.clear();
1343 }
1344 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001345 }
1346
Michael Jurkad771c962011-08-09 15:00:48 -07001347 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001348
1349 @Override
1350 public void requestChildFocus(View child, View focused) {
1351 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001352 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001353 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001354 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001355 }
1356 }
1357
Winson Chunge3193b92010-09-10 11:44:42 -07001358 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1359 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001360 int left;
1361 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001362 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001363 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001364 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001365 if (left <= relativeOffset && relativeOffset <= right) {
1366 return i;
1367 }
Winson Chunge3193b92010-09-10 11:44:42 -07001368 }
1369 return -1;
1370 }
1371
Winson Chung1908d072011-02-24 18:09:44 -08001372 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001373 // This functions are called enough times that it actually makes a difference in the
1374 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001375 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001376 final int minWidth = mMinimumWidth;
1377 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001378 }
1379
Adam Cohend19d3ca2010-09-15 14:43:42 -07001380 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001381 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001382 int minDistanceFromScreenCenterIndex = -1;
1383 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1384 final int childCount = getChildCount();
1385 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001386 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001387 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001388 int halfChildWidth = (childWidth / 2);
1389 int childCenter = getChildOffset(i) + halfChildWidth;
1390 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1391 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1392 minDistanceFromScreenCenter = distanceFromScreenCenter;
1393 minDistanceFromScreenCenterIndex = i;
1394 }
1395 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001396 return minDistanceFromScreenCenterIndex;
1397 }
1398
1399 protected void snapToDestination() {
1400 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 }
1402
Adam Cohene0f66b52010-11-23 15:06:07 -08001403 private static class ScrollInterpolator implements Interpolator {
1404 public ScrollInterpolator() {
1405 }
1406
1407 public float getInterpolation(float t) {
1408 t -= 1.0f;
1409 return t*t*t*t*t + 1;
1410 }
1411 }
1412
1413 // We want the duration of the page snap animation to be influenced by the distance that
1414 // the screen has to travel, however, we don't want this duration to be effected in a
1415 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1416 // of travel has on the overall snap duration.
1417 float distanceInfluenceForSnapDuration(float f) {
1418 f -= 0.5f; // center the values about 0.
1419 f *= 0.3f * Math.PI / 2.0f;
1420 return (float) Math.sin(f);
1421 }
1422
Michael Jurka0142d492010-08-25 17:46:15 -07001423 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001424 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1425 int halfScreenSize = getMeasuredWidth() / 2;
1426
Winson Chung785d2eb2011-04-14 16:08:02 -07001427 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1428 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1429 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001430 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1431 int delta = newX - mUnboundedScrollX;
1432 int duration = 0;
1433
Adam Cohen265b9a62011-12-07 14:37:18 -08001434 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001435 // If the velocity is low enough, then treat this more as an automatic page advance
1436 // as opposed to an apparent physical response to flinging
1437 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1438 return;
1439 }
1440
1441 // Here we compute a "distance" that will be used in the computation of the overall
1442 // snap duration. This is a function of the actual distance that needs to be traveled;
1443 // we keep this value close to half screen size in order to reduce the variance in snap
1444 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001445 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001446 float distance = halfScreenSize + halfScreenSize *
1447 distanceInfluenceForSnapDuration(distanceRatio);
1448
1449 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001450 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001451
1452 // we want the page's snap velocity to approximately match the velocity at which the
1453 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001454 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1455 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001456
1457 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001458 }
1459
1460 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001461 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 }
1463
Michael Jurka0142d492010-08-25 17:46:15 -07001464 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001465 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001466
Winson Chung785d2eb2011-04-14 16:08:02 -07001467 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1468 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1469 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001470 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001471 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001473 snapToPage(whichPage, delta, duration);
1474 }
1475
1476 protected void snapToPage(int whichPage, int delta, int duration) {
1477 mNextPage = whichPage;
1478
1479 View focusedChild = getFocusedChild();
1480 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001481 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001482 focusedChild.clearFocus();
1483 }
1484
1485 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 awakenScrollBars(duration);
1487 if (duration == 0) {
1488 duration = Math.abs(delta);
1489 }
1490
1491 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001492 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001493
Winson Chungb44b5242011-06-13 11:32:14 -07001494 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1495 // loading associated pages until the scroll settles
1496 if (mDeferScrollUpdate) {
1497 loadAssociatedPages(mNextPage);
1498 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001499 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001500 }
Michael Jurka0142d492010-08-25 17:46:15 -07001501 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 invalidate();
1503 }
1504
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 public void scrollLeft() {
1506 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001507 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001509 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001510 }
1511 }
1512
1513 public void scrollRight() {
1514 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001515 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001517 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 }
1519 }
1520
Winson Chung86f77532010-08-24 11:08:22 -07001521 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001522 int result = -1;
1523 if (v != null) {
1524 ViewParent vp = v.getParent();
1525 int count = getChildCount();
1526 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001527 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 return i;
1529 }
1530 }
1531 }
1532 return result;
1533 }
1534
1535 /**
1536 * @return True is long presses are still allowed for the current touch
1537 */
1538 public boolean allowLongPress() {
1539 return mAllowLongPress;
1540 }
1541
Michael Jurka0142d492010-08-25 17:46:15 -07001542 /**
1543 * Set true to allow long-press events to be triggered, usually checked by
1544 * {@link Launcher} to accept or block dpad-initiated long-presses.
1545 */
1546 public void setAllowLongPress(boolean allowLongPress) {
1547 mAllowLongPress = allowLongPress;
1548 }
1549
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001551 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001552
1553 SavedState(Parcelable superState) {
1554 super(superState);
1555 }
1556
1557 private SavedState(Parcel in) {
1558 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001559 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001560 }
1561
1562 @Override
1563 public void writeToParcel(Parcel out, int flags) {
1564 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001565 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001566 }
1567
1568 public static final Parcelable.Creator<SavedState> CREATOR =
1569 new Parcelable.Creator<SavedState>() {
1570 public SavedState createFromParcel(Parcel in) {
1571 return new SavedState(in);
1572 }
1573
1574 public SavedState[] newArray(int size) {
1575 return new SavedState[size];
1576 }
1577 };
1578 }
1579
Winson Chungf314b0e2011-08-16 11:54:27 -07001580 protected void loadAssociatedPages(int page) {
1581 loadAssociatedPages(page, false);
1582 }
1583 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001584 if (mContentIsRefreshable) {
1585 final int count = getChildCount();
1586 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001587 int lowerPageBound = getAssociatedLowerPageBound(page);
1588 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001589 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1590 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001591 // First, clear any pages that should no longer be loaded
1592 for (int i = 0; i < count; ++i) {
1593 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001594 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001595 if (layout.getPageChildCount() > 0) {
1596 layout.removeAllViewsOnPage();
1597 }
1598 mDirtyPageContent.set(i, true);
1599 }
1600 }
1601 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001602 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001603 if ((i != page) && immediateAndOnly) {
1604 continue;
1605 }
Michael Jurka0142d492010-08-25 17:46:15 -07001606 if (lowerPageBound <= i && i <= upperPageBound) {
1607 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001608 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001609 mDirtyPageContent.set(i, false);
1610 }
Winson Chung86f77532010-08-24 11:08:22 -07001611 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001612 }
1613 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001614 }
1615 }
1616
Winson Chunge3193b92010-09-10 11:44:42 -07001617 protected int getAssociatedLowerPageBound(int page) {
1618 return Math.max(0, page - 1);
1619 }
1620 protected int getAssociatedUpperPageBound(int page) {
1621 final int count = getChildCount();
1622 return Math.min(page + 1, count - 1);
1623 }
1624
Winson Chung86f77532010-08-24 11:08:22 -07001625 /**
1626 * This method is called ONLY to synchronize the number of pages that the paged view has.
1627 * To actually fill the pages with information, implement syncPageItems() below. It is
1628 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1629 * and therefore, individual page items do not need to be updated in this method.
1630 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001631 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001632
1633 /**
1634 * This method is called to synchronize the items that are on a particular page. If views on
1635 * the page can be reused, then they should be updated within this method.
1636 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001637 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001638
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001639 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001640 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001641 }
1642 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001643 invalidatePageData(currentPage, false);
1644 }
1645 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001646 if (!mIsDataReady) {
1647 return;
1648 }
1649
Michael Jurka0142d492010-08-25 17:46:15 -07001650 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001651 // Force all scrolling-related behavior to end
1652 mScroller.forceFinished(true);
1653 mNextPage = INVALID_PAGE;
1654
Michael Jurka0142d492010-08-25 17:46:15 -07001655 // Update all the pages
1656 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001657
Winson Chung5a808352011-06-27 19:08:49 -07001658 // We must force a measure after we've loaded the pages to update the content width and
1659 // to determine the full scroll width
1660 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1661 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1662
1663 // Set a new page as the current page if necessary
1664 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001665 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001666 }
1667
Michael Jurka0142d492010-08-25 17:46:15 -07001668 // Mark each of the pages as dirty
1669 final int count = getChildCount();
1670 mDirtyPageContent.clear();
1671 for (int i = 0; i < count; ++i) {
1672 mDirtyPageContent.add(true);
1673 }
1674
1675 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001676 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001677 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001678 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001679 }
Winson Chung007c6982011-06-14 13:27:53 -07001680
Michael Jurkaafaa0502011-12-13 18:22:50 -08001681 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001682 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1683 // found
1684 if (mHasScrollIndicator && mScrollIndicator == null) {
1685 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001686 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001687 mHasScrollIndicator = mScrollIndicator != null;
1688 if (mHasScrollIndicator) {
1689 mScrollIndicator.setVisibility(View.VISIBLE);
1690 }
1691 }
1692 return mScrollIndicator;
1693 }
1694
1695 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001696 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001697 }
1698
Winson Chung5a808352011-06-27 19:08:49 -07001699 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1700 @Override
1701 public void run() {
1702 hideScrollingIndicator(false);
1703 }
1704 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001705 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001706 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001707 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001708 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001709 }
1710
Michael Jurka430e8a52011-08-08 15:52:14 -07001711 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001712 if (getChildCount() <= 1) return;
1713 if (!isScrollingIndicatorEnabled()) return;
1714
1715 getScrollingIndicator();
1716 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001717 // Fade the indicator in
1718 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001719 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001720 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001721 if (immediately) {
1722 mScrollIndicator.setAlpha(1f);
1723 } else {
1724 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1725 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1726 mScrollIndicatorAnimator.start();
1727 }
Winson Chung007c6982011-06-14 13:27:53 -07001728 }
1729 }
1730
Adam Cohen21b41102011-11-01 17:29:52 -07001731 protected void cancelScrollingIndicatorAnimations() {
1732 if (mScrollIndicatorAnimator != null) {
1733 mScrollIndicatorAnimator.cancel();
1734 }
1735 }
1736
Winson Chung007c6982011-06-14 13:27:53 -07001737 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001738 if (getChildCount() <= 1) return;
1739 if (!isScrollingIndicatorEnabled()) return;
1740
1741 getScrollingIndicator();
1742 if (mScrollIndicator != null) {
1743 // Fade the indicator out
1744 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001745 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001746 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001747 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001748 mScrollIndicator.setAlpha(0f);
1749 } else {
1750 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1751 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1752 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1753 private boolean cancelled = false;
1754 @Override
1755 public void onAnimationCancel(android.animation.Animator animation) {
1756 cancelled = true;
1757 }
1758 @Override
1759 public void onAnimationEnd(Animator animation) {
1760 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001761 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001762 }
1763 }
1764 });
1765 mScrollIndicatorAnimator.start();
1766 }
Winson Chung007c6982011-06-14 13:27:53 -07001767 }
1768 }
1769
Winson Chung32174c82011-07-19 15:47:55 -07001770 /**
1771 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1772 * fill its space on the track or not.
1773 */
1774 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001775 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001776 }
1777
Winson Chung007c6982011-06-14 13:27:53 -07001778 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001779 if (getChildCount() <= 1) return;
1780 if (!isScrollingIndicatorEnabled()) return;
1781
1782 getScrollingIndicator();
1783 if (mScrollIndicator != null) {
1784 updateScrollingIndicatorPosition();
1785 }
1786 }
1787
Winson Chung007c6982011-06-14 13:27:53 -07001788 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001789 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001790 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001791 int numPages = getChildCount();
1792 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001793 int lastChildIndex = Math.max(0, getChildCount() - 1);
1794 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001795 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001796 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1797 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001798
Winson Chungae890b82011-09-13 18:08:54 -07001799 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001800 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001801 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001802 if (hasElasticScrollIndicator()) {
1803 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1804 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1805 mScrollIndicator.requestLayout();
1806 }
1807 } else {
1808 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1809 indicatorPos += indicatorCenterOffset;
1810 }
Winson Chung007c6982011-06-14 13:27:53 -07001811 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001812 mScrollIndicator.invalidate();
1813 }
1814
Winson Chung3ac74c52011-06-30 17:39:37 -07001815 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001816 }
1817
1818 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001819 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001820
1821 /* Accessibility */
1822 @Override
1823 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1824 super.onInitializeAccessibilityNodeInfo(info);
1825 info.setScrollable(true);
1826 }
1827
1828 @Override
1829 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1830 super.onInitializeAccessibilityEvent(event);
1831 event.setScrollable(true);
1832 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1833 event.setFromIndex(mCurrentPage);
1834 event.setToIndex(mCurrentPage);
1835 event.setItemCount(getChildCount());
1836 }
1837 }
1838
Winson Chung6a0f57d2011-06-29 20:10:49 -07001839 protected String getCurrentPageDescription() {
1840 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1841 return String.format(mContext.getString(R.string.default_scroll_format),
1842 page + 1, getChildCount());
1843 }
Winson Chungd11265e2011-08-30 13:37:23 -07001844
1845 @Override
1846 public boolean onHoverEvent(android.view.MotionEvent event) {
1847 return true;
1848 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001849}