blob: b6efcfa79fc70f629e051de664ed226d916d3eb6 [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
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700154 // choice modes
155 protected static final int CHOICE_MODE_NONE = 0;
156 protected static final int CHOICE_MODE_SINGLE = 1;
157 // Multiple selection mode is not supported by all Launcher actions atm
158 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700159
Michael Jurkae17e19c2010-09-28 11:01:39 -0700160 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700161 private ActionMode mActionMode;
162
Michael Jurka0142d492010-08-25 17:46:15 -0700163 // If true, syncPages and syncPageItems will be called to refresh pages
164 protected boolean mContentIsRefreshable = true;
165
166 // If true, modify alpha of neighboring pages as user scrolls left/right
167 protected boolean mFadeInAdjacentScreens = true;
168
169 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
170 // to switch to a new page
171 protected boolean mUsePagingTouchSlop = true;
172
173 // If true, the subclass should directly update mScrollX itself in its computeScroll method
174 // (SmoothPagedView does this)
175 protected boolean mDeferScrollUpdate = false;
176
Patrick Dubroy1262e362010-10-06 15:49:50 -0700177 protected boolean mIsPageMoving = false;
178
Winson Chungf0ea4d32011-06-06 14:27:16 -0700179 // All syncs and layout passes are deferred until data is ready.
180 protected boolean mIsDataReady = false;
181
Winson Chung007c6982011-06-14 13:27:53 -0700182 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700183 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800184 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700185 private int mScrollIndicatorPaddingLeft;
186 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700187 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700188 protected static final int sScrollIndicatorFadeInDuration = 150;
189 protected static final int sScrollIndicatorFadeOutDuration = 650;
190 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700191
Winson Chungb44b5242011-06-13 11:32:14 -0700192 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700193 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700194
Winson Chung86f77532010-08-24 11:08:22 -0700195 public interface PageSwitchListener {
196 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 public PagedView(Context context) {
200 this(context, null);
201 }
202
203 public PagedView(Context context, AttributeSet attrs) {
204 this(context, attrs, 0);
205 }
206
207 public PagedView(Context context, AttributeSet attrs, int defStyle) {
208 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700209 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700210
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 TypedArray a = context.obtainStyledAttributes(attrs,
212 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800213 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700214 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800215 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700216 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800217 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700218 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800219 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700220 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800221 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700222 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700223 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700224 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700225 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700226 mScrollIndicatorPaddingLeft =
227 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
228 mScrollIndicatorPaddingRight =
229 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700230 a.recycle();
231
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700233 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 }
235
236 /**
237 * Initializes various states for this workspace.
238 */
Michael Jurka0142d492010-08-25 17:46:15 -0700239 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700240 mDirtyPageContent = new ArrayList<Boolean>();
241 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800242 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700243 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700244 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700245
246 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
247 mTouchSlop = configuration.getScaledTouchSlop();
248 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
249 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700250 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800251
252 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
253 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
254 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Winson Chung321e9ee2010-08-09 13:37:56 -0700255 }
256
Winson Chung86f77532010-08-24 11:08:22 -0700257 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
258 mPageSwitchListener = pageSwitchListener;
259 if (mPageSwitchListener != null) {
260 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700261 }
262 }
263
264 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700265 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
266 * out pages.
267 */
268 protected void setDataIsReady() {
269 mIsDataReady = true;
270 }
271 protected boolean isDataReady() {
272 return mIsDataReady;
273 }
274
275 /**
Winson Chung86f77532010-08-24 11:08:22 -0700276 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 *
Winson Chung86f77532010-08-24 11:08:22 -0700278 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 */
Winson Chung86f77532010-08-24 11:08:22 -0700280 int getCurrentPage() {
281 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 }
283
Winson Chung86f77532010-08-24 11:08:22 -0700284 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 return getChildCount();
286 }
287
Winson Chung86f77532010-08-24 11:08:22 -0700288 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 return getChildAt(index);
290 }
291
Adam Cohenae4f1552011-10-20 00:15:42 -0700292 protected int indexToPage(int index) {
293 return index;
294 }
295
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800297 * Updates the scroll of the current page immediately to its final scroll position. We use this
298 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
299 * the previous tab page.
300 */
301 protected void updateCurrentPageScroll() {
302 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
303 scrollTo(newX, 0);
304 mScroller.setFinalX(newX);
305 }
306
307 /**
Winson Chung86f77532010-08-24 11:08:22 -0700308 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 */
Winson Chung86f77532010-08-24 11:08:22 -0700310 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800311 if (!mScroller.isFinished()) {
312 mScroller.abortAnimation();
313 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800314 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
315 // the default
316 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800317 return;
318 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700319
Winson Chung86f77532010-08-24 11:08:22 -0700320 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800321 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700322 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700323 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800324 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 }
326
Michael Jurka0142d492010-08-25 17:46:15 -0700327 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700328 if (mPageSwitchListener != null) {
329 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 }
331 }
332
Michael Jurkace7e05f2011-02-01 22:02:35 -0800333 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700334 if (!mIsPageMoving) {
335 mIsPageMoving = true;
336 onPageBeginMoving();
337 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700338 }
339
Michael Jurkace7e05f2011-02-01 22:02:35 -0800340 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700341 if (mIsPageMoving) {
342 mIsPageMoving = false;
343 onPageEndMoving();
344 }
Michael Jurka0142d492010-08-25 17:46:15 -0700345 }
346
Adam Cohen26976d92011-03-22 15:33:33 -0700347 protected boolean isPageMoving() {
348 return mIsPageMoving;
349 }
350
Michael Jurka0142d492010-08-25 17:46:15 -0700351 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700352 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700353 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700354 }
355
356 // a method that subclasses can override to add behavior
357 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700358 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700359 }
360
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 /**
Winson Chung86f77532010-08-24 11:08:22 -0700362 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 *
364 * @param l The listener used to respond to long clicks.
365 */
366 @Override
367 public void setOnLongClickListener(OnLongClickListener l) {
368 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700369 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700371 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373 }
374
375 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800376 public void scrollBy(int x, int y) {
377 scrollTo(mUnboundedScrollX + x, mScrollY + y);
378 }
379
380 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700381 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800382 mUnboundedScrollX = x;
383
384 if (x < 0) {
385 super.scrollTo(0, y);
386 if (mAllowOverScroll) {
387 overScroll(x);
388 }
389 } else if (x > mMaxScrollX) {
390 super.scrollTo(mMaxScrollX, y);
391 if (mAllowOverScroll) {
392 overScroll(x - mMaxScrollX);
393 }
394 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800395 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800396 super.scrollTo(x, y);
397 }
398
Michael Jurka0142d492010-08-25 17:46:15 -0700399 mTouchX = x;
400 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
401 }
402
403 // we moved this functionality to a helper function so SmoothPagedView can reuse it
404 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700405 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700406 // Don't bother scrolling if the page does not need to be moved
407 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700408 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
409 }
Michael Jurka0142d492010-08-25 17:46:15 -0700410 invalidate();
411 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700412 } else if (mNextPage != INVALID_PAGE) {
413 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700414 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700415 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700416
417 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700418 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700419 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700420 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700421 }
422
Adam Cohen73aa9752010-11-24 16:26:58 -0800423 // We don't want to trigger a page end moving unless the page has settled
424 // and the user has stopped scrolling
425 if (mTouchState == TOUCH_STATE_REST) {
426 pageEndMoving();
427 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700428
429 // Notify the user when the page changes
430 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
431 AccessibilityEvent ev =
432 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
433 ev.getText().add(getCurrentPageDescription());
434 sendAccessibilityEventUnchecked(ev);
435 }
Michael Jurka0142d492010-08-25 17:46:15 -0700436 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 }
Michael Jurka0142d492010-08-25 17:46:15 -0700438 return false;
439 }
440
441 @Override
442 public void computeScroll() {
443 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 }
445
446 @Override
447 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700448 if (!mIsDataReady) {
449 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
450 return;
451 }
452
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
454 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
455 if (widthMode != MeasureSpec.EXACTLY) {
456 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
457 }
458
Adam Lesinski6b879f02010-11-04 16:15:23 -0700459 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
460 * of the All apps view on XLarge displays to not take up more space then it needs. Width
461 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
462 * each page to have the same width.
463 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700465 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
466 int maxChildHeight = 0;
467
468 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700469 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700470
Michael Jurka36fcb742011-04-06 17:08:58 -0700471
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700473 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700474 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 final int childCount = getChildCount();
476 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700477 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700478 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700479 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
480
481 int childWidthMode;
482 if (lp.width == LayoutParams.WRAP_CONTENT) {
483 childWidthMode = MeasureSpec.AT_MOST;
484 } else {
485 childWidthMode = MeasureSpec.EXACTLY;
486 }
487
488 int childHeightMode;
489 if (lp.height == LayoutParams.WRAP_CONTENT) {
490 childHeightMode = MeasureSpec.AT_MOST;
491 } else {
492 childHeightMode = MeasureSpec.EXACTLY;
493 }
494
495 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700496 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700497 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700498 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700499
500 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700501 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700502 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
503 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700504 }
505
506 if (heightMode == MeasureSpec.AT_MOST) {
507 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 }
Winson Chungae890b82011-09-13 18:08:54 -0700509
Winson Chungae890b82011-09-13 18:08:54 -0700510 setMeasuredDimension(widthSize, heightSize);
511
Adam Cohen25b29952011-11-02 14:49:06 -0700512 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
513 // We also wait until we set the measured dimensions before flushing the cache as well, to
514 // ensure that the cache is filled with good values.
515 invalidateCachedOffsets();
516 updateScrollingIndicatorPosition();
517
Adam Cohenfaa28302010-11-19 12:02:18 -0800518 if (childCount > 0) {
519 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
520 } else {
521 mMaxScrollX = 0;
522 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700523 }
524
Michael Jurka8c920dd2011-01-20 14:16:56 -0800525 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800526 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
527 int delta = newX - mScrollX;
528
Michael Jurka8c920dd2011-01-20 14:16:56 -0800529 final int pageCount = getChildCount();
530 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700531 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800532 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800533 }
534 setCurrentPage(newCurrentPage);
535 }
536
Michael Jurka8c920dd2011-01-20 14:16:56 -0800537 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
538 // 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 -0800539 // tightens the layout accordingly
540 public void setLayoutScale(float childrenScale) {
541 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700542 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800543
544 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
545 int childCount = getChildCount();
546 float childrenX[] = new float[childCount];
547 float childrenY[] = new float[childCount];
548 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 childrenX[i] = child.getX();
551 childrenY[i] = child.getY();
552 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700553 // Trigger a full re-layout (never just call onLayout directly!)
554 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
555 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
556 requestLayout();
557 measure(widthSpec, heightSpec);
558 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700560 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800561 child.setX(childrenX[i]);
562 child.setY(childrenY[i]);
563 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700564
Michael Jurkad3ef3062010-11-23 16:23:58 -0800565 // Also, the page offset has changed (since the pages are now smaller);
566 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800567 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800568 }
569
Adam Cohen60b07122011-11-14 17:26:06 -0800570 public void setPageSpacing(int pageSpacing) {
571 mPageSpacing = pageSpacing;
572 invalidateCachedOffsets();
573 }
574
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700576 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700577 if (!mIsDataReady) {
578 return;
579 }
580
Winson Chung785d2eb2011-04-14 16:08:02 -0700581 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700582 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 final int childCount = getChildCount();
584 int childLeft = 0;
585 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700586 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
587 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700588 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700589
590 // Calculate the variable page spacing if necessary
591 if (mPageSpacing < 0) {
Adam Cohen60b07122011-11-14 17:26:06 -0800592 setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
Winson Chungae890b82011-09-13 18:08:54 -0700593 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
595
596 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700597 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800599 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700600 final int childHeight = child.getMeasuredHeight();
601 int childTop = mPaddingTop;
602 if (mCenterPagesVertically) {
603 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
604 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800605
Winson Chung785d2eb2011-04-14 16:08:02 -0700606 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700607 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800608 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700609 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
611 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700612
613 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
614 setHorizontalScrollBarEnabled(false);
615 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
616 scrollTo(newX, 0);
617 mScroller.setFinalX(newX);
618 setHorizontalScrollBarEnabled(true);
619 mFirstLayout = false;
620 }
621
Michael Jurkad3ef3062010-11-23 16:23:58 -0800622 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
623 mFirstLayout = false;
624 }
Winson Chunge3193b92010-09-10 11:44:42 -0700625 }
626
Adam Cohenf34bab52010-09-30 14:11:56 -0700627 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700628 if (isScrollingIndicatorEnabled()) {
629 updateScrollingIndicator();
630 }
631 if (mFadeInAdjacentScreens) {
632 for (int i = 0; i < getChildCount(); i++) {
633 View child = getChildAt(i);
634 if (child != null) {
635 float scrollProgress = getScrollProgress(screenCenter, child, i);
636 float alpha = 1 - Math.abs(scrollProgress);
637 child.setFastAlpha(alpha);
638 child.fastInvalidate();
639 }
640 }
641 invalidate();
642 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700643 }
644
Winson Chunge3193b92010-09-10 11:44:42 -0700645 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700646 protected void onViewAdded(View child) {
647 super.onViewAdded(child);
648
649 // This ensures that when children are added, they get the correct transforms / alphas
650 // in accordance with any scroll effects.
651 mForceScreenScrolled = true;
652 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700653 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700654 }
655
Adam Cohen73894962011-10-31 13:17:17 -0700656 protected void invalidateCachedOffsets() {
657 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700658 if (count == 0) {
659 mChildOffsets = null;
660 mChildRelativeOffsets = null;
661 mChildOffsetsWithLayoutScale = null;
662 return;
663 }
Adam Cohen73894962011-10-31 13:17:17 -0700664
665 mChildOffsets = new int[count];
666 mChildRelativeOffsets = new int[count];
667 mChildOffsetsWithLayoutScale = new int[count];
668 for (int i = 0; i < count; i++) {
669 mChildOffsets[i] = -1;
670 mChildRelativeOffsets[i] = -1;
671 mChildOffsetsWithLayoutScale[i] = -1;
672 }
673 }
674
675 protected int getChildOffset(int index) {
676 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
677 mChildOffsets : mChildOffsetsWithLayoutScale;
678
679 if (childOffsets != null && childOffsets[index] != -1) {
680 return childOffsets[index];
681 } else {
682 if (getChildCount() == 0)
683 return 0;
684
685 int offset = getRelativeChildOffset(0);
686 for (int i = 0; i < index; ++i) {
687 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
688 }
689 if (childOffsets != null) {
690 childOffsets[index] = offset;
691 }
692 return offset;
693 }
694 }
695
696 protected int getRelativeChildOffset(int index) {
697 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
698 return mChildRelativeOffsets[index];
699 } else {
700 final int padding = mPaddingLeft + mPaddingRight;
701 final int offset = mPaddingLeft +
702 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
703 if (mChildRelativeOffsets != null) {
704 mChildRelativeOffsets[index] = offset;
705 }
706 return offset;
707 }
708 }
709
710 protected int getScaledRelativeChildOffset(int index) {
711 final int padding = mPaddingLeft + mPaddingRight;
712 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
713 getScaledMeasuredWidth(getPageAt(index))) / 2;
714 return offset;
715 }
716
717 protected int getScaledMeasuredWidth(View child) {
718 // This functions are called enough times that it actually makes a difference in the
719 // profiler -- so just inline the max() here
720 final int measuredWidth = child.getMeasuredWidth();
721 final int minWidth = mMinimumWidth;
722 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
723 return (int) (maxWidth * mLayoutScale + 0.5f);
724 }
725
Michael Jurkadde558b2011-11-09 22:09:06 -0800726 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700727 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700728 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700729 final int screenWidth = getMeasuredWidth();
Michael Jurkaafaa0502011-12-13 18:22:50 -0800730 int x = (int) getPageAt(0).getRight();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700731 int leftScreen = 0;
732 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700733 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700734 leftScreen++;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800735 x = getPageAt(leftScreen).getRight();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700736 }
737 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700738 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700739 rightScreen++;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800740 x = (int) getPageAt(rightScreen).getRight();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700741 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800742 range[0] = leftScreen;
743 range[1] = rightScreen;
744 } else {
745 range[0] = -1;
746 range[1] = -1;
747 }
748 }
749
750 @Override
751 protected void dispatchDraw(Canvas canvas) {
752 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800753 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
754 // it is equal to the scaled overscroll position.
755 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800756
757 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
758 screenScrolled(screenCenter);
759 mLastScreenCenter = screenCenter;
760 mForceScreenScrolled = false;
761 }
762
763 // Find out which screens are visible; as an optimization we only call draw on them
764 final int pageCount = getChildCount();
765 if (pageCount > 0) {
766 getVisiblePages(mTempVisiblePagesRange);
767 final int leftScreen = mTempVisiblePagesRange[0];
768 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800769 if (leftScreen != -1 && rightScreen != -1) {
770 final long drawingTime = getDrawingTime();
771 // Clip to the bounds
772 canvas.save();
773 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
774 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700775
Winson Chungc6f10b92011-11-14 11:39:07 -0800776 for (int i = rightScreen; i >= leftScreen; i--) {
777 drawChild(canvas, getPageAt(i), drawingTime);
778 }
779 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700780 }
Michael Jurka0142d492010-08-25 17:46:15 -0700781 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 }
783
784 @Override
785 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700786 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700787 if (page != mCurrentPage || !mScroller.isFinished()) {
788 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700789 return true;
790 }
791 return false;
792 }
793
794 @Override
795 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700796 int focusablePage;
797 if (mNextPage != INVALID_PAGE) {
798 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700800 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 }
Winson Chung86f77532010-08-24 11:08:22 -0700802 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700804 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 }
806 return false;
807 }
808
809 @Override
810 public boolean dispatchUnhandledMove(View focused, int direction) {
811 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700812 if (getCurrentPage() > 0) {
813 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 return true;
815 }
816 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700817 if (getCurrentPage() < getPageCount() - 1) {
818 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 return true;
820 }
821 }
822 return super.dispatchUnhandledMove(focused, direction);
823 }
824
825 @Override
826 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700827 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
828 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 }
830 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700831 if (mCurrentPage > 0) {
832 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 }
834 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700835 if (mCurrentPage < getPageCount() - 1) {
836 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 }
838 }
839 }
840
841 /**
842 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700843 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 *
Winson Chung86f77532010-08-24 11:08:22 -0700845 * This happens when live folders requery, and if they're off page, they
846 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 */
848 @Override
849 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700850 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 View v = focused;
852 while (true) {
853 if (v == current) {
854 super.focusableViewAvailable(focused);
855 return;
856 }
857 if (v == this) {
858 return;
859 }
860 ViewParent parent = v.getParent();
861 if (parent instanceof View) {
862 v = (View)v.getParent();
863 } else {
864 return;
865 }
866 }
867 }
868
869 /**
870 * {@inheritDoc}
871 */
872 @Override
873 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
874 if (disallowIntercept) {
875 // We need to make sure to cancel our long press if
876 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700877 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700878 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 }
880 super.requestDisallowInterceptTouchEvent(disallowIntercept);
881 }
882
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800883 /**
884 * Return true if a tap at (x, y) should trigger a flip to the previous page.
885 */
886 protected boolean hitsPreviousPage(float x, float y) {
887 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
888 }
889
890 /**
891 * Return true if a tap at (x, y) should trigger a flip to the next page.
892 */
893 protected boolean hitsNextPage(float x, float y) {
894 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
895 }
896
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 @Override
898 public boolean onInterceptTouchEvent(MotionEvent ev) {
899 /*
900 * This method JUST determines whether we want to intercept the motion.
901 * If we return true, onTouchEvent will be called and we do the actual
902 * scrolling there.
903 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800904 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700905
Winson Chung45e1d6e2010-11-09 17:19:49 -0800906 // Skip touch handling if there are no pages to swipe
907 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
908
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 /*
910 * Shortcut the most recurring case: the user is in the dragging
911 * state and he is moving his finger. We want to intercept this
912 * motion.
913 */
914 final int action = ev.getAction();
915 if ((action == MotionEvent.ACTION_MOVE) &&
916 (mTouchState == TOUCH_STATE_SCROLLING)) {
917 return true;
918 }
919
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 switch (action & MotionEvent.ACTION_MASK) {
921 case MotionEvent.ACTION_MOVE: {
922 /*
923 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
924 * whether the user has moved far enough from his original down touch.
925 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700926 if (mActivePointerId != INVALID_POINTER) {
927 determineScrollingStart(ev);
928 break;
929 }
930 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
931 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
932 // i.e. fall through to the next case (don't break)
933 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
934 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 }
936
937 case MotionEvent.ACTION_DOWN: {
938 final float x = ev.getX();
939 final float y = ev.getY();
940 // Remember location of down touch
941 mDownMotionX = x;
942 mLastMotionX = x;
943 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800944 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800945 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 mActivePointerId = ev.getPointerId(0);
947 mAllowLongPress = true;
948
949 /*
950 * If being flinged and user touches the screen, initiate drag;
951 * otherwise don't. mScroller.isFinished should be false when
952 * being flinged.
953 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700954 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700955 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
956 if (finishedScrolling) {
957 mTouchState = TOUCH_STATE_REST;
958 mScroller.abortAnimation();
959 } else {
960 mTouchState = TOUCH_STATE_SCROLLING;
961 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700962
Winson Chung86f77532010-08-24 11:08:22 -0700963 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800965 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800967 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800969 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 mTouchState = TOUCH_STATE_NEXT_PAGE;
971 }
972 }
973 }
974 break;
975 }
976
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800978 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 mTouchState = TOUCH_STATE_REST;
980 mAllowLongPress = false;
981 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800982 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 break;
984
985 case MotionEvent.ACTION_POINTER_UP:
986 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800987 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 break;
989 }
990
991 /*
992 * The only time we want to intercept motion events is if we are in the
993 * drag mode.
994 */
995 return mTouchState != TOUCH_STATE_REST;
996 }
997
Winson Chung80baf5a2010-08-09 16:03:15 -0700998 protected void animateClickFeedback(View v, final Runnable r) {
999 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -08001000 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
1001 loadAnimator(mContext, R.anim.paged_view_click_feedback);
1002 anim.setTarget(v);
1003 anim.addListener(new AnimatorListenerAdapter() {
1004 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001005 r.run();
1006 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001007 });
Winson Chung228a0fa2011-01-26 22:14:13 -08001008 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -07001009 }
1010
Adam Cohenf8d28232011-02-01 21:47:00 -08001011 protected void determineScrollingStart(MotionEvent ev) {
1012 determineScrollingStart(ev, 1.0f);
1013 }
1014
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 /*
1016 * Determines if we should change the touch state to start scrolling after the
1017 * user moves their touch point too far.
1018 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001019 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 /*
1021 * Locally do absolute value. mLastMotionX is set to the y value
1022 * of the down event.
1023 */
1024 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001025 if (pointerIndex == -1) {
1026 return;
1027 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001028 final float x = ev.getX(pointerIndex);
1029 final float y = ev.getY(pointerIndex);
1030 final int xDiff = (int) Math.abs(x - mLastMotionX);
1031 final int yDiff = (int) Math.abs(y - mLastMotionY);
1032
Adam Cohenf8d28232011-02-01 21:47:00 -08001033 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 boolean xPaged = xDiff > mPagingTouchSlop;
1035 boolean xMoved = xDiff > touchSlop;
1036 boolean yMoved = yDiff > touchSlop;
1037
Adam Cohenf8d28232011-02-01 21:47:00 -08001038 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001039 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 // Scroll if the user moved far enough along the X axis
1041 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001042 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001044 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001045 mTouchX = mScrollX;
1046 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1047 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 }
1049 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001050 cancelCurrentPageLongPress();
1051 }
1052 }
1053
1054 protected void cancelCurrentPageLongPress() {
1055 if (mAllowLongPress) {
1056 mAllowLongPress = false;
1057 // Try canceling the long press. It could also have been scheduled
1058 // by a distant descendant, so use the mAllowLongPress flag to block
1059 // everything
1060 final View currentPage = getPageAt(mCurrentPage);
1061 if (currentPage != null) {
1062 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 }
1064 }
1065 }
1066
Adam Cohenb5ba0972011-09-07 18:02:31 -07001067 protected float getScrollProgress(int screenCenter, View v, int page) {
1068 final int halfScreenSize = getMeasuredWidth() / 2;
1069
1070 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1071 int delta = screenCenter - (getChildOffset(page) -
1072 getRelativeChildOffset(page) + halfScreenSize);
1073
1074 float scrollProgress = delta / (totalDistance * 1.0f);
1075 scrollProgress = Math.min(scrollProgress, 1.0f);
1076 scrollProgress = Math.max(scrollProgress, -1.0f);
1077 return scrollProgress;
1078 }
1079
Adam Cohene0f66b52010-11-23 15:06:07 -08001080 // This curve determines how the effect of scrolling over the limits of the page dimishes
1081 // as the user pulls further and further from the bounds
1082 private float overScrollInfluenceCurve(float f) {
1083 f -= 1.0f;
1084 return f * f * f + 1.0f;
1085 }
1086
Adam Cohenb5ba0972011-09-07 18:02:31 -07001087 protected void acceleratedOverScroll(float amount) {
1088 int screenSize = getMeasuredWidth();
1089
1090 // We want to reach the max over scroll effect when the user has
1091 // over scrolled half the size of the screen
1092 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1093
1094 if (f == 0) return;
1095
1096 // Clamp this factor, f, to -1 < f < 1
1097 if (Math.abs(f) >= 1) {
1098 f /= Math.abs(f);
1099 }
1100
1101 int overScrollAmount = (int) Math.round(f * screenSize);
1102 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001103 mOverScrollX = overScrollAmount;
1104 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001105 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001106 mOverScrollX = mMaxScrollX + overScrollAmount;
1107 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001108 }
1109 invalidate();
1110 }
1111
1112 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001113 int screenSize = getMeasuredWidth();
1114
1115 float f = (amount / screenSize);
1116
1117 if (f == 0) return;
1118 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1119
Adam Cohen7bfc9792011-01-28 13:52:37 -08001120 // Clamp this factor, f, to -1 < f < 1
1121 if (Math.abs(f) >= 1) {
1122 f /= Math.abs(f);
1123 }
1124
Adam Cohene0f66b52010-11-23 15:06:07 -08001125 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001126 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001127 mOverScrollX = overScrollAmount;
1128 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001129 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001130 mOverScrollX = mMaxScrollX + overScrollAmount;
1131 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001132 }
1133 invalidate();
1134 }
1135
Adam Cohenb5ba0972011-09-07 18:02:31 -07001136 protected void overScroll(float amount) {
1137 dampedOverScroll(amount);
1138 }
1139
Michael Jurkac5b262c2011-01-12 20:24:50 -08001140 protected float maxOverScroll() {
1141 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001142 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001143 float f = 1.0f;
1144 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1145 return OVERSCROLL_DAMP_FACTOR * f;
1146 }
1147
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 @Override
1149 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001150 // Skip touch handling if there are no pages to swipe
1151 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1152
Michael Jurkab8f06722010-10-10 15:58:46 -07001153 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001154
1155 final int action = ev.getAction();
1156
1157 switch (action & MotionEvent.ACTION_MASK) {
1158 case MotionEvent.ACTION_DOWN:
1159 /*
1160 * If being flinged and user touches, stop the fling. isFinished
1161 * will be false if being flinged.
1162 */
1163 if (!mScroller.isFinished()) {
1164 mScroller.abortAnimation();
1165 }
1166
1167 // Remember where the motion event started
1168 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001169 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001170 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001172 if (mTouchState == TOUCH_STATE_SCROLLING) {
1173 pageBeginMoving();
1174 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 break;
1176
1177 case MotionEvent.ACTION_MOVE:
1178 if (mTouchState == TOUCH_STATE_SCROLLING) {
1179 // Scroll to follow the motion event
1180 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1181 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001182 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001183
Adam Cohenaefd4e12011-02-14 16:39:38 -08001184 mTotalMotionX += Math.abs(deltaX);
1185
Winson Chungc0844aa2011-02-02 15:25:58 -08001186 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1187 // keep the remainder because we are actually testing if we've moved from the last
1188 // scrolled position (which is discrete).
1189 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001190 mTouchX += deltaX;
1191 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1192 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001193 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001194 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001195 } else {
1196 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001198 mLastMotionX = x;
1199 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 } else {
1201 awakenScrollBars();
1202 }
Adam Cohen564976a2010-10-13 18:52:07 -07001203 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 determineScrollingStart(ev);
1205 }
1206 break;
1207
1208 case MotionEvent.ACTION_UP:
1209 if (mTouchState == TOUCH_STATE_SCROLLING) {
1210 final int activePointerId = mActivePointerId;
1211 final int pointerIndex = ev.findPointerIndex(activePointerId);
1212 final float x = ev.getX(pointerIndex);
1213 final VelocityTracker velocityTracker = mVelocityTracker;
1214 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1215 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001216 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001217 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1218 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1219 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001220
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001221 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1222
Adam Cohen00481b32011-11-18 12:03:48 -08001223 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001224 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001225
Adam Cohenaefd4e12011-02-14 16:39:38 -08001226 // In the case that the page is moved far to one direction and then is flung
1227 // in the opposite direction, we use a threshold to determine whether we should
1228 // just return to the starting page, or if we should skip one further.
1229 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001230 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001231 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001232 returnToOriginalPage = true;
1233 }
1234
Adam Cohenaefd4e12011-02-14 16:39:38 -08001235 int finalPage;
1236 // We give flings precedence over large moves, which is why we short-circuit our
1237 // test for a large move if a fling has been registered. That is, a large
1238 // move to the left and fling to the right will register as a fling to the right.
1239 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1240 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1241 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1242 snapToPageWithVelocity(finalPage, velocityX);
1243 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1244 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001245 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001246 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1247 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 } else {
1249 snapToDestination();
1250 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001251 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 // at this point we have not moved beyond the touch slop
1253 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1254 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001255 int nextPage = Math.max(0, mCurrentPage - 1);
1256 if (nextPage != mCurrentPage) {
1257 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 } else {
1259 snapToDestination();
1260 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001261 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 // at this point we have not moved beyond the touch slop
1263 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1264 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001265 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1266 if (nextPage != mCurrentPage) {
1267 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 } else {
1269 snapToDestination();
1270 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001271 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001272 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 }
1274 mTouchState = TOUCH_STATE_REST;
1275 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001276 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 break;
1278
1279 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001280 if (mTouchState == TOUCH_STATE_SCROLLING) {
1281 snapToDestination();
1282 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 mTouchState = TOUCH_STATE_REST;
1284 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001285 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 break;
1287
1288 case MotionEvent.ACTION_POINTER_UP:
1289 onSecondaryPointerUp(ev);
1290 break;
1291 }
1292
1293 return true;
1294 }
1295
Winson Chung185d7162011-02-28 13:47:29 -08001296 @Override
1297 public boolean onGenericMotionEvent(MotionEvent event) {
1298 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1299 switch (event.getAction()) {
1300 case MotionEvent.ACTION_SCROLL: {
1301 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1302 final float vscroll;
1303 final float hscroll;
1304 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1305 vscroll = 0;
1306 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1307 } else {
1308 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1309 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1310 }
1311 if (hscroll != 0 || vscroll != 0) {
1312 if (hscroll > 0 || vscroll > 0) {
1313 scrollRight();
1314 } else {
1315 scrollLeft();
1316 }
1317 return true;
1318 }
1319 }
1320 }
1321 }
1322 return super.onGenericMotionEvent(event);
1323 }
1324
Michael Jurkab8f06722010-10-10 15:58:46 -07001325 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1326 if (mVelocityTracker == null) {
1327 mVelocityTracker = VelocityTracker.obtain();
1328 }
1329 mVelocityTracker.addMovement(ev);
1330 }
1331
1332 private void releaseVelocityTracker() {
1333 if (mVelocityTracker != null) {
1334 mVelocityTracker.recycle();
1335 mVelocityTracker = null;
1336 }
1337 }
1338
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 private void onSecondaryPointerUp(MotionEvent ev) {
1340 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1341 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1342 final int pointerId = ev.getPointerId(pointerIndex);
1343 if (pointerId == mActivePointerId) {
1344 // This was our active pointer going up. Choose a new
1345 // active pointer and adjust accordingly.
1346 // TODO: Make this decision more intelligent.
1347 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1348 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1349 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001350 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 mActivePointerId = ev.getPointerId(newPointerIndex);
1352 if (mVelocityTracker != null) {
1353 mVelocityTracker.clear();
1354 }
1355 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001356 }
1357
Michael Jurkad771c962011-08-09 15:00:48 -07001358 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001359
1360 @Override
1361 public void requestChildFocus(View child, View focused) {
1362 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001363 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001364 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001365 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 }
1367 }
1368
Winson Chunge3193b92010-09-10 11:44:42 -07001369 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1370 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001371 int left;
1372 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001373 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001374 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001375 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001376 if (left <= relativeOffset && relativeOffset <= right) {
1377 return i;
1378 }
Winson Chunge3193b92010-09-10 11:44:42 -07001379 }
1380 return -1;
1381 }
1382
Winson Chung1908d072011-02-24 18:09:44 -08001383 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001384 // This functions are called enough times that it actually makes a difference in the
1385 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001386 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001387 final int minWidth = mMinimumWidth;
1388 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001389 }
1390
Adam Cohend19d3ca2010-09-15 14:43:42 -07001391 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001392 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 int minDistanceFromScreenCenterIndex = -1;
1394 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1395 final int childCount = getChildCount();
1396 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001397 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001398 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 int halfChildWidth = (childWidth / 2);
1400 int childCenter = getChildOffset(i) + halfChildWidth;
1401 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1402 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1403 minDistanceFromScreenCenter = distanceFromScreenCenter;
1404 minDistanceFromScreenCenterIndex = i;
1405 }
1406 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001407 return minDistanceFromScreenCenterIndex;
1408 }
1409
1410 protected void snapToDestination() {
1411 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 }
1413
Adam Cohene0f66b52010-11-23 15:06:07 -08001414 private static class ScrollInterpolator implements Interpolator {
1415 public ScrollInterpolator() {
1416 }
1417
1418 public float getInterpolation(float t) {
1419 t -= 1.0f;
1420 return t*t*t*t*t + 1;
1421 }
1422 }
1423
1424 // We want the duration of the page snap animation to be influenced by the distance that
1425 // the screen has to travel, however, we don't want this duration to be effected in a
1426 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1427 // of travel has on the overall snap duration.
1428 float distanceInfluenceForSnapDuration(float f) {
1429 f -= 0.5f; // center the values about 0.
1430 f *= 0.3f * Math.PI / 2.0f;
1431 return (float) Math.sin(f);
1432 }
1433
Michael Jurka0142d492010-08-25 17:46:15 -07001434 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001435 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1436 int halfScreenSize = getMeasuredWidth() / 2;
1437
Winson Chung785d2eb2011-04-14 16:08:02 -07001438 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1439 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1440 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001441 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1442 int delta = newX - mUnboundedScrollX;
1443 int duration = 0;
1444
Adam Cohen265b9a62011-12-07 14:37:18 -08001445 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001446 // If the velocity is low enough, then treat this more as an automatic page advance
1447 // as opposed to an apparent physical response to flinging
1448 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1449 return;
1450 }
1451
1452 // Here we compute a "distance" that will be used in the computation of the overall
1453 // snap duration. This is a function of the actual distance that needs to be traveled;
1454 // we keep this value close to half screen size in order to reduce the variance in snap
1455 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001456 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001457 float distance = halfScreenSize + halfScreenSize *
1458 distanceInfluenceForSnapDuration(distanceRatio);
1459
1460 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001461 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001462
1463 // we want the page's snap velocity to approximately match the velocity at which the
1464 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001465 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1466 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001467
1468 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001469 }
1470
1471 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001472 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 }
1474
Michael Jurka0142d492010-08-25 17:46:15 -07001475 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001476 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001477
Winson Chung785d2eb2011-04-14 16:08:02 -07001478 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1479 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1480 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001481 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001482 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001484 snapToPage(whichPage, delta, duration);
1485 }
1486
1487 protected void snapToPage(int whichPage, int delta, int duration) {
1488 mNextPage = whichPage;
1489
1490 View focusedChild = getFocusedChild();
1491 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001492 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001493 focusedChild.clearFocus();
1494 }
1495
1496 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 awakenScrollBars(duration);
1498 if (duration == 0) {
1499 duration = Math.abs(delta);
1500 }
1501
1502 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001503 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001504
Winson Chungb44b5242011-06-13 11:32:14 -07001505 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1506 // loading associated pages until the scroll settles
1507 if (mDeferScrollUpdate) {
1508 loadAssociatedPages(mNextPage);
1509 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001510 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001511 }
Michael Jurka0142d492010-08-25 17:46:15 -07001512 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 invalidate();
1514 }
1515
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 public void scrollLeft() {
1517 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001518 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001519 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001520 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 }
1522 }
1523
1524 public void scrollRight() {
1525 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001526 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001528 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001529 }
1530 }
1531
Winson Chung86f77532010-08-24 11:08:22 -07001532 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001533 int result = -1;
1534 if (v != null) {
1535 ViewParent vp = v.getParent();
1536 int count = getChildCount();
1537 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001538 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 return i;
1540 }
1541 }
1542 }
1543 return result;
1544 }
1545
1546 /**
1547 * @return True is long presses are still allowed for the current touch
1548 */
1549 public boolean allowLongPress() {
1550 return mAllowLongPress;
1551 }
1552
Michael Jurka0142d492010-08-25 17:46:15 -07001553 /**
1554 * Set true to allow long-press events to be triggered, usually checked by
1555 * {@link Launcher} to accept or block dpad-initiated long-presses.
1556 */
1557 public void setAllowLongPress(boolean allowLongPress) {
1558 mAllowLongPress = allowLongPress;
1559 }
1560
Winson Chung321e9ee2010-08-09 13:37:56 -07001561 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001562 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001563
1564 SavedState(Parcelable superState) {
1565 super(superState);
1566 }
1567
1568 private SavedState(Parcel in) {
1569 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001570 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 }
1572
1573 @Override
1574 public void writeToParcel(Parcel out, int flags) {
1575 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001576 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001577 }
1578
1579 public static final Parcelable.Creator<SavedState> CREATOR =
1580 new Parcelable.Creator<SavedState>() {
1581 public SavedState createFromParcel(Parcel in) {
1582 return new SavedState(in);
1583 }
1584
1585 public SavedState[] newArray(int size) {
1586 return new SavedState[size];
1587 }
1588 };
1589 }
1590
Winson Chungf314b0e2011-08-16 11:54:27 -07001591 protected void loadAssociatedPages(int page) {
1592 loadAssociatedPages(page, false);
1593 }
1594 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001595 if (mContentIsRefreshable) {
1596 final int count = getChildCount();
1597 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001598 int lowerPageBound = getAssociatedLowerPageBound(page);
1599 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1601 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001602 // First, clear any pages that should no longer be loaded
1603 for (int i = 0; i < count; ++i) {
1604 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001605 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001606 if (layout.getPageChildCount() > 0) {
1607 layout.removeAllViewsOnPage();
1608 }
1609 mDirtyPageContent.set(i, true);
1610 }
1611 }
1612 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001613 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001614 if ((i != page) && immediateAndOnly) {
1615 continue;
1616 }
Michael Jurka0142d492010-08-25 17:46:15 -07001617 if (lowerPageBound <= i && i <= upperPageBound) {
1618 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001619 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001620 mDirtyPageContent.set(i, false);
1621 }
Winson Chung86f77532010-08-24 11:08:22 -07001622 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001623 }
1624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001625 }
1626 }
1627
Winson Chunge3193b92010-09-10 11:44:42 -07001628 protected int getAssociatedLowerPageBound(int page) {
1629 return Math.max(0, page - 1);
1630 }
1631 protected int getAssociatedUpperPageBound(int page) {
1632 final int count = getChildCount();
1633 return Math.min(page + 1, count - 1);
1634 }
1635
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001636 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001637 if (isChoiceMode(CHOICE_MODE_NONE)) {
1638 mChoiceMode = mode;
1639 mActionMode = startActionMode(callback);
1640 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001641 }
1642
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001643 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001644 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001645 mChoiceMode = CHOICE_MODE_NONE;
1646 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001647 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001648 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001649 }
1650 }
1651
1652 protected boolean isChoiceMode(int mode) {
1653 return mChoiceMode == mode;
1654 }
1655
1656 protected ArrayList<Checkable> getCheckedGrandchildren() {
1657 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1658 final int childCount = getChildCount();
1659 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001660 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001661 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001662 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001663 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001664 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001665 checked.add((Checkable) v);
1666 }
1667 }
1668 }
1669 return checked;
1670 }
1671
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001672 /**
1673 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1674 * Otherwise, returns null.
1675 */
1676 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001677 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001678 final int childCount = getChildCount();
1679 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001680 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001681 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001682 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001683 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001684 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1685 return (Checkable) v;
1686 }
1687 }
1688 }
1689 }
1690 return null;
1691 }
1692
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001693 protected void resetCheckedGrandchildren() {
1694 // loop through children, and set all of their children to _not_ be checked
1695 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1696 for (int i = 0; i < checked.size(); ++i) {
1697 final Checkable c = checked.get(i);
1698 c.setChecked(false);
1699 }
1700 }
1701
Winson Chung86f77532010-08-24 11:08:22 -07001702 /**
1703 * This method is called ONLY to synchronize the number of pages that the paged view has.
1704 * To actually fill the pages with information, implement syncPageItems() below. It is
1705 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1706 * and therefore, individual page items do not need to be updated in this method.
1707 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001708 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001709
1710 /**
1711 * This method is called to synchronize the items that are on a particular page. If views on
1712 * the page can be reused, then they should be updated within this method.
1713 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001714 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001715
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001716 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001717 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001718 }
1719 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001720 invalidatePageData(currentPage, false);
1721 }
1722 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001723 if (!mIsDataReady) {
1724 return;
1725 }
1726
Michael Jurka0142d492010-08-25 17:46:15 -07001727 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001728 // Force all scrolling-related behavior to end
1729 mScroller.forceFinished(true);
1730 mNextPage = INVALID_PAGE;
1731
Michael Jurka0142d492010-08-25 17:46:15 -07001732 // Update all the pages
1733 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001734
Winson Chung5a808352011-06-27 19:08:49 -07001735 // We must force a measure after we've loaded the pages to update the content width and
1736 // to determine the full scroll width
1737 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1738 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1739
1740 // Set a new page as the current page if necessary
1741 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001742 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001743 }
1744
Michael Jurka0142d492010-08-25 17:46:15 -07001745 // Mark each of the pages as dirty
1746 final int count = getChildCount();
1747 mDirtyPageContent.clear();
1748 for (int i = 0; i < count; ++i) {
1749 mDirtyPageContent.add(true);
1750 }
1751
1752 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001753 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001754 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001755 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001756 }
Winson Chung007c6982011-06-14 13:27:53 -07001757
Michael Jurkaafaa0502011-12-13 18:22:50 -08001758 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001759 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1760 // found
1761 if (mHasScrollIndicator && mScrollIndicator == null) {
1762 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001763 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001764 mHasScrollIndicator = mScrollIndicator != null;
1765 if (mHasScrollIndicator) {
1766 mScrollIndicator.setVisibility(View.VISIBLE);
1767 }
1768 }
1769 return mScrollIndicator;
1770 }
1771
1772 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001773 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001774 }
1775
Winson Chung5a808352011-06-27 19:08:49 -07001776 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1777 @Override
1778 public void run() {
1779 hideScrollingIndicator(false);
1780 }
1781 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001782 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001783 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001784 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001785 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001786 }
1787
Michael Jurka430e8a52011-08-08 15:52:14 -07001788 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001789 if (getChildCount() <= 1) return;
1790 if (!isScrollingIndicatorEnabled()) return;
1791
1792 getScrollingIndicator();
1793 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001794 // Fade the indicator in
1795 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001796 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001797 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001798 if (immediately) {
1799 mScrollIndicator.setAlpha(1f);
1800 } else {
1801 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1802 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1803 mScrollIndicatorAnimator.start();
1804 }
Winson Chung007c6982011-06-14 13:27:53 -07001805 }
1806 }
1807
Adam Cohen21b41102011-11-01 17:29:52 -07001808 protected void cancelScrollingIndicatorAnimations() {
1809 if (mScrollIndicatorAnimator != null) {
1810 mScrollIndicatorAnimator.cancel();
1811 }
1812 }
1813
Winson Chung007c6982011-06-14 13:27:53 -07001814 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001815 if (getChildCount() <= 1) return;
1816 if (!isScrollingIndicatorEnabled()) return;
1817
1818 getScrollingIndicator();
1819 if (mScrollIndicator != null) {
1820 // Fade the indicator out
1821 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001822 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001823 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001824 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001825 mScrollIndicator.setAlpha(0f);
1826 } else {
1827 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1828 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1829 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1830 private boolean cancelled = false;
1831 @Override
1832 public void onAnimationCancel(android.animation.Animator animation) {
1833 cancelled = true;
1834 }
1835 @Override
1836 public void onAnimationEnd(Animator animation) {
1837 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001838 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001839 }
1840 }
1841 });
1842 mScrollIndicatorAnimator.start();
1843 }
Winson Chung007c6982011-06-14 13:27:53 -07001844 }
1845 }
1846
Winson Chung32174c82011-07-19 15:47:55 -07001847 /**
1848 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1849 * fill its space on the track or not.
1850 */
1851 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001852 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001853 }
1854
Winson Chung007c6982011-06-14 13:27:53 -07001855 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001856 if (getChildCount() <= 1) return;
1857 if (!isScrollingIndicatorEnabled()) return;
1858
1859 getScrollingIndicator();
1860 if (mScrollIndicator != null) {
1861 updateScrollingIndicatorPosition();
1862 }
1863 }
1864
Winson Chung007c6982011-06-14 13:27:53 -07001865 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001866 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001867 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001868 int numPages = getChildCount();
1869 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001870 int lastChildIndex = Math.max(0, getChildCount() - 1);
1871 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001872 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001873 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1874 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001875
Winson Chungae890b82011-09-13 18:08:54 -07001876 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001877 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001878 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001879 if (hasElasticScrollIndicator()) {
1880 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1881 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1882 mScrollIndicator.requestLayout();
1883 }
1884 } else {
1885 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1886 indicatorPos += indicatorCenterOffset;
1887 }
Winson Chung007c6982011-06-14 13:27:53 -07001888 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001889 mScrollIndicator.invalidate();
1890 }
1891
Winson Chung3ac74c52011-06-30 17:39:37 -07001892 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001893 }
1894
1895 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001896 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001897
1898 /* Accessibility */
1899 @Override
1900 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1901 super.onInitializeAccessibilityNodeInfo(info);
1902 info.setScrollable(true);
1903 }
1904
1905 @Override
1906 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1907 super.onInitializeAccessibilityEvent(event);
1908 event.setScrollable(true);
1909 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1910 event.setFromIndex(mCurrentPage);
1911 event.setToIndex(mCurrentPage);
1912 event.setItemCount(getChildCount());
1913 }
1914 }
1915
Winson Chung6a0f57d2011-06-29 20:10:49 -07001916 protected String getCurrentPageDescription() {
1917 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1918 return String.format(mContext.getString(R.string.default_scroll_format),
1919 page + 1, getChildCount());
1920 }
Winson Chungd11265e2011-08-30 13:37:23 -07001921
1922 @Override
1923 public boolean onHoverEvent(android.view.MotionEvent event) {
1924 return true;
1925 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001926}