blob: 49c25b551cb85249658b5628a047e7e8729d1777 [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;
64 // The min drag distance to trigger a page shift (regardless of velocity)
65 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Adam Cohene0f66b52010-11-23 15:06:07 -080067 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070068 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb5ba0972011-09-07 18:02:31 -070070 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070071 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080072 private static final int MINIMUM_SNAP_VELOCITY = 2200;
73 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Michael Jurka0142d492010-08-25 17:46:15 -070076 // the velocity at which a fling gesture will cause us to snap to the next page
77 protected int mSnapVelocity = 500;
78
Adam Cohenb5ba0972011-09-07 18:02:31 -070079 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected float mSmoothingTime;
81 protected float mTouchX;
82
83 protected boolean mFirstLayout = true;
84
85 protected int mCurrentPage;
86 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080087 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070089 private VelocityTracker mVelocityTracker;
90
91 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080092 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080093 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080094 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080095 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070096 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -070097 private int[] mChildOffsets;
98 private int[] mChildRelativeOffsets;
99 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700100
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected final static int TOUCH_STATE_REST = 0;
102 protected final static int TOUCH_STATE_SCROLLING = 1;
103 protected final static int TOUCH_STATE_PREV_PAGE = 2;
104 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700105 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700108 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurka7426c422010-11-11 15:23:47 -0800112 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Michael Jurka7426c422010-11-11 15:23:47 -0800114 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115 private int mPagingTouchSlop;
116 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800117 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700118 protected int mPageSpacing;
119 protected int mPageLayoutPaddingTop;
120 protected int mPageLayoutPaddingBottom;
121 protected int mPageLayoutPaddingLeft;
122 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700123 protected int mPageLayoutWidthGap;
124 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700125 protected int mCellCountX = 0;
126 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700127 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800128 protected boolean mAllowOverScroll = true;
129 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800130 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Adam Cohenebea84d2011-11-09 17:20:41 -0800132 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
133 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
134 // the screens from continuing to translate beyond the normal bounds.
135 protected int mOverScrollX;
136
Michael Jurka8c920dd2011-01-20 14:16:56 -0800137 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800138 protected float mLayoutScale = 1.0f;
139
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Michael Jurka5f1c5092010-09-03 14:15:02 -0700142 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700143
Winson Chung86f77532010-08-24 11:08:22 -0700144 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700145
Winson Chung86f77532010-08-24 11:08:22 -0700146 private ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700148 // choice modes
149 protected static final int CHOICE_MODE_NONE = 0;
150 protected static final int CHOICE_MODE_SINGLE = 1;
151 // Multiple selection mode is not supported by all Launcher actions atm
152 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700153
Michael Jurkae17e19c2010-09-28 11:01:39 -0700154 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700155 private ActionMode mActionMode;
156
Michael Jurka0142d492010-08-25 17:46:15 -0700157 // If true, syncPages and syncPageItems will be called to refresh pages
158 protected boolean mContentIsRefreshable = true;
159
160 // If true, modify alpha of neighboring pages as user scrolls left/right
161 protected boolean mFadeInAdjacentScreens = true;
162
163 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
164 // to switch to a new page
165 protected boolean mUsePagingTouchSlop = true;
166
167 // If true, the subclass should directly update mScrollX itself in its computeScroll method
168 // (SmoothPagedView does this)
169 protected boolean mDeferScrollUpdate = false;
170
Patrick Dubroy1262e362010-10-06 15:49:50 -0700171 protected boolean mIsPageMoving = false;
172
Winson Chungf0ea4d32011-06-06 14:27:16 -0700173 // All syncs and layout passes are deferred until data is ready.
174 protected boolean mIsDataReady = false;
175
Winson Chung007c6982011-06-14 13:27:53 -0700176 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700177 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700178 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700179 private int mScrollIndicatorPaddingLeft;
180 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700181 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700182 protected static final int sScrollIndicatorFadeInDuration = 150;
183 protected static final int sScrollIndicatorFadeOutDuration = 650;
184 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700185
Winson Chungb44b5242011-06-13 11:32:14 -0700186 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700187 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public interface PageSwitchListener {
190 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 public PagedView(Context context) {
194 this(context, null);
195 }
196
197 public PagedView(Context context, AttributeSet attrs) {
198 this(context, attrs, 0);
199 }
200
201 public PagedView(Context context, AttributeSet attrs, int defStyle) {
202 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700203 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700204
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 TypedArray a = context.obtainStyledAttributes(attrs,
206 R.styleable.PagedView, defStyle, 0);
207 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
208 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700214 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800215 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700216 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700218 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700220 mScrollIndicatorPaddingLeft =
221 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
222 mScrollIndicatorPaddingRight =
223 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700224 a.recycle();
225
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700227 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
230 /**
231 * Initializes various states for this workspace.
232 */
Michael Jurka0142d492010-08-25 17:46:15 -0700233 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700234 mDirtyPageContent = new ArrayList<Boolean>();
235 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800236 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700237 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700238 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700239
240 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
241 mTouchSlop = configuration.getScaledTouchSlop();
242 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
243 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700244 mDensity = getResources().getDisplayMetrics().density;
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246
Winson Chung86f77532010-08-24 11:08:22 -0700247 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
248 mPageSwitchListener = pageSwitchListener;
249 if (mPageSwitchListener != null) {
250 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 }
252 }
253
254 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700255 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
256 * out pages.
257 */
258 protected void setDataIsReady() {
259 mIsDataReady = true;
260 }
261 protected boolean isDataReady() {
262 return mIsDataReady;
263 }
264
265 /**
Winson Chung86f77532010-08-24 11:08:22 -0700266 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 *
Winson Chung86f77532010-08-24 11:08:22 -0700268 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 */
Winson Chung86f77532010-08-24 11:08:22 -0700270 int getCurrentPage() {
271 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 }
273
Winson Chung86f77532010-08-24 11:08:22 -0700274 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 return getChildCount();
276 }
277
Winson Chung86f77532010-08-24 11:08:22 -0700278 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 return getChildAt(index);
280 }
281
Adam Cohenae4f1552011-10-20 00:15:42 -0700282 protected int indexToPage(int index) {
283 return index;
284 }
285
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800287 * Updates the scroll of the current page immediately to its final scroll position. We use this
288 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
289 * the previous tab page.
290 */
291 protected void updateCurrentPageScroll() {
292 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
293 scrollTo(newX, 0);
294 mScroller.setFinalX(newX);
295 }
296
297 /**
Winson Chung86f77532010-08-24 11:08:22 -0700298 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 */
Winson Chung86f77532010-08-24 11:08:22 -0700300 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800301 if (!mScroller.isFinished()) {
302 mScroller.abortAnimation();
303 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800304 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
305 // the default
306 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800307 return;
308 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700309
Winson Chung86f77532010-08-24 11:08:22 -0700310 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800311 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700312 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700313 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800314 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 }
316
Michael Jurka0142d492010-08-25 17:46:15 -0700317 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700318 if (mPageSwitchListener != null) {
319 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
321 }
322
Michael Jurkace7e05f2011-02-01 22:02:35 -0800323 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700324 if (!mIsPageMoving) {
325 mIsPageMoving = true;
326 onPageBeginMoving();
327 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700328 }
329
Michael Jurkace7e05f2011-02-01 22:02:35 -0800330 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700331 if (mIsPageMoving) {
332 mIsPageMoving = false;
333 onPageEndMoving();
334 }
Michael Jurka0142d492010-08-25 17:46:15 -0700335 }
336
Adam Cohen26976d92011-03-22 15:33:33 -0700337 protected boolean isPageMoving() {
338 return mIsPageMoving;
339 }
340
Michael Jurka0142d492010-08-25 17:46:15 -0700341 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700342 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700343 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700344 }
345
346 // a method that subclasses can override to add behavior
347 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700348 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700349 }
350
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 /**
Winson Chung86f77532010-08-24 11:08:22 -0700352 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 *
354 * @param l The listener used to respond to long clicks.
355 */
356 @Override
357 public void setOnLongClickListener(OnLongClickListener l) {
358 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700359 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700361 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 }
363 }
364
365 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800366 public void scrollBy(int x, int y) {
367 scrollTo(mUnboundedScrollX + x, mScrollY + y);
368 }
369
370 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700371 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800372 mUnboundedScrollX = x;
373
374 if (x < 0) {
375 super.scrollTo(0, y);
376 if (mAllowOverScroll) {
377 overScroll(x);
378 }
379 } else if (x > mMaxScrollX) {
380 super.scrollTo(mMaxScrollX, y);
381 if (mAllowOverScroll) {
382 overScroll(x - mMaxScrollX);
383 }
384 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800385 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800386 super.scrollTo(x, y);
387 }
388
Michael Jurka0142d492010-08-25 17:46:15 -0700389 mTouchX = x;
390 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
391 }
392
393 // we moved this functionality to a helper function so SmoothPagedView can reuse it
394 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700396 // Don't bother scrolling if the page does not need to be moved
397 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700398 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
399 }
Michael Jurka0142d492010-08-25 17:46:15 -0700400 invalidate();
401 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700402 } else if (mNextPage != INVALID_PAGE) {
403 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700404 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700405 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700406
407 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700408 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700409 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700410 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700411 }
412
Adam Cohen73aa9752010-11-24 16:26:58 -0800413 // We don't want to trigger a page end moving unless the page has settled
414 // and the user has stopped scrolling
415 if (mTouchState == TOUCH_STATE_REST) {
416 pageEndMoving();
417 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700418
419 // Notify the user when the page changes
420 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
421 AccessibilityEvent ev =
422 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
423 ev.getText().add(getCurrentPageDescription());
424 sendAccessibilityEventUnchecked(ev);
425 }
Michael Jurka0142d492010-08-25 17:46:15 -0700426 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
Michael Jurka0142d492010-08-25 17:46:15 -0700428 return false;
429 }
430
431 @Override
432 public void computeScroll() {
433 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
435
436 @Override
437 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700438 if (!mIsDataReady) {
439 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
440 return;
441 }
442
Winson Chung321e9ee2010-08-09 13:37:56 -0700443 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
444 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
445 if (widthMode != MeasureSpec.EXACTLY) {
446 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
447 }
448
Adam Lesinski6b879f02010-11-04 16:15:23 -0700449 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
450 * of the All apps view on XLarge displays to not take up more space then it needs. Width
451 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
452 * each page to have the same width.
453 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700455 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
456 int maxChildHeight = 0;
457
458 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700459 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700460
Michael Jurka36fcb742011-04-06 17:08:58 -0700461
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 final int childCount = getChildCount();
466 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700467 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700468 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700469 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
470
471 int childWidthMode;
472 if (lp.width == LayoutParams.WRAP_CONTENT) {
473 childWidthMode = MeasureSpec.AT_MOST;
474 } else {
475 childWidthMode = MeasureSpec.EXACTLY;
476 }
477
478 int childHeightMode;
479 if (lp.height == LayoutParams.WRAP_CONTENT) {
480 childHeightMode = MeasureSpec.AT_MOST;
481 } else {
482 childHeightMode = MeasureSpec.EXACTLY;
483 }
484
485 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700486 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700487 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700488 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700489
490 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700491 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700492 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
493 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700494 }
495
496 if (heightMode == MeasureSpec.AT_MOST) {
497 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
Winson Chungae890b82011-09-13 18:08:54 -0700499
Winson Chungae890b82011-09-13 18:08:54 -0700500 setMeasuredDimension(widthSize, heightSize);
501
Adam Cohen25b29952011-11-02 14:49:06 -0700502 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
503 // We also wait until we set the measured dimensions before flushing the cache as well, to
504 // ensure that the cache is filled with good values.
505 invalidateCachedOffsets();
506 updateScrollingIndicatorPosition();
507
Adam Cohenfaa28302010-11-19 12:02:18 -0800508 if (childCount > 0) {
509 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
510 } else {
511 mMaxScrollX = 0;
512 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 }
514
Michael Jurka8c920dd2011-01-20 14:16:56 -0800515 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800516 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
517 int delta = newX - mScrollX;
518
Michael Jurka8c920dd2011-01-20 14:16:56 -0800519 final int pageCount = getChildCount();
520 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700521 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800522 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800523 }
524 setCurrentPage(newCurrentPage);
525 }
526
Michael Jurka8c920dd2011-01-20 14:16:56 -0800527 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
528 // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and
Michael Jurkad3ef3062010-11-23 16:23:58 -0800529 // tightens the layout accordingly
530 public void setLayoutScale(float childrenScale) {
531 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700532 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533
534 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
535 int childCount = getChildCount();
536 float childrenX[] = new float[childCount];
537 float childrenY[] = new float[childCount];
538 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700539 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800540 childrenX[i] = child.getX();
541 childrenY[i] = child.getY();
542 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700543 // Trigger a full re-layout (never just call onLayout directly!)
544 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
545 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
546 requestLayout();
547 measure(widthSpec, heightSpec);
548 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800549 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700550 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800551 child.setX(childrenX[i]);
552 child.setY(childrenY[i]);
553 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700554
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 // Also, the page offset has changed (since the pages are now smaller);
556 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800557 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558 }
559
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700561 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700562 if (!mIsDataReady) {
563 return;
564 }
565
Winson Chung785d2eb2011-04-14 16:08:02 -0700566 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700567 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 final int childCount = getChildCount();
569 int childLeft = 0;
570 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700571 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
572 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700573 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700574
575 // Calculate the variable page spacing if necessary
576 if (mPageSpacing < 0) {
577 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
578 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 }
580
581 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700582 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800584 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700585 final int childHeight = child.getMeasuredHeight();
586 int childTop = mPaddingTop;
587 if (mCenterPagesVertically) {
588 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
589 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590
Winson Chung785d2eb2011-04-14 16:08:02 -0700591 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700592 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800593 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700594 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 }
596 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700597
598 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
599 setHorizontalScrollBarEnabled(false);
600 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
601 scrollTo(newX, 0);
602 mScroller.setFinalX(newX);
603 setHorizontalScrollBarEnabled(true);
604 mFirstLayout = false;
605 }
606
Michael Jurkad3ef3062010-11-23 16:23:58 -0800607 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
608 mFirstLayout = false;
609 }
Winson Chunge3193b92010-09-10 11:44:42 -0700610 }
611
Adam Cohenf34bab52010-09-30 14:11:56 -0700612 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700613 if (isScrollingIndicatorEnabled()) {
614 updateScrollingIndicator();
615 }
616 if (mFadeInAdjacentScreens) {
617 for (int i = 0; i < getChildCount(); i++) {
618 View child = getChildAt(i);
619 if (child != null) {
620 float scrollProgress = getScrollProgress(screenCenter, child, i);
621 float alpha = 1 - Math.abs(scrollProgress);
622 child.setFastAlpha(alpha);
623 child.fastInvalidate();
624 }
625 }
626 invalidate();
627 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700628 }
629
Winson Chunge3193b92010-09-10 11:44:42 -0700630 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700631 protected void onViewAdded(View child) {
632 super.onViewAdded(child);
633
634 // This ensures that when children are added, they get the correct transforms / alphas
635 // in accordance with any scroll effects.
636 mForceScreenScrolled = true;
637 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700638 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700639 }
640
Adam Cohen73894962011-10-31 13:17:17 -0700641 protected void invalidateCachedOffsets() {
642 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700643 if (count == 0) {
644 mChildOffsets = null;
645 mChildRelativeOffsets = null;
646 mChildOffsetsWithLayoutScale = null;
647 return;
648 }
Adam Cohen73894962011-10-31 13:17:17 -0700649
650 mChildOffsets = new int[count];
651 mChildRelativeOffsets = new int[count];
652 mChildOffsetsWithLayoutScale = new int[count];
653 for (int i = 0; i < count; i++) {
654 mChildOffsets[i] = -1;
655 mChildRelativeOffsets[i] = -1;
656 mChildOffsetsWithLayoutScale[i] = -1;
657 }
658 }
659
660 protected int getChildOffset(int index) {
661 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
662 mChildOffsets : mChildOffsetsWithLayoutScale;
663
664 if (childOffsets != null && childOffsets[index] != -1) {
665 return childOffsets[index];
666 } else {
667 if (getChildCount() == 0)
668 return 0;
669
670 int offset = getRelativeChildOffset(0);
671 for (int i = 0; i < index; ++i) {
672 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
673 }
674 if (childOffsets != null) {
675 childOffsets[index] = offset;
676 }
677 return offset;
678 }
679 }
680
681 protected int getRelativeChildOffset(int index) {
682 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
683 return mChildRelativeOffsets[index];
684 } else {
685 final int padding = mPaddingLeft + mPaddingRight;
686 final int offset = mPaddingLeft +
687 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
688 if (mChildRelativeOffsets != null) {
689 mChildRelativeOffsets[index] = offset;
690 }
691 return offset;
692 }
693 }
694
695 protected int getScaledRelativeChildOffset(int index) {
696 final int padding = mPaddingLeft + mPaddingRight;
697 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
698 getScaledMeasuredWidth(getPageAt(index))) / 2;
699 return offset;
700 }
701
702 protected int getScaledMeasuredWidth(View child) {
703 // This functions are called enough times that it actually makes a difference in the
704 // profiler -- so just inline the max() here
705 final int measuredWidth = child.getMeasuredWidth();
706 final int minWidth = mMinimumWidth;
707 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
708 return (int) (maxWidth * mLayoutScale + 0.5f);
709 }
710
Michael Jurkadde558b2011-11-09 22:09:06 -0800711 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700712 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700713 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700714 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700715 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700716 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700717 int leftScreen = 0;
718 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700719 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700720 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700721 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700722 }
723 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700724 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700726 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700727 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800728 range[0] = leftScreen;
729 range[1] = rightScreen;
730 } else {
731 range[0] = -1;
732 range[1] = -1;
733 }
734 }
735
736 @Override
737 protected void dispatchDraw(Canvas canvas) {
738 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800739 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
740 // it is equal to the scaled overscroll position.
741 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800742
743 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
744 screenScrolled(screenCenter);
745 mLastScreenCenter = screenCenter;
746 mForceScreenScrolled = false;
747 }
748
749 // Find out which screens are visible; as an optimization we only call draw on them
750 final int pageCount = getChildCount();
751 if (pageCount > 0) {
752 getVisiblePages(mTempVisiblePagesRange);
753 final int leftScreen = mTempVisiblePagesRange[0];
754 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800755 if (leftScreen != -1 && rightScreen != -1) {
756 final long drawingTime = getDrawingTime();
757 // Clip to the bounds
758 canvas.save();
759 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
760 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700761
Winson Chungc6f10b92011-11-14 11:39:07 -0800762 for (int i = rightScreen; i >= leftScreen; i--) {
763 drawChild(canvas, getPageAt(i), drawingTime);
764 }
765 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700766 }
Michael Jurka0142d492010-08-25 17:46:15 -0700767 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
769
770 @Override
771 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700772 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700773 if (page != mCurrentPage || !mScroller.isFinished()) {
774 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 return true;
776 }
777 return false;
778 }
779
780 @Override
781 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700782 int focusablePage;
783 if (mNextPage != INVALID_PAGE) {
784 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700786 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 }
Winson Chung86f77532010-08-24 11:08:22 -0700788 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700789 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700790 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 }
792 return false;
793 }
794
795 @Override
796 public boolean dispatchUnhandledMove(View focused, int direction) {
797 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700798 if (getCurrentPage() > 0) {
799 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 return true;
801 }
802 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700803 if (getCurrentPage() < getPageCount() - 1) {
804 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 return true;
806 }
807 }
808 return super.dispatchUnhandledMove(focused, direction);
809 }
810
811 @Override
812 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700813 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
814 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 }
816 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700817 if (mCurrentPage > 0) {
818 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 }
820 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700821 if (mCurrentPage < getPageCount() - 1) {
822 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 }
824 }
825 }
826
827 /**
828 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700829 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 *
Winson Chung86f77532010-08-24 11:08:22 -0700831 * This happens when live folders requery, and if they're off page, they
832 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 */
834 @Override
835 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700836 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 View v = focused;
838 while (true) {
839 if (v == current) {
840 super.focusableViewAvailable(focused);
841 return;
842 }
843 if (v == this) {
844 return;
845 }
846 ViewParent parent = v.getParent();
847 if (parent instanceof View) {
848 v = (View)v.getParent();
849 } else {
850 return;
851 }
852 }
853 }
854
855 /**
856 * {@inheritDoc}
857 */
858 @Override
859 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
860 if (disallowIntercept) {
861 // We need to make sure to cancel our long press if
862 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700863 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700864 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866 super.requestDisallowInterceptTouchEvent(disallowIntercept);
867 }
868
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800869 /**
870 * Return true if a tap at (x, y) should trigger a flip to the previous page.
871 */
872 protected boolean hitsPreviousPage(float x, float y) {
873 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
874 }
875
876 /**
877 * Return true if a tap at (x, y) should trigger a flip to the next page.
878 */
879 protected boolean hitsNextPage(float x, float y) {
880 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
881 }
882
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 @Override
884 public boolean onInterceptTouchEvent(MotionEvent ev) {
885 /*
886 * This method JUST determines whether we want to intercept the motion.
887 * If we return true, onTouchEvent will be called and we do the actual
888 * scrolling there.
889 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800890 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700891
Winson Chung45e1d6e2010-11-09 17:19:49 -0800892 // Skip touch handling if there are no pages to swipe
893 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
894
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 /*
896 * Shortcut the most recurring case: the user is in the dragging
897 * state and he is moving his finger. We want to intercept this
898 * motion.
899 */
900 final int action = ev.getAction();
901 if ((action == MotionEvent.ACTION_MOVE) &&
902 (mTouchState == TOUCH_STATE_SCROLLING)) {
903 return true;
904 }
905
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 switch (action & MotionEvent.ACTION_MASK) {
907 case MotionEvent.ACTION_MOVE: {
908 /*
909 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
910 * whether the user has moved far enough from his original down touch.
911 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700912 if (mActivePointerId != INVALID_POINTER) {
913 determineScrollingStart(ev);
914 break;
915 }
916 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
917 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
918 // i.e. fall through to the next case (don't break)
919 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
920 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 }
922
923 case MotionEvent.ACTION_DOWN: {
924 final float x = ev.getX();
925 final float y = ev.getY();
926 // Remember location of down touch
927 mDownMotionX = x;
928 mLastMotionX = x;
929 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800930 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800931 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 mActivePointerId = ev.getPointerId(0);
933 mAllowLongPress = true;
934
935 /*
936 * If being flinged and user touches the screen, initiate drag;
937 * otherwise don't. mScroller.isFinished should be false when
938 * being flinged.
939 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700940 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700941 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
942 if (finishedScrolling) {
943 mTouchState = TOUCH_STATE_REST;
944 mScroller.abortAnimation();
945 } else {
946 mTouchState = TOUCH_STATE_SCROLLING;
947 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700948
Winson Chung86f77532010-08-24 11:08:22 -0700949 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800951 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800953 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800955 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 mTouchState = TOUCH_STATE_NEXT_PAGE;
957 }
958 }
959 }
960 break;
961 }
962
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800964 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 mTouchState = TOUCH_STATE_REST;
966 mAllowLongPress = false;
967 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800968 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 break;
970
971 case MotionEvent.ACTION_POINTER_UP:
972 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800973 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 break;
975 }
976
977 /*
978 * The only time we want to intercept motion events is if we are in the
979 * drag mode.
980 */
981 return mTouchState != TOUCH_STATE_REST;
982 }
983
Winson Chung80baf5a2010-08-09 16:03:15 -0700984 protected void animateClickFeedback(View v, final Runnable r) {
985 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800986 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
987 loadAnimator(mContext, R.anim.paged_view_click_feedback);
988 anim.setTarget(v);
989 anim.addListener(new AnimatorListenerAdapter() {
990 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700991 r.run();
992 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700993 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800994 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700995 }
996
Adam Cohenf8d28232011-02-01 21:47:00 -0800997 protected void determineScrollingStart(MotionEvent ev) {
998 determineScrollingStart(ev, 1.0f);
999 }
1000
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 /*
1002 * Determines if we should change the touch state to start scrolling after the
1003 * user moves their touch point too far.
1004 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001005 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 /*
1007 * Locally do absolute value. mLastMotionX is set to the y value
1008 * of the down event.
1009 */
1010 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001011 if (pointerIndex == -1) {
1012 return;
1013 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001014 final float x = ev.getX(pointerIndex);
1015 final float y = ev.getY(pointerIndex);
1016 final int xDiff = (int) Math.abs(x - mLastMotionX);
1017 final int yDiff = (int) Math.abs(y - mLastMotionY);
1018
Adam Cohenf8d28232011-02-01 21:47:00 -08001019 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 boolean xPaged = xDiff > mPagingTouchSlop;
1021 boolean xMoved = xDiff > touchSlop;
1022 boolean yMoved = yDiff > touchSlop;
1023
Adam Cohenf8d28232011-02-01 21:47:00 -08001024 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001025 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001026 // Scroll if the user moved far enough along the X axis
1027 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001028 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001030 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001031 mTouchX = mScrollX;
1032 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1033 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 }
1035 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001036 cancelCurrentPageLongPress();
1037 }
1038 }
1039
1040 protected void cancelCurrentPageLongPress() {
1041 if (mAllowLongPress) {
1042 mAllowLongPress = false;
1043 // Try canceling the long press. It could also have been scheduled
1044 // by a distant descendant, so use the mAllowLongPress flag to block
1045 // everything
1046 final View currentPage = getPageAt(mCurrentPage);
1047 if (currentPage != null) {
1048 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 }
1050 }
1051 }
1052
Adam Cohenb5ba0972011-09-07 18:02:31 -07001053 protected float getScrollProgress(int screenCenter, View v, int page) {
1054 final int halfScreenSize = getMeasuredWidth() / 2;
1055
1056 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1057 int delta = screenCenter - (getChildOffset(page) -
1058 getRelativeChildOffset(page) + halfScreenSize);
1059
1060 float scrollProgress = delta / (totalDistance * 1.0f);
1061 scrollProgress = Math.min(scrollProgress, 1.0f);
1062 scrollProgress = Math.max(scrollProgress, -1.0f);
1063 return scrollProgress;
1064 }
1065
Adam Cohene0f66b52010-11-23 15:06:07 -08001066 // This curve determines how the effect of scrolling over the limits of the page dimishes
1067 // as the user pulls further and further from the bounds
1068 private float overScrollInfluenceCurve(float f) {
1069 f -= 1.0f;
1070 return f * f * f + 1.0f;
1071 }
1072
Adam Cohenb5ba0972011-09-07 18:02:31 -07001073 protected void acceleratedOverScroll(float amount) {
1074 int screenSize = getMeasuredWidth();
1075
1076 // We want to reach the max over scroll effect when the user has
1077 // over scrolled half the size of the screen
1078 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1079
1080 if (f == 0) return;
1081
1082 // Clamp this factor, f, to -1 < f < 1
1083 if (Math.abs(f) >= 1) {
1084 f /= Math.abs(f);
1085 }
1086
1087 int overScrollAmount = (int) Math.round(f * screenSize);
1088 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001089 mOverScrollX = overScrollAmount;
1090 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001091 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001092 mOverScrollX = mMaxScrollX + overScrollAmount;
1093 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001094 }
1095 invalidate();
1096 }
1097
1098 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001099 int screenSize = getMeasuredWidth();
1100
1101 float f = (amount / screenSize);
1102
1103 if (f == 0) return;
1104 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1105
Adam Cohen7bfc9792011-01-28 13:52:37 -08001106 // Clamp this factor, f, to -1 < f < 1
1107 if (Math.abs(f) >= 1) {
1108 f /= Math.abs(f);
1109 }
1110
Adam Cohene0f66b52010-11-23 15:06:07 -08001111 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001112 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001113 mOverScrollX = overScrollAmount;
1114 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001115 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001116 mOverScrollX = mMaxScrollX + overScrollAmount;
1117 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001118 }
1119 invalidate();
1120 }
1121
Adam Cohenb5ba0972011-09-07 18:02:31 -07001122 protected void overScroll(float amount) {
1123 dampedOverScroll(amount);
1124 }
1125
Michael Jurkac5b262c2011-01-12 20:24:50 -08001126 protected float maxOverScroll() {
1127 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001128 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001129 float f = 1.0f;
1130 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1131 return OVERSCROLL_DAMP_FACTOR * f;
1132 }
1133
Winson Chung321e9ee2010-08-09 13:37:56 -07001134 @Override
1135 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001136 // Skip touch handling if there are no pages to swipe
1137 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1138
Michael Jurkab8f06722010-10-10 15:58:46 -07001139 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001140
1141 final int action = ev.getAction();
1142
1143 switch (action & MotionEvent.ACTION_MASK) {
1144 case MotionEvent.ACTION_DOWN:
1145 /*
1146 * If being flinged and user touches, stop the fling. isFinished
1147 * will be false if being flinged.
1148 */
1149 if (!mScroller.isFinished()) {
1150 mScroller.abortAnimation();
1151 }
1152
1153 // Remember where the motion event started
1154 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001155 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001156 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001158 if (mTouchState == TOUCH_STATE_SCROLLING) {
1159 pageBeginMoving();
1160 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 break;
1162
1163 case MotionEvent.ACTION_MOVE:
1164 if (mTouchState == TOUCH_STATE_SCROLLING) {
1165 // Scroll to follow the motion event
1166 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1167 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001168 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001169
Adam Cohenaefd4e12011-02-14 16:39:38 -08001170 mTotalMotionX += Math.abs(deltaX);
1171
Winson Chungc0844aa2011-02-02 15:25:58 -08001172 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1173 // keep the remainder because we are actually testing if we've moved from the last
1174 // scrolled position (which is discrete).
1175 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001176 mTouchX += deltaX;
1177 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1178 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001179 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001180 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001181 } else {
1182 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001184 mLastMotionX = x;
1185 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 } else {
1187 awakenScrollBars();
1188 }
Adam Cohen564976a2010-10-13 18:52:07 -07001189 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 determineScrollingStart(ev);
1191 }
1192 break;
1193
1194 case MotionEvent.ACTION_UP:
1195 if (mTouchState == TOUCH_STATE_SCROLLING) {
1196 final int activePointerId = mActivePointerId;
1197 final int pointerIndex = ev.findPointerIndex(activePointerId);
1198 final float x = ev.getX(pointerIndex);
1199 final VelocityTracker velocityTracker = mVelocityTracker;
1200 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1201 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001202 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001203 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001204 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001205
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001206 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1207
Adam Cohenaefd4e12011-02-14 16:39:38 -08001208 // In the case that the page is moved far to one direction and then is flung
1209 // in the opposite direction, we use a threshold to determine whether we should
1210 // just return to the starting page, or if we should skip one further.
1211 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001212 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001213 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001214 Math.signum(velocityX) != Math.signum(deltaX)) {
1215 returnToOriginalPage = true;
1216 }
1217
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001218 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001219 Math.abs(velocityX) > snapVelocity;
1220
1221 int finalPage;
1222 // We give flings precedence over large moves, which is why we short-circuit our
1223 // test for a large move if a fling has been registered. That is, a large
1224 // move to the left and fling to the right will register as a fling to the right.
1225 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1226 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1227 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1228 snapToPageWithVelocity(finalPage, velocityX);
1229 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1230 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001231 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001232 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1233 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 } else {
1235 snapToDestination();
1236 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001237 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 // at this point we have not moved beyond the touch slop
1239 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1240 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001241 int nextPage = Math.max(0, mCurrentPage - 1);
1242 if (nextPage != mCurrentPage) {
1243 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001244 } else {
1245 snapToDestination();
1246 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001247 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 // at this point we have not moved beyond the touch slop
1249 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1250 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001251 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1252 if (nextPage != mCurrentPage) {
1253 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001254 } else {
1255 snapToDestination();
1256 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001257 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001258 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 }
1260 mTouchState = TOUCH_STATE_REST;
1261 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001262 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 break;
1264
1265 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001266 if (mTouchState == TOUCH_STATE_SCROLLING) {
1267 snapToDestination();
1268 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 mTouchState = TOUCH_STATE_REST;
1270 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001271 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 break;
1273
1274 case MotionEvent.ACTION_POINTER_UP:
1275 onSecondaryPointerUp(ev);
1276 break;
1277 }
1278
1279 return true;
1280 }
1281
Winson Chung185d7162011-02-28 13:47:29 -08001282 @Override
1283 public boolean onGenericMotionEvent(MotionEvent event) {
1284 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1285 switch (event.getAction()) {
1286 case MotionEvent.ACTION_SCROLL: {
1287 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1288 final float vscroll;
1289 final float hscroll;
1290 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1291 vscroll = 0;
1292 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1293 } else {
1294 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1295 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1296 }
1297 if (hscroll != 0 || vscroll != 0) {
1298 if (hscroll > 0 || vscroll > 0) {
1299 scrollRight();
1300 } else {
1301 scrollLeft();
1302 }
1303 return true;
1304 }
1305 }
1306 }
1307 }
1308 return super.onGenericMotionEvent(event);
1309 }
1310
Michael Jurkab8f06722010-10-10 15:58:46 -07001311 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1312 if (mVelocityTracker == null) {
1313 mVelocityTracker = VelocityTracker.obtain();
1314 }
1315 mVelocityTracker.addMovement(ev);
1316 }
1317
1318 private void releaseVelocityTracker() {
1319 if (mVelocityTracker != null) {
1320 mVelocityTracker.recycle();
1321 mVelocityTracker = null;
1322 }
1323 }
1324
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 private void onSecondaryPointerUp(MotionEvent ev) {
1326 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1327 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1328 final int pointerId = ev.getPointerId(pointerIndex);
1329 if (pointerId == mActivePointerId) {
1330 // This was our active pointer going up. Choose a new
1331 // active pointer and adjust accordingly.
1332 // TODO: Make this decision more intelligent.
1333 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1334 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1335 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001336 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001337 mActivePointerId = ev.getPointerId(newPointerIndex);
1338 if (mVelocityTracker != null) {
1339 mVelocityTracker.clear();
1340 }
1341 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001342 }
1343
Michael Jurkad771c962011-08-09 15:00:48 -07001344 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001345
1346 @Override
1347 public void requestChildFocus(View child, View focused) {
1348 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001349 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001350 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001351 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 }
1353 }
1354
Winson Chunge3193b92010-09-10 11:44:42 -07001355 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1356 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001357 int left;
1358 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001359 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001360 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001361 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001362 if (left <= relativeOffset && relativeOffset <= right) {
1363 return i;
1364 }
Winson Chunge3193b92010-09-10 11:44:42 -07001365 }
1366 return -1;
1367 }
1368
Winson Chung1908d072011-02-24 18:09:44 -08001369 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001370 // This functions are called enough times that it actually makes a difference in the
1371 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001372 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001373 final int minWidth = mMinimumWidth;
1374 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001375 }
1376
Adam Cohend19d3ca2010-09-15 14:43:42 -07001377 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001378 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 int minDistanceFromScreenCenterIndex = -1;
1380 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1381 final int childCount = getChildCount();
1382 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001383 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001384 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 int halfChildWidth = (childWidth / 2);
1386 int childCenter = getChildOffset(i) + halfChildWidth;
1387 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1388 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1389 minDistanceFromScreenCenter = distanceFromScreenCenter;
1390 minDistanceFromScreenCenterIndex = i;
1391 }
1392 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001393 return minDistanceFromScreenCenterIndex;
1394 }
1395
1396 protected void snapToDestination() {
1397 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 }
1399
Adam Cohene0f66b52010-11-23 15:06:07 -08001400 private static class ScrollInterpolator implements Interpolator {
1401 public ScrollInterpolator() {
1402 }
1403
1404 public float getInterpolation(float t) {
1405 t -= 1.0f;
1406 return t*t*t*t*t + 1;
1407 }
1408 }
1409
1410 // We want the duration of the page snap animation to be influenced by the distance that
1411 // the screen has to travel, however, we don't want this duration to be effected in a
1412 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1413 // of travel has on the overall snap duration.
1414 float distanceInfluenceForSnapDuration(float f) {
1415 f -= 0.5f; // center the values about 0.
1416 f *= 0.3f * Math.PI / 2.0f;
1417 return (float) Math.sin(f);
1418 }
1419
Michael Jurka0142d492010-08-25 17:46:15 -07001420 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001421 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1422 int halfScreenSize = getMeasuredWidth() / 2;
1423
Winson Chung785d2eb2011-04-14 16:08:02 -07001424 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1425 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1426 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001427 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1428 int delta = newX - mUnboundedScrollX;
1429 int duration = 0;
1430
1431 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1432 // If the velocity is low enough, then treat this more as an automatic page advance
1433 // as opposed to an apparent physical response to flinging
1434 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1435 return;
1436 }
1437
1438 // Here we compute a "distance" that will be used in the computation of the overall
1439 // snap duration. This is a function of the actual distance that needs to be traveled;
1440 // we keep this value close to half screen size in order to reduce the variance in snap
1441 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001442 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001443 float distance = halfScreenSize + halfScreenSize *
1444 distanceInfluenceForSnapDuration(distanceRatio);
1445
1446 velocity = Math.abs(velocity);
1447 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1448
1449 // we want the page's snap velocity to approximately match the velocity at which the
1450 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001451 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1452 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001453
1454 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001455 }
1456
1457 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001458 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001459 }
1460
Michael Jurka0142d492010-08-25 17:46:15 -07001461 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001462 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001463
Winson Chung785d2eb2011-04-14 16:08:02 -07001464 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1465 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1466 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001467 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001468 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001469 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001470 snapToPage(whichPage, delta, duration);
1471 }
1472
1473 protected void snapToPage(int whichPage, int delta, int duration) {
1474 mNextPage = whichPage;
1475
1476 View focusedChild = getFocusedChild();
1477 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001478 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001479 focusedChild.clearFocus();
1480 }
1481
1482 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 awakenScrollBars(duration);
1484 if (duration == 0) {
1485 duration = Math.abs(delta);
1486 }
1487
1488 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001489 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001490
Winson Chungb44b5242011-06-13 11:32:14 -07001491 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1492 // loading associated pages until the scroll settles
1493 if (mDeferScrollUpdate) {
1494 loadAssociatedPages(mNextPage);
1495 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001496 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001497 }
Michael Jurka0142d492010-08-25 17:46:15 -07001498 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 invalidate();
1500 }
1501
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 public void scrollLeft() {
1503 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001504 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001506 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 }
1508 }
1509
1510 public void scrollRight() {
1511 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001512 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001514 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 }
1516 }
1517
Winson Chung86f77532010-08-24 11:08:22 -07001518 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001519 int result = -1;
1520 if (v != null) {
1521 ViewParent vp = v.getParent();
1522 int count = getChildCount();
1523 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001524 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 return i;
1526 }
1527 }
1528 }
1529 return result;
1530 }
1531
1532 /**
1533 * @return True is long presses are still allowed for the current touch
1534 */
1535 public boolean allowLongPress() {
1536 return mAllowLongPress;
1537 }
1538
Michael Jurka0142d492010-08-25 17:46:15 -07001539 /**
1540 * Set true to allow long-press events to be triggered, usually checked by
1541 * {@link Launcher} to accept or block dpad-initiated long-presses.
1542 */
1543 public void setAllowLongPress(boolean allowLongPress) {
1544 mAllowLongPress = allowLongPress;
1545 }
1546
Winson Chung321e9ee2010-08-09 13:37:56 -07001547 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001548 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001549
1550 SavedState(Parcelable superState) {
1551 super(superState);
1552 }
1553
1554 private SavedState(Parcel in) {
1555 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001556 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 }
1558
1559 @Override
1560 public void writeToParcel(Parcel out, int flags) {
1561 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001562 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001563 }
1564
1565 public static final Parcelable.Creator<SavedState> CREATOR =
1566 new Parcelable.Creator<SavedState>() {
1567 public SavedState createFromParcel(Parcel in) {
1568 return new SavedState(in);
1569 }
1570
1571 public SavedState[] newArray(int size) {
1572 return new SavedState[size];
1573 }
1574 };
1575 }
1576
Winson Chungf314b0e2011-08-16 11:54:27 -07001577 protected void loadAssociatedPages(int page) {
1578 loadAssociatedPages(page, false);
1579 }
1580 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001581 if (mContentIsRefreshable) {
1582 final int count = getChildCount();
1583 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001584 int lowerPageBound = getAssociatedLowerPageBound(page);
1585 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001586 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1587 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001588 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001589 if ((i != page) && immediateAndOnly) {
1590 continue;
1591 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001592 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001593 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001594 if (lowerPageBound <= i && i <= upperPageBound) {
1595 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001596 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001597 mDirtyPageContent.set(i, false);
1598 }
1599 } else {
1600 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001601 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001602 }
1603 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001604 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001605 }
1606 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001607 }
1608 }
1609
Winson Chunge3193b92010-09-10 11:44:42 -07001610 protected int getAssociatedLowerPageBound(int page) {
1611 return Math.max(0, page - 1);
1612 }
1613 protected int getAssociatedUpperPageBound(int page) {
1614 final int count = getChildCount();
1615 return Math.min(page + 1, count - 1);
1616 }
1617
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001618 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001619 if (isChoiceMode(CHOICE_MODE_NONE)) {
1620 mChoiceMode = mode;
1621 mActionMode = startActionMode(callback);
1622 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001623 }
1624
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001625 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001626 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001627 mChoiceMode = CHOICE_MODE_NONE;
1628 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001629 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001630 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001631 }
1632 }
1633
1634 protected boolean isChoiceMode(int mode) {
1635 return mChoiceMode == mode;
1636 }
1637
1638 protected ArrayList<Checkable> getCheckedGrandchildren() {
1639 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1640 final int childCount = getChildCount();
1641 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001642 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001643 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001644 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001645 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001646 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001647 checked.add((Checkable) v);
1648 }
1649 }
1650 }
1651 return checked;
1652 }
1653
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001654 /**
1655 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1656 * Otherwise, returns null.
1657 */
1658 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001659 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001660 final int childCount = getChildCount();
1661 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001662 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001663 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001664 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001665 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001666 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1667 return (Checkable) v;
1668 }
1669 }
1670 }
1671 }
1672 return null;
1673 }
1674
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001675 protected void resetCheckedGrandchildren() {
1676 // loop through children, and set all of their children to _not_ be checked
1677 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1678 for (int i = 0; i < checked.size(); ++i) {
1679 final Checkable c = checked.get(i);
1680 c.setChecked(false);
1681 }
1682 }
1683
Winson Chung86f77532010-08-24 11:08:22 -07001684 /**
1685 * This method is called ONLY to synchronize the number of pages that the paged view has.
1686 * To actually fill the pages with information, implement syncPageItems() below. It is
1687 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1688 * and therefore, individual page items do not need to be updated in this method.
1689 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001690 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001691
1692 /**
1693 * This method is called to synchronize the items that are on a particular page. If views on
1694 * the page can be reused, then they should be updated within this method.
1695 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001696 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001697
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001698 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001699 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001700 }
1701 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001702 invalidatePageData(currentPage, false);
1703 }
1704 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001705 if (!mIsDataReady) {
1706 return;
1707 }
1708
Michael Jurka0142d492010-08-25 17:46:15 -07001709 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001710 // Force all scrolling-related behavior to end
1711 mScroller.forceFinished(true);
1712 mNextPage = INVALID_PAGE;
1713
Michael Jurka0142d492010-08-25 17:46:15 -07001714 // Update all the pages
1715 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001716
Winson Chung5a808352011-06-27 19:08:49 -07001717 // We must force a measure after we've loaded the pages to update the content width and
1718 // to determine the full scroll width
1719 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1720 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1721
1722 // Set a new page as the current page if necessary
1723 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001724 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001725 }
1726
Michael Jurka0142d492010-08-25 17:46:15 -07001727 // Mark each of the pages as dirty
1728 final int count = getChildCount();
1729 mDirtyPageContent.clear();
1730 for (int i = 0; i < count; ++i) {
1731 mDirtyPageContent.add(true);
1732 }
1733
1734 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001735 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001736 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001737 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001738 }
Winson Chung007c6982011-06-14 13:27:53 -07001739
Adam Cohen21b41102011-11-01 17:29:52 -07001740 protected ImageView getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001741 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1742 // found
1743 if (mHasScrollIndicator && mScrollIndicator == null) {
1744 ViewGroup parent = (ViewGroup) getParent();
1745 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1746 mHasScrollIndicator = mScrollIndicator != null;
1747 if (mHasScrollIndicator) {
1748 mScrollIndicator.setVisibility(View.VISIBLE);
1749 }
1750 }
1751 return mScrollIndicator;
1752 }
1753
1754 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001755 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001756 }
1757
Winson Chung5a808352011-06-27 19:08:49 -07001758 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1759 @Override
1760 public void run() {
1761 hideScrollingIndicator(false);
1762 }
1763 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001764 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001765 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001766 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001767 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001768 }
1769
Michael Jurka430e8a52011-08-08 15:52:14 -07001770 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001771 if (getChildCount() <= 1) return;
1772 if (!isScrollingIndicatorEnabled()) return;
1773
1774 getScrollingIndicator();
1775 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001776 // Fade the indicator in
1777 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001778 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001779 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001780 if (immediately) {
1781 mScrollIndicator.setAlpha(1f);
1782 } else {
1783 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1784 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1785 mScrollIndicatorAnimator.start();
1786 }
Winson Chung007c6982011-06-14 13:27:53 -07001787 }
1788 }
1789
Adam Cohen21b41102011-11-01 17:29:52 -07001790 protected void cancelScrollingIndicatorAnimations() {
1791 if (mScrollIndicatorAnimator != null) {
1792 mScrollIndicatorAnimator.cancel();
1793 }
1794 }
1795
Winson Chung007c6982011-06-14 13:27:53 -07001796 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001797 if (getChildCount() <= 1) return;
1798 if (!isScrollingIndicatorEnabled()) return;
1799
1800 getScrollingIndicator();
1801 if (mScrollIndicator != null) {
1802 // Fade the indicator out
1803 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001804 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001805 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001806 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001807 mScrollIndicator.setAlpha(0f);
1808 } else {
1809 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1810 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1811 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1812 private boolean cancelled = false;
1813 @Override
1814 public void onAnimationCancel(android.animation.Animator animation) {
1815 cancelled = true;
1816 }
1817 @Override
1818 public void onAnimationEnd(Animator animation) {
1819 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001820 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001821 }
1822 }
1823 });
1824 mScrollIndicatorAnimator.start();
1825 }
Winson Chung007c6982011-06-14 13:27:53 -07001826 }
1827 }
1828
Winson Chung32174c82011-07-19 15:47:55 -07001829 /**
1830 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1831 * fill its space on the track or not.
1832 */
1833 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001834 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001835 }
1836
Winson Chung007c6982011-06-14 13:27:53 -07001837 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001838 if (getChildCount() <= 1) return;
1839 if (!isScrollingIndicatorEnabled()) return;
1840
1841 getScrollingIndicator();
1842 if (mScrollIndicator != null) {
1843 updateScrollingIndicatorPosition();
1844 }
1845 }
1846
Winson Chung007c6982011-06-14 13:27:53 -07001847 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001848 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001849 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001850 int numPages = getChildCount();
1851 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001852 int lastChildIndex = Math.max(0, getChildCount() - 1);
1853 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001854 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001855 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1856 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001857
Winson Chungae890b82011-09-13 18:08:54 -07001858 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001859 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001860 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001861 if (hasElasticScrollIndicator()) {
1862 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1863 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1864 mScrollIndicator.requestLayout();
1865 }
1866 } else {
1867 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1868 indicatorPos += indicatorCenterOffset;
1869 }
Winson Chung007c6982011-06-14 13:27:53 -07001870 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001871 mScrollIndicator.invalidate();
1872 }
1873
Winson Chung3ac74c52011-06-30 17:39:37 -07001874 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001875 }
1876
1877 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001878 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001879
1880 /* Accessibility */
1881 @Override
1882 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1883 super.onInitializeAccessibilityNodeInfo(info);
1884 info.setScrollable(true);
1885 }
1886
1887 @Override
1888 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1889 super.onInitializeAccessibilityEvent(event);
1890 event.setScrollable(true);
1891 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1892 event.setFromIndex(mCurrentPage);
1893 event.setToIndex(mCurrentPage);
1894 event.setItemCount(getChildCount());
1895 }
1896 }
1897
Winson Chung6a0f57d2011-06-29 20:10:49 -07001898 protected String getCurrentPageDescription() {
1899 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1900 return String.format(mContext.getString(R.string.default_scroll_format),
1901 page + 1, getChildCount());
1902 }
Winson Chungd11265e2011-08-30 13:37:23 -07001903
1904 @Override
1905 public boolean onHoverEvent(android.view.MotionEvent event) {
1906 return true;
1907 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001908}