blob: 7be19bf23ea6a20ba7182bc7dc5b123705d94c4a [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;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected final static int TOUCH_STATE_REST = 0;
99 protected final static int TOUCH_STATE_SCROLLING = 1;
100 protected final static int TOUCH_STATE_PREV_PAGE = 2;
101 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700102 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Michael Jurka7426c422010-11-11 15:23:47 -0800108 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka7426c422010-11-11 15:23:47 -0800110 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private int mPagingTouchSlop;
112 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800113 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700114 protected int mPageSpacing;
115 protected int mPageLayoutPaddingTop;
116 protected int mPageLayoutPaddingBottom;
117 protected int mPageLayoutPaddingLeft;
118 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700119 protected int mPageLayoutWidthGap;
120 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700121 protected int mCellCountX = 0;
122 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700123 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800124 protected boolean mAllowOverScroll = true;
125 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Michael Jurka8c920dd2011-01-20 14:16:56 -0800127 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800128 protected float mLayoutScale = 1.0f;
129
Michael Jurka5f1c5092010-09-03 14:15:02 -0700130 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Michael Jurka5f1c5092010-09-03 14:15:02 -0700132 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Winson Chung86f77532010-08-24 11:08:22 -0700134 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Winson Chung86f77532010-08-24 11:08:22 -0700136 private ArrayList<Boolean> mDirtyPageContent;
Michael Jurka86c119a2011-08-05 20:35:36 -0700137 private boolean mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700139 // choice modes
140 protected static final int CHOICE_MODE_NONE = 0;
141 protected static final int CHOICE_MODE_SINGLE = 1;
142 // Multiple selection mode is not supported by all Launcher actions atm
143 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700144
Michael Jurkae17e19c2010-09-28 11:01:39 -0700145 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700146 private ActionMode mActionMode;
147
Michael Jurka0142d492010-08-25 17:46:15 -0700148 // If true, syncPages and syncPageItems will be called to refresh pages
149 protected boolean mContentIsRefreshable = true;
150
151 // If true, modify alpha of neighboring pages as user scrolls left/right
152 protected boolean mFadeInAdjacentScreens = true;
153
154 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
155 // to switch to a new page
156 protected boolean mUsePagingTouchSlop = true;
157
158 // If true, the subclass should directly update mScrollX itself in its computeScroll method
159 // (SmoothPagedView does this)
160 protected boolean mDeferScrollUpdate = false;
161
Patrick Dubroy1262e362010-10-06 15:49:50 -0700162 protected boolean mIsPageMoving = false;
163
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 // All syncs and layout passes are deferred until data is ready.
165 protected boolean mIsDataReady = false;
166
Winson Chung007c6982011-06-14 13:27:53 -0700167 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700168 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700169 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700170 private int mScrollIndicatorPaddingLeft;
171 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700172 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700173 protected static final int sScrollIndicatorFadeInDuration = 150;
174 protected static final int sScrollIndicatorFadeOutDuration = 650;
175 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700176
Winson Chungb44b5242011-06-13 11:32:14 -0700177 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700178 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700179
Winson Chung86f77532010-08-24 11:08:22 -0700180 public interface PageSwitchListener {
181 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700182 }
183
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 public PagedView(Context context) {
185 this(context, null);
186 }
187
188 public PagedView(Context context, AttributeSet attrs) {
189 this(context, attrs, 0);
190 }
191
192 public PagedView(Context context, AttributeSet attrs, int defStyle) {
193 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700194 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700195
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 TypedArray a = context.obtainStyledAttributes(attrs,
197 R.styleable.PagedView, defStyle, 0);
198 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
199 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800200 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800202 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800204 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800206 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700207 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700208 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700209 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700210 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700211 mScrollIndicatorPaddingLeft =
212 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
213 mScrollIndicatorPaddingRight =
214 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700215 a.recycle();
216
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700218 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 }
220
221 /**
222 * Initializes various states for this workspace.
223 */
Michael Jurka0142d492010-08-25 17:46:15 -0700224 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700225 mDirtyPageContent = new ArrayList<Boolean>();
226 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800227 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700228 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700229 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700230
231 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
232 mTouchSlop = configuration.getScaledTouchSlop();
233 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
234 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700235 mDensity = getResources().getDisplayMetrics().density;
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 }
237
Winson Chung86f77532010-08-24 11:08:22 -0700238 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
239 mPageSwitchListener = pageSwitchListener;
240 if (mPageSwitchListener != null) {
241 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 }
243 }
244
245 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700246 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
247 * out pages.
248 */
249 protected void setDataIsReady() {
250 mIsDataReady = true;
251 }
252 protected boolean isDataReady() {
253 return mIsDataReady;
254 }
255
256 /**
Winson Chung86f77532010-08-24 11:08:22 -0700257 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 *
Winson Chung86f77532010-08-24 11:08:22 -0700259 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700260 */
Winson Chung86f77532010-08-24 11:08:22 -0700261 int getCurrentPage() {
262 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700263 }
264
Winson Chung86f77532010-08-24 11:08:22 -0700265 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 return getChildCount();
267 }
268
Winson Chung86f77532010-08-24 11:08:22 -0700269 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 return getChildAt(index);
271 }
272
Adam Cohenae4f1552011-10-20 00:15:42 -0700273 protected int indexToPage(int index) {
274 return index;
275 }
276
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800278 * Updates the scroll of the current page immediately to its final scroll position. We use this
279 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
280 * the previous tab page.
281 */
282 protected void updateCurrentPageScroll() {
283 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
284 scrollTo(newX, 0);
285 mScroller.setFinalX(newX);
286 }
287
288 /**
Winson Chung86f77532010-08-24 11:08:22 -0700289 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 */
Winson Chung86f77532010-08-24 11:08:22 -0700291 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800292 if (!mScroller.isFinished()) {
293 mScroller.abortAnimation();
294 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800295 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
296 // the default
297 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800298 return;
299 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700300
Winson Chung86f77532010-08-24 11:08:22 -0700301 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800302 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700303 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700304 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800305 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 }
307
Michael Jurka0142d492010-08-25 17:46:15 -0700308 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700309 if (mPageSwitchListener != null) {
310 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 }
312 }
313
Michael Jurkace7e05f2011-02-01 22:02:35 -0800314 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700315 if (!mIsPageMoving) {
316 mIsPageMoving = true;
317 onPageBeginMoving();
318 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700319 }
320
Michael Jurkace7e05f2011-02-01 22:02:35 -0800321 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700322 if (mIsPageMoving) {
323 mIsPageMoving = false;
324 onPageEndMoving();
325 }
Michael Jurka0142d492010-08-25 17:46:15 -0700326 }
327
Adam Cohen26976d92011-03-22 15:33:33 -0700328 protected boolean isPageMoving() {
329 return mIsPageMoving;
330 }
331
Michael Jurka0142d492010-08-25 17:46:15 -0700332 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700333 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700334 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700335 }
336
337 // a method that subclasses can override to add behavior
338 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700339 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700340 }
341
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 /**
Winson Chung86f77532010-08-24 11:08:22 -0700343 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 *
345 * @param l The listener used to respond to long clicks.
346 */
347 @Override
348 public void setOnLongClickListener(OnLongClickListener l) {
349 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700350 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700352 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 }
354 }
355
356 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800357 public void scrollBy(int x, int y) {
358 scrollTo(mUnboundedScrollX + x, mScrollY + y);
359 }
360
361 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700362 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800363 mUnboundedScrollX = x;
364
365 if (x < 0) {
366 super.scrollTo(0, y);
367 if (mAllowOverScroll) {
368 overScroll(x);
369 }
370 } else if (x > mMaxScrollX) {
371 super.scrollTo(mMaxScrollX, y);
372 if (mAllowOverScroll) {
373 overScroll(x - mMaxScrollX);
374 }
375 } else {
376 super.scrollTo(x, y);
377 }
378
Michael Jurka0142d492010-08-25 17:46:15 -0700379 mTouchX = x;
380 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
381 }
382
383 // we moved this functionality to a helper function so SmoothPagedView can reuse it
384 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700385 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700386 // Don't bother scrolling if the page does not need to be moved
387 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
388 mDirtyPageAlpha = true;
389 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
390 }
Michael Jurka0142d492010-08-25 17:46:15 -0700391 invalidate();
392 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700393 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700394 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700395 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700396 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700397 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700398
399 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700400 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700401 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700402 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700403 }
404
Adam Cohen73aa9752010-11-24 16:26:58 -0800405 // We don't want to trigger a page end moving unless the page has settled
406 // and the user has stopped scrolling
407 if (mTouchState == TOUCH_STATE_REST) {
408 pageEndMoving();
409 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700410
411 // Notify the user when the page changes
412 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
413 AccessibilityEvent ev =
414 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
415 ev.getText().add(getCurrentPageDescription());
416 sendAccessibilityEventUnchecked(ev);
417 }
Michael Jurka0142d492010-08-25 17:46:15 -0700418 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 }
Michael Jurka0142d492010-08-25 17:46:15 -0700420 return false;
421 }
422
423 @Override
424 public void computeScroll() {
425 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 }
427
428 @Override
429 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700430 if (!mIsDataReady) {
431 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
432 return;
433 }
434
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
436 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
437 if (widthMode != MeasureSpec.EXACTLY) {
438 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
439 }
440
Adam Lesinski6b879f02010-11-04 16:15:23 -0700441 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
442 * of the All apps view on XLarge displays to not take up more space then it needs. Width
443 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
444 * each page to have the same width.
445 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700447 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
448 int maxChildHeight = 0;
449
450 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700451 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700452
Michael Jurka36fcb742011-04-06 17:08:58 -0700453
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700455 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700456 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 final int childCount = getChildCount();
458 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700459 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700460 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700461 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
462
463 int childWidthMode;
464 if (lp.width == LayoutParams.WRAP_CONTENT) {
465 childWidthMode = MeasureSpec.AT_MOST;
466 } else {
467 childWidthMode = MeasureSpec.EXACTLY;
468 }
469
470 int childHeightMode;
471 if (lp.height == LayoutParams.WRAP_CONTENT) {
472 childHeightMode = MeasureSpec.AT_MOST;
473 } else {
474 childHeightMode = MeasureSpec.EXACTLY;
475 }
476
477 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700478 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700479 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700480 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700481
482 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700483 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700484 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
485 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700486 }
487
488 if (heightMode == MeasureSpec.AT_MOST) {
489 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 }
Winson Chungae890b82011-09-13 18:08:54 -0700491
492 updateScrollingIndicatorPosition();
493
494 setMeasuredDimension(widthSize, heightSize);
495
496 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions
Adam Cohenfaa28302010-11-19 12:02:18 -0800497 if (childCount > 0) {
498 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
499 } else {
500 mMaxScrollX = 0;
501 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 }
503
Michael Jurka8c920dd2011-01-20 14:16:56 -0800504 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800505 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
506 int delta = newX - mScrollX;
507
Michael Jurka8c920dd2011-01-20 14:16:56 -0800508 final int pageCount = getChildCount();
509 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700510 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800511 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800512 }
513 setCurrentPage(newCurrentPage);
514 }
515
Michael Jurka8c920dd2011-01-20 14:16:56 -0800516 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
517 // 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 -0800518 // tightens the layout accordingly
519 public void setLayoutScale(float childrenScale) {
520 mLayoutScale = childrenScale;
521
522 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
523 int childCount = getChildCount();
524 float childrenX[] = new float[childCount];
525 float childrenY[] = new float[childCount];
526 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700527 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800528 childrenX[i] = child.getX();
529 childrenY[i] = child.getY();
530 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700531 // Trigger a full re-layout (never just call onLayout directly!)
532 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
533 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
534 requestLayout();
535 measure(widthSpec, heightSpec);
536 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800537 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700538 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539 child.setX(childrenX[i]);
540 child.setY(childrenY[i]);
541 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700542
Michael Jurkad3ef3062010-11-23 16:23:58 -0800543 // Also, the page offset has changed (since the pages are now smaller);
544 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800545 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800546 }
547
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700549 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700550 if (!mIsDataReady) {
551 return;
552 }
553
Winson Chung785d2eb2011-04-14 16:08:02 -0700554 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700555 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 final int childCount = getChildCount();
557 int childLeft = 0;
558 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700559 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
560 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700561 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700562
563 // Calculate the variable page spacing if necessary
564 if (mPageSpacing < 0) {
565 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
566 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 }
568
569 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700570 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700571 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800572 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700573 final int childHeight = child.getMeasuredHeight();
574 int childTop = mPaddingTop;
575 if (mCenterPagesVertically) {
576 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
577 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800578
Winson Chung785d2eb2011-04-14 16:08:02 -0700579 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700580 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800581 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700582 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700583 }
584 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700585
586 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
587 setHorizontalScrollBarEnabled(false);
588 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
589 scrollTo(newX, 0);
590 mScroller.setFinalX(newX);
591 setHorizontalScrollBarEnabled(true);
592 mFirstLayout = false;
593 }
594
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
596 mFirstLayout = false;
597 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
599
Winson Chung03929772011-02-23 17:07:10 -0800600 protected void forceUpdateAdjacentPagesAlpha() {
601 mDirtyPageAlpha = true;
602 updateAdjacentPagesAlpha();
603 }
604
Winson Chunge3193b92010-09-10 11:44:42 -0700605 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700606 if (mFadeInAdjacentScreens) {
607 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700608 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700609 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700610 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700611 final int childCount = getChildCount();
612 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700613 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800614 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700615 int halfChildWidth = (childWidth / 2);
616 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700617
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700618 // On the first layout, we may not have a width nor a proper offset, so for now
619 // we should just assume full page width (and calculate the offset according to
620 // that).
621 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700622 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700623 childCenter = (i * childWidth) + (childWidth / 2);
624 }
625
Winson Chunge8878e32010-09-15 20:37:09 -0700626 int d = halfChildWidth;
627 int distanceFromScreenCenter = childCenter - screenCenter;
628 if (distanceFromScreenCenter > 0) {
629 if (i > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700630 d += getScaledMeasuredWidth(getPageAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700631 } else {
632 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700633 }
Michael Jurka0142d492010-08-25 17:46:15 -0700634 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700635 if (i < childCount - 1) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700636 d += getScaledMeasuredWidth(getPageAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700637 } else {
638 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700639 }
Michael Jurka0142d492010-08-25 17:46:15 -0700640 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700641 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700642
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700643 // Preventing potential divide-by-zero
644 d = Math.max(1, d);
645
Winson Chunge8878e32010-09-15 20:37:09 -0700646 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
647 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
648 float alpha = 1.0f - dimAlpha;
649
Adam Cohenf34bab52010-09-30 14:11:56 -0700650 if (alpha < ALPHA_QUANTIZE_LEVEL) {
651 alpha = 0.0f;
652 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
653 alpha = 1.0f;
654 }
655
Michael Jurkaa40829a2011-02-16 18:02:45 -0800656 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
657 // this optimization causes alpha to not be properly updated sometimes (repro
658 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
659 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
660 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800661 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700662 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800663 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700664 }
Michael Jurka0142d492010-08-25 17:46:15 -0700665 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700666 }
667 }
Winson Chunge3193b92010-09-10 11:44:42 -0700668 }
669
Adam Cohenf34bab52010-09-30 14:11:56 -0700670 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700671 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700672 }
673
Winson Chunge3193b92010-09-10 11:44:42 -0700674 @Override
675 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700676 int halfScreenSize = getMeasuredWidth() / 2;
677 int screenCenter = mScrollX + halfScreenSize;
678
679 if (screenCenter != mLastScreenCenter) {
680 screenScrolled(screenCenter);
681 updateAdjacentPagesAlpha();
682 mLastScreenCenter = screenCenter;
683 }
Michael Jurka0142d492010-08-25 17:46:15 -0700684
685 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700686 // As an optimization, this code assumes that all pages have the same width as the 0th
687 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700688 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700689 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700690 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700691 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700692 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700693 int leftScreen = 0;
694 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700695 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700696 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700697 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700698 }
699 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700700 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700701 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700702 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700703 }
704 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700705
Michael Jurkac4fb9172010-09-02 17:19:20 -0700706 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800707 // Clip to the bounds
708 canvas.save();
709 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
710 mScrollY + mBottom - mTop);
711
Adam Cohen22f823d2011-09-01 17:22:18 -0700712 for (int i = rightScreen; i >= leftScreen; i--) {
713 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700714 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800715 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700716 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 }
718
719 @Override
720 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700721 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700722 if (page != mCurrentPage || !mScroller.isFinished()) {
723 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 return true;
725 }
726 return false;
727 }
728
729 @Override
730 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700731 int focusablePage;
732 if (mNextPage != INVALID_PAGE) {
733 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700735 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 }
Winson Chung86f77532010-08-24 11:08:22 -0700737 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700739 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741 return false;
742 }
743
744 @Override
745 public boolean dispatchUnhandledMove(View focused, int direction) {
746 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700747 if (getCurrentPage() > 0) {
748 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700749 return true;
750 }
751 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700752 if (getCurrentPage() < getPageCount() - 1) {
753 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 return true;
755 }
756 }
757 return super.dispatchUnhandledMove(focused, direction);
758 }
759
760 @Override
761 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700762 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
763 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700766 if (mCurrentPage > 0) {
767 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
769 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700770 if (mCurrentPage < getPageCount() - 1) {
771 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 }
773 }
774 }
775
776 /**
777 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700778 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 *
Winson Chung86f77532010-08-24 11:08:22 -0700780 * This happens when live folders requery, and if they're off page, they
781 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 */
783 @Override
784 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700785 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 View v = focused;
787 while (true) {
788 if (v == current) {
789 super.focusableViewAvailable(focused);
790 return;
791 }
792 if (v == this) {
793 return;
794 }
795 ViewParent parent = v.getParent();
796 if (parent instanceof View) {
797 v = (View)v.getParent();
798 } else {
799 return;
800 }
801 }
802 }
803
804 /**
805 * {@inheritDoc}
806 */
807 @Override
808 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
809 if (disallowIntercept) {
810 // We need to make sure to cancel our long press if
811 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700812 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700813 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 }
815 super.requestDisallowInterceptTouchEvent(disallowIntercept);
816 }
817
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800818 /**
819 * Return true if a tap at (x, y) should trigger a flip to the previous page.
820 */
821 protected boolean hitsPreviousPage(float x, float y) {
822 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
823 }
824
825 /**
826 * Return true if a tap at (x, y) should trigger a flip to the next page.
827 */
828 protected boolean hitsNextPage(float x, float y) {
829 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
830 }
831
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 @Override
833 public boolean onInterceptTouchEvent(MotionEvent ev) {
834 /*
835 * This method JUST determines whether we want to intercept the motion.
836 * If we return true, onTouchEvent will be called and we do the actual
837 * scrolling there.
838 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800839 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700840
Winson Chung45e1d6e2010-11-09 17:19:49 -0800841 // Skip touch handling if there are no pages to swipe
842 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
843
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 /*
845 * Shortcut the most recurring case: the user is in the dragging
846 * state and he is moving his finger. We want to intercept this
847 * motion.
848 */
849 final int action = ev.getAction();
850 if ((action == MotionEvent.ACTION_MOVE) &&
851 (mTouchState == TOUCH_STATE_SCROLLING)) {
852 return true;
853 }
854
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 switch (action & MotionEvent.ACTION_MASK) {
856 case MotionEvent.ACTION_MOVE: {
857 /*
858 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
859 * whether the user has moved far enough from his original down touch.
860 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700861 if (mActivePointerId != INVALID_POINTER) {
862 determineScrollingStart(ev);
863 break;
864 }
865 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
866 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
867 // i.e. fall through to the next case (don't break)
868 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
869 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 }
871
872 case MotionEvent.ACTION_DOWN: {
873 final float x = ev.getX();
874 final float y = ev.getY();
875 // Remember location of down touch
876 mDownMotionX = x;
877 mLastMotionX = x;
878 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800879 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800880 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 mActivePointerId = ev.getPointerId(0);
882 mAllowLongPress = true;
883
884 /*
885 * If being flinged and user touches the screen, initiate drag;
886 * otherwise don't. mScroller.isFinished should be false when
887 * being flinged.
888 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700889 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700890 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
891 if (finishedScrolling) {
892 mTouchState = TOUCH_STATE_REST;
893 mScroller.abortAnimation();
894 } else {
895 mTouchState = TOUCH_STATE_SCROLLING;
896 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700897
Winson Chung86f77532010-08-24 11:08:22 -0700898 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800900 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800902 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800904 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 mTouchState = TOUCH_STATE_NEXT_PAGE;
906 }
907 }
908 }
909 break;
910 }
911
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800913 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 mTouchState = TOUCH_STATE_REST;
915 mAllowLongPress = false;
916 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800917 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 break;
919
920 case MotionEvent.ACTION_POINTER_UP:
921 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800922 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 break;
924 }
925
926 /*
927 * The only time we want to intercept motion events is if we are in the
928 * drag mode.
929 */
930 return mTouchState != TOUCH_STATE_REST;
931 }
932
Winson Chung80baf5a2010-08-09 16:03:15 -0700933 protected void animateClickFeedback(View v, final Runnable r) {
934 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800935 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
936 loadAnimator(mContext, R.anim.paged_view_click_feedback);
937 anim.setTarget(v);
938 anim.addListener(new AnimatorListenerAdapter() {
939 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700940 r.run();
941 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700942 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800943 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700944 }
945
Adam Cohenf8d28232011-02-01 21:47:00 -0800946 protected void determineScrollingStart(MotionEvent ev) {
947 determineScrollingStart(ev, 1.0f);
948 }
949
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 /*
951 * Determines if we should change the touch state to start scrolling after the
952 * user moves their touch point too far.
953 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800954 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 /*
956 * Locally do absolute value. mLastMotionX is set to the y value
957 * of the down event.
958 */
959 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700960 if (pointerIndex == -1) {
961 return;
962 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 final float x = ev.getX(pointerIndex);
964 final float y = ev.getY(pointerIndex);
965 final int xDiff = (int) Math.abs(x - mLastMotionX);
966 final int yDiff = (int) Math.abs(y - mLastMotionY);
967
Adam Cohenf8d28232011-02-01 21:47:00 -0800968 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 boolean xPaged = xDiff > mPagingTouchSlop;
970 boolean xMoved = xDiff > touchSlop;
971 boolean yMoved = yDiff > touchSlop;
972
Adam Cohenf8d28232011-02-01 21:47:00 -0800973 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700974 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 // Scroll if the user moved far enough along the X axis
976 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800977 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800979 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700980 mTouchX = mScrollX;
981 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
982 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 }
984 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800985 cancelCurrentPageLongPress();
986 }
987 }
988
989 protected void cancelCurrentPageLongPress() {
990 if (mAllowLongPress) {
991 mAllowLongPress = false;
992 // Try canceling the long press. It could also have been scheduled
993 // by a distant descendant, so use the mAllowLongPress flag to block
994 // everything
995 final View currentPage = getPageAt(mCurrentPage);
996 if (currentPage != null) {
997 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 }
999 }
1000 }
1001
Adam Cohenb5ba0972011-09-07 18:02:31 -07001002 protected float getScrollProgress(int screenCenter, View v, int page) {
1003 final int halfScreenSize = getMeasuredWidth() / 2;
1004
1005 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1006 int delta = screenCenter - (getChildOffset(page) -
1007 getRelativeChildOffset(page) + halfScreenSize);
1008
1009 float scrollProgress = delta / (totalDistance * 1.0f);
1010 scrollProgress = Math.min(scrollProgress, 1.0f);
1011 scrollProgress = Math.max(scrollProgress, -1.0f);
1012 return scrollProgress;
1013 }
1014
Adam Cohene0f66b52010-11-23 15:06:07 -08001015 // This curve determines how the effect of scrolling over the limits of the page dimishes
1016 // as the user pulls further and further from the bounds
1017 private float overScrollInfluenceCurve(float f) {
1018 f -= 1.0f;
1019 return f * f * f + 1.0f;
1020 }
1021
Adam Cohenb5ba0972011-09-07 18:02:31 -07001022 protected void acceleratedOverScroll(float amount) {
1023 int screenSize = getMeasuredWidth();
1024
1025 // We want to reach the max over scroll effect when the user has
1026 // over scrolled half the size of the screen
1027 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1028
1029 if (f == 0) return;
1030
1031 // Clamp this factor, f, to -1 < f < 1
1032 if (Math.abs(f) >= 1) {
1033 f /= Math.abs(f);
1034 }
1035
1036 int overScrollAmount = (int) Math.round(f * screenSize);
1037 if (amount < 0) {
1038 mScrollX = overScrollAmount;
1039 } else {
1040 mScrollX = mMaxScrollX + overScrollAmount;
1041 }
1042 invalidate();
1043 }
1044
1045 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001046 int screenSize = getMeasuredWidth();
1047
1048 float f = (amount / screenSize);
1049
1050 if (f == 0) return;
1051 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1052
Adam Cohen7bfc9792011-01-28 13:52:37 -08001053 // Clamp this factor, f, to -1 < f < 1
1054 if (Math.abs(f) >= 1) {
1055 f /= Math.abs(f);
1056 }
1057
Adam Cohene0f66b52010-11-23 15:06:07 -08001058 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001059 if (amount < 0) {
1060 mScrollX = overScrollAmount;
1061 } else {
1062 mScrollX = mMaxScrollX + overScrollAmount;
1063 }
1064 invalidate();
1065 }
1066
Adam Cohenb5ba0972011-09-07 18:02:31 -07001067 protected void overScroll(float amount) {
1068 dampedOverScroll(amount);
1069 }
1070
Michael Jurkac5b262c2011-01-12 20:24:50 -08001071 protected float maxOverScroll() {
1072 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001073 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001074 float f = 1.0f;
1075 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1076 return OVERSCROLL_DAMP_FACTOR * f;
1077 }
1078
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 @Override
1080 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001081 // Skip touch handling if there are no pages to swipe
1082 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1083
Michael Jurkab8f06722010-10-10 15:58:46 -07001084 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001085
1086 final int action = ev.getAction();
1087
1088 switch (action & MotionEvent.ACTION_MASK) {
1089 case MotionEvent.ACTION_DOWN:
1090 /*
1091 * If being flinged and user touches, stop the fling. isFinished
1092 * will be false if being flinged.
1093 */
1094 if (!mScroller.isFinished()) {
1095 mScroller.abortAnimation();
1096 }
1097
1098 // Remember where the motion event started
1099 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001100 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001101 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001103 if (mTouchState == TOUCH_STATE_SCROLLING) {
1104 pageBeginMoving();
1105 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 break;
1107
1108 case MotionEvent.ACTION_MOVE:
1109 if (mTouchState == TOUCH_STATE_SCROLLING) {
1110 // Scroll to follow the motion event
1111 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1112 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001113 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001114
Adam Cohenaefd4e12011-02-14 16:39:38 -08001115 mTotalMotionX += Math.abs(deltaX);
1116
Winson Chungc0844aa2011-02-02 15:25:58 -08001117 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1118 // keep the remainder because we are actually testing if we've moved from the last
1119 // scrolled position (which is discrete).
1120 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001121 mTouchX += deltaX;
1122 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1123 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001124 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001126 } else {
1127 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001128 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001129 mLastMotionX = x;
1130 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 } else {
1132 awakenScrollBars();
1133 }
Adam Cohen564976a2010-10-13 18:52:07 -07001134 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 determineScrollingStart(ev);
1136 }
1137 break;
1138
1139 case MotionEvent.ACTION_UP:
1140 if (mTouchState == TOUCH_STATE_SCROLLING) {
1141 final int activePointerId = mActivePointerId;
1142 final int pointerIndex = ev.findPointerIndex(activePointerId);
1143 final float x = ev.getX(pointerIndex);
1144 final VelocityTracker velocityTracker = mVelocityTracker;
1145 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1146 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001147 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001148 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001149 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001150
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001151 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1152
Adam Cohenaefd4e12011-02-14 16:39:38 -08001153 // In the case that the page is moved far to one direction and then is flung
1154 // in the opposite direction, we use a threshold to determine whether we should
1155 // just return to the starting page, or if we should skip one further.
1156 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001157 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001158 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001159 Math.signum(velocityX) != Math.signum(deltaX)) {
1160 returnToOriginalPage = true;
1161 }
1162
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001163 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001164 Math.abs(velocityX) > snapVelocity;
1165
1166 int finalPage;
1167 // We give flings precedence over large moves, which is why we short-circuit our
1168 // test for a large move if a fling has been registered. That is, a large
1169 // move to the left and fling to the right will register as a fling to the right.
1170 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1171 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1172 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1173 snapToPageWithVelocity(finalPage, velocityX);
1174 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1175 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001176 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001177 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1178 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 } else {
1180 snapToDestination();
1181 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001182 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 // at this point we have not moved beyond the touch slop
1184 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1185 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001186 int nextPage = Math.max(0, mCurrentPage - 1);
1187 if (nextPage != mCurrentPage) {
1188 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 } else {
1190 snapToDestination();
1191 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001192 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 // at this point we have not moved beyond the touch slop
1194 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1195 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001196 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1197 if (nextPage != mCurrentPage) {
1198 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 } else {
1200 snapToDestination();
1201 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001202 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001203 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 }
1205 mTouchState = TOUCH_STATE_REST;
1206 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001207 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 break;
1209
1210 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001211 if (mTouchState == TOUCH_STATE_SCROLLING) {
1212 snapToDestination();
1213 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 mTouchState = TOUCH_STATE_REST;
1215 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001216 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 break;
1218
1219 case MotionEvent.ACTION_POINTER_UP:
1220 onSecondaryPointerUp(ev);
1221 break;
1222 }
1223
1224 return true;
1225 }
1226
Winson Chung185d7162011-02-28 13:47:29 -08001227 @Override
1228 public boolean onGenericMotionEvent(MotionEvent event) {
1229 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1230 switch (event.getAction()) {
1231 case MotionEvent.ACTION_SCROLL: {
1232 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1233 final float vscroll;
1234 final float hscroll;
1235 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1236 vscroll = 0;
1237 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1238 } else {
1239 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1240 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1241 }
1242 if (hscroll != 0 || vscroll != 0) {
1243 if (hscroll > 0 || vscroll > 0) {
1244 scrollRight();
1245 } else {
1246 scrollLeft();
1247 }
1248 return true;
1249 }
1250 }
1251 }
1252 }
1253 return super.onGenericMotionEvent(event);
1254 }
1255
Michael Jurkab8f06722010-10-10 15:58:46 -07001256 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1257 if (mVelocityTracker == null) {
1258 mVelocityTracker = VelocityTracker.obtain();
1259 }
1260 mVelocityTracker.addMovement(ev);
1261 }
1262
1263 private void releaseVelocityTracker() {
1264 if (mVelocityTracker != null) {
1265 mVelocityTracker.recycle();
1266 mVelocityTracker = null;
1267 }
1268 }
1269
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 private void onSecondaryPointerUp(MotionEvent ev) {
1271 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1272 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1273 final int pointerId = ev.getPointerId(pointerIndex);
1274 if (pointerId == mActivePointerId) {
1275 // This was our active pointer going up. Choose a new
1276 // active pointer and adjust accordingly.
1277 // TODO: Make this decision more intelligent.
1278 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1279 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1280 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001281 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 mActivePointerId = ev.getPointerId(newPointerIndex);
1283 if (mVelocityTracker != null) {
1284 mVelocityTracker.clear();
1285 }
1286 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001287 }
1288
Michael Jurkad771c962011-08-09 15:00:48 -07001289 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001290
1291 @Override
1292 public void requestChildFocus(View child, View focused) {
1293 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001294 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001295 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001296 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001297 }
1298 }
1299
Winson Chunge3193b92010-09-10 11:44:42 -07001300 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1301 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001302 int left;
1303 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001304 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001305 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001306 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001307 if (left <= relativeOffset && relativeOffset <= right) {
1308 return i;
1309 }
Winson Chunge3193b92010-09-10 11:44:42 -07001310 }
1311 return -1;
1312 }
1313
Winson Chung1908d072011-02-24 18:09:44 -08001314 protected void setMinimumWidthOverride(int minimumWidth) {
1315 mMinimumWidth = minimumWidth;
1316 }
Winson Chung34b23d52011-03-18 11:29:34 -07001317 protected void resetMinimumWidthOverride() {
1318 mMinimumWidth = 0;
1319 }
Winson Chung1908d072011-02-24 18:09:44 -08001320
1321 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001322 // This functions are called enough times that it actually makes a difference in the
1323 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001324 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001325 final int minWidth = mMinimumWidth;
1326 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001327 }
1328
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001330 int padding = mPaddingLeft + mPaddingRight;
1331 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001333 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001334 int padding = mPaddingLeft + mPaddingRight;
1335 return mPaddingLeft + (getMeasuredWidth() - padding -
Adam Cohen22f823d2011-09-01 17:22:18 -07001336 getScaledMeasuredWidth(getPageAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001337 }
1338
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 protected int getChildOffset(int index) {
1340 if (getChildCount() == 0)
1341 return 0;
1342
1343 int offset = getRelativeChildOffset(0);
1344 for (int i = 0; i < index; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001345 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 }
1347 return offset;
1348 }
1349
Michael Jurkad3ef3062010-11-23 16:23:58 -08001350 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001351 // This functions are called enough times that it actually makes a difference in the
1352 // profiler -- so just inline the max() here
1353 final int measuredWidth = child.getMeasuredWidth();
1354 final int minWidth = mMinimumWidth;
1355 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1356 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001357 }
1358
Adam Cohend19d3ca2010-09-15 14:43:42 -07001359 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001360 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 int minDistanceFromScreenCenterIndex = -1;
1362 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1363 final int childCount = getChildCount();
1364 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001365 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001366 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 int halfChildWidth = (childWidth / 2);
1368 int childCenter = getChildOffset(i) + halfChildWidth;
1369 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1370 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1371 minDistanceFromScreenCenter = distanceFromScreenCenter;
1372 minDistanceFromScreenCenterIndex = i;
1373 }
1374 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001375 return minDistanceFromScreenCenterIndex;
1376 }
1377
1378 protected void snapToDestination() {
1379 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 }
1381
Adam Cohene0f66b52010-11-23 15:06:07 -08001382 private static class ScrollInterpolator implements Interpolator {
1383 public ScrollInterpolator() {
1384 }
1385
1386 public float getInterpolation(float t) {
1387 t -= 1.0f;
1388 return t*t*t*t*t + 1;
1389 }
1390 }
1391
1392 // We want the duration of the page snap animation to be influenced by the distance that
1393 // the screen has to travel, however, we don't want this duration to be effected in a
1394 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1395 // of travel has on the overall snap duration.
1396 float distanceInfluenceForSnapDuration(float f) {
1397 f -= 0.5f; // center the values about 0.
1398 f *= 0.3f * Math.PI / 2.0f;
1399 return (float) Math.sin(f);
1400 }
1401
Michael Jurka0142d492010-08-25 17:46:15 -07001402 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001403 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1404 int halfScreenSize = getMeasuredWidth() / 2;
1405
Winson Chung785d2eb2011-04-14 16:08:02 -07001406 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1407 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1408 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001409 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1410 int delta = newX - mUnboundedScrollX;
1411 int duration = 0;
1412
1413 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1414 // If the velocity is low enough, then treat this more as an automatic page advance
1415 // as opposed to an apparent physical response to flinging
1416 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1417 return;
1418 }
1419
1420 // Here we compute a "distance" that will be used in the computation of the overall
1421 // snap duration. This is a function of the actual distance that needs to be traveled;
1422 // we keep this value close to half screen size in order to reduce the variance in snap
1423 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001424 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001425 float distance = halfScreenSize + halfScreenSize *
1426 distanceInfluenceForSnapDuration(distanceRatio);
1427
1428 velocity = Math.abs(velocity);
1429 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1430
1431 // we want the page's snap velocity to approximately match the velocity at which the
1432 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001433 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1434 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001435
1436 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001437 }
1438
1439 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001440 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 }
1442
Michael Jurka0142d492010-08-25 17:46:15 -07001443 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001444 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001445
Winson Chung785d2eb2011-04-14 16:08:02 -07001446 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1447 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1448 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001449 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001450 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001452 snapToPage(whichPage, delta, duration);
1453 }
1454
1455 protected void snapToPage(int whichPage, int delta, int duration) {
1456 mNextPage = whichPage;
1457
1458 View focusedChild = getFocusedChild();
1459 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001460 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001461 focusedChild.clearFocus();
1462 }
1463
1464 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 awakenScrollBars(duration);
1466 if (duration == 0) {
1467 duration = Math.abs(delta);
1468 }
1469
1470 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001471 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001472
Winson Chungb44b5242011-06-13 11:32:14 -07001473 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1474 // loading associated pages until the scroll settles
1475 if (mDeferScrollUpdate) {
1476 loadAssociatedPages(mNextPage);
1477 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001478 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001479 }
Michael Jurka0142d492010-08-25 17:46:15 -07001480 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 invalidate();
1482 }
1483
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 public void scrollLeft() {
1485 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001486 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001488 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 }
1490 }
1491
1492 public void scrollRight() {
1493 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001494 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001496 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 }
1498 }
1499
Winson Chung86f77532010-08-24 11:08:22 -07001500 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001501 int result = -1;
1502 if (v != null) {
1503 ViewParent vp = v.getParent();
1504 int count = getChildCount();
1505 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001506 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 return i;
1508 }
1509 }
1510 }
1511 return result;
1512 }
1513
1514 /**
1515 * @return True is long presses are still allowed for the current touch
1516 */
1517 public boolean allowLongPress() {
1518 return mAllowLongPress;
1519 }
1520
Michael Jurka0142d492010-08-25 17:46:15 -07001521 /**
1522 * Set true to allow long-press events to be triggered, usually checked by
1523 * {@link Launcher} to accept or block dpad-initiated long-presses.
1524 */
1525 public void setAllowLongPress(boolean allowLongPress) {
1526 mAllowLongPress = allowLongPress;
1527 }
1528
Winson Chung321e9ee2010-08-09 13:37:56 -07001529 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001530 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001531
1532 SavedState(Parcelable superState) {
1533 super(superState);
1534 }
1535
1536 private SavedState(Parcel in) {
1537 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001538 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 }
1540
1541 @Override
1542 public void writeToParcel(Parcel out, int flags) {
1543 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001544 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001545 }
1546
1547 public static final Parcelable.Creator<SavedState> CREATOR =
1548 new Parcelable.Creator<SavedState>() {
1549 public SavedState createFromParcel(Parcel in) {
1550 return new SavedState(in);
1551 }
1552
1553 public SavedState[] newArray(int size) {
1554 return new SavedState[size];
1555 }
1556 };
1557 }
1558
Winson Chungf314b0e2011-08-16 11:54:27 -07001559 protected void loadAssociatedPages(int page) {
1560 loadAssociatedPages(page, false);
1561 }
1562 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001563 if (mContentIsRefreshable) {
1564 final int count = getChildCount();
1565 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001566 int lowerPageBound = getAssociatedLowerPageBound(page);
1567 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001568 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1569 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001570 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001571 if ((i != page) && immediateAndOnly) {
1572 continue;
1573 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001574 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001575 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001576 if (lowerPageBound <= i && i <= upperPageBound) {
1577 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001578 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001579 mDirtyPageContent.set(i, false);
1580 }
1581 } else {
1582 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001583 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001584 }
1585 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001586 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001587 }
1588 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001589 }
1590 }
1591
Winson Chunge3193b92010-09-10 11:44:42 -07001592 protected int getAssociatedLowerPageBound(int page) {
1593 return Math.max(0, page - 1);
1594 }
1595 protected int getAssociatedUpperPageBound(int page) {
1596 final int count = getChildCount();
1597 return Math.min(page + 1, count - 1);
1598 }
1599
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001600 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001601 if (isChoiceMode(CHOICE_MODE_NONE)) {
1602 mChoiceMode = mode;
1603 mActionMode = startActionMode(callback);
1604 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001605 }
1606
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001607 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001608 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001609 mChoiceMode = CHOICE_MODE_NONE;
1610 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001611 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001612 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001613 }
1614 }
1615
1616 protected boolean isChoiceMode(int mode) {
1617 return mChoiceMode == mode;
1618 }
1619
1620 protected ArrayList<Checkable> getCheckedGrandchildren() {
1621 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1622 final int childCount = getChildCount();
1623 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001624 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001625 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001626 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001627 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001628 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001629 checked.add((Checkable) v);
1630 }
1631 }
1632 }
1633 return checked;
1634 }
1635
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001636 /**
1637 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1638 * Otherwise, returns null.
1639 */
1640 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001641 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001642 final int childCount = getChildCount();
1643 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001644 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001645 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001646 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001647 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001648 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1649 return (Checkable) v;
1650 }
1651 }
1652 }
1653 }
1654 return null;
1655 }
1656
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001657 protected void resetCheckedGrandchildren() {
1658 // loop through children, and set all of their children to _not_ be checked
1659 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1660 for (int i = 0; i < checked.size(); ++i) {
1661 final Checkable c = checked.get(i);
1662 c.setChecked(false);
1663 }
1664 }
1665
Winson Chung86f77532010-08-24 11:08:22 -07001666 /**
1667 * This method is called ONLY to synchronize the number of pages that the paged view has.
1668 * To actually fill the pages with information, implement syncPageItems() below. It is
1669 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1670 * and therefore, individual page items do not need to be updated in this method.
1671 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001672 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001673
1674 /**
1675 * This method is called to synchronize the items that are on a particular page. If views on
1676 * the page can be reused, then they should be updated within this method.
1677 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001678 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001679
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001680 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001681 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001682 }
1683 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001684 invalidatePageData(currentPage, false);
1685 }
1686 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001687 if (!mIsDataReady) {
1688 return;
1689 }
1690
Michael Jurka0142d492010-08-25 17:46:15 -07001691 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001692 // Force all scrolling-related behavior to end
1693 mScroller.forceFinished(true);
1694 mNextPage = INVALID_PAGE;
1695
Michael Jurka0142d492010-08-25 17:46:15 -07001696 // Update all the pages
1697 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001698
Winson Chung5a808352011-06-27 19:08:49 -07001699 // We must force a measure after we've loaded the pages to update the content width and
1700 // to determine the full scroll width
1701 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1702 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1703
1704 // Set a new page as the current page if necessary
1705 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001706 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001707 }
1708
Michael Jurka0142d492010-08-25 17:46:15 -07001709 // Mark each of the pages as dirty
1710 final int count = getChildCount();
1711 mDirtyPageContent.clear();
1712 for (int i = 0; i < count; ++i) {
1713 mDirtyPageContent.add(true);
1714 }
1715
1716 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001717 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001718 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001719 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001720 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001721 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001722 }
Winson Chung007c6982011-06-14 13:27:53 -07001723
1724 private ImageView getScrollingIndicator() {
1725 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1726 // found
1727 if (mHasScrollIndicator && mScrollIndicator == null) {
1728 ViewGroup parent = (ViewGroup) getParent();
1729 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1730 mHasScrollIndicator = mScrollIndicator != null;
1731 if (mHasScrollIndicator) {
1732 mScrollIndicator.setVisibility(View.VISIBLE);
1733 }
1734 }
1735 return mScrollIndicator;
1736 }
1737
1738 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001739 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001740 }
1741
Winson Chung5a808352011-06-27 19:08:49 -07001742 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1743 @Override
1744 public void run() {
1745 hideScrollingIndicator(false);
1746 }
1747 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001748 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001749 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001750 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001751 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001752 }
1753
Michael Jurka430e8a52011-08-08 15:52:14 -07001754 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001755 if (getChildCount() <= 1) return;
1756 if (!isScrollingIndicatorEnabled()) return;
1757
1758 getScrollingIndicator();
1759 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001760 // Fade the indicator in
1761 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001762 mScrollIndicator.setVisibility(View.VISIBLE);
1763 if (mScrollIndicatorAnimator != null) {
1764 mScrollIndicatorAnimator.cancel();
1765 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001766 if (immediately) {
1767 mScrollIndicator.setAlpha(1f);
1768 } else {
1769 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1770 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1771 mScrollIndicatorAnimator.start();
1772 }
Winson Chung007c6982011-06-14 13:27:53 -07001773 }
1774 }
1775
1776 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001777 if (getChildCount() <= 1) return;
1778 if (!isScrollingIndicatorEnabled()) return;
1779
1780 getScrollingIndicator();
1781 if (mScrollIndicator != null) {
1782 // Fade the indicator out
1783 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001784 if (mScrollIndicatorAnimator != null) {
1785 mScrollIndicatorAnimator.cancel();
1786 }
1787 if (immediately) {
1788 mScrollIndicator.setVisibility(View.GONE);
1789 mScrollIndicator.setAlpha(0f);
1790 } else {
1791 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1792 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1793 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1794 private boolean cancelled = false;
1795 @Override
1796 public void onAnimationCancel(android.animation.Animator animation) {
1797 cancelled = true;
1798 }
1799 @Override
1800 public void onAnimationEnd(Animator animation) {
1801 if (!cancelled) {
1802 mScrollIndicator.setVisibility(View.GONE);
1803 }
1804 }
1805 });
1806 mScrollIndicatorAnimator.start();
1807 }
Winson Chung007c6982011-06-14 13:27:53 -07001808 }
1809 }
1810
Winson Chung32174c82011-07-19 15:47:55 -07001811 /**
1812 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1813 * fill its space on the track or not.
1814 */
1815 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001816 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001817 }
1818
Winson Chung007c6982011-06-14 13:27:53 -07001819 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001820 if (getChildCount() <= 1) return;
1821 if (!isScrollingIndicatorEnabled()) return;
1822
1823 getScrollingIndicator();
1824 if (mScrollIndicator != null) {
1825 updateScrollingIndicatorPosition();
1826 }
1827 }
1828
Winson Chung007c6982011-06-14 13:27:53 -07001829 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001830 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001831 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001832 int numPages = getChildCount();
1833 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001834 int lastChildIndex = Math.max(0, getChildCount() - 1);
1835 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001836 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001837 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1838 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001839
Winson Chungae890b82011-09-13 18:08:54 -07001840 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001841 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001842 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001843 if (hasElasticScrollIndicator()) {
1844 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1845 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1846 mScrollIndicator.requestLayout();
1847 }
1848 } else {
1849 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1850 indicatorPos += indicatorCenterOffset;
1851 }
Winson Chung007c6982011-06-14 13:27:53 -07001852 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001853 mScrollIndicator.invalidate();
1854 }
1855
Winson Chung3ac74c52011-06-30 17:39:37 -07001856 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001857 }
1858
1859 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001860 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001861
1862 /* Accessibility */
1863 @Override
1864 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1865 super.onInitializeAccessibilityNodeInfo(info);
1866 info.setScrollable(true);
1867 }
1868
1869 @Override
1870 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1871 super.onInitializeAccessibilityEvent(event);
1872 event.setScrollable(true);
1873 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1874 event.setFromIndex(mCurrentPage);
1875 event.setToIndex(mCurrentPage);
1876 event.setItemCount(getChildCount());
1877 }
1878 }
1879
Winson Chung6a0f57d2011-06-29 20:10:49 -07001880 protected String getCurrentPageDescription() {
1881 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1882 return String.format(mContext.getString(R.string.default_scroll_format),
1883 page + 1, getChildCount());
1884 }
Winson Chungd11265e2011-08-30 13:37:23 -07001885
1886 @Override
1887 public boolean onHoverEvent(android.view.MotionEvent event) {
1888 return true;
1889 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001890}