blob: afb2b94f51dc1eae3afe1199041a84d944337f73 [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
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800274 * Updates the scroll of the current page immediately to its final scroll position. We use this
275 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
276 * the previous tab page.
277 */
278 protected void updateCurrentPageScroll() {
279 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
280 scrollTo(newX, 0);
281 mScroller.setFinalX(newX);
282 }
283
284 /**
Winson Chung86f77532010-08-24 11:08:22 -0700285 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 */
Winson Chung86f77532010-08-24 11:08:22 -0700287 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800288 if (!mScroller.isFinished()) {
289 mScroller.abortAnimation();
290 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800291 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
292 // the default
293 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800294 return;
295 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700296
Winson Chung86f77532010-08-24 11:08:22 -0700297 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800298 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700299 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700300 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800301 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 }
303
Michael Jurka0142d492010-08-25 17:46:15 -0700304 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700305 if (mPageSwitchListener != null) {
306 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 }
308 }
309
Michael Jurkace7e05f2011-02-01 22:02:35 -0800310 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700311 if (!mIsPageMoving) {
312 mIsPageMoving = true;
313 onPageBeginMoving();
314 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700315 }
316
Michael Jurkace7e05f2011-02-01 22:02:35 -0800317 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700318 if (mIsPageMoving) {
319 mIsPageMoving = false;
320 onPageEndMoving();
321 }
Michael Jurka0142d492010-08-25 17:46:15 -0700322 }
323
Adam Cohen26976d92011-03-22 15:33:33 -0700324 protected boolean isPageMoving() {
325 return mIsPageMoving;
326 }
327
Michael Jurka0142d492010-08-25 17:46:15 -0700328 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700329 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700330 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700331 }
332
333 // a method that subclasses can override to add behavior
334 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700335 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700336 }
337
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 /**
Winson Chung86f77532010-08-24 11:08:22 -0700339 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700340 *
341 * @param l The listener used to respond to long clicks.
342 */
343 @Override
344 public void setOnLongClickListener(OnLongClickListener l) {
345 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700346 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700348 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 }
350 }
351
352 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800353 public void scrollBy(int x, int y) {
354 scrollTo(mUnboundedScrollX + x, mScrollY + y);
355 }
356
357 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700358 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800359 mUnboundedScrollX = x;
360
361 if (x < 0) {
362 super.scrollTo(0, y);
363 if (mAllowOverScroll) {
364 overScroll(x);
365 }
366 } else if (x > mMaxScrollX) {
367 super.scrollTo(mMaxScrollX, y);
368 if (mAllowOverScroll) {
369 overScroll(x - mMaxScrollX);
370 }
371 } else {
372 super.scrollTo(x, y);
373 }
374
Michael Jurka0142d492010-08-25 17:46:15 -0700375 mTouchX = x;
376 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
377 }
378
379 // we moved this functionality to a helper function so SmoothPagedView can reuse it
380 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700382 // Don't bother scrolling if the page does not need to be moved
383 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
384 mDirtyPageAlpha = true;
385 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
386 }
Michael Jurka0142d492010-08-25 17:46:15 -0700387 invalidate();
388 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700389 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700390 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700391 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700392 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700393 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700394
395 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700396 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700397 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700398 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700399 }
400
Adam Cohen73aa9752010-11-24 16:26:58 -0800401 // We don't want to trigger a page end moving unless the page has settled
402 // and the user has stopped scrolling
403 if (mTouchState == TOUCH_STATE_REST) {
404 pageEndMoving();
405 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700406
407 // Notify the user when the page changes
408 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
409 AccessibilityEvent ev =
410 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
411 ev.getText().add(getCurrentPageDescription());
412 sendAccessibilityEventUnchecked(ev);
413 }
Michael Jurka0142d492010-08-25 17:46:15 -0700414 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700415 }
Michael Jurka0142d492010-08-25 17:46:15 -0700416 return false;
417 }
418
419 @Override
420 public void computeScroll() {
421 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 }
423
424 @Override
425 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700426 if (!mIsDataReady) {
427 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
428 return;
429 }
430
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
432 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
433 if (widthMode != MeasureSpec.EXACTLY) {
434 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
435 }
436
Adam Lesinski6b879f02010-11-04 16:15:23 -0700437 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
438 * of the All apps view on XLarge displays to not take up more space then it needs. Width
439 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
440 * each page to have the same width.
441 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700443 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
444 int maxChildHeight = 0;
445
446 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700447 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700448
Michael Jurka36fcb742011-04-06 17:08:58 -0700449
Winson Chung321e9ee2010-08-09 13:37:56 -0700450 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700451 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700452 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 final int childCount = getChildCount();
454 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700455 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700456 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700457 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
458
459 int childWidthMode;
460 if (lp.width == LayoutParams.WRAP_CONTENT) {
461 childWidthMode = MeasureSpec.AT_MOST;
462 } else {
463 childWidthMode = MeasureSpec.EXACTLY;
464 }
465
466 int childHeightMode;
467 if (lp.height == LayoutParams.WRAP_CONTENT) {
468 childHeightMode = MeasureSpec.AT_MOST;
469 } else {
470 childHeightMode = MeasureSpec.EXACTLY;
471 }
472
473 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700474 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700475 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700476 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700477
478 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700479 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700480 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
481 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700482 }
483
484 if (heightMode == MeasureSpec.AT_MOST) {
485 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
Winson Chungae890b82011-09-13 18:08:54 -0700487
488 updateScrollingIndicatorPosition();
489
490 setMeasuredDimension(widthSize, heightSize);
491
492 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions
Adam Cohenfaa28302010-11-19 12:02:18 -0800493 if (childCount > 0) {
494 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
495 } else {
496 mMaxScrollX = 0;
497 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
499
Michael Jurka8c920dd2011-01-20 14:16:56 -0800500 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800501 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
502 int delta = newX - mScrollX;
503
Michael Jurka8c920dd2011-01-20 14:16:56 -0800504 final int pageCount = getChildCount();
505 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700506 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800507 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800508 }
509 setCurrentPage(newCurrentPage);
510 }
511
Michael Jurka8c920dd2011-01-20 14:16:56 -0800512 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
513 // 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 -0800514 // tightens the layout accordingly
515 public void setLayoutScale(float childrenScale) {
516 mLayoutScale = childrenScale;
517
518 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
519 int childCount = getChildCount();
520 float childrenX[] = new float[childCount];
521 float childrenY[] = new float[childCount];
522 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700523 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 childrenX[i] = child.getX();
525 childrenY[i] = child.getY();
526 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700527 // Trigger a full re-layout (never just call onLayout directly!)
528 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
529 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
530 requestLayout();
531 measure(widthSpec, heightSpec);
532 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700534 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800535 child.setX(childrenX[i]);
536 child.setY(childrenY[i]);
537 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700538
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539 // Also, the page offset has changed (since the pages are now smaller);
540 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800541 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542 }
543
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700545 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700546 if (!mIsDataReady) {
547 return;
548 }
549
Winson Chung785d2eb2011-04-14 16:08:02 -0700550 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700551 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 final int childCount = getChildCount();
553 int childLeft = 0;
554 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700555 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
556 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700557 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700558
559 // Calculate the variable page spacing if necessary
560 if (mPageSpacing < 0) {
561 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
562 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564
565 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700566 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800568 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700569 final int childHeight = child.getMeasuredHeight();
570 int childTop = mPaddingTop;
571 if (mCenterPagesVertically) {
572 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
573 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800574
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700576 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800577 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700578 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 }
580 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700581
582 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
583 setHorizontalScrollBarEnabled(false);
584 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
585 scrollTo(newX, 0);
586 mScroller.setFinalX(newX);
587 setHorizontalScrollBarEnabled(true);
588 mFirstLayout = false;
589 }
590
Michael Jurkad3ef3062010-11-23 16:23:58 -0800591 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
592 mFirstLayout = false;
593 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
595
Winson Chung03929772011-02-23 17:07:10 -0800596 protected void forceUpdateAdjacentPagesAlpha() {
597 mDirtyPageAlpha = true;
598 updateAdjacentPagesAlpha();
599 }
600
Winson Chunge3193b92010-09-10 11:44:42 -0700601 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700602 if (mFadeInAdjacentScreens) {
603 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700604 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700605 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700606 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700607 final int childCount = getChildCount();
608 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700609 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800610 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700611 int halfChildWidth = (childWidth / 2);
612 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700613
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700614 // On the first layout, we may not have a width nor a proper offset, so for now
615 // we should just assume full page width (and calculate the offset according to
616 // that).
617 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700618 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700619 childCenter = (i * childWidth) + (childWidth / 2);
620 }
621
Winson Chunge8878e32010-09-15 20:37:09 -0700622 int d = halfChildWidth;
623 int distanceFromScreenCenter = childCenter - screenCenter;
624 if (distanceFromScreenCenter > 0) {
625 if (i > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700626 d += getScaledMeasuredWidth(getPageAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700627 } else {
628 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700629 }
Michael Jurka0142d492010-08-25 17:46:15 -0700630 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700631 if (i < childCount - 1) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700632 d += getScaledMeasuredWidth(getPageAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700633 } else {
634 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700635 }
Michael Jurka0142d492010-08-25 17:46:15 -0700636 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700637 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700638
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700639 // Preventing potential divide-by-zero
640 d = Math.max(1, d);
641
Winson Chunge8878e32010-09-15 20:37:09 -0700642 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
643 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
644 float alpha = 1.0f - dimAlpha;
645
Adam Cohenf34bab52010-09-30 14:11:56 -0700646 if (alpha < ALPHA_QUANTIZE_LEVEL) {
647 alpha = 0.0f;
648 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
649 alpha = 1.0f;
650 }
651
Michael Jurkaa40829a2011-02-16 18:02:45 -0800652 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
653 // this optimization causes alpha to not be properly updated sometimes (repro
654 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
655 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
656 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800657 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700658 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800659 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 }
Michael Jurka0142d492010-08-25 17:46:15 -0700661 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 }
663 }
Winson Chunge3193b92010-09-10 11:44:42 -0700664 }
665
Adam Cohenf34bab52010-09-30 14:11:56 -0700666 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700667 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700668 }
669
Winson Chunge3193b92010-09-10 11:44:42 -0700670 @Override
671 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700672 int halfScreenSize = getMeasuredWidth() / 2;
673 int screenCenter = mScrollX + halfScreenSize;
674
675 if (screenCenter != mLastScreenCenter) {
676 screenScrolled(screenCenter);
677 updateAdjacentPagesAlpha();
678 mLastScreenCenter = screenCenter;
679 }
Michael Jurka0142d492010-08-25 17:46:15 -0700680
681 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700682 // As an optimization, this code assumes that all pages have the same width as the 0th
683 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700684 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700685 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700686 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700687 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700688 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700689 int leftScreen = 0;
690 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700691 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700692 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700693 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700694 }
695 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700696 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700697 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700698 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700699 }
700 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700701
Michael Jurkac4fb9172010-09-02 17:19:20 -0700702 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800703 // Clip to the bounds
704 canvas.save();
705 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
706 mScrollY + mBottom - mTop);
707
Adam Cohen22f823d2011-09-01 17:22:18 -0700708 for (int i = rightScreen; i >= leftScreen; i--) {
709 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700710 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800711 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700712 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 }
714
715 @Override
716 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700717 int page = indexOfChild(child);
718 if (page != mCurrentPage || !mScroller.isFinished()) {
719 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 return true;
721 }
722 return false;
723 }
724
725 @Override
726 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700727 int focusablePage;
728 if (mNextPage != INVALID_PAGE) {
729 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700731 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 }
Winson Chung86f77532010-08-24 11:08:22 -0700733 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700735 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 }
737 return false;
738 }
739
740 @Override
741 public boolean dispatchUnhandledMove(View focused, int direction) {
742 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700743 if (getCurrentPage() > 0) {
744 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700745 return true;
746 }
747 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700748 if (getCurrentPage() < getPageCount() - 1) {
749 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 return true;
751 }
752 }
753 return super.dispatchUnhandledMove(focused, direction);
754 }
755
756 @Override
757 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700758 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
759 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 }
761 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700762 if (mCurrentPage > 0) {
763 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700766 if (mCurrentPage < getPageCount() - 1) {
767 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
769 }
770 }
771
772 /**
773 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700774 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 *
Winson Chung86f77532010-08-24 11:08:22 -0700776 * This happens when live folders requery, and if they're off page, they
777 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 */
779 @Override
780 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700781 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 View v = focused;
783 while (true) {
784 if (v == current) {
785 super.focusableViewAvailable(focused);
786 return;
787 }
788 if (v == this) {
789 return;
790 }
791 ViewParent parent = v.getParent();
792 if (parent instanceof View) {
793 v = (View)v.getParent();
794 } else {
795 return;
796 }
797 }
798 }
799
800 /**
801 * {@inheritDoc}
802 */
803 @Override
804 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
805 if (disallowIntercept) {
806 // We need to make sure to cancel our long press if
807 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700808 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700809 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 }
811 super.requestDisallowInterceptTouchEvent(disallowIntercept);
812 }
813
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800814 /**
815 * Return true if a tap at (x, y) should trigger a flip to the previous page.
816 */
817 protected boolean hitsPreviousPage(float x, float y) {
818 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
819 }
820
821 /**
822 * Return true if a tap at (x, y) should trigger a flip to the next page.
823 */
824 protected boolean hitsNextPage(float x, float y) {
825 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
826 }
827
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 @Override
829 public boolean onInterceptTouchEvent(MotionEvent ev) {
830 /*
831 * This method JUST determines whether we want to intercept the motion.
832 * If we return true, onTouchEvent will be called and we do the actual
833 * scrolling there.
834 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800835 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700836
Winson Chung45e1d6e2010-11-09 17:19:49 -0800837 // Skip touch handling if there are no pages to swipe
838 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
839
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 /*
841 * Shortcut the most recurring case: the user is in the dragging
842 * state and he is moving his finger. We want to intercept this
843 * motion.
844 */
845 final int action = ev.getAction();
846 if ((action == MotionEvent.ACTION_MOVE) &&
847 (mTouchState == TOUCH_STATE_SCROLLING)) {
848 return true;
849 }
850
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 switch (action & MotionEvent.ACTION_MASK) {
852 case MotionEvent.ACTION_MOVE: {
853 /*
854 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
855 * whether the user has moved far enough from his original down touch.
856 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700857 if (mActivePointerId != INVALID_POINTER) {
858 determineScrollingStart(ev);
859 break;
860 }
861 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
862 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
863 // i.e. fall through to the next case (don't break)
864 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
865 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 }
867
868 case MotionEvent.ACTION_DOWN: {
869 final float x = ev.getX();
870 final float y = ev.getY();
871 // Remember location of down touch
872 mDownMotionX = x;
873 mLastMotionX = x;
874 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800875 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800876 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 mActivePointerId = ev.getPointerId(0);
878 mAllowLongPress = true;
879
880 /*
881 * If being flinged and user touches the screen, initiate drag;
882 * otherwise don't. mScroller.isFinished should be false when
883 * being flinged.
884 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700885 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700886 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
887 if (finishedScrolling) {
888 mTouchState = TOUCH_STATE_REST;
889 mScroller.abortAnimation();
890 } else {
891 mTouchState = TOUCH_STATE_SCROLLING;
892 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700893
Winson Chung86f77532010-08-24 11:08:22 -0700894 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800896 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800898 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800900 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 mTouchState = TOUCH_STATE_NEXT_PAGE;
902 }
903 }
904 }
905 break;
906 }
907
Winson Chung321e9ee2010-08-09 13:37:56 -0700908 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800909 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 mTouchState = TOUCH_STATE_REST;
911 mAllowLongPress = false;
912 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800913 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 break;
915
916 case MotionEvent.ACTION_POINTER_UP:
917 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800918 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 break;
920 }
921
922 /*
923 * The only time we want to intercept motion events is if we are in the
924 * drag mode.
925 */
926 return mTouchState != TOUCH_STATE_REST;
927 }
928
Winson Chung80baf5a2010-08-09 16:03:15 -0700929 protected void animateClickFeedback(View v, final Runnable r) {
930 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800931 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
932 loadAnimator(mContext, R.anim.paged_view_click_feedback);
933 anim.setTarget(v);
934 anim.addListener(new AnimatorListenerAdapter() {
935 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700936 r.run();
937 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700938 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800939 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700940 }
941
Adam Cohenf8d28232011-02-01 21:47:00 -0800942 protected void determineScrollingStart(MotionEvent ev) {
943 determineScrollingStart(ev, 1.0f);
944 }
945
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 /*
947 * Determines if we should change the touch state to start scrolling after the
948 * user moves their touch point too far.
949 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800950 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 /*
952 * Locally do absolute value. mLastMotionX is set to the y value
953 * of the down event.
954 */
955 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700956 if (pointerIndex == -1) {
957 return;
958 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 final float x = ev.getX(pointerIndex);
960 final float y = ev.getY(pointerIndex);
961 final int xDiff = (int) Math.abs(x - mLastMotionX);
962 final int yDiff = (int) Math.abs(y - mLastMotionY);
963
Adam Cohenf8d28232011-02-01 21:47:00 -0800964 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 boolean xPaged = xDiff > mPagingTouchSlop;
966 boolean xMoved = xDiff > touchSlop;
967 boolean yMoved = yDiff > touchSlop;
968
Adam Cohenf8d28232011-02-01 21:47:00 -0800969 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700970 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 // Scroll if the user moved far enough along the X axis
972 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800973 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800975 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700976 mTouchX = mScrollX;
977 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
978 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 }
980 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800981 cancelCurrentPageLongPress();
982 }
983 }
984
985 protected void cancelCurrentPageLongPress() {
986 if (mAllowLongPress) {
987 mAllowLongPress = false;
988 // Try canceling the long press. It could also have been scheduled
989 // by a distant descendant, so use the mAllowLongPress flag to block
990 // everything
991 final View currentPage = getPageAt(mCurrentPage);
992 if (currentPage != null) {
993 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 }
995 }
996 }
997
Adam Cohenb5ba0972011-09-07 18:02:31 -0700998 protected float getScrollProgress(int screenCenter, View v, int page) {
999 final int halfScreenSize = getMeasuredWidth() / 2;
1000
1001 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1002 int delta = screenCenter - (getChildOffset(page) -
1003 getRelativeChildOffset(page) + halfScreenSize);
1004
1005 float scrollProgress = delta / (totalDistance * 1.0f);
1006 scrollProgress = Math.min(scrollProgress, 1.0f);
1007 scrollProgress = Math.max(scrollProgress, -1.0f);
1008 return scrollProgress;
1009 }
1010
Adam Cohene0f66b52010-11-23 15:06:07 -08001011 // This curve determines how the effect of scrolling over the limits of the page dimishes
1012 // as the user pulls further and further from the bounds
1013 private float overScrollInfluenceCurve(float f) {
1014 f -= 1.0f;
1015 return f * f * f + 1.0f;
1016 }
1017
Adam Cohenb5ba0972011-09-07 18:02:31 -07001018 protected void acceleratedOverScroll(float amount) {
1019 int screenSize = getMeasuredWidth();
1020
1021 // We want to reach the max over scroll effect when the user has
1022 // over scrolled half the size of the screen
1023 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1024
1025 if (f == 0) return;
1026
1027 // Clamp this factor, f, to -1 < f < 1
1028 if (Math.abs(f) >= 1) {
1029 f /= Math.abs(f);
1030 }
1031
1032 int overScrollAmount = (int) Math.round(f * screenSize);
1033 if (amount < 0) {
1034 mScrollX = overScrollAmount;
1035 } else {
1036 mScrollX = mMaxScrollX + overScrollAmount;
1037 }
1038 invalidate();
1039 }
1040
1041 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001042 int screenSize = getMeasuredWidth();
1043
1044 float f = (amount / screenSize);
1045
1046 if (f == 0) return;
1047 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1048
Adam Cohen7bfc9792011-01-28 13:52:37 -08001049 // Clamp this factor, f, to -1 < f < 1
1050 if (Math.abs(f) >= 1) {
1051 f /= Math.abs(f);
1052 }
1053
Adam Cohene0f66b52010-11-23 15:06:07 -08001054 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001055 if (amount < 0) {
1056 mScrollX = overScrollAmount;
1057 } else {
1058 mScrollX = mMaxScrollX + overScrollAmount;
1059 }
1060 invalidate();
1061 }
1062
Adam Cohenb5ba0972011-09-07 18:02:31 -07001063 protected void overScroll(float amount) {
1064 dampedOverScroll(amount);
1065 }
1066
Michael Jurkac5b262c2011-01-12 20:24:50 -08001067 protected float maxOverScroll() {
1068 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001069 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001070 float f = 1.0f;
1071 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1072 return OVERSCROLL_DAMP_FACTOR * f;
1073 }
1074
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 @Override
1076 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001077 // Skip touch handling if there are no pages to swipe
1078 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1079
Michael Jurkab8f06722010-10-10 15:58:46 -07001080 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001081
1082 final int action = ev.getAction();
1083
1084 switch (action & MotionEvent.ACTION_MASK) {
1085 case MotionEvent.ACTION_DOWN:
1086 /*
1087 * If being flinged and user touches, stop the fling. isFinished
1088 * will be false if being flinged.
1089 */
1090 if (!mScroller.isFinished()) {
1091 mScroller.abortAnimation();
1092 }
1093
1094 // Remember where the motion event started
1095 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001096 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001097 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001098 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001099 if (mTouchState == TOUCH_STATE_SCROLLING) {
1100 pageBeginMoving();
1101 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 break;
1103
1104 case MotionEvent.ACTION_MOVE:
1105 if (mTouchState == TOUCH_STATE_SCROLLING) {
1106 // Scroll to follow the motion event
1107 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1108 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001109 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001110
Adam Cohenaefd4e12011-02-14 16:39:38 -08001111 mTotalMotionX += Math.abs(deltaX);
1112
Winson Chungc0844aa2011-02-02 15:25:58 -08001113 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1114 // keep the remainder because we are actually testing if we've moved from the last
1115 // scrolled position (which is discrete).
1116 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001117 mTouchX += deltaX;
1118 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1119 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001120 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001121 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001122 } else {
1123 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001125 mLastMotionX = x;
1126 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 } else {
1128 awakenScrollBars();
1129 }
Adam Cohen564976a2010-10-13 18:52:07 -07001130 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 determineScrollingStart(ev);
1132 }
1133 break;
1134
1135 case MotionEvent.ACTION_UP:
1136 if (mTouchState == TOUCH_STATE_SCROLLING) {
1137 final int activePointerId = mActivePointerId;
1138 final int pointerIndex = ev.findPointerIndex(activePointerId);
1139 final float x = ev.getX(pointerIndex);
1140 final VelocityTracker velocityTracker = mVelocityTracker;
1141 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1142 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001143 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001144 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001145 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001146
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001147 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1148
Adam Cohenaefd4e12011-02-14 16:39:38 -08001149 // In the case that the page is moved far to one direction and then is flung
1150 // in the opposite direction, we use a threshold to determine whether we should
1151 // just return to the starting page, or if we should skip one further.
1152 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001153 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001154 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001155 Math.signum(velocityX) != Math.signum(deltaX)) {
1156 returnToOriginalPage = true;
1157 }
1158
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001159 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001160 Math.abs(velocityX) > snapVelocity;
1161
1162 int finalPage;
1163 // We give flings precedence over large moves, which is why we short-circuit our
1164 // test for a large move if a fling has been registered. That is, a large
1165 // move to the left and fling to the right will register as a fling to the right.
1166 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1167 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1168 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1169 snapToPageWithVelocity(finalPage, velocityX);
1170 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1171 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001172 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001173 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1174 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 } else {
1176 snapToDestination();
1177 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001178 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 // at this point we have not moved beyond the touch slop
1180 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1181 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001182 int nextPage = Math.max(0, mCurrentPage - 1);
1183 if (nextPage != mCurrentPage) {
1184 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 } else {
1186 snapToDestination();
1187 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001188 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 // at this point we have not moved beyond the touch slop
1190 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1191 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001192 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1193 if (nextPage != mCurrentPage) {
1194 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 } else {
1196 snapToDestination();
1197 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001198 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001199 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 }
1201 mTouchState = TOUCH_STATE_REST;
1202 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001203 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 break;
1205
1206 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001207 if (mTouchState == TOUCH_STATE_SCROLLING) {
1208 snapToDestination();
1209 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 mTouchState = TOUCH_STATE_REST;
1211 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001212 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 break;
1214
1215 case MotionEvent.ACTION_POINTER_UP:
1216 onSecondaryPointerUp(ev);
1217 break;
1218 }
1219
1220 return true;
1221 }
1222
Winson Chung185d7162011-02-28 13:47:29 -08001223 @Override
1224 public boolean onGenericMotionEvent(MotionEvent event) {
1225 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1226 switch (event.getAction()) {
1227 case MotionEvent.ACTION_SCROLL: {
1228 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1229 final float vscroll;
1230 final float hscroll;
1231 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1232 vscroll = 0;
1233 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1234 } else {
1235 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1236 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1237 }
1238 if (hscroll != 0 || vscroll != 0) {
1239 if (hscroll > 0 || vscroll > 0) {
1240 scrollRight();
1241 } else {
1242 scrollLeft();
1243 }
1244 return true;
1245 }
1246 }
1247 }
1248 }
1249 return super.onGenericMotionEvent(event);
1250 }
1251
Michael Jurkab8f06722010-10-10 15:58:46 -07001252 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1253 if (mVelocityTracker == null) {
1254 mVelocityTracker = VelocityTracker.obtain();
1255 }
1256 mVelocityTracker.addMovement(ev);
1257 }
1258
1259 private void releaseVelocityTracker() {
1260 if (mVelocityTracker != null) {
1261 mVelocityTracker.recycle();
1262 mVelocityTracker = null;
1263 }
1264 }
1265
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 private void onSecondaryPointerUp(MotionEvent ev) {
1267 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1268 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1269 final int pointerId = ev.getPointerId(pointerIndex);
1270 if (pointerId == mActivePointerId) {
1271 // This was our active pointer going up. Choose a new
1272 // active pointer and adjust accordingly.
1273 // TODO: Make this decision more intelligent.
1274 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1275 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1276 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001277 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 mActivePointerId = ev.getPointerId(newPointerIndex);
1279 if (mVelocityTracker != null) {
1280 mVelocityTracker.clear();
1281 }
1282 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001283 }
1284
Michael Jurkad771c962011-08-09 15:00:48 -07001285 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001286
1287 @Override
1288 public void requestChildFocus(View child, View focused) {
1289 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001290 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001291 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001292 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 }
1294 }
1295
Winson Chunge3193b92010-09-10 11:44:42 -07001296 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1297 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001298 int left;
1299 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001300 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001301 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001302 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001303 if (left <= relativeOffset && relativeOffset <= right) {
1304 return i;
1305 }
Winson Chunge3193b92010-09-10 11:44:42 -07001306 }
1307 return -1;
1308 }
1309
Winson Chung1908d072011-02-24 18:09:44 -08001310 protected void setMinimumWidthOverride(int minimumWidth) {
1311 mMinimumWidth = minimumWidth;
1312 }
Winson Chung34b23d52011-03-18 11:29:34 -07001313 protected void resetMinimumWidthOverride() {
1314 mMinimumWidth = 0;
1315 }
Winson Chung1908d072011-02-24 18:09:44 -08001316
1317 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001318 // This functions are called enough times that it actually makes a difference in the
1319 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001320 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001321 final int minWidth = mMinimumWidth;
1322 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001323 }
1324
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001326 int padding = mPaddingLeft + mPaddingRight;
1327 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001328 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001329 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001330 int padding = mPaddingLeft + mPaddingRight;
1331 return mPaddingLeft + (getMeasuredWidth() - padding -
Adam Cohen22f823d2011-09-01 17:22:18 -07001332 getScaledMeasuredWidth(getPageAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001333 }
1334
Winson Chung321e9ee2010-08-09 13:37:56 -07001335 protected int getChildOffset(int index) {
1336 if (getChildCount() == 0)
1337 return 0;
1338
1339 int offset = getRelativeChildOffset(0);
1340 for (int i = 0; i < index; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001341 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 }
1343 return offset;
1344 }
1345
Michael Jurkad3ef3062010-11-23 16:23:58 -08001346 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001347 // This functions are called enough times that it actually makes a difference in the
1348 // profiler -- so just inline the max() here
1349 final int measuredWidth = child.getMeasuredWidth();
1350 final int minWidth = mMinimumWidth;
1351 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1352 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001353 }
1354
Adam Cohend19d3ca2010-09-15 14:43:42 -07001355 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001356 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 int minDistanceFromScreenCenterIndex = -1;
1358 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1359 final int childCount = getChildCount();
1360 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001361 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001362 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 int halfChildWidth = (childWidth / 2);
1364 int childCenter = getChildOffset(i) + halfChildWidth;
1365 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1366 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1367 minDistanceFromScreenCenter = distanceFromScreenCenter;
1368 minDistanceFromScreenCenterIndex = i;
1369 }
1370 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001371 return minDistanceFromScreenCenterIndex;
1372 }
1373
1374 protected void snapToDestination() {
1375 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 }
1377
Adam Cohene0f66b52010-11-23 15:06:07 -08001378 private static class ScrollInterpolator implements Interpolator {
1379 public ScrollInterpolator() {
1380 }
1381
1382 public float getInterpolation(float t) {
1383 t -= 1.0f;
1384 return t*t*t*t*t + 1;
1385 }
1386 }
1387
1388 // We want the duration of the page snap animation to be influenced by the distance that
1389 // the screen has to travel, however, we don't want this duration to be effected in a
1390 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1391 // of travel has on the overall snap duration.
1392 float distanceInfluenceForSnapDuration(float f) {
1393 f -= 0.5f; // center the values about 0.
1394 f *= 0.3f * Math.PI / 2.0f;
1395 return (float) Math.sin(f);
1396 }
1397
Michael Jurka0142d492010-08-25 17:46:15 -07001398 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001399 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1400 int halfScreenSize = getMeasuredWidth() / 2;
1401
Winson Chung785d2eb2011-04-14 16:08:02 -07001402 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1403 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1404 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001405 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1406 int delta = newX - mUnboundedScrollX;
1407 int duration = 0;
1408
1409 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1410 // If the velocity is low enough, then treat this more as an automatic page advance
1411 // as opposed to an apparent physical response to flinging
1412 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1413 return;
1414 }
1415
1416 // Here we compute a "distance" that will be used in the computation of the overall
1417 // snap duration. This is a function of the actual distance that needs to be traveled;
1418 // we keep this value close to half screen size in order to reduce the variance in snap
1419 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001420 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001421 float distance = halfScreenSize + halfScreenSize *
1422 distanceInfluenceForSnapDuration(distanceRatio);
1423
1424 velocity = Math.abs(velocity);
1425 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1426
1427 // we want the page's snap velocity to approximately match the velocity at which the
1428 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001429 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1430 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001431
1432 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001433 }
1434
1435 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001436 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 }
1438
Michael Jurka0142d492010-08-25 17:46:15 -07001439 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001440 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001441
Winson Chung785d2eb2011-04-14 16:08:02 -07001442 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1443 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1444 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001445 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001446 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001447 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001448 snapToPage(whichPage, delta, duration);
1449 }
1450
1451 protected void snapToPage(int whichPage, int delta, int duration) {
1452 mNextPage = whichPage;
1453
1454 View focusedChild = getFocusedChild();
1455 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001456 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001457 focusedChild.clearFocus();
1458 }
1459
1460 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 awakenScrollBars(duration);
1462 if (duration == 0) {
1463 duration = Math.abs(delta);
1464 }
1465
1466 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001467 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001468
Winson Chungb44b5242011-06-13 11:32:14 -07001469 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1470 // loading associated pages until the scroll settles
1471 if (mDeferScrollUpdate) {
1472 loadAssociatedPages(mNextPage);
1473 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001474 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001475 }
Michael Jurka0142d492010-08-25 17:46:15 -07001476 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001477 invalidate();
1478 }
1479
Winson Chung321e9ee2010-08-09 13:37:56 -07001480 public void scrollLeft() {
1481 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001482 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001484 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 }
1486 }
1487
1488 public void scrollRight() {
1489 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001490 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001492 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 }
1494 }
1495
Winson Chung86f77532010-08-24 11:08:22 -07001496 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 int result = -1;
1498 if (v != null) {
1499 ViewParent vp = v.getParent();
1500 int count = getChildCount();
1501 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001502 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001503 return i;
1504 }
1505 }
1506 }
1507 return result;
1508 }
1509
1510 /**
1511 * @return True is long presses are still allowed for the current touch
1512 */
1513 public boolean allowLongPress() {
1514 return mAllowLongPress;
1515 }
1516
Michael Jurka0142d492010-08-25 17:46:15 -07001517 /**
1518 * Set true to allow long-press events to be triggered, usually checked by
1519 * {@link Launcher} to accept or block dpad-initiated long-presses.
1520 */
1521 public void setAllowLongPress(boolean allowLongPress) {
1522 mAllowLongPress = allowLongPress;
1523 }
1524
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001526 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001527
1528 SavedState(Parcelable superState) {
1529 super(superState);
1530 }
1531
1532 private SavedState(Parcel in) {
1533 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001534 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001535 }
1536
1537 @Override
1538 public void writeToParcel(Parcel out, int flags) {
1539 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001540 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001541 }
1542
1543 public static final Parcelable.Creator<SavedState> CREATOR =
1544 new Parcelable.Creator<SavedState>() {
1545 public SavedState createFromParcel(Parcel in) {
1546 return new SavedState(in);
1547 }
1548
1549 public SavedState[] newArray(int size) {
1550 return new SavedState[size];
1551 }
1552 };
1553 }
1554
Winson Chungf314b0e2011-08-16 11:54:27 -07001555 protected void loadAssociatedPages(int page) {
1556 loadAssociatedPages(page, false);
1557 }
1558 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001559 if (mContentIsRefreshable) {
1560 final int count = getChildCount();
1561 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001562 int lowerPageBound = getAssociatedLowerPageBound(page);
1563 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001564 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1565 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001566 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001567 if ((i != page) && immediateAndOnly) {
1568 continue;
1569 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001570 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001571 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001572 if (lowerPageBound <= i && i <= upperPageBound) {
1573 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001574 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001575 mDirtyPageContent.set(i, false);
1576 }
1577 } else {
1578 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001579 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001580 }
1581 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001582 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001583 }
1584 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001585 }
1586 }
1587
Winson Chunge3193b92010-09-10 11:44:42 -07001588 protected int getAssociatedLowerPageBound(int page) {
1589 return Math.max(0, page - 1);
1590 }
1591 protected int getAssociatedUpperPageBound(int page) {
1592 final int count = getChildCount();
1593 return Math.min(page + 1, count - 1);
1594 }
1595
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001596 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001597 if (isChoiceMode(CHOICE_MODE_NONE)) {
1598 mChoiceMode = mode;
1599 mActionMode = startActionMode(callback);
1600 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001601 }
1602
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001603 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001604 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001605 mChoiceMode = CHOICE_MODE_NONE;
1606 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001607 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001608 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001609 }
1610 }
1611
1612 protected boolean isChoiceMode(int mode) {
1613 return mChoiceMode == mode;
1614 }
1615
1616 protected ArrayList<Checkable> getCheckedGrandchildren() {
1617 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1618 final int childCount = getChildCount();
1619 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001620 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001621 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001622 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001623 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001624 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001625 checked.add((Checkable) v);
1626 }
1627 }
1628 }
1629 return checked;
1630 }
1631
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001632 /**
1633 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1634 * Otherwise, returns null.
1635 */
1636 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001637 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001638 final int childCount = getChildCount();
1639 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001640 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001641 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001642 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001643 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001644 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1645 return (Checkable) v;
1646 }
1647 }
1648 }
1649 }
1650 return null;
1651 }
1652
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001653 protected void resetCheckedGrandchildren() {
1654 // loop through children, and set all of their children to _not_ be checked
1655 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1656 for (int i = 0; i < checked.size(); ++i) {
1657 final Checkable c = checked.get(i);
1658 c.setChecked(false);
1659 }
1660 }
1661
Winson Chung86f77532010-08-24 11:08:22 -07001662 /**
1663 * This method is called ONLY to synchronize the number of pages that the paged view has.
1664 * To actually fill the pages with information, implement syncPageItems() below. It is
1665 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1666 * and therefore, individual page items do not need to be updated in this method.
1667 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001668 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001669
1670 /**
1671 * This method is called to synchronize the items that are on a particular page. If views on
1672 * the page can be reused, then they should be updated within this method.
1673 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001674 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001675
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001676 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001677 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001678 }
1679 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001680 invalidatePageData(currentPage, false);
1681 }
1682 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001683 if (!mIsDataReady) {
1684 return;
1685 }
1686
Michael Jurka0142d492010-08-25 17:46:15 -07001687 if (mContentIsRefreshable) {
1688 // Update all the pages
1689 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001690
Winson Chung5a808352011-06-27 19:08:49 -07001691 // We must force a measure after we've loaded the pages to update the content width and
1692 // to determine the full scroll width
1693 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1694 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1695
1696 // Set a new page as the current page if necessary
1697 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001698 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001699 }
1700
Michael Jurka0142d492010-08-25 17:46:15 -07001701 // Mark each of the pages as dirty
1702 final int count = getChildCount();
1703 mDirtyPageContent.clear();
1704 for (int i = 0; i < count; ++i) {
1705 mDirtyPageContent.add(true);
1706 }
1707
1708 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001709 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001710 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001711 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001712 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001713 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001714 }
Winson Chung007c6982011-06-14 13:27:53 -07001715
1716 private ImageView getScrollingIndicator() {
1717 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1718 // found
1719 if (mHasScrollIndicator && mScrollIndicator == null) {
1720 ViewGroup parent = (ViewGroup) getParent();
1721 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1722 mHasScrollIndicator = mScrollIndicator != null;
1723 if (mHasScrollIndicator) {
1724 mScrollIndicator.setVisibility(View.VISIBLE);
1725 }
1726 }
1727 return mScrollIndicator;
1728 }
1729
1730 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001731 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001732 }
1733
Winson Chung5a808352011-06-27 19:08:49 -07001734 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1735 @Override
1736 public void run() {
1737 hideScrollingIndicator(false);
1738 }
1739 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001740 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001741 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001742 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001743 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001744 }
1745
Michael Jurka430e8a52011-08-08 15:52:14 -07001746 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001747 if (getChildCount() <= 1) return;
1748 if (!isScrollingIndicatorEnabled()) return;
1749
1750 getScrollingIndicator();
1751 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001752 // Fade the indicator in
1753 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001754 mScrollIndicator.setVisibility(View.VISIBLE);
1755 if (mScrollIndicatorAnimator != null) {
1756 mScrollIndicatorAnimator.cancel();
1757 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001758 if (immediately) {
1759 mScrollIndicator.setAlpha(1f);
1760 } else {
1761 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1762 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1763 mScrollIndicatorAnimator.start();
1764 }
Winson Chung007c6982011-06-14 13:27:53 -07001765 }
1766 }
1767
1768 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001769 if (getChildCount() <= 1) return;
1770 if (!isScrollingIndicatorEnabled()) return;
1771
1772 getScrollingIndicator();
1773 if (mScrollIndicator != null) {
1774 // Fade the indicator out
1775 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001776 if (mScrollIndicatorAnimator != null) {
1777 mScrollIndicatorAnimator.cancel();
1778 }
1779 if (immediately) {
1780 mScrollIndicator.setVisibility(View.GONE);
1781 mScrollIndicator.setAlpha(0f);
1782 } else {
1783 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1784 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1785 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1786 private boolean cancelled = false;
1787 @Override
1788 public void onAnimationCancel(android.animation.Animator animation) {
1789 cancelled = true;
1790 }
1791 @Override
1792 public void onAnimationEnd(Animator animation) {
1793 if (!cancelled) {
1794 mScrollIndicator.setVisibility(View.GONE);
1795 }
1796 }
1797 });
1798 mScrollIndicatorAnimator.start();
1799 }
Winson Chung007c6982011-06-14 13:27:53 -07001800 }
1801 }
1802
Winson Chung32174c82011-07-19 15:47:55 -07001803 /**
1804 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1805 * fill its space on the track or not.
1806 */
1807 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001808 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001809 }
1810
Winson Chung007c6982011-06-14 13:27:53 -07001811 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001812 if (getChildCount() <= 1) return;
1813 if (!isScrollingIndicatorEnabled()) return;
1814
1815 getScrollingIndicator();
1816 if (mScrollIndicator != null) {
1817 updateScrollingIndicatorPosition();
1818 }
1819 }
1820
Winson Chung007c6982011-06-14 13:27:53 -07001821 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001822 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001823 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001824 int numPages = getChildCount();
1825 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001826 int lastChildIndex = Math.max(0, getChildCount() - 1);
1827 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001828 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001829 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1830 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001831
Winson Chungae890b82011-09-13 18:08:54 -07001832 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001833 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001834 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001835 if (hasElasticScrollIndicator()) {
1836 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1837 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1838 mScrollIndicator.requestLayout();
1839 }
1840 } else {
1841 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1842 indicatorPos += indicatorCenterOffset;
1843 }
Winson Chung007c6982011-06-14 13:27:53 -07001844 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001845 mScrollIndicator.invalidate();
1846 }
1847
Winson Chung3ac74c52011-06-30 17:39:37 -07001848 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001849 }
1850
1851 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001852 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001853
1854 /* Accessibility */
1855 @Override
1856 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1857 super.onInitializeAccessibilityNodeInfo(info);
1858 info.setScrollable(true);
1859 }
1860
1861 @Override
1862 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1863 super.onInitializeAccessibilityEvent(event);
1864 event.setScrollable(true);
1865 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1866 event.setFromIndex(mCurrentPage);
1867 event.setToIndex(mCurrentPage);
1868 event.setItemCount(getChildCount());
1869 }
1870 }
1871
Winson Chung6a0f57d2011-06-29 20:10:49 -07001872 protected String getCurrentPageDescription() {
1873 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1874 return String.format(mContext.getString(R.string.default_scroll_format),
1875 page + 1, getChildCount());
1876 }
Winson Chungd11265e2011-08-30 13:37:23 -07001877
1878 @Override
1879 public boolean onHoverEvent(android.view.MotionEvent event) {
1880 return true;
1881 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001882}