blob: afee8b07686a0f0f6970eca18fd53293b57fa68d [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);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800295 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800296 }
297
298 /**
Winson Chung86f77532010-08-24 11:08:22 -0700299 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 */
Winson Chung86f77532010-08-24 11:08:22 -0700301 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800302 if (!mScroller.isFinished()) {
303 mScroller.abortAnimation();
304 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800305 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
306 // the default
307 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800308 return;
309 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700310
Winson Chung86f77532010-08-24 11:08:22 -0700311 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800312 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700313 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700314 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800315 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700316 }
317
Michael Jurka0142d492010-08-25 17:46:15 -0700318 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700319 if (mPageSwitchListener != null) {
320 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 }
322 }
323
Michael Jurkace7e05f2011-02-01 22:02:35 -0800324 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700325 if (!mIsPageMoving) {
326 mIsPageMoving = true;
327 onPageBeginMoving();
328 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700329 }
330
Michael Jurkace7e05f2011-02-01 22:02:35 -0800331 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700332 if (mIsPageMoving) {
333 mIsPageMoving = false;
334 onPageEndMoving();
335 }
Michael Jurka0142d492010-08-25 17:46:15 -0700336 }
337
Adam Cohen26976d92011-03-22 15:33:33 -0700338 protected boolean isPageMoving() {
339 return mIsPageMoving;
340 }
341
Michael Jurka0142d492010-08-25 17:46:15 -0700342 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700343 protected void onPageBeginMoving() {
344 }
345
346 // a method that subclasses can override to add behavior
347 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700348 }
349
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 /**
Winson Chung86f77532010-08-24 11:08:22 -0700351 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 *
353 * @param l The listener used to respond to long clicks.
354 */
355 @Override
356 public void setOnLongClickListener(OnLongClickListener l) {
357 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700358 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700359 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700360 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 }
362 }
363
364 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800365 public void scrollBy(int x, int y) {
366 scrollTo(mUnboundedScrollX + x, mScrollY + y);
367 }
368
369 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700370 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800371 mUnboundedScrollX = x;
372
373 if (x < 0) {
374 super.scrollTo(0, y);
375 if (mAllowOverScroll) {
376 overScroll(x);
377 }
378 } else if (x > mMaxScrollX) {
379 super.scrollTo(mMaxScrollX, y);
380 if (mAllowOverScroll) {
381 overScroll(x - mMaxScrollX);
382 }
383 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800384 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800385 super.scrollTo(x, y);
386 }
387
Michael Jurka0142d492010-08-25 17:46:15 -0700388 mTouchX = x;
389 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
390 }
391
392 // we moved this functionality to a helper function so SmoothPagedView can reuse it
393 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700395 // Don't bother scrolling if the page does not need to be moved
396 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700397 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
398 }
Michael Jurka0142d492010-08-25 17:46:15 -0700399 invalidate();
400 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700401 } else if (mNextPage != INVALID_PAGE) {
402 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700403 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700404 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700405
406 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700407 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700408 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700409 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700410 }
411
Adam Cohen73aa9752010-11-24 16:26:58 -0800412 // We don't want to trigger a page end moving unless the page has settled
413 // and the user has stopped scrolling
414 if (mTouchState == TOUCH_STATE_REST) {
415 pageEndMoving();
416 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700417
418 // Notify the user when the page changes
419 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
420 AccessibilityEvent ev =
421 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
422 ev.getText().add(getCurrentPageDescription());
423 sendAccessibilityEventUnchecked(ev);
424 }
Michael Jurka0142d492010-08-25 17:46:15 -0700425 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 }
Michael Jurka0142d492010-08-25 17:46:15 -0700427 return false;
428 }
429
430 @Override
431 public void computeScroll() {
432 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 }
434
435 @Override
436 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700437 if (!mIsDataReady) {
438 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
439 return;
440 }
441
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
443 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
444 if (widthMode != MeasureSpec.EXACTLY) {
445 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
446 }
447
Adam Lesinski6b879f02010-11-04 16:15:23 -0700448 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
449 * of the All apps view on XLarge displays to not take up more space then it needs. Width
450 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
451 * each page to have the same width.
452 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700454 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
455 int maxChildHeight = 0;
456
457 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700458 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700459
Michael Jurka36fcb742011-04-06 17:08:58 -0700460
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700462 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700463 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 final int childCount = getChildCount();
465 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700466 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700467 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700468 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
469
470 int childWidthMode;
471 if (lp.width == LayoutParams.WRAP_CONTENT) {
472 childWidthMode = MeasureSpec.AT_MOST;
473 } else {
474 childWidthMode = MeasureSpec.EXACTLY;
475 }
476
477 int childHeightMode;
478 if (lp.height == LayoutParams.WRAP_CONTENT) {
479 childHeightMode = MeasureSpec.AT_MOST;
480 } else {
481 childHeightMode = MeasureSpec.EXACTLY;
482 }
483
484 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700485 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700486 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700487 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700488
489 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700491 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
492 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700493 }
494
495 if (heightMode == MeasureSpec.AT_MOST) {
496 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 }
Winson Chungae890b82011-09-13 18:08:54 -0700498
Winson Chungae890b82011-09-13 18:08:54 -0700499 setMeasuredDimension(widthSize, heightSize);
500
Adam Cohen25b29952011-11-02 14:49:06 -0700501 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
502 // We also wait until we set the measured dimensions before flushing the cache as well, to
503 // ensure that the cache is filled with good values.
504 invalidateCachedOffsets();
505 updateScrollingIndicatorPosition();
506
Adam Cohenfaa28302010-11-19 12:02:18 -0800507 if (childCount > 0) {
508 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
509 } else {
510 mMaxScrollX = 0;
511 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700512 }
513
Michael Jurka8c920dd2011-01-20 14:16:56 -0800514 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800515 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
516 int delta = newX - mScrollX;
517
Michael Jurka8c920dd2011-01-20 14:16:56 -0800518 final int pageCount = getChildCount();
519 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700520 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800521 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800522 }
523 setCurrentPage(newCurrentPage);
524 }
525
Michael Jurka8c920dd2011-01-20 14:16:56 -0800526 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
527 // 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 -0800528 // tightens the layout accordingly
529 public void setLayoutScale(float childrenScale) {
530 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700531 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800532
533 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
534 int childCount = getChildCount();
535 float childrenX[] = new float[childCount];
536 float childrenY[] = new float[childCount];
537 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700538 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539 childrenX[i] = child.getX();
540 childrenY[i] = child.getY();
541 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700542 // Trigger a full re-layout (never just call onLayout directly!)
543 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
544 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
545 requestLayout();
546 measure(widthSpec, heightSpec);
547 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800548 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700549 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800550 child.setX(childrenX[i]);
551 child.setY(childrenY[i]);
552 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700553
Michael Jurkad3ef3062010-11-23 16:23:58 -0800554 // Also, the page offset has changed (since the pages are now smaller);
555 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800556 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800557 }
558
Adam Cohen60b07122011-11-14 17:26:06 -0800559 public void setPageSpacing(int pageSpacing) {
560 mPageSpacing = pageSpacing;
561 invalidateCachedOffsets();
562 }
563
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700565 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700566 if (!mIsDataReady) {
567 return;
568 }
569
Winson Chung785d2eb2011-04-14 16:08:02 -0700570 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700571 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 final int childCount = getChildCount();
573 int childLeft = 0;
574 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
576 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700577 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700578
579 // Calculate the variable page spacing if necessary
580 if (mPageSpacing < 0) {
Adam Cohen60b07122011-11-14 17:26:06 -0800581 setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
Winson Chungae890b82011-09-13 18:08:54 -0700582 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 }
584
585 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700586 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700587 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800588 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700589 final int childHeight = child.getMeasuredHeight();
590 int childTop = mPaddingTop;
591 if (mCenterPagesVertically) {
592 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
593 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800594
Winson Chung785d2eb2011-04-14 16:08:02 -0700595 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700596 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800597 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700598 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
600 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700601
602 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
603 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800604 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700605 setHorizontalScrollBarEnabled(true);
606 mFirstLayout = false;
607 }
608
Michael Jurkad3ef3062010-11-23 16:23:58 -0800609 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
610 mFirstLayout = false;
611 }
Winson Chunge3193b92010-09-10 11:44:42 -0700612 }
613
Adam Cohenf34bab52010-09-30 14:11:56 -0700614 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700615 if (isScrollingIndicatorEnabled()) {
616 updateScrollingIndicator();
617 }
618 if (mFadeInAdjacentScreens) {
619 for (int i = 0; i < getChildCount(); i++) {
620 View child = getChildAt(i);
621 if (child != null) {
622 float scrollProgress = getScrollProgress(screenCenter, child, i);
623 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800624 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700625 }
626 }
627 invalidate();
628 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700629 }
630
Winson Chunge3193b92010-09-10 11:44:42 -0700631 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700632 protected void onViewAdded(View child) {
633 super.onViewAdded(child);
634
635 // This ensures that when children are added, they get the correct transforms / alphas
636 // in accordance with any scroll effects.
637 mForceScreenScrolled = true;
638 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700639 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700640 }
641
Adam Cohen73894962011-10-31 13:17:17 -0700642 protected void invalidateCachedOffsets() {
643 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700644 if (count == 0) {
645 mChildOffsets = null;
646 mChildRelativeOffsets = null;
647 mChildOffsetsWithLayoutScale = null;
648 return;
649 }
Adam Cohen73894962011-10-31 13:17:17 -0700650
651 mChildOffsets = new int[count];
652 mChildRelativeOffsets = new int[count];
653 mChildOffsetsWithLayoutScale = new int[count];
654 for (int i = 0; i < count; i++) {
655 mChildOffsets[i] = -1;
656 mChildRelativeOffsets[i] = -1;
657 mChildOffsetsWithLayoutScale[i] = -1;
658 }
659 }
660
661 protected int getChildOffset(int index) {
662 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
663 mChildOffsets : mChildOffsetsWithLayoutScale;
664
665 if (childOffsets != null && childOffsets[index] != -1) {
666 return childOffsets[index];
667 } else {
668 if (getChildCount() == 0)
669 return 0;
670
671 int offset = getRelativeChildOffset(0);
672 for (int i = 0; i < index; ++i) {
673 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
674 }
675 if (childOffsets != null) {
676 childOffsets[index] = offset;
677 }
678 return offset;
679 }
680 }
681
682 protected int getRelativeChildOffset(int index) {
683 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
684 return mChildRelativeOffsets[index];
685 } else {
686 final int padding = mPaddingLeft + mPaddingRight;
687 final int offset = mPaddingLeft +
688 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
689 if (mChildRelativeOffsets != null) {
690 mChildRelativeOffsets[index] = offset;
691 }
692 return offset;
693 }
694 }
695
696 protected int getScaledRelativeChildOffset(int index) {
697 final int padding = mPaddingLeft + mPaddingRight;
698 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
699 getScaledMeasuredWidth(getPageAt(index))) / 2;
700 return offset;
701 }
702
703 protected int getScaledMeasuredWidth(View child) {
704 // This functions are called enough times that it actually makes a difference in the
705 // profiler -- so just inline the max() here
706 final int measuredWidth = child.getMeasuredWidth();
707 final int minWidth = mMinimumWidth;
708 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
709 return (int) (maxWidth * mLayoutScale + 0.5f);
710 }
711
Michael Jurkadde558b2011-11-09 22:09:06 -0800712 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700713 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700714 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700715 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700716 int leftScreen = 0;
717 int rightScreen = 0;
Michael Jurka80c69852011-12-16 14:16:32 -0800718 while (leftScreen < pageCount - 1 &&
719 getPageAt(leftScreen).getRight() <= mScrollX) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700720 leftScreen++;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700721 }
722 rightScreen = leftScreen;
Michael Jurka80c69852011-12-16 14:16:32 -0800723 while (rightScreen < pageCount - 1 &&
724 getPageAt(rightScreen + 1).getLeft() < mScrollX + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 rightScreen++;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700726 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800727 range[0] = leftScreen;
728 range[1] = rightScreen;
729 } else {
730 range[0] = -1;
731 range[1] = -1;
732 }
733 }
734
735 @Override
736 protected void dispatchDraw(Canvas canvas) {
737 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800738 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
739 // it is equal to the scaled overscroll position.
740 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800741
742 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
743 screenScrolled(screenCenter);
744 mLastScreenCenter = screenCenter;
745 mForceScreenScrolled = false;
746 }
747
748 // Find out which screens are visible; as an optimization we only call draw on them
749 final int pageCount = getChildCount();
750 if (pageCount > 0) {
751 getVisiblePages(mTempVisiblePagesRange);
752 final int leftScreen = mTempVisiblePagesRange[0];
753 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800754 if (leftScreen != -1 && rightScreen != -1) {
755 final long drawingTime = getDrawingTime();
756 // Clip to the bounds
757 canvas.save();
758 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
759 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700760
Michael Jurka80c69852011-12-16 14:16:32 -0800761 // On certain graphics drivers, if you draw to a off-screen buffer that's not
762 // used, it can lead to poor performance. We were running into this when
763 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
764 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
765 // As a fix, below we set pages that aren't going to be rendered are to be
766 // View.INVISIBLE, preventing re-drawing of their hardware layer
767 for (int i = getChildCount() - 1; i >= 0; i--) {
768 final View v = getPageAt(i);
769 if (leftScreen <= i && i <= rightScreen &&
770 v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
771 v.setVisibility(VISIBLE);
772 drawChild(canvas, v, drawingTime);
773 } else {
774 v.setVisibility(INVISIBLE);
775 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800776 }
777 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700778 }
Michael Jurka0142d492010-08-25 17:46:15 -0700779 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 }
781
782 @Override
783 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700784 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700785 if (page != mCurrentPage || !mScroller.isFinished()) {
786 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 return true;
788 }
789 return false;
790 }
791
792 @Override
793 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700794 int focusablePage;
795 if (mNextPage != INVALID_PAGE) {
796 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700798 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 }
Winson Chung86f77532010-08-24 11:08:22 -0700800 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700802 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 }
804 return false;
805 }
806
807 @Override
808 public boolean dispatchUnhandledMove(View focused, int direction) {
809 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700810 if (getCurrentPage() > 0) {
811 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 return true;
813 }
814 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700815 if (getCurrentPage() < getPageCount() - 1) {
816 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 return true;
818 }
819 }
820 return super.dispatchUnhandledMove(focused, direction);
821 }
822
823 @Override
824 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700825 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
826 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 }
828 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700829 if (mCurrentPage > 0) {
830 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 }
832 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700833 if (mCurrentPage < getPageCount() - 1) {
834 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
836 }
837 }
838
839 /**
840 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700841 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 *
Winson Chung86f77532010-08-24 11:08:22 -0700843 * This happens when live folders requery, and if they're off page, they
844 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 */
846 @Override
847 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700848 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 View v = focused;
850 while (true) {
851 if (v == current) {
852 super.focusableViewAvailable(focused);
853 return;
854 }
855 if (v == this) {
856 return;
857 }
858 ViewParent parent = v.getParent();
859 if (parent instanceof View) {
860 v = (View)v.getParent();
861 } else {
862 return;
863 }
864 }
865 }
866
867 /**
868 * {@inheritDoc}
869 */
870 @Override
871 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
872 if (disallowIntercept) {
873 // We need to make sure to cancel our long press if
874 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700875 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700876 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 }
878 super.requestDisallowInterceptTouchEvent(disallowIntercept);
879 }
880
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800881 /**
882 * Return true if a tap at (x, y) should trigger a flip to the previous page.
883 */
884 protected boolean hitsPreviousPage(float x, float y) {
885 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
886 }
887
888 /**
889 * Return true if a tap at (x, y) should trigger a flip to the next page.
890 */
891 protected boolean hitsNextPage(float x, float y) {
892 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
893 }
894
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 @Override
896 public boolean onInterceptTouchEvent(MotionEvent ev) {
897 /*
898 * This method JUST determines whether we want to intercept the motion.
899 * If we return true, onTouchEvent will be called and we do the actual
900 * scrolling there.
901 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800902 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700903
Winson Chung45e1d6e2010-11-09 17:19:49 -0800904 // Skip touch handling if there are no pages to swipe
905 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
906
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 /*
908 * Shortcut the most recurring case: the user is in the dragging
909 * state and he is moving his finger. We want to intercept this
910 * motion.
911 */
912 final int action = ev.getAction();
913 if ((action == MotionEvent.ACTION_MOVE) &&
914 (mTouchState == TOUCH_STATE_SCROLLING)) {
915 return true;
916 }
917
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 switch (action & MotionEvent.ACTION_MASK) {
919 case MotionEvent.ACTION_MOVE: {
920 /*
921 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
922 * whether the user has moved far enough from his original down touch.
923 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700924 if (mActivePointerId != INVALID_POINTER) {
925 determineScrollingStart(ev);
926 break;
927 }
928 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
929 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
930 // i.e. fall through to the next case (don't break)
931 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
932 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 }
934
935 case MotionEvent.ACTION_DOWN: {
936 final float x = ev.getX();
937 final float y = ev.getY();
938 // Remember location of down touch
939 mDownMotionX = x;
940 mLastMotionX = x;
941 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800942 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800943 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 mActivePointerId = ev.getPointerId(0);
945 mAllowLongPress = true;
946
947 /*
948 * If being flinged and user touches the screen, initiate drag;
949 * otherwise don't. mScroller.isFinished should be false when
950 * being flinged.
951 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700952 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700953 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
954 if (finishedScrolling) {
955 mTouchState = TOUCH_STATE_REST;
956 mScroller.abortAnimation();
957 } else {
958 mTouchState = TOUCH_STATE_SCROLLING;
959 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700960
Winson Chung86f77532010-08-24 11:08:22 -0700961 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800963 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800965 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800967 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 mTouchState = TOUCH_STATE_NEXT_PAGE;
969 }
970 }
971 }
972 break;
973 }
974
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800976 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 mTouchState = TOUCH_STATE_REST;
978 mAllowLongPress = false;
979 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800980 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 break;
982
983 case MotionEvent.ACTION_POINTER_UP:
984 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800985 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 break;
987 }
988
989 /*
990 * The only time we want to intercept motion events is if we are in the
991 * drag mode.
992 */
993 return mTouchState != TOUCH_STATE_REST;
994 }
995
Adam Cohenf8d28232011-02-01 21:47:00 -0800996 protected void determineScrollingStart(MotionEvent ev) {
997 determineScrollingStart(ev, 1.0f);
998 }
999
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 /*
1001 * Determines if we should change the touch state to start scrolling after the
1002 * user moves their touch point too far.
1003 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001004 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 /*
1006 * Locally do absolute value. mLastMotionX is set to the y value
1007 * of the down event.
1008 */
1009 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001010 if (pointerIndex == -1) {
1011 return;
1012 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 final float x = ev.getX(pointerIndex);
1014 final float y = ev.getY(pointerIndex);
1015 final int xDiff = (int) Math.abs(x - mLastMotionX);
1016 final int yDiff = (int) Math.abs(y - mLastMotionY);
1017
Adam Cohenf8d28232011-02-01 21:47:00 -08001018 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 boolean xPaged = xDiff > mPagingTouchSlop;
1020 boolean xMoved = xDiff > touchSlop;
1021 boolean yMoved = yDiff > touchSlop;
1022
Adam Cohenf8d28232011-02-01 21:47:00 -08001023 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001024 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 // Scroll if the user moved far enough along the X axis
1026 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001027 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001028 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001029 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001030 mTouchX = mScrollX;
1031 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1032 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 }
1034 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001035 cancelCurrentPageLongPress();
1036 }
1037 }
1038
1039 protected void cancelCurrentPageLongPress() {
1040 if (mAllowLongPress) {
1041 mAllowLongPress = false;
1042 // Try canceling the long press. It could also have been scheduled
1043 // by a distant descendant, so use the mAllowLongPress flag to block
1044 // everything
1045 final View currentPage = getPageAt(mCurrentPage);
1046 if (currentPage != null) {
1047 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 }
1049 }
1050 }
1051
Adam Cohenb5ba0972011-09-07 18:02:31 -07001052 protected float getScrollProgress(int screenCenter, View v, int page) {
1053 final int halfScreenSize = getMeasuredWidth() / 2;
1054
1055 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1056 int delta = screenCenter - (getChildOffset(page) -
1057 getRelativeChildOffset(page) + halfScreenSize);
1058
1059 float scrollProgress = delta / (totalDistance * 1.0f);
1060 scrollProgress = Math.min(scrollProgress, 1.0f);
1061 scrollProgress = Math.max(scrollProgress, -1.0f);
1062 return scrollProgress;
1063 }
1064
Adam Cohene0f66b52010-11-23 15:06:07 -08001065 // This curve determines how the effect of scrolling over the limits of the page dimishes
1066 // as the user pulls further and further from the bounds
1067 private float overScrollInfluenceCurve(float f) {
1068 f -= 1.0f;
1069 return f * f * f + 1.0f;
1070 }
1071
Adam Cohenb5ba0972011-09-07 18:02:31 -07001072 protected void acceleratedOverScroll(float amount) {
1073 int screenSize = getMeasuredWidth();
1074
1075 // We want to reach the max over scroll effect when the user has
1076 // over scrolled half the size of the screen
1077 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1078
1079 if (f == 0) return;
1080
1081 // Clamp this factor, f, to -1 < f < 1
1082 if (Math.abs(f) >= 1) {
1083 f /= Math.abs(f);
1084 }
1085
1086 int overScrollAmount = (int) Math.round(f * screenSize);
1087 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001088 mOverScrollX = overScrollAmount;
1089 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001090 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001091 mOverScrollX = mMaxScrollX + overScrollAmount;
1092 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001093 }
1094 invalidate();
1095 }
1096
1097 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001098 int screenSize = getMeasuredWidth();
1099
1100 float f = (amount / screenSize);
1101
1102 if (f == 0) return;
1103 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1104
Adam Cohen7bfc9792011-01-28 13:52:37 -08001105 // Clamp this factor, f, to -1 < f < 1
1106 if (Math.abs(f) >= 1) {
1107 f /= Math.abs(f);
1108 }
1109
Adam Cohene0f66b52010-11-23 15:06:07 -08001110 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001111 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001112 mOverScrollX = overScrollAmount;
1113 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001114 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001115 mOverScrollX = mMaxScrollX + overScrollAmount;
1116 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001117 }
1118 invalidate();
1119 }
1120
Adam Cohenb5ba0972011-09-07 18:02:31 -07001121 protected void overScroll(float amount) {
1122 dampedOverScroll(amount);
1123 }
1124
Michael Jurkac5b262c2011-01-12 20:24:50 -08001125 protected float maxOverScroll() {
1126 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001127 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001128 float f = 1.0f;
1129 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1130 return OVERSCROLL_DAMP_FACTOR * f;
1131 }
1132
Winson Chung321e9ee2010-08-09 13:37:56 -07001133 @Override
1134 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001135 // Skip touch handling if there are no pages to swipe
1136 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1137
Michael Jurkab8f06722010-10-10 15:58:46 -07001138 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001139
1140 final int action = ev.getAction();
1141
1142 switch (action & MotionEvent.ACTION_MASK) {
1143 case MotionEvent.ACTION_DOWN:
1144 /*
1145 * If being flinged and user touches, stop the fling. isFinished
1146 * will be false if being flinged.
1147 */
1148 if (!mScroller.isFinished()) {
1149 mScroller.abortAnimation();
1150 }
1151
1152 // Remember where the motion event started
1153 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001154 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001155 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001157 if (mTouchState == TOUCH_STATE_SCROLLING) {
1158 pageBeginMoving();
1159 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 break;
1161
1162 case MotionEvent.ACTION_MOVE:
1163 if (mTouchState == TOUCH_STATE_SCROLLING) {
1164 // Scroll to follow the motion event
1165 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1166 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001167 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001168
Adam Cohenaefd4e12011-02-14 16:39:38 -08001169 mTotalMotionX += Math.abs(deltaX);
1170
Winson Chungc0844aa2011-02-02 15:25:58 -08001171 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1172 // keep the remainder because we are actually testing if we've moved from the last
1173 // scrolled position (which is discrete).
1174 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001175 mTouchX += deltaX;
1176 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1177 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001178 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001179 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001180 } else {
1181 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001183 mLastMotionX = x;
1184 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 } else {
1186 awakenScrollBars();
1187 }
Adam Cohen564976a2010-10-13 18:52:07 -07001188 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 determineScrollingStart(ev);
1190 }
1191 break;
1192
1193 case MotionEvent.ACTION_UP:
1194 if (mTouchState == TOUCH_STATE_SCROLLING) {
1195 final int activePointerId = mActivePointerId;
1196 final int pointerIndex = ev.findPointerIndex(activePointerId);
1197 final float x = ev.getX(pointerIndex);
1198 final VelocityTracker velocityTracker = mVelocityTracker;
1199 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1200 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001201 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001202 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1203 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1204 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001205
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001206 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1207
Adam Cohen00481b32011-11-18 12:03:48 -08001208 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001209 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001210
Adam Cohenaefd4e12011-02-14 16:39:38 -08001211 // In the case that the page is moved far to one direction and then is flung
1212 // in the opposite direction, we use a threshold to determine whether we should
1213 // just return to the starting page, or if we should skip one further.
1214 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001215 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001216 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001217 returnToOriginalPage = true;
1218 }
1219
Adam Cohenaefd4e12011-02-14 16:39:38 -08001220 int finalPage;
1221 // We give flings precedence over large moves, which is why we short-circuit our
1222 // test for a large move if a fling has been registered. That is, a large
1223 // move to the left and fling to the right will register as a fling to the right.
1224 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1225 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1226 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1227 snapToPageWithVelocity(finalPage, velocityX);
1228 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1229 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001230 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001231 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1232 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 } else {
1234 snapToDestination();
1235 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001236 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 // at this point we have not moved beyond the touch slop
1238 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1239 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001240 int nextPage = Math.max(0, mCurrentPage - 1);
1241 if (nextPage != mCurrentPage) {
1242 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 } else {
1244 snapToDestination();
1245 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001246 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 // at this point we have not moved beyond the touch slop
1248 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1249 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001250 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1251 if (nextPage != mCurrentPage) {
1252 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 } else {
1254 snapToDestination();
1255 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001256 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001257 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 }
1259 mTouchState = TOUCH_STATE_REST;
1260 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001261 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 break;
1263
1264 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001265 if (mTouchState == TOUCH_STATE_SCROLLING) {
1266 snapToDestination();
1267 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 mTouchState = TOUCH_STATE_REST;
1269 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001270 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 break;
1272
1273 case MotionEvent.ACTION_POINTER_UP:
1274 onSecondaryPointerUp(ev);
1275 break;
1276 }
1277
1278 return true;
1279 }
1280
Winson Chung185d7162011-02-28 13:47:29 -08001281 @Override
1282 public boolean onGenericMotionEvent(MotionEvent event) {
1283 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1284 switch (event.getAction()) {
1285 case MotionEvent.ACTION_SCROLL: {
1286 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1287 final float vscroll;
1288 final float hscroll;
1289 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1290 vscroll = 0;
1291 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1292 } else {
1293 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1294 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1295 }
1296 if (hscroll != 0 || vscroll != 0) {
1297 if (hscroll > 0 || vscroll > 0) {
1298 scrollRight();
1299 } else {
1300 scrollLeft();
1301 }
1302 return true;
1303 }
1304 }
1305 }
1306 }
1307 return super.onGenericMotionEvent(event);
1308 }
1309
Michael Jurkab8f06722010-10-10 15:58:46 -07001310 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1311 if (mVelocityTracker == null) {
1312 mVelocityTracker = VelocityTracker.obtain();
1313 }
1314 mVelocityTracker.addMovement(ev);
1315 }
1316
1317 private void releaseVelocityTracker() {
1318 if (mVelocityTracker != null) {
1319 mVelocityTracker.recycle();
1320 mVelocityTracker = null;
1321 }
1322 }
1323
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 private void onSecondaryPointerUp(MotionEvent ev) {
1325 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1326 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1327 final int pointerId = ev.getPointerId(pointerIndex);
1328 if (pointerId == mActivePointerId) {
1329 // This was our active pointer going up. Choose a new
1330 // active pointer and adjust accordingly.
1331 // TODO: Make this decision more intelligent.
1332 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1333 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1334 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001335 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 mActivePointerId = ev.getPointerId(newPointerIndex);
1337 if (mVelocityTracker != null) {
1338 mVelocityTracker.clear();
1339 }
1340 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001341 }
1342
Michael Jurkad771c962011-08-09 15:00:48 -07001343 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001344
1345 @Override
1346 public void requestChildFocus(View child, View focused) {
1347 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001348 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001349 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001350 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 }
1352 }
1353
Winson Chunge3193b92010-09-10 11:44:42 -07001354 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1355 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001356 int left;
1357 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001358 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001359 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001360 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001361 if (left <= relativeOffset && relativeOffset <= right) {
1362 return i;
1363 }
Winson Chunge3193b92010-09-10 11:44:42 -07001364 }
1365 return -1;
1366 }
1367
Winson Chung1908d072011-02-24 18:09:44 -08001368 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001369 // This functions are called enough times that it actually makes a difference in the
1370 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001371 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001372 final int minWidth = mMinimumWidth;
1373 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001374 }
1375
Adam Cohend19d3ca2010-09-15 14:43:42 -07001376 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001377 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 int minDistanceFromScreenCenterIndex = -1;
1379 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1380 final int childCount = getChildCount();
1381 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001382 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001383 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001384 int halfChildWidth = (childWidth / 2);
1385 int childCenter = getChildOffset(i) + halfChildWidth;
1386 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1387 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1388 minDistanceFromScreenCenter = distanceFromScreenCenter;
1389 minDistanceFromScreenCenterIndex = i;
1390 }
1391 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001392 return minDistanceFromScreenCenterIndex;
1393 }
1394
1395 protected void snapToDestination() {
1396 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001397 }
1398
Adam Cohene0f66b52010-11-23 15:06:07 -08001399 private static class ScrollInterpolator implements Interpolator {
1400 public ScrollInterpolator() {
1401 }
1402
1403 public float getInterpolation(float t) {
1404 t -= 1.0f;
1405 return t*t*t*t*t + 1;
1406 }
1407 }
1408
1409 // We want the duration of the page snap animation to be influenced by the distance that
1410 // the screen has to travel, however, we don't want this duration to be effected in a
1411 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1412 // of travel has on the overall snap duration.
1413 float distanceInfluenceForSnapDuration(float f) {
1414 f -= 0.5f; // center the values about 0.
1415 f *= 0.3f * Math.PI / 2.0f;
1416 return (float) Math.sin(f);
1417 }
1418
Michael Jurka0142d492010-08-25 17:46:15 -07001419 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001420 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1421 int halfScreenSize = getMeasuredWidth() / 2;
1422
Winson Chung785d2eb2011-04-14 16:08:02 -07001423 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1424 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1425 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001426 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1427 int delta = newX - mUnboundedScrollX;
1428 int duration = 0;
1429
Adam Cohen265b9a62011-12-07 14:37:18 -08001430 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001431 // If the velocity is low enough, then treat this more as an automatic page advance
1432 // as opposed to an apparent physical response to flinging
1433 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1434 return;
1435 }
1436
1437 // Here we compute a "distance" that will be used in the computation of the overall
1438 // snap duration. This is a function of the actual distance that needs to be traveled;
1439 // we keep this value close to half screen size in order to reduce the variance in snap
1440 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001441 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001442 float distance = halfScreenSize + halfScreenSize *
1443 distanceInfluenceForSnapDuration(distanceRatio);
1444
1445 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001446 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001447
1448 // we want the page's snap velocity to approximately match the velocity at which the
1449 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001450 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1451 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001452
1453 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001454 }
1455
1456 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001457 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 }
1459
Michael Jurka0142d492010-08-25 17:46:15 -07001460 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001461 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001462
Winson Chung785d2eb2011-04-14 16:08:02 -07001463 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1464 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1465 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001466 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001467 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001469 snapToPage(whichPage, delta, duration);
1470 }
1471
1472 protected void snapToPage(int whichPage, int delta, int duration) {
1473 mNextPage = whichPage;
1474
1475 View focusedChild = getFocusedChild();
1476 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001477 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001478 focusedChild.clearFocus();
1479 }
1480
1481 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 awakenScrollBars(duration);
1483 if (duration == 0) {
1484 duration = Math.abs(delta);
1485 }
1486
1487 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001488 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001489
Winson Chungb44b5242011-06-13 11:32:14 -07001490 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1491 // loading associated pages until the scroll settles
1492 if (mDeferScrollUpdate) {
1493 loadAssociatedPages(mNextPage);
1494 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001495 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001496 }
Michael Jurka0142d492010-08-25 17:46:15 -07001497 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 invalidate();
1499 }
1500
Winson Chung321e9ee2010-08-09 13:37:56 -07001501 public void scrollLeft() {
1502 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001503 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001505 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 }
1507 }
1508
1509 public void scrollRight() {
1510 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001511 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001513 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001514 }
1515 }
1516
Winson Chung86f77532010-08-24 11:08:22 -07001517 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 int result = -1;
1519 if (v != null) {
1520 ViewParent vp = v.getParent();
1521 int count = getChildCount();
1522 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001523 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 return i;
1525 }
1526 }
1527 }
1528 return result;
1529 }
1530
1531 /**
1532 * @return True is long presses are still allowed for the current touch
1533 */
1534 public boolean allowLongPress() {
1535 return mAllowLongPress;
1536 }
1537
Michael Jurka0142d492010-08-25 17:46:15 -07001538 /**
1539 * Set true to allow long-press events to be triggered, usually checked by
1540 * {@link Launcher} to accept or block dpad-initiated long-presses.
1541 */
1542 public void setAllowLongPress(boolean allowLongPress) {
1543 mAllowLongPress = allowLongPress;
1544 }
1545
Winson Chung321e9ee2010-08-09 13:37:56 -07001546 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001547 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001548
1549 SavedState(Parcelable superState) {
1550 super(superState);
1551 }
1552
1553 private SavedState(Parcel in) {
1554 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001555 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001556 }
1557
1558 @Override
1559 public void writeToParcel(Parcel out, int flags) {
1560 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001561 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001562 }
1563
1564 public static final Parcelable.Creator<SavedState> CREATOR =
1565 new Parcelable.Creator<SavedState>() {
1566 public SavedState createFromParcel(Parcel in) {
1567 return new SavedState(in);
1568 }
1569
1570 public SavedState[] newArray(int size) {
1571 return new SavedState[size];
1572 }
1573 };
1574 }
1575
Winson Chungf314b0e2011-08-16 11:54:27 -07001576 protected void loadAssociatedPages(int page) {
1577 loadAssociatedPages(page, false);
1578 }
1579 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001580 if (mContentIsRefreshable) {
1581 final int count = getChildCount();
1582 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001583 int lowerPageBound = getAssociatedLowerPageBound(page);
1584 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001585 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1586 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001587 // First, clear any pages that should no longer be loaded
1588 for (int i = 0; i < count; ++i) {
1589 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001590 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001591 if (layout.getPageChildCount() > 0) {
1592 layout.removeAllViewsOnPage();
1593 }
1594 mDirtyPageContent.set(i, true);
1595 }
1596 }
1597 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001598 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001599 if ((i != page) && immediateAndOnly) {
1600 continue;
1601 }
Michael Jurka0142d492010-08-25 17:46:15 -07001602 if (lowerPageBound <= i && i <= upperPageBound) {
1603 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001604 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001605 mDirtyPageContent.set(i, false);
1606 }
Winson Chung86f77532010-08-24 11:08:22 -07001607 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001608 }
1609 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001610 }
1611 }
1612
Winson Chunge3193b92010-09-10 11:44:42 -07001613 protected int getAssociatedLowerPageBound(int page) {
1614 return Math.max(0, page - 1);
1615 }
1616 protected int getAssociatedUpperPageBound(int page) {
1617 final int count = getChildCount();
1618 return Math.min(page + 1, count - 1);
1619 }
1620
Winson Chung86f77532010-08-24 11:08:22 -07001621 /**
1622 * This method is called ONLY to synchronize the number of pages that the paged view has.
1623 * To actually fill the pages with information, implement syncPageItems() below. It is
1624 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1625 * and therefore, individual page items do not need to be updated in this method.
1626 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001627 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001628
1629 /**
1630 * This method is called to synchronize the items that are on a particular page. If views on
1631 * the page can be reused, then they should be updated within this method.
1632 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001633 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001634
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001635 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001636 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001637 }
1638 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001639 invalidatePageData(currentPage, false);
1640 }
1641 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001642 if (!mIsDataReady) {
1643 return;
1644 }
1645
Michael Jurka0142d492010-08-25 17:46:15 -07001646 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001647 // Force all scrolling-related behavior to end
1648 mScroller.forceFinished(true);
1649 mNextPage = INVALID_PAGE;
1650
Michael Jurka0142d492010-08-25 17:46:15 -07001651 // Update all the pages
1652 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001653
Winson Chung5a808352011-06-27 19:08:49 -07001654 // We must force a measure after we've loaded the pages to update the content width and
1655 // to determine the full scroll width
1656 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1657 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1658
1659 // Set a new page as the current page if necessary
1660 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001661 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001662 }
1663
Michael Jurka0142d492010-08-25 17:46:15 -07001664 // Mark each of the pages as dirty
1665 final int count = getChildCount();
1666 mDirtyPageContent.clear();
1667 for (int i = 0; i < count; ++i) {
1668 mDirtyPageContent.add(true);
1669 }
1670
1671 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001672 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001673 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001674 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001675 }
Winson Chung007c6982011-06-14 13:27:53 -07001676
Michael Jurkaafaa0502011-12-13 18:22:50 -08001677 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001678 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1679 // found
1680 if (mHasScrollIndicator && mScrollIndicator == null) {
1681 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001682 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Michael Jurka9433fa72012-01-19 08:50:41 -08001683 mScrollIndicator.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Winson Chung007c6982011-06-14 13:27:53 -07001684 mHasScrollIndicator = mScrollIndicator != null;
1685 if (mHasScrollIndicator) {
1686 mScrollIndicator.setVisibility(View.VISIBLE);
1687 }
1688 }
1689 return mScrollIndicator;
1690 }
1691
1692 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001693 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001694 }
1695
Winson Chung5a808352011-06-27 19:08:49 -07001696 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1697 @Override
1698 public void run() {
1699 hideScrollingIndicator(false);
1700 }
1701 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001702 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001703 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001704 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001705 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001706 }
1707
Michael Jurka430e8a52011-08-08 15:52:14 -07001708 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001709 if (getChildCount() <= 1) return;
1710 if (!isScrollingIndicatorEnabled()) return;
1711
1712 getScrollingIndicator();
1713 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001714 // Fade the indicator in
1715 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001716 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001717 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001718 if (immediately) {
1719 mScrollIndicator.setAlpha(1f);
1720 } else {
1721 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1722 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1723 mScrollIndicatorAnimator.start();
1724 }
Winson Chung007c6982011-06-14 13:27:53 -07001725 }
1726 }
1727
Adam Cohen21b41102011-11-01 17:29:52 -07001728 protected void cancelScrollingIndicatorAnimations() {
1729 if (mScrollIndicatorAnimator != null) {
1730 mScrollIndicatorAnimator.cancel();
1731 }
1732 }
1733
Winson Chung007c6982011-06-14 13:27:53 -07001734 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001735 if (getChildCount() <= 1) return;
1736 if (!isScrollingIndicatorEnabled()) return;
1737
1738 getScrollingIndicator();
1739 if (mScrollIndicator != null) {
1740 // Fade the indicator out
1741 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001742 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001743 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001744 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001745 mScrollIndicator.setAlpha(0f);
1746 } else {
1747 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1748 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1749 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1750 private boolean cancelled = false;
1751 @Override
1752 public void onAnimationCancel(android.animation.Animator animation) {
1753 cancelled = true;
1754 }
1755 @Override
1756 public void onAnimationEnd(Animator animation) {
1757 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001758 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001759 }
1760 }
1761 });
1762 mScrollIndicatorAnimator.start();
1763 }
Winson Chung007c6982011-06-14 13:27:53 -07001764 }
1765 }
1766
Winson Chung32174c82011-07-19 15:47:55 -07001767 /**
1768 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1769 * fill its space on the track or not.
1770 */
1771 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001772 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001773 }
1774
Winson Chung007c6982011-06-14 13:27:53 -07001775 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001776 if (getChildCount() <= 1) return;
1777 if (!isScrollingIndicatorEnabled()) return;
1778
1779 getScrollingIndicator();
1780 if (mScrollIndicator != null) {
1781 updateScrollingIndicatorPosition();
1782 }
1783 }
1784
Winson Chung007c6982011-06-14 13:27:53 -07001785 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001786 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001787 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001788 int numPages = getChildCount();
1789 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001790 int lastChildIndex = Math.max(0, getChildCount() - 1);
1791 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001792 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001793 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1794 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001795
Winson Chungae890b82011-09-13 18:08:54 -07001796 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001797 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001798 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001799 if (hasElasticScrollIndicator()) {
1800 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1801 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1802 mScrollIndicator.requestLayout();
1803 }
1804 } else {
1805 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1806 indicatorPos += indicatorCenterOffset;
1807 }
Winson Chung007c6982011-06-14 13:27:53 -07001808 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001809 }
1810
Winson Chung3ac74c52011-06-30 17:39:37 -07001811 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001812 }
1813
1814 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001815 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001816
1817 /* Accessibility */
1818 @Override
1819 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1820 super.onInitializeAccessibilityNodeInfo(info);
1821 info.setScrollable(true);
1822 }
1823
1824 @Override
1825 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1826 super.onInitializeAccessibilityEvent(event);
1827 event.setScrollable(true);
1828 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1829 event.setFromIndex(mCurrentPage);
1830 event.setToIndex(mCurrentPage);
1831 event.setItemCount(getChildCount());
1832 }
1833 }
1834
Winson Chung6a0f57d2011-06-29 20:10:49 -07001835 protected String getCurrentPageDescription() {
1836 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1837 return String.format(mContext.getString(R.string.default_scroll_format),
1838 page + 1, getChildCount());
1839 }
Winson Chungd11265e2011-08-30 13:37:23 -07001840
1841 @Override
1842 public boolean onHoverEvent(android.view.MotionEvent event) {
1843 return true;
1844 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001845}