blob: 8ceac8fe7eb15024ffb023aaaa2277628e609b28 [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;
Winson Chungbb6f6a52011-07-25 17:55:44 -070021import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.graphics.Canvas;
25import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070026import android.os.Bundle;
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
Winson Chung8aad6102012-05-11 16:27:49 -070078 static final int AUTOMATIC_PAGE_SPACING = -1;
79
Adam Cohen265b9a62011-12-07 14:37:18 -080080 protected int mFlingThresholdVelocity;
81 protected int mMinFlingVelocity;
82 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070083
Adam Cohenb5ba0972011-09-07 18:02:31 -070084 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected float mSmoothingTime;
86 protected float mTouchX;
87
88 protected boolean mFirstLayout = true;
89
90 protected int mCurrentPage;
91 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080092 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070094 private VelocityTracker mVelocityTracker;
95
96 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080098 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080099 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800100 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700101 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700102 private int[] mChildOffsets;
103 private int[] mChildRelativeOffsets;
104 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka0142d492010-08-25 17:46:15 -0700106 protected final static int TOUCH_STATE_REST = 0;
107 protected final static int TOUCH_STATE_SCROLLING = 1;
108 protected final static int TOUCH_STATE_PREV_PAGE = 2;
109 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700110 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurka0142d492010-08-25 17:46:15 -0700112 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700113 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Michael Jurka7426c422010-11-11 15:23:47 -0800119 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120 private int mPagingTouchSlop;
121 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800122 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700123 protected int mPageSpacing;
124 protected int mPageLayoutPaddingTop;
125 protected int mPageLayoutPaddingBottom;
126 protected int mPageLayoutPaddingLeft;
127 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700128 protected int mPageLayoutWidthGap;
129 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700130 protected int mCellCountX = 0;
131 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700132 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800133 protected boolean mAllowOverScroll = true;
134 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800135 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700136 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka8b805b12012-04-18 14:23:14 -0700138 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800139 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
140 // the screens from continuing to translate beyond the normal bounds.
141 protected int mOverScrollX;
142
Michael Jurka8c920dd2011-01-20 14:16:56 -0800143 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800144 protected float mLayoutScale = 1.0f;
145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka5f1c5092010-09-03 14:15:02 -0700148 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149
Winson Chung86f77532010-08-24 11:08:22 -0700150 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurkae326f182011-11-21 14:05:46 -0800152 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 // If true, syncPages and syncPageItems will be called to refresh pages
155 protected boolean mContentIsRefreshable = true;
156
157 // If true, modify alpha of neighboring pages as user scrolls left/right
158 protected boolean mFadeInAdjacentScreens = true;
159
160 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
161 // to switch to a new page
162 protected boolean mUsePagingTouchSlop = true;
163
Michael Jurka8b805b12012-04-18 14:23:14 -0700164 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700165 // (SmoothPagedView does this)
166 protected boolean mDeferScrollUpdate = false;
167
Patrick Dubroy1262e362010-10-06 15:49:50 -0700168 protected boolean mIsPageMoving = false;
169
Winson Chungf0ea4d32011-06-06 14:27:16 -0700170 // All syncs and layout passes are deferred until data is ready.
171 protected boolean mIsDataReady = false;
172
Winson Chung007c6982011-06-14 13:27:53 -0700173 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700174 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800175 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700176 private int mScrollIndicatorPaddingLeft;
177 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700178 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800179 private boolean mShouldShowScrollIndicator = false;
180 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700181 protected static final int sScrollIndicatorFadeInDuration = 150;
182 protected static final int sScrollIndicatorFadeOutDuration = 650;
183 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700184
Winson Chungb44b5242011-06-13 11:32:14 -0700185 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700186 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700187
Winson Chung86f77532010-08-24 11:08:22 -0700188 public interface PageSwitchListener {
189 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 }
191
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 public PagedView(Context context) {
193 this(context, null);
194 }
195
196 public PagedView(Context context, AttributeSet attrs) {
197 this(context, attrs, 0);
198 }
199
200 public PagedView(Context context, AttributeSet attrs, int defStyle) {
201 super(context, attrs, defStyle);
202
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 TypedArray a = context.obtainStyledAttributes(attrs,
204 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800205 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800207 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700214 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700215 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700216 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700218 mScrollIndicatorPaddingLeft =
219 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
220 mScrollIndicatorPaddingRight =
221 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700222 a.recycle();
223
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700225 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227
228 /**
229 * Initializes various states for this workspace.
230 */
Michael Jurka0142d492010-08-25 17:46:15 -0700231 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700232 mDirtyPageContent = new ArrayList<Boolean>();
233 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800234 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700235 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700236 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700237
238 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
239 mTouchSlop = configuration.getScaledTouchSlop();
240 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
241 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700242 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800243
244 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
245 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
246 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700247 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 }
249
Winson Chung86f77532010-08-24 11:08:22 -0700250 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
251 mPageSwitchListener = pageSwitchListener;
252 if (mPageSwitchListener != null) {
253 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 }
255 }
256
257 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700258 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
259 * out pages.
260 */
261 protected void setDataIsReady() {
262 mIsDataReady = true;
263 }
264 protected boolean isDataReady() {
265 return mIsDataReady;
266 }
267
268 /**
Winson Chung86f77532010-08-24 11:08:22 -0700269 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 *
Winson Chung86f77532010-08-24 11:08:22 -0700271 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 */
Winson Chung86f77532010-08-24 11:08:22 -0700273 int getCurrentPage() {
274 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 }
Winson Chung360e63f2012-04-27 13:48:05 -0700276 int getNextPage() {
277 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
278 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700279
Winson Chung86f77532010-08-24 11:08:22 -0700280 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 return getChildCount();
282 }
283
Winson Chung86f77532010-08-24 11:08:22 -0700284 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 return getChildAt(index);
286 }
287
Adam Cohenae4f1552011-10-20 00:15:42 -0700288 protected int indexToPage(int index) {
289 return index;
290 }
291
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800293 * Updates the scroll of the current page immediately to its final scroll position. We use this
294 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
295 * the previous tab page.
296 */
297 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700298 int offset = getChildOffset(mCurrentPage);
299 int relOffset = getRelativeChildOffset(mCurrentPage);
300 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800301 scrollTo(newX, 0);
302 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800303 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800304 }
305
306 /**
Winson Chung86f77532010-08-24 11:08:22 -0700307 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 */
Winson Chung86f77532010-08-24 11:08:22 -0700309 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800310 if (!mScroller.isFinished()) {
311 mScroller.abortAnimation();
312 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800313 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
314 // the default
315 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800316 return;
317 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700318
Winson Chungbfeac062012-06-06 15:56:08 -0700319
Winson Chung86f77532010-08-24 11:08:22 -0700320 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800321 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700322 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700323 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800324 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 }
326
Michael Jurka0142d492010-08-25 17:46:15 -0700327 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700328 if (mPageSwitchListener != null) {
329 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 }
331 }
332
Michael Jurkace7e05f2011-02-01 22:02:35 -0800333 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700334 if (!mIsPageMoving) {
335 mIsPageMoving = true;
336 onPageBeginMoving();
337 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700338 }
339
Michael Jurkace7e05f2011-02-01 22:02:35 -0800340 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700341 if (mIsPageMoving) {
342 mIsPageMoving = false;
343 onPageEndMoving();
344 }
Michael Jurka0142d492010-08-25 17:46:15 -0700345 }
346
Adam Cohen26976d92011-03-22 15:33:33 -0700347 protected boolean isPageMoving() {
348 return mIsPageMoving;
349 }
350
Michael Jurka0142d492010-08-25 17:46:15 -0700351 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700352 protected void onPageBeginMoving() {
353 }
354
355 // a method that subclasses can override to add behavior
356 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700357 }
358
Winson Chung321e9ee2010-08-09 13:37:56 -0700359 /**
Winson Chung86f77532010-08-24 11:08:22 -0700360 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 *
362 * @param l The listener used to respond to long clicks.
363 */
364 @Override
365 public void setOnLongClickListener(OnLongClickListener l) {
366 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700367 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700368 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700369 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 }
371 }
372
373 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800374 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700375 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800376 }
377
378 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700379 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800380 mUnboundedScrollX = x;
381
382 if (x < 0) {
383 super.scrollTo(0, y);
384 if (mAllowOverScroll) {
385 overScroll(x);
386 }
387 } else if (x > mMaxScrollX) {
388 super.scrollTo(mMaxScrollX, y);
389 if (mAllowOverScroll) {
390 overScroll(x - mMaxScrollX);
391 }
392 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800393 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800394 super.scrollTo(x, y);
395 }
396
Michael Jurka0142d492010-08-25 17:46:15 -0700397 mTouchX = x;
398 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
399 }
400
401 // we moved this functionality to a helper function so SmoothPagedView can reuse it
402 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700404 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700405 if (getScrollX() != mScroller.getCurrX()
406 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700407 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700408 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
409 }
Michael Jurka0142d492010-08-25 17:46:15 -0700410 invalidate();
411 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700412 } else if (mNextPage != INVALID_PAGE) {
413 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700414 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700415 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700416
417 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700418 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700419 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700420 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700421 }
422
Adam Cohen73aa9752010-11-24 16:26:58 -0800423 // We don't want to trigger a page end moving unless the page has settled
424 // and the user has stopped scrolling
425 if (mTouchState == TOUCH_STATE_REST) {
426 pageEndMoving();
427 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700428
429 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700430 AccessibilityManager accessibilityManager = (AccessibilityManager)
431 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
432 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700433 AccessibilityEvent ev =
434 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
435 ev.getText().add(getCurrentPageDescription());
436 sendAccessibilityEventUnchecked(ev);
437 }
Michael Jurka0142d492010-08-25 17:46:15 -0700438 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 }
Michael Jurka0142d492010-08-25 17:46:15 -0700440 return false;
441 }
442
443 @Override
444 public void computeScroll() {
445 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 }
447
448 @Override
449 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 if (!mIsDataReady) {
451 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
452 return;
453 }
454
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
456 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700457 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
458 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 if (widthMode != MeasureSpec.EXACTLY) {
460 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
461 }
462
Winson Chung8aad6102012-05-11 16:27:49 -0700463 // Return early if we aren't given a proper dimension
464 if (widthSize <= 0 || heightSize <= 0) {
465 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
466 return;
467 }
468
Adam Lesinski6b879f02010-11-04 16:15:23 -0700469 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
470 * of the All apps view on XLarge displays to not take up more space then it needs. Width
471 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
472 * each page to have the same width.
473 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700474 int maxChildHeight = 0;
475
Michael Jurka8b805b12012-04-18 14:23:14 -0700476 final int verticalPadding = getPaddingTop() + getPaddingBottom();
477 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700478
Michael Jurka36fcb742011-04-06 17:08:58 -0700479
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700481 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700482 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 final int childCount = getChildCount();
484 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700485 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700486 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700487 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
488
489 int childWidthMode;
490 if (lp.width == LayoutParams.WRAP_CONTENT) {
491 childWidthMode = MeasureSpec.AT_MOST;
492 } else {
493 childWidthMode = MeasureSpec.EXACTLY;
494 }
495
496 int childHeightMode;
497 if (lp.height == LayoutParams.WRAP_CONTENT) {
498 childHeightMode = MeasureSpec.AT_MOST;
499 } else {
500 childHeightMode = MeasureSpec.EXACTLY;
501 }
502
503 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700504 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700505 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700506 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700507
508 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700509 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700510 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
511 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700512 }
513
514 if (heightMode == MeasureSpec.AT_MOST) {
515 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700516 }
Winson Chungae890b82011-09-13 18:08:54 -0700517
Winson Chungae890b82011-09-13 18:08:54 -0700518 setMeasuredDimension(widthSize, heightSize);
519
Winson Chung8aad6102012-05-11 16:27:49 -0700520 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
521 // We also wait until we set the measured dimensions before flushing the cache as well, to
522 // ensure that the cache is filled with good values.
523 invalidateCachedOffsets();
524
Winson Chunga128a7b2012-04-30 15:23:15 -0700525 if (childCount > 0) {
526 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
527 + getChildWidth(0));
528
529 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700530 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700531 // The gap between pages in the PagedView should be equal to the gap from the page
532 // to the edge of the screen (so it is not visible in the current screen). To
533 // account for unequal padding on each side of the paged view, we take the maximum
534 // of the left/right gap and use that as the gap between each page.
535 int offset = getRelativeChildOffset(0);
536 int spacing = Math.max(offset, widthSize - offset -
537 getChildAt(0).getMeasuredWidth());
538 setPageSpacing(spacing);
539 }
540 }
541
Adam Cohen25b29952011-11-02 14:49:06 -0700542 updateScrollingIndicatorPosition();
543
Adam Cohenfaa28302010-11-19 12:02:18 -0800544 if (childCount > 0) {
545 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
546 } else {
547 mMaxScrollX = 0;
548 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 }
550
Michael Jurka8c920dd2011-01-20 14:16:56 -0800551 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800552 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700553 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800554
Michael Jurka8c920dd2011-01-20 14:16:56 -0800555 final int pageCount = getChildCount();
556 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700557 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800558 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800559 }
560 setCurrentPage(newCurrentPage);
561 }
562
Michael Jurka8c920dd2011-01-20 14:16:56 -0800563 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
564 // 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 -0800565 // tightens the layout accordingly
566 public void setLayoutScale(float childrenScale) {
567 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700568 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800569
570 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
571 int childCount = getChildCount();
572 float childrenX[] = new float[childCount];
573 float childrenY[] = new float[childCount];
574 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 childrenX[i] = child.getX();
577 childrenY[i] = child.getY();
578 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700579 // Trigger a full re-layout (never just call onLayout directly!)
580 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
581 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
582 requestLayout();
583 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800585 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700586 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800587 child.setX(childrenX[i]);
588 child.setY(childrenY[i]);
589 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700590
Michael Jurkad3ef3062010-11-23 16:23:58 -0800591 // Also, the page offset has changed (since the pages are now smaller);
592 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800593 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800594 }
595
Adam Cohen60b07122011-11-14 17:26:06 -0800596 public void setPageSpacing(int pageSpacing) {
597 mPageSpacing = pageSpacing;
598 invalidateCachedOffsets();
599 }
600
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700602 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700603 if (!mIsDataReady) {
604 return;
605 }
606
Winson Chung785d2eb2011-04-14 16:08:02 -0700607 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700610 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700611
612 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700613 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700614 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800615 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700616 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700617 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700618 if (mCenterPagesVertically) {
619 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
620 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800621
Winson Chung785d2eb2011-04-14 16:08:02 -0700622 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700623 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800624 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700625 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 }
627 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700628
629 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
630 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800631 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700632 setHorizontalScrollBarEnabled(true);
633 mFirstLayout = false;
634 }
Winson Chunge3193b92010-09-10 11:44:42 -0700635 }
636
Adam Cohenf34bab52010-09-30 14:11:56 -0700637 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700638 if (isScrollingIndicatorEnabled()) {
639 updateScrollingIndicator();
640 }
Michael Jurka869390b2012-05-06 15:55:19 -0700641 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
642
643 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700644 for (int i = 0; i < getChildCount(); i++) {
645 View child = getChildAt(i);
646 if (child != null) {
647 float scrollProgress = getScrollProgress(screenCenter, child, i);
648 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800649 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700650 }
651 }
652 invalidate();
653 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700654 }
655
Winson Chunge3193b92010-09-10 11:44:42 -0700656 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700657 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700658 // This ensures that when children are added, they get the correct transforms / alphas
659 // in accordance with any scroll effects.
660 mForceScreenScrolled = true;
661 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700662 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700663 }
664
Michael Jurka8b805b12012-04-18 14:23:14 -0700665 @Override
666 public void onChildViewRemoved(View parent, View child) {
667 }
668
Adam Cohen73894962011-10-31 13:17:17 -0700669 protected void invalidateCachedOffsets() {
670 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700671 if (count == 0) {
672 mChildOffsets = null;
673 mChildRelativeOffsets = null;
674 mChildOffsetsWithLayoutScale = null;
675 return;
676 }
Adam Cohen73894962011-10-31 13:17:17 -0700677
678 mChildOffsets = new int[count];
679 mChildRelativeOffsets = new int[count];
680 mChildOffsetsWithLayoutScale = new int[count];
681 for (int i = 0; i < count; i++) {
682 mChildOffsets[i] = -1;
683 mChildRelativeOffsets[i] = -1;
684 mChildOffsetsWithLayoutScale[i] = -1;
685 }
686 }
687
688 protected int getChildOffset(int index) {
689 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
690 mChildOffsets : mChildOffsetsWithLayoutScale;
691
692 if (childOffsets != null && childOffsets[index] != -1) {
693 return childOffsets[index];
694 } else {
695 if (getChildCount() == 0)
696 return 0;
697
698 int offset = getRelativeChildOffset(0);
699 for (int i = 0; i < index; ++i) {
700 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
701 }
702 if (childOffsets != null) {
703 childOffsets[index] = offset;
704 }
705 return offset;
706 }
707 }
708
709 protected int getRelativeChildOffset(int index) {
710 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
711 return mChildRelativeOffsets[index];
712 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 final int padding = getPaddingLeft() + getPaddingRight();
714 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700715 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
716 if (mChildRelativeOffsets != null) {
717 mChildRelativeOffsets[index] = offset;
718 }
719 return offset;
720 }
721 }
722
Adam Cohen73894962011-10-31 13:17:17 -0700723 protected int getScaledMeasuredWidth(View child) {
724 // This functions are called enough times that it actually makes a difference in the
725 // profiler -- so just inline the max() here
726 final int measuredWidth = child.getMeasuredWidth();
727 final int minWidth = mMinimumWidth;
728 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
729 return (int) (maxWidth * mLayoutScale + 0.5f);
730 }
731
Michael Jurkadde558b2011-11-09 22:09:06 -0800732 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700733 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700734
Michael Jurkac4fb9172010-09-02 17:19:20 -0700735 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700736 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700737 int leftScreen = 0;
738 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800739 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800740 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 currPage.getX() + currPage.getWidth() -
742 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700743 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800744 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700745 }
746 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800747 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800748 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700750 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800751 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700752 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800753 range[0] = leftScreen;
754 range[1] = rightScreen;
755 } else {
756 range[0] = -1;
757 range[1] = -1;
758 }
759 }
760
Michael Jurka920d7f42012-05-14 16:29:55 -0700761 protected boolean shouldDrawChild(View child) {
762 return child.getAlpha() > 0;
763 }
764
Michael Jurkadde558b2011-11-09 22:09:06 -0800765 @Override
766 protected void dispatchDraw(Canvas canvas) {
767 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
769 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800770 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800771
772 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700773 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
774 // set it for the next frame
775 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800776 screenScrolled(screenCenter);
777 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800778 }
779
780 // Find out which screens are visible; as an optimization we only call draw on them
781 final int pageCount = getChildCount();
782 if (pageCount > 0) {
783 getVisiblePages(mTempVisiblePagesRange);
784 final int leftScreen = mTempVisiblePagesRange[0];
785 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800786 if (leftScreen != -1 && rightScreen != -1) {
787 final long drawingTime = getDrawingTime();
788 // Clip to the bounds
789 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
791 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700792
Michael Jurka80c69852011-12-16 14:16:32 -0800793 // On certain graphics drivers, if you draw to a off-screen buffer that's not
794 // used, it can lead to poor performance. We were running into this when
795 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
796 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
797 // As a fix, below we set pages that aren't going to be rendered are to be
798 // View.INVISIBLE, preventing re-drawing of their hardware layer
799 for (int i = getChildCount() - 1; i >= 0; i--) {
800 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700801 if (mForceDrawAllChildrenNextFrame ||
802 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800803 v.setVisibility(VISIBLE);
804 drawChild(canvas, v, drawingTime);
805 } else {
806 v.setVisibility(INVISIBLE);
807 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800808 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700809 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800810 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700811 }
Michael Jurka0142d492010-08-25 17:46:15 -0700812 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 }
814
815 @Override
816 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700817 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700818 if (page != mCurrentPage || !mScroller.isFinished()) {
819 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 return true;
821 }
822 return false;
823 }
824
825 @Override
826 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700827 int focusablePage;
828 if (mNextPage != INVALID_PAGE) {
829 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700831 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 }
Winson Chung86f77532010-08-24 11:08:22 -0700833 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700835 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700836 }
837 return false;
838 }
839
840 @Override
841 public boolean dispatchUnhandledMove(View focused, int direction) {
842 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700843 if (getCurrentPage() > 0) {
844 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 return true;
846 }
847 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700848 if (getCurrentPage() < getPageCount() - 1) {
849 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 return true;
851 }
852 }
853 return super.dispatchUnhandledMove(focused, direction);
854 }
855
856 @Override
857 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700858 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700859 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 }
861 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700862 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700863 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 }
865 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700866 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700867 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 }
869 }
870 }
871
872 /**
873 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700874 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 *
Winson Chung86f77532010-08-24 11:08:22 -0700876 * This happens when live folders requery, and if they're off page, they
877 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 */
879 @Override
880 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700881 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 View v = focused;
883 while (true) {
884 if (v == current) {
885 super.focusableViewAvailable(focused);
886 return;
887 }
888 if (v == this) {
889 return;
890 }
891 ViewParent parent = v.getParent();
892 if (parent instanceof View) {
893 v = (View)v.getParent();
894 } else {
895 return;
896 }
897 }
898 }
899
900 /**
901 * {@inheritDoc}
902 */
903 @Override
904 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
905 if (disallowIntercept) {
906 // We need to make sure to cancel our long press if
907 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700908 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700909 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 }
911 super.requestDisallowInterceptTouchEvent(disallowIntercept);
912 }
913
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800914 /**
915 * Return true if a tap at (x, y) should trigger a flip to the previous page.
916 */
917 protected boolean hitsPreviousPage(float x, float y) {
918 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
919 }
920
921 /**
922 * Return true if a tap at (x, y) should trigger a flip to the next page.
923 */
924 protected boolean hitsNextPage(float x, float y) {
925 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
926 }
927
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 @Override
929 public boolean onInterceptTouchEvent(MotionEvent ev) {
930 /*
931 * This method JUST determines whether we want to intercept the motion.
932 * If we return true, onTouchEvent will be called and we do the actual
933 * scrolling there.
934 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800935 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700936
Winson Chung45e1d6e2010-11-09 17:19:49 -0800937 // Skip touch handling if there are no pages to swipe
938 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
939
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 /*
941 * Shortcut the most recurring case: the user is in the dragging
942 * state and he is moving his finger. We want to intercept this
943 * motion.
944 */
945 final int action = ev.getAction();
946 if ((action == MotionEvent.ACTION_MOVE) &&
947 (mTouchState == TOUCH_STATE_SCROLLING)) {
948 return true;
949 }
950
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 switch (action & MotionEvent.ACTION_MASK) {
952 case MotionEvent.ACTION_MOVE: {
953 /*
954 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
955 * whether the user has moved far enough from his original down touch.
956 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700957 if (mActivePointerId != INVALID_POINTER) {
958 determineScrollingStart(ev);
959 break;
960 }
961 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
962 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
963 // i.e. fall through to the next case (don't break)
964 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
965 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 }
967
968 case MotionEvent.ACTION_DOWN: {
969 final float x = ev.getX();
970 final float y = ev.getY();
971 // Remember location of down touch
972 mDownMotionX = x;
973 mLastMotionX = x;
974 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800975 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800976 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 mActivePointerId = ev.getPointerId(0);
978 mAllowLongPress = true;
979
980 /*
981 * If being flinged and user touches the screen, initiate drag;
982 * otherwise don't. mScroller.isFinished should be false when
983 * being flinged.
984 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700985 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700986 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
987 if (finishedScrolling) {
988 mTouchState = TOUCH_STATE_REST;
989 mScroller.abortAnimation();
990 } else {
991 mTouchState = TOUCH_STATE_SCROLLING;
992 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700993
Winson Chung86f77532010-08-24 11:08:22 -0700994 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800996 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800998 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001000 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 mTouchState = TOUCH_STATE_NEXT_PAGE;
1002 }
1003 }
1004 }
1005 break;
1006 }
1007
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001009 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 mTouchState = TOUCH_STATE_REST;
1011 mAllowLongPress = false;
1012 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001013 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001014 break;
1015
1016 case MotionEvent.ACTION_POINTER_UP:
1017 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001018 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 break;
1020 }
1021
1022 /*
1023 * The only time we want to intercept motion events is if we are in the
1024 * drag mode.
1025 */
1026 return mTouchState != TOUCH_STATE_REST;
1027 }
1028
Adam Cohenf8d28232011-02-01 21:47:00 -08001029 protected void determineScrollingStart(MotionEvent ev) {
1030 determineScrollingStart(ev, 1.0f);
1031 }
1032
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 /*
1034 * Determines if we should change the touch state to start scrolling after the
1035 * user moves their touch point too far.
1036 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001037 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 /*
1039 * Locally do absolute value. mLastMotionX is set to the y value
1040 * of the down event.
1041 */
1042 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001043 if (pointerIndex == -1) {
1044 return;
1045 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 final float x = ev.getX(pointerIndex);
1047 final float y = ev.getY(pointerIndex);
1048 final int xDiff = (int) Math.abs(x - mLastMotionX);
1049 final int yDiff = (int) Math.abs(y - mLastMotionY);
1050
Adam Cohenf8d28232011-02-01 21:47:00 -08001051 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 boolean xPaged = xDiff > mPagingTouchSlop;
1053 boolean xMoved = xDiff > touchSlop;
1054 boolean yMoved = yDiff > touchSlop;
1055
Adam Cohenf8d28232011-02-01 21:47:00 -08001056 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001057 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 // Scroll if the user moved far enough along the X axis
1059 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001060 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001062 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001063 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001064 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1065 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 }
1067 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001068 cancelCurrentPageLongPress();
1069 }
1070 }
1071
1072 protected void cancelCurrentPageLongPress() {
1073 if (mAllowLongPress) {
1074 mAllowLongPress = false;
1075 // Try canceling the long press. It could also have been scheduled
1076 // by a distant descendant, so use the mAllowLongPress flag to block
1077 // everything
1078 final View currentPage = getPageAt(mCurrentPage);
1079 if (currentPage != null) {
1080 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 }
1082 }
1083 }
1084
Adam Cohenb5ba0972011-09-07 18:02:31 -07001085 protected float getScrollProgress(int screenCenter, View v, int page) {
1086 final int halfScreenSize = getMeasuredWidth() / 2;
1087
1088 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1089 int delta = screenCenter - (getChildOffset(page) -
1090 getRelativeChildOffset(page) + halfScreenSize);
1091
1092 float scrollProgress = delta / (totalDistance * 1.0f);
1093 scrollProgress = Math.min(scrollProgress, 1.0f);
1094 scrollProgress = Math.max(scrollProgress, -1.0f);
1095 return scrollProgress;
1096 }
1097
Adam Cohene0f66b52010-11-23 15:06:07 -08001098 // This curve determines how the effect of scrolling over the limits of the page dimishes
1099 // as the user pulls further and further from the bounds
1100 private float overScrollInfluenceCurve(float f) {
1101 f -= 1.0f;
1102 return f * f * f + 1.0f;
1103 }
1104
Adam Cohenb5ba0972011-09-07 18:02:31 -07001105 protected void acceleratedOverScroll(float amount) {
1106 int screenSize = getMeasuredWidth();
1107
1108 // We want to reach the max over scroll effect when the user has
1109 // over scrolled half the size of the screen
1110 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1111
1112 if (f == 0) return;
1113
1114 // Clamp this factor, f, to -1 < f < 1
1115 if (Math.abs(f) >= 1) {
1116 f /= Math.abs(f);
1117 }
1118
1119 int overScrollAmount = (int) Math.round(f * screenSize);
1120 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001121 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001122 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001123 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001124 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001125 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001126 }
1127 invalidate();
1128 }
1129
1130 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001131 int screenSize = getMeasuredWidth();
1132
1133 float f = (amount / screenSize);
1134
1135 if (f == 0) return;
1136 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1137
Adam Cohen7bfc9792011-01-28 13:52:37 -08001138 // Clamp this factor, f, to -1 < f < 1
1139 if (Math.abs(f) >= 1) {
1140 f /= Math.abs(f);
1141 }
1142
Adam Cohene0f66b52010-11-23 15:06:07 -08001143 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001144 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001145 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001146 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001147 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001148 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001149 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001150 }
1151 invalidate();
1152 }
1153
Adam Cohenb5ba0972011-09-07 18:02:31 -07001154 protected void overScroll(float amount) {
1155 dampedOverScroll(amount);
1156 }
1157
Michael Jurkac5b262c2011-01-12 20:24:50 -08001158 protected float maxOverScroll() {
1159 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001160 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001161 float f = 1.0f;
1162 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1163 return OVERSCROLL_DAMP_FACTOR * f;
1164 }
1165
Winson Chung321e9ee2010-08-09 13:37:56 -07001166 @Override
1167 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001168 // Skip touch handling if there are no pages to swipe
1169 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1170
Michael Jurkab8f06722010-10-10 15:58:46 -07001171 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001172
1173 final int action = ev.getAction();
1174
1175 switch (action & MotionEvent.ACTION_MASK) {
1176 case MotionEvent.ACTION_DOWN:
1177 /*
1178 * If being flinged and user touches, stop the fling. isFinished
1179 * will be false if being flinged.
1180 */
1181 if (!mScroller.isFinished()) {
1182 mScroller.abortAnimation();
1183 }
1184
1185 // Remember where the motion event started
1186 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001187 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001188 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001190 if (mTouchState == TOUCH_STATE_SCROLLING) {
1191 pageBeginMoving();
1192 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 break;
1194
1195 case MotionEvent.ACTION_MOVE:
1196 if (mTouchState == TOUCH_STATE_SCROLLING) {
1197 // Scroll to follow the motion event
1198 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1199 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001200 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001201
Adam Cohenaefd4e12011-02-14 16:39:38 -08001202 mTotalMotionX += Math.abs(deltaX);
1203
Winson Chungc0844aa2011-02-02 15:25:58 -08001204 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1205 // keep the remainder because we are actually testing if we've moved from the last
1206 // scrolled position (which is discrete).
1207 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001208 mTouchX += deltaX;
1209 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1210 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001211 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001213 } else {
1214 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001216 mLastMotionX = x;
1217 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 } else {
1219 awakenScrollBars();
1220 }
Adam Cohen564976a2010-10-13 18:52:07 -07001221 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 determineScrollingStart(ev);
1223 }
1224 break;
1225
1226 case MotionEvent.ACTION_UP:
1227 if (mTouchState == TOUCH_STATE_SCROLLING) {
1228 final int activePointerId = mActivePointerId;
1229 final int pointerIndex = ev.findPointerIndex(activePointerId);
1230 final float x = ev.getX(pointerIndex);
1231 final VelocityTracker velocityTracker = mVelocityTracker;
1232 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1233 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001234 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001235 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1236 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1237 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001238
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001239 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1240
Adam Cohen00481b32011-11-18 12:03:48 -08001241 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001242 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001243
Adam Cohenaefd4e12011-02-14 16:39:38 -08001244 // In the case that the page is moved far to one direction and then is flung
1245 // in the opposite direction, we use a threshold to determine whether we should
1246 // just return to the starting page, or if we should skip one further.
1247 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001248 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001249 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001250 returnToOriginalPage = true;
1251 }
1252
Adam Cohenaefd4e12011-02-14 16:39:38 -08001253 int finalPage;
1254 // We give flings precedence over large moves, which is why we short-circuit our
1255 // test for a large move if a fling has been registered. That is, a large
1256 // move to the left and fling to the right will register as a fling to the right.
1257 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1258 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1259 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1260 snapToPageWithVelocity(finalPage, velocityX);
1261 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1262 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001263 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001264 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1265 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 } else {
1267 snapToDestination();
1268 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001269 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 // at this point we have not moved beyond the touch slop
1271 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1272 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001273 int nextPage = Math.max(0, mCurrentPage - 1);
1274 if (nextPage != mCurrentPage) {
1275 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 } else {
1277 snapToDestination();
1278 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001279 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 // at this point we have not moved beyond the touch slop
1281 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1282 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001283 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1284 if (nextPage != mCurrentPage) {
1285 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 } else {
1287 snapToDestination();
1288 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001289 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001290 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 }
1292 mTouchState = TOUCH_STATE_REST;
1293 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001294 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 break;
1296
1297 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001298 if (mTouchState == TOUCH_STATE_SCROLLING) {
1299 snapToDestination();
1300 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 mTouchState = TOUCH_STATE_REST;
1302 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001303 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 break;
1305
1306 case MotionEvent.ACTION_POINTER_UP:
1307 onSecondaryPointerUp(ev);
1308 break;
1309 }
1310
1311 return true;
1312 }
1313
Winson Chung185d7162011-02-28 13:47:29 -08001314 @Override
1315 public boolean onGenericMotionEvent(MotionEvent event) {
1316 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1317 switch (event.getAction()) {
1318 case MotionEvent.ACTION_SCROLL: {
1319 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1320 final float vscroll;
1321 final float hscroll;
1322 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1323 vscroll = 0;
1324 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1325 } else {
1326 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1327 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1328 }
1329 if (hscroll != 0 || vscroll != 0) {
1330 if (hscroll > 0 || vscroll > 0) {
1331 scrollRight();
1332 } else {
1333 scrollLeft();
1334 }
1335 return true;
1336 }
1337 }
1338 }
1339 }
1340 return super.onGenericMotionEvent(event);
1341 }
1342
Michael Jurkab8f06722010-10-10 15:58:46 -07001343 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1344 if (mVelocityTracker == null) {
1345 mVelocityTracker = VelocityTracker.obtain();
1346 }
1347 mVelocityTracker.addMovement(ev);
1348 }
1349
1350 private void releaseVelocityTracker() {
1351 if (mVelocityTracker != null) {
1352 mVelocityTracker.recycle();
1353 mVelocityTracker = null;
1354 }
1355 }
1356
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 private void onSecondaryPointerUp(MotionEvent ev) {
1358 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1359 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1360 final int pointerId = ev.getPointerId(pointerIndex);
1361 if (pointerId == mActivePointerId) {
1362 // This was our active pointer going up. Choose a new
1363 // active pointer and adjust accordingly.
1364 // TODO: Make this decision more intelligent.
1365 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1366 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1367 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001368 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 mActivePointerId = ev.getPointerId(newPointerIndex);
1370 if (mVelocityTracker != null) {
1371 mVelocityTracker.clear();
1372 }
1373 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001374 }
1375
Michael Jurkad771c962011-08-09 15:00:48 -07001376 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001377
1378 @Override
1379 public void requestChildFocus(View child, View focused) {
1380 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001381 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001382 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001383 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001384 }
1385 }
1386
Winson Chunge3193b92010-09-10 11:44:42 -07001387 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1388 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001389 int left;
1390 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001391 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001392 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001393 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001394 if (left <= relativeOffset && relativeOffset <= right) {
1395 return i;
1396 }
Winson Chunge3193b92010-09-10 11:44:42 -07001397 }
1398 return -1;
1399 }
1400
Winson Chung1908d072011-02-24 18:09:44 -08001401 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001402 // This functions are called enough times that it actually makes a difference in the
1403 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001404 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001405 final int minWidth = mMinimumWidth;
1406 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001407 }
1408
Adam Cohend19d3ca2010-09-15 14:43:42 -07001409 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001410 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001411 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001412 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 final int childCount = getChildCount();
1414 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001415 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001416 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001417 int halfChildWidth = (childWidth / 2);
1418 int childCenter = getChildOffset(i) + halfChildWidth;
1419 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1420 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1421 minDistanceFromScreenCenter = distanceFromScreenCenter;
1422 minDistanceFromScreenCenterIndex = i;
1423 }
1424 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001425 return minDistanceFromScreenCenterIndex;
1426 }
1427
1428 protected void snapToDestination() {
1429 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 }
1431
Adam Cohene0f66b52010-11-23 15:06:07 -08001432 private static class ScrollInterpolator implements Interpolator {
1433 public ScrollInterpolator() {
1434 }
1435
1436 public float getInterpolation(float t) {
1437 t -= 1.0f;
1438 return t*t*t*t*t + 1;
1439 }
1440 }
1441
1442 // We want the duration of the page snap animation to be influenced by the distance that
1443 // the screen has to travel, however, we don't want this duration to be effected in a
1444 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1445 // of travel has on the overall snap duration.
1446 float distanceInfluenceForSnapDuration(float f) {
1447 f -= 0.5f; // center the values about 0.
1448 f *= 0.3f * Math.PI / 2.0f;
1449 return (float) Math.sin(f);
1450 }
1451
Michael Jurka0142d492010-08-25 17:46:15 -07001452 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001453 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1454 int halfScreenSize = getMeasuredWidth() / 2;
1455
Winson Chung785d2eb2011-04-14 16:08:02 -07001456 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1457 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1458 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001459 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1460 int delta = newX - mUnboundedScrollX;
1461 int duration = 0;
1462
Adam Cohen265b9a62011-12-07 14:37:18 -08001463 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001464 // If the velocity is low enough, then treat this more as an automatic page advance
1465 // as opposed to an apparent physical response to flinging
1466 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1467 return;
1468 }
1469
1470 // Here we compute a "distance" that will be used in the computation of the overall
1471 // snap duration. This is a function of the actual distance that needs to be traveled;
1472 // we keep this value close to half screen size in order to reduce the variance in snap
1473 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001474 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001475 float distance = halfScreenSize + halfScreenSize *
1476 distanceInfluenceForSnapDuration(distanceRatio);
1477
1478 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001479 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001480
1481 // we want the page's snap velocity to approximately match the velocity at which the
1482 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001483 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1484 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001485
1486 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001487 }
1488
1489 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001490 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 }
1492
Michael Jurka0142d492010-08-25 17:46:15 -07001493 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001494 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001495
Winson Chung785d2eb2011-04-14 16:08:02 -07001496 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1497 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1498 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001499 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001500 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001501 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001502 snapToPage(whichPage, delta, duration);
1503 }
1504
1505 protected void snapToPage(int whichPage, int delta, int duration) {
1506 mNextPage = whichPage;
1507
1508 View focusedChild = getFocusedChild();
1509 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001510 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001511 focusedChild.clearFocus();
1512 }
1513
1514 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 awakenScrollBars(duration);
1516 if (duration == 0) {
1517 duration = Math.abs(delta);
1518 }
1519
1520 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001521 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001522
Winson Chungb44b5242011-06-13 11:32:14 -07001523 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1524 // loading associated pages until the scroll settles
1525 if (mDeferScrollUpdate) {
1526 loadAssociatedPages(mNextPage);
1527 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001528 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001529 }
Michael Jurka0142d492010-08-25 17:46:15 -07001530 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001531 invalidate();
1532 }
1533
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 public void scrollLeft() {
1535 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001536 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001537 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001538 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 }
1540 }
1541
1542 public void scrollRight() {
1543 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001544 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001545 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001546 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001547 }
1548 }
1549
Winson Chung86f77532010-08-24 11:08:22 -07001550 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001551 int result = -1;
1552 if (v != null) {
1553 ViewParent vp = v.getParent();
1554 int count = getChildCount();
1555 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001556 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 return i;
1558 }
1559 }
1560 }
1561 return result;
1562 }
1563
1564 /**
1565 * @return True is long presses are still allowed for the current touch
1566 */
1567 public boolean allowLongPress() {
1568 return mAllowLongPress;
1569 }
1570
Michael Jurka0142d492010-08-25 17:46:15 -07001571 /**
1572 * Set true to allow long-press events to be triggered, usually checked by
1573 * {@link Launcher} to accept or block dpad-initiated long-presses.
1574 */
1575 public void setAllowLongPress(boolean allowLongPress) {
1576 mAllowLongPress = allowLongPress;
1577 }
1578
Winson Chung321e9ee2010-08-09 13:37:56 -07001579 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001580 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001581
1582 SavedState(Parcelable superState) {
1583 super(superState);
1584 }
1585
1586 private SavedState(Parcel in) {
1587 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001588 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001589 }
1590
1591 @Override
1592 public void writeToParcel(Parcel out, int flags) {
1593 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001594 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001595 }
1596
1597 public static final Parcelable.Creator<SavedState> CREATOR =
1598 new Parcelable.Creator<SavedState>() {
1599 public SavedState createFromParcel(Parcel in) {
1600 return new SavedState(in);
1601 }
1602
1603 public SavedState[] newArray(int size) {
1604 return new SavedState[size];
1605 }
1606 };
1607 }
1608
Winson Chungf314b0e2011-08-16 11:54:27 -07001609 protected void loadAssociatedPages(int page) {
1610 loadAssociatedPages(page, false);
1611 }
1612 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001613 if (mContentIsRefreshable) {
1614 final int count = getChildCount();
1615 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001616 int lowerPageBound = getAssociatedLowerPageBound(page);
1617 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001618 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1619 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001620 // First, clear any pages that should no longer be loaded
1621 for (int i = 0; i < count; ++i) {
1622 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001623 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001624 if (layout.getPageChildCount() > 0) {
1625 layout.removeAllViewsOnPage();
1626 }
1627 mDirtyPageContent.set(i, true);
1628 }
1629 }
1630 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001631 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001632 if ((i != page) && immediateAndOnly) {
1633 continue;
1634 }
Michael Jurka0142d492010-08-25 17:46:15 -07001635 if (lowerPageBound <= i && i <= upperPageBound) {
1636 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001637 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001638 mDirtyPageContent.set(i, false);
1639 }
Winson Chung86f77532010-08-24 11:08:22 -07001640 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001641 }
1642 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001643 }
1644 }
1645
Winson Chunge3193b92010-09-10 11:44:42 -07001646 protected int getAssociatedLowerPageBound(int page) {
1647 return Math.max(0, page - 1);
1648 }
1649 protected int getAssociatedUpperPageBound(int page) {
1650 final int count = getChildCount();
1651 return Math.min(page + 1, count - 1);
1652 }
1653
Winson Chung86f77532010-08-24 11:08:22 -07001654 /**
1655 * This method is called ONLY to synchronize the number of pages that the paged view has.
1656 * To actually fill the pages with information, implement syncPageItems() below. It is
1657 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1658 * and therefore, individual page items do not need to be updated in this method.
1659 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001660 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001661
1662 /**
1663 * This method is called to synchronize the items that are on a particular page. If views on
1664 * the page can be reused, then they should be updated within this method.
1665 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001666 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001667
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001668 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001669 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001670 }
1671 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001672 invalidatePageData(currentPage, false);
1673 }
1674 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001675 if (!mIsDataReady) {
1676 return;
1677 }
1678
Michael Jurka0142d492010-08-25 17:46:15 -07001679 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001680 // Force all scrolling-related behavior to end
1681 mScroller.forceFinished(true);
1682 mNextPage = INVALID_PAGE;
1683
Michael Jurka0142d492010-08-25 17:46:15 -07001684 // Update all the pages
1685 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001686
Winson Chung5a808352011-06-27 19:08:49 -07001687 // We must force a measure after we've loaded the pages to update the content width and
1688 // to determine the full scroll width
1689 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1690 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1691
1692 // Set a new page as the current page if necessary
1693 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001694 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001695 }
1696
Michael Jurka0142d492010-08-25 17:46:15 -07001697 // Mark each of the pages as dirty
1698 final int count = getChildCount();
1699 mDirtyPageContent.clear();
1700 for (int i = 0; i < count; ++i) {
1701 mDirtyPageContent.add(true);
1702 }
1703
1704 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001705 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001706 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001707 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001708 }
Winson Chung007c6982011-06-14 13:27:53 -07001709
Michael Jurkaafaa0502011-12-13 18:22:50 -08001710 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001711 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1712 // found
1713 if (mHasScrollIndicator && mScrollIndicator == null) {
1714 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001715 if (parent != null) {
1716 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1717 mHasScrollIndicator = mScrollIndicator != null;
1718 if (mHasScrollIndicator) {
1719 mScrollIndicator.setVisibility(View.VISIBLE);
1720 }
Winson Chung007c6982011-06-14 13:27:53 -07001721 }
1722 }
1723 return mScrollIndicator;
1724 }
1725
1726 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001727 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001728 }
1729
Winson Chung5a808352011-06-27 19:08:49 -07001730 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1731 @Override
1732 public void run() {
1733 hideScrollingIndicator(false);
1734 }
1735 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001736 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001737 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001738 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001739 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001740 }
1741
Michael Jurka430e8a52011-08-08 15:52:14 -07001742 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001743 mShouldShowScrollIndicator = true;
1744 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001745 if (getChildCount() <= 1) return;
1746 if (!isScrollingIndicatorEnabled()) return;
1747
Michael Jurkabed61d22012-02-14 22:51:29 -08001748 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001749 getScrollingIndicator();
1750 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001751 // Fade the indicator in
1752 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001753 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001754 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001755 if (immediately) {
1756 mScrollIndicator.setAlpha(1f);
1757 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001758 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f);
Michael Jurka430e8a52011-08-08 15:52:14 -07001759 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1760 mScrollIndicatorAnimator.start();
1761 }
Winson Chung007c6982011-06-14 13:27:53 -07001762 }
1763 }
1764
Adam Cohen21b41102011-11-01 17:29:52 -07001765 protected void cancelScrollingIndicatorAnimations() {
1766 if (mScrollIndicatorAnimator != null) {
1767 mScrollIndicatorAnimator.cancel();
1768 }
1769 }
1770
Winson Chung007c6982011-06-14 13:27:53 -07001771 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001772 if (getChildCount() <= 1) return;
1773 if (!isScrollingIndicatorEnabled()) return;
1774
1775 getScrollingIndicator();
1776 if (mScrollIndicator != null) {
1777 // Fade the indicator out
1778 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001779 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001780 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001781 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001782 mScrollIndicator.setAlpha(0f);
1783 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001784 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f);
Winson Chung32174c82011-07-19 15:47:55 -07001785 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1786 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1787 private boolean cancelled = false;
1788 @Override
1789 public void onAnimationCancel(android.animation.Animator animation) {
1790 cancelled = true;
1791 }
1792 @Override
1793 public void onAnimationEnd(Animator animation) {
1794 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001795 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001796 }
1797 }
1798 });
1799 mScrollIndicatorAnimator.start();
1800 }
Winson Chung007c6982011-06-14 13:27:53 -07001801 }
1802 }
1803
Winson Chung32174c82011-07-19 15:47:55 -07001804 /**
1805 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1806 * fill its space on the track or not.
1807 */
1808 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001809 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001810 }
1811
Winson Chung007c6982011-06-14 13:27:53 -07001812 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001813 if (getChildCount() <= 1) return;
1814 if (!isScrollingIndicatorEnabled()) return;
1815
1816 getScrollingIndicator();
1817 if (mScrollIndicator != null) {
1818 updateScrollingIndicatorPosition();
1819 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001820 if (mShouldShowScrollIndicator) {
1821 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1822 }
Winson Chung007c6982011-06-14 13:27:53 -07001823 }
1824
Winson Chung007c6982011-06-14 13:27:53 -07001825 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001826 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001827 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001828 int numPages = getChildCount();
1829 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001830 int lastChildIndex = Math.max(0, getChildCount() - 1);
1831 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001832 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001833 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1834 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001835
Winson Chungae890b82011-09-13 18:08:54 -07001836 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001837 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001838 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001839 if (hasElasticScrollIndicator()) {
1840 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1841 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1842 mScrollIndicator.requestLayout();
1843 }
1844 } else {
1845 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1846 indicatorPos += indicatorCenterOffset;
1847 }
Winson Chung007c6982011-06-14 13:27:53 -07001848 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001849 }
1850
Winson Chung3ac74c52011-06-30 17:39:37 -07001851 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001852 }
1853
1854 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001855 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001856
1857 /* Accessibility */
1858 @Override
1859 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1860 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001861 info.setScrollable(getPageCount() > 1);
1862 if (getCurrentPage() < getPageCount() - 1) {
1863 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1864 }
1865 if (getCurrentPage() > 0) {
1866 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1867 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001868 }
1869
1870 @Override
1871 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1872 super.onInitializeAccessibilityEvent(event);
1873 event.setScrollable(true);
1874 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1875 event.setFromIndex(mCurrentPage);
1876 event.setToIndex(mCurrentPage);
1877 event.setItemCount(getChildCount());
1878 }
1879 }
1880
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001881 @Override
1882 public boolean performAccessibilityAction(int action, Bundle arguments) {
1883 if (super.performAccessibilityAction(action, arguments)) {
1884 return true;
1885 }
1886 switch (action) {
1887 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1888 if (getCurrentPage() < getPageCount() - 1) {
1889 scrollRight();
1890 return true;
1891 }
1892 } break;
1893 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1894 if (getCurrentPage() > 0) {
1895 scrollLeft();
1896 return true;
1897 }
1898 } break;
1899 }
1900 return false;
1901 }
1902
Winson Chung6a0f57d2011-06-29 20:10:49 -07001903 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001904 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001905 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001906 }
Winson Chungd11265e2011-08-30 13:37:23 -07001907
1908 @Override
1909 public boolean onHoverEvent(android.view.MotionEvent event) {
1910 return true;
1911 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001912}