blob: adfe0de8135825e8cd603d37b376b7ae7dc00c2a [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;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070022import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070024import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080031import android.view.InputDevice;
32import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070039import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070040import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import java.util.ArrayList;
48
Winson Chung321e9ee2010-08-09 13:37:56 -070049/**
50 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070051 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070052 */
Michael Jurka8b805b12012-04-18 14:23:14 -070053public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070054 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070055 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chung86f77532010-08-24 11:08:22 -070058 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070059 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Winson Chungf0c6ae02012-03-21 16:10:31 -070061 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
62 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohenb5ba0972011-09-07 18:02:31 -070065 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070066 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080067
Adam Cohenb64cb5a2011-02-15 13:53:42 -080068 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080069 // The page is moved more than halfway, automatically move to the next page on touch up.
70 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080071
Adam Cohen265b9a62011-12-07 14:37:18 -080072 // The following constants need to be scaled based on density. The scaled versions will be
73 // assigned to the corresponding member variables below.
74 private static final int FLING_THRESHOLD_VELOCITY = 500;
75 private static final int MIN_SNAP_VELOCITY = 1500;
76 private static final int MIN_FLING_VELOCITY = 250;
77
78 protected int mFlingThresholdVelocity;
79 protected int mMinFlingVelocity;
80 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070081
Adam Cohenb5ba0972011-09-07 18:02:31 -070082 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070083 protected float mSmoothingTime;
84 protected float mTouchX;
85
86 protected boolean mFirstLayout = true;
87
88 protected int mCurrentPage;
89 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080090 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070092 private VelocityTracker mVelocityTracker;
93
94 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080095 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080096 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080098 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070099 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700100 private int[] mChildOffsets;
101 private int[] mChildRelativeOffsets;
102 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected final static int TOUCH_STATE_REST = 0;
105 protected final static int TOUCH_STATE_SCROLLING = 1;
106 protected final static int TOUCH_STATE_PREV_PAGE = 2;
107 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700108 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700111 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka7426c422010-11-11 15:23:47 -0800115 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 private int mPagingTouchSlop;
119 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800120 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700121 protected int mPageSpacing;
122 protected int mPageLayoutPaddingTop;
123 protected int mPageLayoutPaddingBottom;
124 protected int mPageLayoutPaddingLeft;
125 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700126 protected int mPageLayoutWidthGap;
127 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700128 protected int mCellCountX = 0;
129 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700130 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800131 protected boolean mAllowOverScroll = true;
132 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800133 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka8b805b12012-04-18 14:23:14 -0700135 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800136 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
137 // the screens from continuing to translate beyond the normal bounds.
138 protected int mOverScrollX;
139
Michael Jurka8c920dd2011-01-20 14:16:56 -0800140 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800141 protected float mLayoutScale = 1.0f;
142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka5f1c5092010-09-03 14:15:02 -0700145 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Winson Chung86f77532010-08-24 11:08:22 -0700147 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurkae326f182011-11-21 14:05:46 -0800149 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Michael Jurka0142d492010-08-25 17:46:15 -0700151 // If true, syncPages and syncPageItems will be called to refresh pages
152 protected boolean mContentIsRefreshable = true;
153
154 // If true, modify alpha of neighboring pages as user scrolls left/right
155 protected boolean mFadeInAdjacentScreens = true;
156
157 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
158 // to switch to a new page
159 protected boolean mUsePagingTouchSlop = true;
160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700162 // (SmoothPagedView does this)
163 protected boolean mDeferScrollUpdate = false;
164
Patrick Dubroy1262e362010-10-06 15:49:50 -0700165 protected boolean mIsPageMoving = false;
166
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 // All syncs and layout passes are deferred until data is ready.
168 protected boolean mIsDataReady = false;
169
Winson Chung007c6982011-06-14 13:27:53 -0700170 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700171 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800172 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700173 private int mScrollIndicatorPaddingLeft;
174 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700175 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800176 private boolean mShouldShowScrollIndicator = false;
177 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700178 protected static final int sScrollIndicatorFadeInDuration = 150;
179 protected static final int sScrollIndicatorFadeOutDuration = 650;
180 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700181
Winson Chungb44b5242011-06-13 11:32:14 -0700182 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700183 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700184
Winson Chung86f77532010-08-24 11:08:22 -0700185 public interface PageSwitchListener {
186 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 }
188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 public PagedView(Context context) {
190 this(context, null);
191 }
192
193 public PagedView(Context context, AttributeSet attrs) {
194 this(context, attrs, 0);
195 }
196
197 public PagedView(Context context, AttributeSet attrs, int defStyle) {
198 super(context, attrs, defStyle);
199
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800202 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800204 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800206 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700211 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700213 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700214 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700215 mScrollIndicatorPaddingLeft =
216 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
217 mScrollIndicatorPaddingRight =
218 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700219 a.recycle();
220
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700222 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224
225 /**
226 * Initializes various states for this workspace.
227 */
Michael Jurka0142d492010-08-25 17:46:15 -0700228 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700229 mDirtyPageContent = new ArrayList<Boolean>();
230 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800231 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700232 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700233 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700234
235 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
236 mTouchSlop = configuration.getScaledTouchSlop();
237 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
238 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700239 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800240
241 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
242 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
243 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700244 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246
Winson Chung86f77532010-08-24 11:08:22 -0700247 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
248 mPageSwitchListener = pageSwitchListener;
249 if (mPageSwitchListener != null) {
250 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 }
252 }
253
254 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700255 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
256 * out pages.
257 */
258 protected void setDataIsReady() {
259 mIsDataReady = true;
260 }
261 protected boolean isDataReady() {
262 return mIsDataReady;
263 }
264
265 /**
Winson Chung86f77532010-08-24 11:08:22 -0700266 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 *
Winson Chung86f77532010-08-24 11:08:22 -0700268 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 */
Winson Chung86f77532010-08-24 11:08:22 -0700270 int getCurrentPage() {
271 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 }
Winson Chung360e63f2012-04-27 13:48:05 -0700273 int getNextPage() {
274 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
275 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700276
Winson Chung86f77532010-08-24 11:08:22 -0700277 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 return getChildCount();
279 }
280
Winson Chung86f77532010-08-24 11:08:22 -0700281 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 return getChildAt(index);
283 }
284
Adam Cohenae4f1552011-10-20 00:15:42 -0700285 protected int indexToPage(int index) {
286 return index;
287 }
288
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800290 * Updates the scroll of the current page immediately to its final scroll position. We use this
291 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
292 * the previous tab page.
293 */
294 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700295 int offset = getChildOffset(mCurrentPage);
296 int relOffset = getRelativeChildOffset(mCurrentPage);
297 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800298 scrollTo(newX, 0);
299 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800300 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800301 }
302
303 /**
Winson Chung86f77532010-08-24 11:08:22 -0700304 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 */
Winson Chung86f77532010-08-24 11:08:22 -0700306 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800307 if (!mScroller.isFinished()) {
308 mScroller.abortAnimation();
309 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800310 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
311 // the default
312 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800313 return;
314 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700315
Winson Chung86f77532010-08-24 11:08:22 -0700316 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800317 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700318 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700319 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800320 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 }
322
Michael Jurka0142d492010-08-25 17:46:15 -0700323 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700324 if (mPageSwitchListener != null) {
325 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 }
327 }
328
Michael Jurkace7e05f2011-02-01 22:02:35 -0800329 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700330 if (!mIsPageMoving) {
331 mIsPageMoving = true;
332 onPageBeginMoving();
333 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700334 }
335
Michael Jurkace7e05f2011-02-01 22:02:35 -0800336 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700337 if (mIsPageMoving) {
338 mIsPageMoving = false;
339 onPageEndMoving();
340 }
Michael Jurka0142d492010-08-25 17:46:15 -0700341 }
342
Adam Cohen26976d92011-03-22 15:33:33 -0700343 protected boolean isPageMoving() {
344 return mIsPageMoving;
345 }
346
Michael Jurka0142d492010-08-25 17:46:15 -0700347 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700348 protected void onPageBeginMoving() {
349 }
350
351 // a method that subclasses can override to add behavior
352 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700353 }
354
Winson Chung321e9ee2010-08-09 13:37:56 -0700355 /**
Winson Chung86f77532010-08-24 11:08:22 -0700356 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 *
358 * @param l The listener used to respond to long clicks.
359 */
360 @Override
361 public void setOnLongClickListener(OnLongClickListener l) {
362 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700363 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700365 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 }
367 }
368
369 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800370 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700371 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800372 }
373
374 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700375 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800376 mUnboundedScrollX = x;
377
378 if (x < 0) {
379 super.scrollTo(0, y);
380 if (mAllowOverScroll) {
381 overScroll(x);
382 }
383 } else if (x > mMaxScrollX) {
384 super.scrollTo(mMaxScrollX, y);
385 if (mAllowOverScroll) {
386 overScroll(x - mMaxScrollX);
387 }
388 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800389 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800390 super.scrollTo(x, y);
391 }
392
Michael Jurka0142d492010-08-25 17:46:15 -0700393 mTouchX = x;
394 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
395 }
396
397 // we moved this functionality to a helper function so SmoothPagedView can reuse it
398 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700400 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700401 if (getScrollX() != mScroller.getCurrX()
402 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700403 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700404 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
405 }
Michael Jurka0142d492010-08-25 17:46:15 -0700406 invalidate();
407 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700408 } else if (mNextPage != INVALID_PAGE) {
409 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700410 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700411 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700412
413 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700414 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700415 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700416 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700417 }
418
Adam Cohen73aa9752010-11-24 16:26:58 -0800419 // We don't want to trigger a page end moving unless the page has settled
420 // and the user has stopped scrolling
421 if (mTouchState == TOUCH_STATE_REST) {
422 pageEndMoving();
423 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700424
425 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700426 AccessibilityManager accessibilityManager = (AccessibilityManager)
427 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
428 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700429 AccessibilityEvent ev =
430 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
431 ev.getText().add(getCurrentPageDescription());
432 sendAccessibilityEventUnchecked(ev);
433 }
Michael Jurka0142d492010-08-25 17:46:15 -0700434 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
Michael Jurka0142d492010-08-25 17:46:15 -0700436 return false;
437 }
438
439 @Override
440 public void computeScroll() {
441 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 }
443
444 @Override
445 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 if (!mIsDataReady) {
447 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
448 return;
449 }
450
Winson Chung321e9ee2010-08-09 13:37:56 -0700451 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
452 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
453 if (widthMode != MeasureSpec.EXACTLY) {
454 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
455 }
456
Adam Lesinski6b879f02010-11-04 16:15:23 -0700457 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
458 * of the All apps view on XLarge displays to not take up more space then it needs. Width
459 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
460 * each page to have the same width.
461 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700463 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
464 int maxChildHeight = 0;
465
Michael Jurka8b805b12012-04-18 14:23:14 -0700466 final int verticalPadding = getPaddingTop() + getPaddingBottom();
467 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700468
Michael Jurka36fcb742011-04-06 17:08:58 -0700469
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700471 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700472 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700473 final int childCount = getChildCount();
474 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700475 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700476 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700477 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
478
479 int childWidthMode;
480 if (lp.width == LayoutParams.WRAP_CONTENT) {
481 childWidthMode = MeasureSpec.AT_MOST;
482 } else {
483 childWidthMode = MeasureSpec.EXACTLY;
484 }
485
486 int childHeightMode;
487 if (lp.height == LayoutParams.WRAP_CONTENT) {
488 childHeightMode = MeasureSpec.AT_MOST;
489 } else {
490 childHeightMode = MeasureSpec.EXACTLY;
491 }
492
493 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700494 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700495 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700496 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700497
498 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700499 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700500 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
501 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700502 }
503
504 if (heightMode == MeasureSpec.AT_MOST) {
505 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
Winson Chungae890b82011-09-13 18:08:54 -0700507
Winson Chungae890b82011-09-13 18:08:54 -0700508 setMeasuredDimension(widthSize, heightSize);
509
Winson Chunga128a7b2012-04-30 15:23:15 -0700510 if (childCount > 0) {
511 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
512 + getChildWidth(0));
513
514 // Calculate the variable page spacing if necessary
515 if (mPageSpacing < 0) {
516 // The gap between pages in the PagedView should be equal to the gap from the page
517 // to the edge of the screen (so it is not visible in the current screen). To
518 // account for unequal padding on each side of the paged view, we take the maximum
519 // of the left/right gap and use that as the gap between each page.
520 int offset = getRelativeChildOffset(0);
521 int spacing = Math.max(offset, widthSize - offset -
522 getChildAt(0).getMeasuredWidth());
523 setPageSpacing(spacing);
524 }
525 }
526
Adam Cohen25b29952011-11-02 14:49:06 -0700527 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
528 // We also wait until we set the measured dimensions before flushing the cache as well, to
529 // ensure that the cache is filled with good values.
530 invalidateCachedOffsets();
531 updateScrollingIndicatorPosition();
532
Adam Cohenfaa28302010-11-19 12:02:18 -0800533 if (childCount > 0) {
534 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
535 } else {
536 mMaxScrollX = 0;
537 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700538 }
539
Michael Jurka8c920dd2011-01-20 14:16:56 -0800540 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800541 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700542 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800543
Michael Jurka8c920dd2011-01-20 14:16:56 -0800544 final int pageCount = getChildCount();
545 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700546 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800547 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800548 }
549 setCurrentPage(newCurrentPage);
550 }
551
Michael Jurka8c920dd2011-01-20 14:16:56 -0800552 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
553 // 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 -0800554 // tightens the layout accordingly
555 public void setLayoutScale(float childrenScale) {
556 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700557 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558
559 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
560 int childCount = getChildCount();
561 float childrenX[] = new float[childCount];
562 float childrenY[] = new float[childCount];
563 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700564 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800565 childrenX[i] = child.getX();
566 childrenY[i] = child.getY();
567 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700568 // Trigger a full re-layout (never just call onLayout directly!)
569 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
570 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
571 requestLayout();
572 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800574 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700575 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800576 child.setX(childrenX[i]);
577 child.setY(childrenY[i]);
578 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700579
Michael Jurkad3ef3062010-11-23 16:23:58 -0800580 // Also, the page offset has changed (since the pages are now smaller);
581 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800582 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800583 }
584
Adam Cohen60b07122011-11-14 17:26:06 -0800585 public void setPageSpacing(int pageSpacing) {
586 mPageSpacing = pageSpacing;
587 invalidateCachedOffsets();
588 }
589
Winson Chung321e9ee2010-08-09 13:37:56 -0700590 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700591 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700592 if (!mIsDataReady) {
593 return;
594 }
595
Winson Chung785d2eb2011-04-14 16:08:02 -0700596 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700597 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700599 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700600
601 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700602 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800604 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700605 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700606 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700607 if (mCenterPagesVertically) {
608 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
609 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800610
Winson Chung785d2eb2011-04-14 16:08:02 -0700611 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700612 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800613 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700614 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 }
616 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700617
618 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
619 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800620 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700621 setHorizontalScrollBarEnabled(true);
622 mFirstLayout = false;
623 }
Winson Chunge3193b92010-09-10 11:44:42 -0700624 }
625
Adam Cohenf34bab52010-09-30 14:11:56 -0700626 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700627 if (isScrollingIndicatorEnabled()) {
628 updateScrollingIndicator();
629 }
630 if (mFadeInAdjacentScreens) {
631 for (int i = 0; i < getChildCount(); i++) {
632 View child = getChildAt(i);
633 if (child != null) {
634 float scrollProgress = getScrollProgress(screenCenter, child, i);
635 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800636 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700637 }
638 }
639 invalidate();
640 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700641 }
642
Winson Chunge3193b92010-09-10 11:44:42 -0700643 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700644 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700645 // This ensures that when children are added, they get the correct transforms / alphas
646 // in accordance with any scroll effects.
647 mForceScreenScrolled = true;
648 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700649 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700650 }
651
Michael Jurka8b805b12012-04-18 14:23:14 -0700652 @Override
653 public void onChildViewRemoved(View parent, View child) {
654 }
655
Adam Cohen73894962011-10-31 13:17:17 -0700656 protected void invalidateCachedOffsets() {
657 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700658 if (count == 0) {
659 mChildOffsets = null;
660 mChildRelativeOffsets = null;
661 mChildOffsetsWithLayoutScale = null;
662 return;
663 }
Adam Cohen73894962011-10-31 13:17:17 -0700664
665 mChildOffsets = new int[count];
666 mChildRelativeOffsets = new int[count];
667 mChildOffsetsWithLayoutScale = new int[count];
668 for (int i = 0; i < count; i++) {
669 mChildOffsets[i] = -1;
670 mChildRelativeOffsets[i] = -1;
671 mChildOffsetsWithLayoutScale[i] = -1;
672 }
673 }
674
675 protected int getChildOffset(int index) {
676 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
677 mChildOffsets : mChildOffsetsWithLayoutScale;
678
679 if (childOffsets != null && childOffsets[index] != -1) {
680 return childOffsets[index];
681 } else {
682 if (getChildCount() == 0)
683 return 0;
684
685 int offset = getRelativeChildOffset(0);
686 for (int i = 0; i < index; ++i) {
687 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
688 }
689 if (childOffsets != null) {
690 childOffsets[index] = offset;
691 }
692 return offset;
693 }
694 }
695
696 protected int getRelativeChildOffset(int index) {
697 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
698 return mChildRelativeOffsets[index];
699 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 final int padding = getPaddingLeft() + getPaddingRight();
701 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700702 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
703 if (mChildRelativeOffsets != null) {
704 mChildRelativeOffsets[index] = offset;
705 }
706 return offset;
707 }
708 }
709
Adam Cohen73894962011-10-31 13:17:17 -0700710 protected int getScaledMeasuredWidth(View child) {
711 // This functions are called enough times that it actually makes a difference in the
712 // profiler -- so just inline the max() here
713 final int measuredWidth = child.getMeasuredWidth();
714 final int minWidth = mMinimumWidth;
715 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
716 return (int) (maxWidth * mLayoutScale + 0.5f);
717 }
718
Michael Jurkadde558b2011-11-09 22:09:06 -0800719 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700720 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700721
Michael Jurkac4fb9172010-09-02 17:19:20 -0700722 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700723 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700724 int leftScreen = 0;
725 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800726 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800727 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 currPage.getX() + currPage.getWidth() -
729 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700730 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800731 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700732 }
733 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800734 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800735 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700737 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800738 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700739 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800740 range[0] = leftScreen;
741 range[1] = rightScreen;
742 } else {
743 range[0] = -1;
744 range[1] = -1;
745 }
746 }
747
748 @Override
749 protected void dispatchDraw(Canvas canvas) {
750 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
752 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800753 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800754
755 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700756 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
757 // set it for the next frame
758 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800759 screenScrolled(screenCenter);
760 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800761 }
762
763 // Find out which screens are visible; as an optimization we only call draw on them
764 final int pageCount = getChildCount();
765 if (pageCount > 0) {
766 getVisiblePages(mTempVisiblePagesRange);
767 final int leftScreen = mTempVisiblePagesRange[0];
768 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800769 if (leftScreen != -1 && rightScreen != -1) {
770 final long drawingTime = getDrawingTime();
771 // Clip to the bounds
772 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
774 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700775
Michael Jurka80c69852011-12-16 14:16:32 -0800776 // On certain graphics drivers, if you draw to a off-screen buffer that's not
777 // used, it can lead to poor performance. We were running into this when
778 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
779 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
780 // As a fix, below we set pages that aren't going to be rendered are to be
781 // View.INVISIBLE, preventing re-drawing of their hardware layer
782 for (int i = getChildCount() - 1; i >= 0; i--) {
783 final View v = getPageAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 if (leftScreen <= i && i <= rightScreen && v.getAlpha() > 0) {
Michael Jurka80c69852011-12-16 14:16:32 -0800785 v.setVisibility(VISIBLE);
786 drawChild(canvas, v, drawingTime);
787 } else {
788 v.setVisibility(INVISIBLE);
789 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800790 }
791 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700792 }
Michael Jurka0142d492010-08-25 17:46:15 -0700793 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 }
795
796 @Override
797 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700798 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700799 if (page != mCurrentPage || !mScroller.isFinished()) {
800 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 return true;
802 }
803 return false;
804 }
805
806 @Override
807 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700808 int focusablePage;
809 if (mNextPage != INVALID_PAGE) {
810 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700812 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 }
Winson Chung86f77532010-08-24 11:08:22 -0700814 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700816 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 }
818 return false;
819 }
820
821 @Override
822 public boolean dispatchUnhandledMove(View focused, int direction) {
823 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700824 if (getCurrentPage() > 0) {
825 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 return true;
827 }
828 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700829 if (getCurrentPage() < getPageCount() - 1) {
830 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 return true;
832 }
833 }
834 return super.dispatchUnhandledMove(focused, direction);
835 }
836
837 @Override
838 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700839 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
840 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 }
842 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700843 if (mCurrentPage > 0) {
844 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 }
846 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700847 if (mCurrentPage < getPageCount() - 1) {
848 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 }
850 }
851 }
852
853 /**
854 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700855 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 *
Winson Chung86f77532010-08-24 11:08:22 -0700857 * This happens when live folders requery, and if they're off page, they
858 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 */
860 @Override
861 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700862 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 View v = focused;
864 while (true) {
865 if (v == current) {
866 super.focusableViewAvailable(focused);
867 return;
868 }
869 if (v == this) {
870 return;
871 }
872 ViewParent parent = v.getParent();
873 if (parent instanceof View) {
874 v = (View)v.getParent();
875 } else {
876 return;
877 }
878 }
879 }
880
881 /**
882 * {@inheritDoc}
883 */
884 @Override
885 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
886 if (disallowIntercept) {
887 // We need to make sure to cancel our long press if
888 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700889 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700890 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 }
892 super.requestDisallowInterceptTouchEvent(disallowIntercept);
893 }
894
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800895 /**
896 * Return true if a tap at (x, y) should trigger a flip to the previous page.
897 */
898 protected boolean hitsPreviousPage(float x, float y) {
899 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
900 }
901
902 /**
903 * Return true if a tap at (x, y) should trigger a flip to the next page.
904 */
905 protected boolean hitsNextPage(float x, float y) {
906 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
907 }
908
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 @Override
910 public boolean onInterceptTouchEvent(MotionEvent ev) {
911 /*
912 * This method JUST determines whether we want to intercept the motion.
913 * If we return true, onTouchEvent will be called and we do the actual
914 * scrolling there.
915 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800916 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700917
Winson Chung45e1d6e2010-11-09 17:19:49 -0800918 // Skip touch handling if there are no pages to swipe
919 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
920
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 /*
922 * Shortcut the most recurring case: the user is in the dragging
923 * state and he is moving his finger. We want to intercept this
924 * motion.
925 */
926 final int action = ev.getAction();
927 if ((action == MotionEvent.ACTION_MOVE) &&
928 (mTouchState == TOUCH_STATE_SCROLLING)) {
929 return true;
930 }
931
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 switch (action & MotionEvent.ACTION_MASK) {
933 case MotionEvent.ACTION_MOVE: {
934 /*
935 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
936 * whether the user has moved far enough from his original down touch.
937 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700938 if (mActivePointerId != INVALID_POINTER) {
939 determineScrollingStart(ev);
940 break;
941 }
942 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
943 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
944 // i.e. fall through to the next case (don't break)
945 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
946 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 }
948
949 case MotionEvent.ACTION_DOWN: {
950 final float x = ev.getX();
951 final float y = ev.getY();
952 // Remember location of down touch
953 mDownMotionX = x;
954 mLastMotionX = x;
955 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800956 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800957 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 mActivePointerId = ev.getPointerId(0);
959 mAllowLongPress = true;
960
961 /*
962 * If being flinged and user touches the screen, initiate drag;
963 * otherwise don't. mScroller.isFinished should be false when
964 * being flinged.
965 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700966 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700967 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
968 if (finishedScrolling) {
969 mTouchState = TOUCH_STATE_REST;
970 mScroller.abortAnimation();
971 } else {
972 mTouchState = TOUCH_STATE_SCROLLING;
973 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700974
Winson Chung86f77532010-08-24 11:08:22 -0700975 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800977 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800979 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800981 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 mTouchState = TOUCH_STATE_NEXT_PAGE;
983 }
984 }
985 }
986 break;
987 }
988
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800990 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 mTouchState = TOUCH_STATE_REST;
992 mAllowLongPress = false;
993 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800994 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 break;
996
997 case MotionEvent.ACTION_POINTER_UP:
998 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800999 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 break;
1001 }
1002
1003 /*
1004 * The only time we want to intercept motion events is if we are in the
1005 * drag mode.
1006 */
1007 return mTouchState != TOUCH_STATE_REST;
1008 }
1009
Adam Cohenf8d28232011-02-01 21:47:00 -08001010 protected void determineScrollingStart(MotionEvent ev) {
1011 determineScrollingStart(ev, 1.0f);
1012 }
1013
Winson Chung321e9ee2010-08-09 13:37:56 -07001014 /*
1015 * Determines if we should change the touch state to start scrolling after the
1016 * user moves their touch point too far.
1017 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001018 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 /*
1020 * Locally do absolute value. mLastMotionX is set to the y value
1021 * of the down event.
1022 */
1023 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001024 if (pointerIndex == -1) {
1025 return;
1026 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 final float x = ev.getX(pointerIndex);
1028 final float y = ev.getY(pointerIndex);
1029 final int xDiff = (int) Math.abs(x - mLastMotionX);
1030 final int yDiff = (int) Math.abs(y - mLastMotionY);
1031
Adam Cohenf8d28232011-02-01 21:47:00 -08001032 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 boolean xPaged = xDiff > mPagingTouchSlop;
1034 boolean xMoved = xDiff > touchSlop;
1035 boolean yMoved = yDiff > touchSlop;
1036
Adam Cohenf8d28232011-02-01 21:47:00 -08001037 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001038 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 // Scroll if the user moved far enough along the X axis
1040 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001041 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001043 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001044 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001045 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1046 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001047 }
1048 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001049 cancelCurrentPageLongPress();
1050 }
1051 }
1052
1053 protected void cancelCurrentPageLongPress() {
1054 if (mAllowLongPress) {
1055 mAllowLongPress = false;
1056 // Try canceling the long press. It could also have been scheduled
1057 // by a distant descendant, so use the mAllowLongPress flag to block
1058 // everything
1059 final View currentPage = getPageAt(mCurrentPage);
1060 if (currentPage != null) {
1061 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 }
1063 }
1064 }
1065
Adam Cohenb5ba0972011-09-07 18:02:31 -07001066 protected float getScrollProgress(int screenCenter, View v, int page) {
1067 final int halfScreenSize = getMeasuredWidth() / 2;
1068
1069 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1070 int delta = screenCenter - (getChildOffset(page) -
1071 getRelativeChildOffset(page) + halfScreenSize);
1072
1073 float scrollProgress = delta / (totalDistance * 1.0f);
1074 scrollProgress = Math.min(scrollProgress, 1.0f);
1075 scrollProgress = Math.max(scrollProgress, -1.0f);
1076 return scrollProgress;
1077 }
1078
Adam Cohene0f66b52010-11-23 15:06:07 -08001079 // This curve determines how the effect of scrolling over the limits of the page dimishes
1080 // as the user pulls further and further from the bounds
1081 private float overScrollInfluenceCurve(float f) {
1082 f -= 1.0f;
1083 return f * f * f + 1.0f;
1084 }
1085
Adam Cohenb5ba0972011-09-07 18:02:31 -07001086 protected void acceleratedOverScroll(float amount) {
1087 int screenSize = getMeasuredWidth();
1088
1089 // We want to reach the max over scroll effect when the user has
1090 // over scrolled half the size of the screen
1091 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1092
1093 if (f == 0) return;
1094
1095 // Clamp this factor, f, to -1 < f < 1
1096 if (Math.abs(f) >= 1) {
1097 f /= Math.abs(f);
1098 }
1099
1100 int overScrollAmount = (int) Math.round(f * screenSize);
1101 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001102 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001103 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001104 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001105 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001106 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001107 }
1108 invalidate();
1109 }
1110
1111 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001112 int screenSize = getMeasuredWidth();
1113
1114 float f = (amount / screenSize);
1115
1116 if (f == 0) return;
1117 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1118
Adam Cohen7bfc9792011-01-28 13:52:37 -08001119 // Clamp this factor, f, to -1 < f < 1
1120 if (Math.abs(f) >= 1) {
1121 f /= Math.abs(f);
1122 }
1123
Adam Cohene0f66b52010-11-23 15:06:07 -08001124 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001125 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001126 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001127 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001128 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001129 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001130 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001131 }
1132 invalidate();
1133 }
1134
Adam Cohenb5ba0972011-09-07 18:02:31 -07001135 protected void overScroll(float amount) {
1136 dampedOverScroll(amount);
1137 }
1138
Michael Jurkac5b262c2011-01-12 20:24:50 -08001139 protected float maxOverScroll() {
1140 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001141 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001142 float f = 1.0f;
1143 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1144 return OVERSCROLL_DAMP_FACTOR * f;
1145 }
1146
Winson Chung321e9ee2010-08-09 13:37:56 -07001147 @Override
1148 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001149 // Skip touch handling if there are no pages to swipe
1150 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1151
Michael Jurkab8f06722010-10-10 15:58:46 -07001152 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001153
1154 final int action = ev.getAction();
1155
1156 switch (action & MotionEvent.ACTION_MASK) {
1157 case MotionEvent.ACTION_DOWN:
1158 /*
1159 * If being flinged and user touches, stop the fling. isFinished
1160 * will be false if being flinged.
1161 */
1162 if (!mScroller.isFinished()) {
1163 mScroller.abortAnimation();
1164 }
1165
1166 // Remember where the motion event started
1167 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001168 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001169 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001171 if (mTouchState == TOUCH_STATE_SCROLLING) {
1172 pageBeginMoving();
1173 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001174 break;
1175
1176 case MotionEvent.ACTION_MOVE:
1177 if (mTouchState == TOUCH_STATE_SCROLLING) {
1178 // Scroll to follow the motion event
1179 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1180 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001181 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001182
Adam Cohenaefd4e12011-02-14 16:39:38 -08001183 mTotalMotionX += Math.abs(deltaX);
1184
Winson Chungc0844aa2011-02-02 15:25:58 -08001185 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1186 // keep the remainder because we are actually testing if we've moved from the last
1187 // scrolled position (which is discrete).
1188 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001189 mTouchX += deltaX;
1190 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1191 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001192 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001193 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001194 } else {
1195 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001197 mLastMotionX = x;
1198 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 } else {
1200 awakenScrollBars();
1201 }
Adam Cohen564976a2010-10-13 18:52:07 -07001202 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 determineScrollingStart(ev);
1204 }
1205 break;
1206
1207 case MotionEvent.ACTION_UP:
1208 if (mTouchState == TOUCH_STATE_SCROLLING) {
1209 final int activePointerId = mActivePointerId;
1210 final int pointerIndex = ev.findPointerIndex(activePointerId);
1211 final float x = ev.getX(pointerIndex);
1212 final VelocityTracker velocityTracker = mVelocityTracker;
1213 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1214 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001215 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001216 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1217 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1218 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001219
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001220 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1221
Adam Cohen00481b32011-11-18 12:03:48 -08001222 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001223 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001224
Adam Cohenaefd4e12011-02-14 16:39:38 -08001225 // In the case that the page is moved far to one direction and then is flung
1226 // in the opposite direction, we use a threshold to determine whether we should
1227 // just return to the starting page, or if we should skip one further.
1228 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001229 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001230 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001231 returnToOriginalPage = true;
1232 }
1233
Adam Cohenaefd4e12011-02-14 16:39:38 -08001234 int finalPage;
1235 // We give flings precedence over large moves, which is why we short-circuit our
1236 // test for a large move if a fling has been registered. That is, a large
1237 // move to the left and fling to the right will register as a fling to the right.
1238 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1239 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1240 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1241 snapToPageWithVelocity(finalPage, velocityX);
1242 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1243 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001244 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001245 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1246 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 } else {
1248 snapToDestination();
1249 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001250 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 // at this point we have not moved beyond the touch slop
1252 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1253 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001254 int nextPage = Math.max(0, mCurrentPage - 1);
1255 if (nextPage != mCurrentPage) {
1256 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 } else {
1258 snapToDestination();
1259 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001260 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 // at this point we have not moved beyond the touch slop
1262 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1263 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001264 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1265 if (nextPage != mCurrentPage) {
1266 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 } else {
1268 snapToDestination();
1269 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001270 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001271 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273 mTouchState = TOUCH_STATE_REST;
1274 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001275 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 break;
1277
1278 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001279 if (mTouchState == TOUCH_STATE_SCROLLING) {
1280 snapToDestination();
1281 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 mTouchState = TOUCH_STATE_REST;
1283 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001284 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 break;
1286
1287 case MotionEvent.ACTION_POINTER_UP:
1288 onSecondaryPointerUp(ev);
1289 break;
1290 }
1291
1292 return true;
1293 }
1294
Winson Chung185d7162011-02-28 13:47:29 -08001295 @Override
1296 public boolean onGenericMotionEvent(MotionEvent event) {
1297 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1298 switch (event.getAction()) {
1299 case MotionEvent.ACTION_SCROLL: {
1300 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1301 final float vscroll;
1302 final float hscroll;
1303 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1304 vscroll = 0;
1305 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1306 } else {
1307 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1308 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1309 }
1310 if (hscroll != 0 || vscroll != 0) {
1311 if (hscroll > 0 || vscroll > 0) {
1312 scrollRight();
1313 } else {
1314 scrollLeft();
1315 }
1316 return true;
1317 }
1318 }
1319 }
1320 }
1321 return super.onGenericMotionEvent(event);
1322 }
1323
Michael Jurkab8f06722010-10-10 15:58:46 -07001324 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1325 if (mVelocityTracker == null) {
1326 mVelocityTracker = VelocityTracker.obtain();
1327 }
1328 mVelocityTracker.addMovement(ev);
1329 }
1330
1331 private void releaseVelocityTracker() {
1332 if (mVelocityTracker != null) {
1333 mVelocityTracker.recycle();
1334 mVelocityTracker = null;
1335 }
1336 }
1337
Winson Chung321e9ee2010-08-09 13:37:56 -07001338 private void onSecondaryPointerUp(MotionEvent ev) {
1339 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1340 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1341 final int pointerId = ev.getPointerId(pointerIndex);
1342 if (pointerId == mActivePointerId) {
1343 // This was our active pointer going up. Choose a new
1344 // active pointer and adjust accordingly.
1345 // TODO: Make this decision more intelligent.
1346 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1347 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1348 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001349 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 mActivePointerId = ev.getPointerId(newPointerIndex);
1351 if (mVelocityTracker != null) {
1352 mVelocityTracker.clear();
1353 }
1354 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001355 }
1356
Michael Jurkad771c962011-08-09 15:00:48 -07001357 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001358
1359 @Override
1360 public void requestChildFocus(View child, View focused) {
1361 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001362 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001363 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001364 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001365 }
1366 }
1367
Winson Chunge3193b92010-09-10 11:44:42 -07001368 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1369 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001370 int left;
1371 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001372 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001373 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001374 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001375 if (left <= relativeOffset && relativeOffset <= right) {
1376 return i;
1377 }
Winson Chunge3193b92010-09-10 11:44:42 -07001378 }
1379 return -1;
1380 }
1381
Winson Chung1908d072011-02-24 18:09:44 -08001382 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001383 // This functions are called enough times that it actually makes a difference in the
1384 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001385 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001386 final int minWidth = mMinimumWidth;
1387 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001388 }
1389
Adam Cohend19d3ca2010-09-15 14:43:42 -07001390 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001391 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001392 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001393 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 final int childCount = getChildCount();
1395 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001396 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001397 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 int halfChildWidth = (childWidth / 2);
1399 int childCenter = getChildOffset(i) + halfChildWidth;
1400 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1401 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1402 minDistanceFromScreenCenter = distanceFromScreenCenter;
1403 minDistanceFromScreenCenterIndex = i;
1404 }
1405 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001406 return minDistanceFromScreenCenterIndex;
1407 }
1408
1409 protected void snapToDestination() {
1410 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 }
1412
Adam Cohene0f66b52010-11-23 15:06:07 -08001413 private static class ScrollInterpolator implements Interpolator {
1414 public ScrollInterpolator() {
1415 }
1416
1417 public float getInterpolation(float t) {
1418 t -= 1.0f;
1419 return t*t*t*t*t + 1;
1420 }
1421 }
1422
1423 // We want the duration of the page snap animation to be influenced by the distance that
1424 // the screen has to travel, however, we don't want this duration to be effected in a
1425 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1426 // of travel has on the overall snap duration.
1427 float distanceInfluenceForSnapDuration(float f) {
1428 f -= 0.5f; // center the values about 0.
1429 f *= 0.3f * Math.PI / 2.0f;
1430 return (float) Math.sin(f);
1431 }
1432
Michael Jurka0142d492010-08-25 17:46:15 -07001433 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001434 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1435 int halfScreenSize = getMeasuredWidth() / 2;
1436
Winson Chung785d2eb2011-04-14 16:08:02 -07001437 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1438 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1439 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001440 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1441 int delta = newX - mUnboundedScrollX;
1442 int duration = 0;
1443
Adam Cohen265b9a62011-12-07 14:37:18 -08001444 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001445 // If the velocity is low enough, then treat this more as an automatic page advance
1446 // as opposed to an apparent physical response to flinging
1447 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1448 return;
1449 }
1450
1451 // Here we compute a "distance" that will be used in the computation of the overall
1452 // snap duration. This is a function of the actual distance that needs to be traveled;
1453 // we keep this value close to half screen size in order to reduce the variance in snap
1454 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001455 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001456 float distance = halfScreenSize + halfScreenSize *
1457 distanceInfluenceForSnapDuration(distanceRatio);
1458
1459 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001460 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001461
1462 // we want the page's snap velocity to approximately match the velocity at which the
1463 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001464 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1465 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001466
1467 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001468 }
1469
1470 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001471 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 }
1473
Michael Jurka0142d492010-08-25 17:46:15 -07001474 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001475 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001476
Winson Chung785d2eb2011-04-14 16:08:02 -07001477 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1478 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1479 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001480 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001481 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001483 snapToPage(whichPage, delta, duration);
1484 }
1485
1486 protected void snapToPage(int whichPage, int delta, int duration) {
1487 mNextPage = whichPage;
1488
1489 View focusedChild = getFocusedChild();
1490 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001491 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001492 focusedChild.clearFocus();
1493 }
1494
1495 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001496 awakenScrollBars(duration);
1497 if (duration == 0) {
1498 duration = Math.abs(delta);
1499 }
1500
1501 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001502 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001503
Winson Chungb44b5242011-06-13 11:32:14 -07001504 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1505 // loading associated pages until the scroll settles
1506 if (mDeferScrollUpdate) {
1507 loadAssociatedPages(mNextPage);
1508 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001509 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001510 }
Michael Jurka0142d492010-08-25 17:46:15 -07001511 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 invalidate();
1513 }
1514
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 public void scrollLeft() {
1516 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001517 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001519 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001520 }
1521 }
1522
1523 public void scrollRight() {
1524 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001525 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001526 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001527 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 }
1529 }
1530
Winson Chung86f77532010-08-24 11:08:22 -07001531 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001532 int result = -1;
1533 if (v != null) {
1534 ViewParent vp = v.getParent();
1535 int count = getChildCount();
1536 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001537 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001538 return i;
1539 }
1540 }
1541 }
1542 return result;
1543 }
1544
1545 /**
1546 * @return True is long presses are still allowed for the current touch
1547 */
1548 public boolean allowLongPress() {
1549 return mAllowLongPress;
1550 }
1551
Michael Jurka0142d492010-08-25 17:46:15 -07001552 /**
1553 * Set true to allow long-press events to be triggered, usually checked by
1554 * {@link Launcher} to accept or block dpad-initiated long-presses.
1555 */
1556 public void setAllowLongPress(boolean allowLongPress) {
1557 mAllowLongPress = allowLongPress;
1558 }
1559
Winson Chung321e9ee2010-08-09 13:37:56 -07001560 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001561 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001562
1563 SavedState(Parcelable superState) {
1564 super(superState);
1565 }
1566
1567 private SavedState(Parcel in) {
1568 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001569 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001570 }
1571
1572 @Override
1573 public void writeToParcel(Parcel out, int flags) {
1574 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001575 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001576 }
1577
1578 public static final Parcelable.Creator<SavedState> CREATOR =
1579 new Parcelable.Creator<SavedState>() {
1580 public SavedState createFromParcel(Parcel in) {
1581 return new SavedState(in);
1582 }
1583
1584 public SavedState[] newArray(int size) {
1585 return new SavedState[size];
1586 }
1587 };
1588 }
1589
Winson Chungf314b0e2011-08-16 11:54:27 -07001590 protected void loadAssociatedPages(int page) {
1591 loadAssociatedPages(page, false);
1592 }
1593 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001594 if (mContentIsRefreshable) {
1595 final int count = getChildCount();
1596 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001597 int lowerPageBound = getAssociatedLowerPageBound(page);
1598 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001599 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1600 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001601 // First, clear any pages that should no longer be loaded
1602 for (int i = 0; i < count; ++i) {
1603 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001604 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001605 if (layout.getPageChildCount() > 0) {
1606 layout.removeAllViewsOnPage();
1607 }
1608 mDirtyPageContent.set(i, true);
1609 }
1610 }
1611 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001612 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001613 if ((i != page) && immediateAndOnly) {
1614 continue;
1615 }
Michael Jurka0142d492010-08-25 17:46:15 -07001616 if (lowerPageBound <= i && i <= upperPageBound) {
1617 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001618 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001619 mDirtyPageContent.set(i, false);
1620 }
Winson Chung86f77532010-08-24 11:08:22 -07001621 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001622 }
1623 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001624 }
1625 }
1626
Winson Chunge3193b92010-09-10 11:44:42 -07001627 protected int getAssociatedLowerPageBound(int page) {
1628 return Math.max(0, page - 1);
1629 }
1630 protected int getAssociatedUpperPageBound(int page) {
1631 final int count = getChildCount();
1632 return Math.min(page + 1, count - 1);
1633 }
1634
Winson Chung86f77532010-08-24 11:08:22 -07001635 /**
1636 * This method is called ONLY to synchronize the number of pages that the paged view has.
1637 * To actually fill the pages with information, implement syncPageItems() below. It is
1638 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1639 * and therefore, individual page items do not need to be updated in this method.
1640 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001641 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001642
1643 /**
1644 * This method is called to synchronize the items that are on a particular page. If views on
1645 * the page can be reused, then they should be updated within this method.
1646 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001647 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001648
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001649 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001650 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001651 }
1652 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001653 invalidatePageData(currentPage, false);
1654 }
1655 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001656 if (!mIsDataReady) {
1657 return;
1658 }
1659
Michael Jurka0142d492010-08-25 17:46:15 -07001660 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001661 // Force all scrolling-related behavior to end
1662 mScroller.forceFinished(true);
1663 mNextPage = INVALID_PAGE;
1664
Michael Jurka0142d492010-08-25 17:46:15 -07001665 // Update all the pages
1666 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001667
Winson Chung5a808352011-06-27 19:08:49 -07001668 // We must force a measure after we've loaded the pages to update the content width and
1669 // to determine the full scroll width
1670 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1671 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1672
1673 // Set a new page as the current page if necessary
1674 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001675 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001676 }
1677
Michael Jurka0142d492010-08-25 17:46:15 -07001678 // Mark each of the pages as dirty
1679 final int count = getChildCount();
1680 mDirtyPageContent.clear();
1681 for (int i = 0; i < count; ++i) {
1682 mDirtyPageContent.add(true);
1683 }
1684
1685 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001686 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001687 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001688 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001689 }
Winson Chung007c6982011-06-14 13:27:53 -07001690
Michael Jurkaafaa0502011-12-13 18:22:50 -08001691 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001692 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1693 // found
1694 if (mHasScrollIndicator && mScrollIndicator == null) {
1695 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001696 if (parent != null) {
1697 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1698 mHasScrollIndicator = mScrollIndicator != null;
1699 if (mHasScrollIndicator) {
1700 mScrollIndicator.setVisibility(View.VISIBLE);
1701 }
Winson Chung007c6982011-06-14 13:27:53 -07001702 }
1703 }
1704 return mScrollIndicator;
1705 }
1706
1707 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001708 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001709 }
1710
Winson Chung5a808352011-06-27 19:08:49 -07001711 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1712 @Override
1713 public void run() {
1714 hideScrollingIndicator(false);
1715 }
1716 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001717 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001718 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001719 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001720 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001721 }
1722
Michael Jurka430e8a52011-08-08 15:52:14 -07001723 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001724 mShouldShowScrollIndicator = true;
1725 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001726 if (getChildCount() <= 1) return;
1727 if (!isScrollingIndicatorEnabled()) return;
1728
Michael Jurkabed61d22012-02-14 22:51:29 -08001729 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001730 getScrollingIndicator();
1731 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001732 // Fade the indicator in
1733 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001734 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001735 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001736 if (immediately) {
1737 mScrollIndicator.setAlpha(1f);
1738 } else {
1739 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1740 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1741 mScrollIndicatorAnimator.start();
1742 }
Winson Chung007c6982011-06-14 13:27:53 -07001743 }
1744 }
1745
Adam Cohen21b41102011-11-01 17:29:52 -07001746 protected void cancelScrollingIndicatorAnimations() {
1747 if (mScrollIndicatorAnimator != null) {
1748 mScrollIndicatorAnimator.cancel();
1749 }
1750 }
1751
Winson Chung007c6982011-06-14 13:27:53 -07001752 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001753 if (getChildCount() <= 1) return;
1754 if (!isScrollingIndicatorEnabled()) return;
1755
1756 getScrollingIndicator();
1757 if (mScrollIndicator != null) {
1758 // Fade the indicator out
1759 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001760 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001761 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001762 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001763 mScrollIndicator.setAlpha(0f);
1764 } else {
1765 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1766 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1767 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1768 private boolean cancelled = false;
1769 @Override
1770 public void onAnimationCancel(android.animation.Animator animation) {
1771 cancelled = true;
1772 }
1773 @Override
1774 public void onAnimationEnd(Animator animation) {
1775 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001776 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001777 }
1778 }
1779 });
1780 mScrollIndicatorAnimator.start();
1781 }
Winson Chung007c6982011-06-14 13:27:53 -07001782 }
1783 }
1784
Winson Chung32174c82011-07-19 15:47:55 -07001785 /**
1786 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1787 * fill its space on the track or not.
1788 */
1789 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001790 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001791 }
1792
Winson Chung007c6982011-06-14 13:27:53 -07001793 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001794 if (getChildCount() <= 1) return;
1795 if (!isScrollingIndicatorEnabled()) return;
1796
1797 getScrollingIndicator();
1798 if (mScrollIndicator != null) {
1799 updateScrollingIndicatorPosition();
1800 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001801 if (mShouldShowScrollIndicator) {
1802 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1803 }
Winson Chung007c6982011-06-14 13:27:53 -07001804 }
1805
Winson Chung007c6982011-06-14 13:27:53 -07001806 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001807 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001808 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001809 int numPages = getChildCount();
1810 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001811 int lastChildIndex = Math.max(0, getChildCount() - 1);
1812 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001813 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001814 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1815 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001816
Winson Chungae890b82011-09-13 18:08:54 -07001817 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001818 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001819 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001820 if (hasElasticScrollIndicator()) {
1821 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1822 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1823 mScrollIndicator.requestLayout();
1824 }
1825 } else {
1826 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1827 indicatorPos += indicatorCenterOffset;
1828 }
Winson Chung007c6982011-06-14 13:27:53 -07001829 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001830 }
1831
Winson Chung3ac74c52011-06-30 17:39:37 -07001832 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001833 }
1834
1835 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001836 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001837
1838 /* Accessibility */
1839 @Override
1840 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1841 super.onInitializeAccessibilityNodeInfo(info);
1842 info.setScrollable(true);
1843 }
1844
1845 @Override
1846 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1847 super.onInitializeAccessibilityEvent(event);
1848 event.setScrollable(true);
1849 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1850 event.setFromIndex(mCurrentPage);
1851 event.setToIndex(mCurrentPage);
1852 event.setItemCount(getChildCount());
1853 }
1854 }
1855
Winson Chung6a0f57d2011-06-29 20:10:49 -07001856 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001857 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001858 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001859 }
Winson Chungd11265e2011-08-30 13:37:23 -07001860
1861 @Override
1862 public boolean onHoverEvent(android.view.MotionEvent event) {
1863 return true;
1864 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001865}