blob: 5434704eaf74ca898f5442759e2405b04fbbfdb8 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
20import android.animation.AnimatorInflater;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070023import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070025import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.graphics.Canvas;
27import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070042import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070043import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070045import android.widget.Checkable;
Winson Chung007c6982011-06-14 13:27:53 -070046import android.widget.ImageView;
Winson Chung321e9ee2010-08-09 13:37:56 -070047import android.widget.Scroller;
48
Winson Chung04998342011-01-05 13:54:43 -080049import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070050
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
57public abstract class PagedView extends ViewGroup {
58 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chung86f77532010-08-24 11:08:22 -070062 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070063 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chungf0c6ae02012-03-21 16:10:31 -070065 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
66 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070067 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Adam Cohenb5ba0972011-09-07 18:02:31 -070069 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070070 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080071
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080073 // The page is moved more than halfway, automatically move to the next page on touch up.
74 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Adam Cohen265b9a62011-12-07 14:37:18 -080076 // The following constants need to be scaled based on density. The scaled versions will be
77 // assigned to the corresponding member variables below.
78 private static final int FLING_THRESHOLD_VELOCITY = 500;
79 private static final int MIN_SNAP_VELOCITY = 1500;
80 private static final int MIN_FLING_VELOCITY = 250;
81
82 protected int mFlingThresholdVelocity;
83 protected int mMinFlingVelocity;
84 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070085
Adam Cohenb5ba0972011-09-07 18:02:31 -070086 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected float mSmoothingTime;
88 protected float mTouchX;
89
90 protected boolean mFirstLayout = true;
91
92 protected int mCurrentPage;
93 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080094 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070096 private VelocityTracker mVelocityTracker;
97
98 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080099 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800100 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800101 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800102 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700103 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700104 private int[] mChildOffsets;
105 private int[] mChildRelativeOffsets;
106 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Michael Jurka0142d492010-08-25 17:46:15 -0700108 protected final static int TOUCH_STATE_REST = 0;
109 protected final static int TOUCH_STATE_SCROLLING = 1;
110 protected final static int TOUCH_STATE_PREV_PAGE = 2;
111 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700112 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Michael Jurka0142d492010-08-25 17:46:15 -0700114 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700115 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka0142d492010-08-25 17:46:15 -0700117 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Michael Jurka7426c422010-11-11 15:23:47 -0800119 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka7426c422010-11-11 15:23:47 -0800121 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122 private int mPagingTouchSlop;
123 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800124 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700125 protected int mPageSpacing;
126 protected int mPageLayoutPaddingTop;
127 protected int mPageLayoutPaddingBottom;
128 protected int mPageLayoutPaddingLeft;
129 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700130 protected int mPageLayoutWidthGap;
131 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700132 protected int mCellCountX = 0;
133 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700134 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800135 protected boolean mAllowOverScroll = true;
136 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800137 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Adam Cohenebea84d2011-11-09 17:20:41 -0800139 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
140 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
141 // the screens from continuing to translate beyond the normal bounds.
142 protected int mOverScrollX;
143
Michael Jurka8c920dd2011-01-20 14:16:56 -0800144 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800145 protected float mLayoutScale = 1.0f;
146
Michael Jurka5f1c5092010-09-03 14:15:02 -0700147 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurka5f1c5092010-09-03 14:15:02 -0700149 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Winson Chung86f77532010-08-24 11:08:22 -0700151 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurkae326f182011-11-21 14:05:46 -0800153 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700154
Michael Jurka0142d492010-08-25 17:46:15 -0700155 // If true, syncPages and syncPageItems will be called to refresh pages
156 protected boolean mContentIsRefreshable = true;
157
158 // If true, modify alpha of neighboring pages as user scrolls left/right
159 protected boolean mFadeInAdjacentScreens = true;
160
161 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
162 // to switch to a new page
163 protected boolean mUsePagingTouchSlop = true;
164
165 // If true, the subclass should directly update mScrollX itself in its computeScroll method
166 // (SmoothPagedView does this)
167 protected boolean mDeferScrollUpdate = false;
168
Patrick Dubroy1262e362010-10-06 15:49:50 -0700169 protected boolean mIsPageMoving = false;
170
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 // All syncs and layout passes are deferred until data is ready.
172 protected boolean mIsDataReady = false;
173
Winson Chung007c6982011-06-14 13:27:53 -0700174 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700175 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800176 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700177 private int mScrollIndicatorPaddingLeft;
178 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700179 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800180 private boolean mShouldShowScrollIndicator = false;
181 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700182 protected static final int sScrollIndicatorFadeInDuration = 150;
183 protected static final int sScrollIndicatorFadeOutDuration = 650;
184 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700185
Winson Chungb44b5242011-06-13 11:32:14 -0700186 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700187 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public interface PageSwitchListener {
190 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 public PagedView(Context context) {
194 this(context, null);
195 }
196
197 public PagedView(Context context, AttributeSet attrs) {
198 this(context, attrs, 0);
199 }
200
201 public PagedView(Context context, AttributeSet attrs, int defStyle) {
202 super(context, attrs, defStyle);
203
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 TypedArray a = context.obtainStyledAttributes(attrs,
205 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800206 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800212 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800214 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700215 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700216 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700217 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700218 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700219 mScrollIndicatorPaddingLeft =
220 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
221 mScrollIndicatorPaddingRight =
222 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700223 a.recycle();
224
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700226 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
228
229 /**
230 * Initializes various states for this workspace.
231 */
Michael Jurka0142d492010-08-25 17:46:15 -0700232 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700233 mDirtyPageContent = new ArrayList<Boolean>();
234 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800235 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700236 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700237 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700238
239 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
240 mTouchSlop = configuration.getScaledTouchSlop();
241 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
242 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700243 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800244
245 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
246 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
247 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
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 }
276
Winson Chung86f77532010-08-24 11:08:22 -0700277 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 return getChildCount();
279 }
280
Winson Chung86f77532010-08-24 11:08:22 -0700281 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 return getChildAt(index);
283 }
284
Adam Cohenae4f1552011-10-20 00:15:42 -0700285 protected int indexToPage(int index) {
286 return index;
287 }
288
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800290 * Updates the scroll of the current page immediately to its final scroll position. We use this
291 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
292 * the previous tab page.
293 */
294 protected void updateCurrentPageScroll() {
295 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
296 scrollTo(newX, 0);
297 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800298 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800299 }
300
301 /**
Winson Chung86f77532010-08-24 11:08:22 -0700302 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 */
Winson Chung86f77532010-08-24 11:08:22 -0700304 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800305 if (!mScroller.isFinished()) {
306 mScroller.abortAnimation();
307 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800308 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
309 // the default
310 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800311 return;
312 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700313
Winson Chung86f77532010-08-24 11:08:22 -0700314 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800315 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700316 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700317 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800318 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700319 }
320
Michael Jurka0142d492010-08-25 17:46:15 -0700321 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700322 if (mPageSwitchListener != null) {
323 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 }
325 }
326
Michael Jurkace7e05f2011-02-01 22:02:35 -0800327 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700328 if (!mIsPageMoving) {
329 mIsPageMoving = true;
330 onPageBeginMoving();
331 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700332 }
333
Michael Jurkace7e05f2011-02-01 22:02:35 -0800334 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700335 if (mIsPageMoving) {
336 mIsPageMoving = false;
337 onPageEndMoving();
338 }
Michael Jurka0142d492010-08-25 17:46:15 -0700339 }
340
Adam Cohen26976d92011-03-22 15:33:33 -0700341 protected boolean isPageMoving() {
342 return mIsPageMoving;
343 }
344
Michael Jurka0142d492010-08-25 17:46:15 -0700345 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700346 protected void onPageBeginMoving() {
347 }
348
349 // a method that subclasses can override to add behavior
350 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700351 }
352
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 /**
Winson Chung86f77532010-08-24 11:08:22 -0700354 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700355 *
356 * @param l The listener used to respond to long clicks.
357 */
358 @Override
359 public void setOnLongClickListener(OnLongClickListener l) {
360 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700361 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700363 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 }
365 }
366
367 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800368 public void scrollBy(int x, int y) {
369 scrollTo(mUnboundedScrollX + x, mScrollY + y);
370 }
371
372 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700373 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800374 mUnboundedScrollX = x;
375
376 if (x < 0) {
377 super.scrollTo(0, y);
378 if (mAllowOverScroll) {
379 overScroll(x);
380 }
381 } else if (x > mMaxScrollX) {
382 super.scrollTo(mMaxScrollX, y);
383 if (mAllowOverScroll) {
384 overScroll(x - mMaxScrollX);
385 }
386 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800387 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800388 super.scrollTo(x, y);
389 }
390
Michael Jurka0142d492010-08-25 17:46:15 -0700391 mTouchX = x;
392 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
393 }
394
395 // we moved this functionality to a helper function so SmoothPagedView can reuse it
396 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700397 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700398 // Don't bother scrolling if the page does not need to be moved
399 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700400 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
401 }
Michael Jurka0142d492010-08-25 17:46:15 -0700402 invalidate();
403 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700404 } else if (mNextPage != INVALID_PAGE) {
405 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700406 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700407 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700408
409 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700410 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700411 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700412 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700413 }
414
Adam Cohen73aa9752010-11-24 16:26:58 -0800415 // We don't want to trigger a page end moving unless the page has settled
416 // and the user has stopped scrolling
417 if (mTouchState == TOUCH_STATE_REST) {
418 pageEndMoving();
419 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700420
421 // Notify the user when the page changes
422 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
423 AccessibilityEvent ev =
424 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
425 ev.getText().add(getCurrentPageDescription());
426 sendAccessibilityEventUnchecked(ev);
427 }
Michael Jurka0142d492010-08-25 17:46:15 -0700428 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700429 }
Michael Jurka0142d492010-08-25 17:46:15 -0700430 return false;
431 }
432
433 @Override
434 public void computeScroll() {
435 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 }
437
438 @Override
439 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700440 if (!mIsDataReady) {
441 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
442 return;
443 }
444
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
446 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
447 if (widthMode != MeasureSpec.EXACTLY) {
448 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
449 }
450
Adam Lesinski6b879f02010-11-04 16:15:23 -0700451 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
452 * of the All apps view on XLarge displays to not take up more space then it needs. Width
453 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
454 * each page to have the same width.
455 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700457 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
458 int maxChildHeight = 0;
459
460 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700461 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700462
Michael Jurka36fcb742011-04-06 17:08:58 -0700463
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700465 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700466 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700467 final int childCount = getChildCount();
468 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700469 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700470 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700471 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
472
473 int childWidthMode;
474 if (lp.width == LayoutParams.WRAP_CONTENT) {
475 childWidthMode = MeasureSpec.AT_MOST;
476 } else {
477 childWidthMode = MeasureSpec.EXACTLY;
478 }
479
480 int childHeightMode;
481 if (lp.height == LayoutParams.WRAP_CONTENT) {
482 childHeightMode = MeasureSpec.AT_MOST;
483 } else {
484 childHeightMode = MeasureSpec.EXACTLY;
485 }
486
487 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700488 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700489 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700491
492 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700493 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700494 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
495 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700496 }
497
498 if (heightMode == MeasureSpec.AT_MOST) {
499 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 }
Winson Chungae890b82011-09-13 18:08:54 -0700501
Winson Chungae890b82011-09-13 18:08:54 -0700502 setMeasuredDimension(widthSize, heightSize);
503
Adam Cohen25b29952011-11-02 14:49:06 -0700504 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
505 // We also wait until we set the measured dimensions before flushing the cache as well, to
506 // ensure that the cache is filled with good values.
507 invalidateCachedOffsets();
508 updateScrollingIndicatorPosition();
509
Adam Cohenfaa28302010-11-19 12:02:18 -0800510 if (childCount > 0) {
511 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
512 } else {
513 mMaxScrollX = 0;
514 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
516
Michael Jurka8c920dd2011-01-20 14:16:56 -0800517 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800518 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
519 int delta = newX - mScrollX;
520
Michael Jurka8c920dd2011-01-20 14:16:56 -0800521 final int pageCount = getChildCount();
522 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700523 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800524 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800525 }
526 setCurrentPage(newCurrentPage);
527 }
528
Michael Jurka8c920dd2011-01-20 14:16:56 -0800529 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
530 // 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 -0800531 // tightens the layout accordingly
532 public void setLayoutScale(float childrenScale) {
533 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700534 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800535
536 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
537 int childCount = getChildCount();
538 float childrenX[] = new float[childCount];
539 float childrenY[] = new float[childCount];
540 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700541 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542 childrenX[i] = child.getX();
543 childrenY[i] = child.getY();
544 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700545 // Trigger a full re-layout (never just call onLayout directly!)
546 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
547 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
548 requestLayout();
549 measure(widthSpec, heightSpec);
550 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800551 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700552 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800553 child.setX(childrenX[i]);
554 child.setY(childrenY[i]);
555 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700556
Michael Jurkad3ef3062010-11-23 16:23:58 -0800557 // Also, the page offset has changed (since the pages are now smaller);
558 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800559 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800560 }
561
Adam Cohen60b07122011-11-14 17:26:06 -0800562 public void setPageSpacing(int pageSpacing) {
563 mPageSpacing = pageSpacing;
564 invalidateCachedOffsets();
565 }
566
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700568 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569 if (!mIsDataReady) {
570 return;
571 }
572
Winson Chung785d2eb2011-04-14 16:08:02 -0700573 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700574 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 final int childCount = getChildCount();
576 int childLeft = 0;
577 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700578 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
579 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700580 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700581
582 // Calculate the variable page spacing if necessary
583 if (mPageSpacing < 0) {
Winson Chungeecf02d2012-03-02 17:14:58 -0800584 // The gap between pages in the PagedView should be equal to the gap from the page
585 // to the edge of the screen (so it is not visible in the current screen). To
586 // account for unequal padding on each side of the paged view, we take the maximum
587 // of the left/right gap and use that as the gap between each page.
588 int offset = getRelativeChildOffset(0);
589 int spacing = Math.max(offset, (right - left) - offset -
590 getChildAt(0).getMeasuredWidth());
591 setPageSpacing(spacing);
Winson Chungae890b82011-09-13 18:08:54 -0700592 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700593 }
594
595 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700596 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800598 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700599 final int childHeight = child.getMeasuredHeight();
600 int childTop = mPaddingTop;
601 if (mCenterPagesVertically) {
602 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
603 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800604
Winson Chung785d2eb2011-04-14 16:08:02 -0700605 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700606 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800607 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700608 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 }
610 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700611
612 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
613 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800614 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700615 setHorizontalScrollBarEnabled(true);
616 mFirstLayout = false;
617 }
618
Michael Jurkad3ef3062010-11-23 16:23:58 -0800619 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
620 mFirstLayout = false;
621 }
Winson Chunge3193b92010-09-10 11:44:42 -0700622 }
623
Adam Cohenf34bab52010-09-30 14:11:56 -0700624 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700625 if (isScrollingIndicatorEnabled()) {
626 updateScrollingIndicator();
627 }
628 if (mFadeInAdjacentScreens) {
629 for (int i = 0; i < getChildCount(); i++) {
630 View child = getChildAt(i);
631 if (child != null) {
632 float scrollProgress = getScrollProgress(screenCenter, child, i);
633 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800634 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700635 }
636 }
637 invalidate();
638 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700639 }
640
Winson Chunge3193b92010-09-10 11:44:42 -0700641 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700642 protected void onViewAdded(View child) {
643 super.onViewAdded(child);
644
645 // This ensures that when children are added, they get the correct transforms / alphas
646 // in accordance with any scroll effects.
647 mForceScreenScrolled = true;
648 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700649 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700650 }
651
Adam Cohen73894962011-10-31 13:17:17 -0700652 protected void invalidateCachedOffsets() {
653 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700654 if (count == 0) {
655 mChildOffsets = null;
656 mChildRelativeOffsets = null;
657 mChildOffsetsWithLayoutScale = null;
658 return;
659 }
Adam Cohen73894962011-10-31 13:17:17 -0700660
661 mChildOffsets = new int[count];
662 mChildRelativeOffsets = new int[count];
663 mChildOffsetsWithLayoutScale = new int[count];
664 for (int i = 0; i < count; i++) {
665 mChildOffsets[i] = -1;
666 mChildRelativeOffsets[i] = -1;
667 mChildOffsetsWithLayoutScale[i] = -1;
668 }
669 }
670
671 protected int getChildOffset(int index) {
672 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
673 mChildOffsets : mChildOffsetsWithLayoutScale;
674
675 if (childOffsets != null && childOffsets[index] != -1) {
676 return childOffsets[index];
677 } else {
678 if (getChildCount() == 0)
679 return 0;
680
681 int offset = getRelativeChildOffset(0);
682 for (int i = 0; i < index; ++i) {
683 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
684 }
685 if (childOffsets != null) {
686 childOffsets[index] = offset;
687 }
688 return offset;
689 }
690 }
691
692 protected int getRelativeChildOffset(int index) {
693 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
694 return mChildRelativeOffsets[index];
695 } else {
696 final int padding = mPaddingLeft + mPaddingRight;
697 final int offset = mPaddingLeft +
698 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
699 if (mChildRelativeOffsets != null) {
700 mChildRelativeOffsets[index] = offset;
701 }
702 return offset;
703 }
704 }
705
706 protected int getScaledRelativeChildOffset(int index) {
707 final int padding = mPaddingLeft + mPaddingRight;
708 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
709 getScaledMeasuredWidth(getPageAt(index))) / 2;
710 return offset;
711 }
712
713 protected int getScaledMeasuredWidth(View child) {
714 // This functions are called enough times that it actually makes a difference in the
715 // profiler -- so just inline the max() here
716 final int measuredWidth = child.getMeasuredWidth();
717 final int minWidth = mMinimumWidth;
718 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
719 return (int) (maxWidth * mLayoutScale + 0.5f);
720 }
721
Michael Jurkadde558b2011-11-09 22:09:06 -0800722 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700723 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700724 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700726 int leftScreen = 0;
727 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800728 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800729 while (leftScreen < pageCount - 1 &&
Michael Jurka47f74742012-03-02 13:39:13 -0800730 currPage.getRight() - currPage.getPaddingRight() < mScrollX) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700731 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800732 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700733 }
734 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800735 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800736 while (rightScreen < pageCount - 1 &&
Michael Jurka47f74742012-03-02 13:39:13 -0800737 currPage.getLeft() + currPage.getPaddingLeft() < mScrollX + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700738 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800739 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700740 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800741 range[0] = leftScreen;
742 range[1] = rightScreen;
743 } else {
744 range[0] = -1;
745 range[1] = -1;
746 }
747 }
748
749 @Override
750 protected void dispatchDraw(Canvas canvas) {
751 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800752 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
753 // it is equal to the scaled overscroll position.
754 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800755
756 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
757 screenScrolled(screenCenter);
758 mLastScreenCenter = screenCenter;
759 mForceScreenScrolled = false;
760 }
761
762 // Find out which screens are visible; as an optimization we only call draw on them
763 final int pageCount = getChildCount();
764 if (pageCount > 0) {
765 getVisiblePages(mTempVisiblePagesRange);
766 final int leftScreen = mTempVisiblePagesRange[0];
767 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800768 if (leftScreen != -1 && rightScreen != -1) {
769 final long drawingTime = getDrawingTime();
770 // Clip to the bounds
771 canvas.save();
772 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
773 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700774
Michael Jurka80c69852011-12-16 14:16:32 -0800775 // On certain graphics drivers, if you draw to a off-screen buffer that's not
776 // used, it can lead to poor performance. We were running into this when
777 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
778 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
779 // As a fix, below we set pages that aren't going to be rendered are to be
780 // View.INVISIBLE, preventing re-drawing of their hardware layer
781 for (int i = getChildCount() - 1; i >= 0; i--) {
782 final View v = getPageAt(i);
Winson Chungb33e05f2012-01-30 12:18:04 -0800783
Michael Jurkabed61d22012-02-14 22:51:29 -0800784 if (leftScreen <= i && i <= rightScreen &&
785 v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
Michael Jurka80c69852011-12-16 14:16:32 -0800786 v.setVisibility(VISIBLE);
787 drawChild(canvas, v, drawingTime);
788 } else {
789 v.setVisibility(INVISIBLE);
790 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800791 }
792 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700793 }
Michael Jurka0142d492010-08-25 17:46:15 -0700794 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700795 }
796
797 @Override
798 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700799 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700800 if (page != mCurrentPage || !mScroller.isFinished()) {
801 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 return true;
803 }
804 return false;
805 }
806
807 @Override
808 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700809 int focusablePage;
810 if (mNextPage != INVALID_PAGE) {
811 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700813 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 }
Winson Chung86f77532010-08-24 11:08:22 -0700815 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700817 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 }
819 return false;
820 }
821
822 @Override
823 public boolean dispatchUnhandledMove(View focused, int direction) {
824 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700825 if (getCurrentPage() > 0) {
826 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 return true;
828 }
829 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700830 if (getCurrentPage() < getPageCount() - 1) {
831 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 return true;
833 }
834 }
835 return super.dispatchUnhandledMove(focused, direction);
836 }
837
838 @Override
839 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
841 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700844 if (mCurrentPage > 0) {
845 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 }
847 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700848 if (mCurrentPage < getPageCount() - 1) {
849 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 }
851 }
852 }
853
854 /**
855 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700856 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 *
Winson Chung86f77532010-08-24 11:08:22 -0700858 * This happens when live folders requery, and if they're off page, they
859 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 */
861 @Override
862 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700863 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 View v = focused;
865 while (true) {
866 if (v == current) {
867 super.focusableViewAvailable(focused);
868 return;
869 }
870 if (v == this) {
871 return;
872 }
873 ViewParent parent = v.getParent();
874 if (parent instanceof View) {
875 v = (View)v.getParent();
876 } else {
877 return;
878 }
879 }
880 }
881
882 /**
883 * {@inheritDoc}
884 */
885 @Override
886 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
887 if (disallowIntercept) {
888 // We need to make sure to cancel our long press if
889 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700890 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700891 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 }
893 super.requestDisallowInterceptTouchEvent(disallowIntercept);
894 }
895
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800896 /**
897 * Return true if a tap at (x, y) should trigger a flip to the previous page.
898 */
899 protected boolean hitsPreviousPage(float x, float y) {
900 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
901 }
902
903 /**
904 * Return true if a tap at (x, y) should trigger a flip to the next page.
905 */
906 protected boolean hitsNextPage(float x, float y) {
907 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
908 }
909
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 @Override
911 public boolean onInterceptTouchEvent(MotionEvent ev) {
912 /*
913 * This method JUST determines whether we want to intercept the motion.
914 * If we return true, onTouchEvent will be called and we do the actual
915 * scrolling there.
916 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800917 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700918
Winson Chung45e1d6e2010-11-09 17:19:49 -0800919 // Skip touch handling if there are no pages to swipe
920 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
921
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 /*
923 * Shortcut the most recurring case: the user is in the dragging
924 * state and he is moving his finger. We want to intercept this
925 * motion.
926 */
927 final int action = ev.getAction();
928 if ((action == MotionEvent.ACTION_MOVE) &&
929 (mTouchState == TOUCH_STATE_SCROLLING)) {
930 return true;
931 }
932
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 switch (action & MotionEvent.ACTION_MASK) {
934 case MotionEvent.ACTION_MOVE: {
935 /*
936 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
937 * whether the user has moved far enough from his original down touch.
938 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700939 if (mActivePointerId != INVALID_POINTER) {
940 determineScrollingStart(ev);
941 break;
942 }
943 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
944 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
945 // i.e. fall through to the next case (don't break)
946 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
947 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 }
949
950 case MotionEvent.ACTION_DOWN: {
951 final float x = ev.getX();
952 final float y = ev.getY();
953 // Remember location of down touch
954 mDownMotionX = x;
955 mLastMotionX = x;
956 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800957 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800958 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 mActivePointerId = ev.getPointerId(0);
960 mAllowLongPress = true;
961
962 /*
963 * If being flinged and user touches the screen, initiate drag;
964 * otherwise don't. mScroller.isFinished should be false when
965 * being flinged.
966 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700967 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700968 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
969 if (finishedScrolling) {
970 mTouchState = TOUCH_STATE_REST;
971 mScroller.abortAnimation();
972 } else {
973 mTouchState = TOUCH_STATE_SCROLLING;
974 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700975
Winson Chung86f77532010-08-24 11:08:22 -0700976 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800978 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800980 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800982 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 mTouchState = TOUCH_STATE_NEXT_PAGE;
984 }
985 }
986 }
987 break;
988 }
989
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800991 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 mTouchState = TOUCH_STATE_REST;
993 mAllowLongPress = false;
994 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800995 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 break;
997
998 case MotionEvent.ACTION_POINTER_UP:
999 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001000 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 break;
1002 }
1003
1004 /*
1005 * The only time we want to intercept motion events is if we are in the
1006 * drag mode.
1007 */
1008 return mTouchState != TOUCH_STATE_REST;
1009 }
1010
Adam Cohenf8d28232011-02-01 21:47:00 -08001011 protected void determineScrollingStart(MotionEvent ev) {
1012 determineScrollingStart(ev, 1.0f);
1013 }
1014
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 /*
1016 * Determines if we should change the touch state to start scrolling after the
1017 * user moves their touch point too far.
1018 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001019 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 /*
1021 * Locally do absolute value. mLastMotionX is set to the y value
1022 * of the down event.
1023 */
1024 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001025 if (pointerIndex == -1) {
1026 return;
1027 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001028 final float x = ev.getX(pointerIndex);
1029 final float y = ev.getY(pointerIndex);
1030 final int xDiff = (int) Math.abs(x - mLastMotionX);
1031 final int yDiff = (int) Math.abs(y - mLastMotionY);
1032
Adam Cohenf8d28232011-02-01 21:47:00 -08001033 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 boolean xPaged = xDiff > mPagingTouchSlop;
1035 boolean xMoved = xDiff > touchSlop;
1036 boolean yMoved = yDiff > touchSlop;
1037
Adam Cohenf8d28232011-02-01 21:47:00 -08001038 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001039 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 // Scroll if the user moved far enough along the X axis
1041 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001042 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001044 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001045 mTouchX = mScrollX;
1046 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1047 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 }
1049 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001050 cancelCurrentPageLongPress();
1051 }
1052 }
1053
1054 protected void cancelCurrentPageLongPress() {
1055 if (mAllowLongPress) {
1056 mAllowLongPress = false;
1057 // Try canceling the long press. It could also have been scheduled
1058 // by a distant descendant, so use the mAllowLongPress flag to block
1059 // everything
1060 final View currentPage = getPageAt(mCurrentPage);
1061 if (currentPage != null) {
1062 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 }
1064 }
1065 }
1066
Adam Cohenb5ba0972011-09-07 18:02:31 -07001067 protected float getScrollProgress(int screenCenter, View v, int page) {
1068 final int halfScreenSize = getMeasuredWidth() / 2;
1069
1070 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1071 int delta = screenCenter - (getChildOffset(page) -
1072 getRelativeChildOffset(page) + halfScreenSize);
1073
1074 float scrollProgress = delta / (totalDistance * 1.0f);
1075 scrollProgress = Math.min(scrollProgress, 1.0f);
1076 scrollProgress = Math.max(scrollProgress, -1.0f);
1077 return scrollProgress;
1078 }
1079
Adam Cohene0f66b52010-11-23 15:06:07 -08001080 // This curve determines how the effect of scrolling over the limits of the page dimishes
1081 // as the user pulls further and further from the bounds
1082 private float overScrollInfluenceCurve(float f) {
1083 f -= 1.0f;
1084 return f * f * f + 1.0f;
1085 }
1086
Adam Cohenb5ba0972011-09-07 18:02:31 -07001087 protected void acceleratedOverScroll(float amount) {
1088 int screenSize = getMeasuredWidth();
1089
1090 // We want to reach the max over scroll effect when the user has
1091 // over scrolled half the size of the screen
1092 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1093
1094 if (f == 0) return;
1095
1096 // Clamp this factor, f, to -1 < f < 1
1097 if (Math.abs(f) >= 1) {
1098 f /= Math.abs(f);
1099 }
1100
1101 int overScrollAmount = (int) Math.round(f * screenSize);
1102 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001103 mOverScrollX = overScrollAmount;
1104 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001105 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001106 mOverScrollX = mMaxScrollX + overScrollAmount;
1107 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001108 }
1109 invalidate();
1110 }
1111
1112 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001113 int screenSize = getMeasuredWidth();
1114
1115 float f = (amount / screenSize);
1116
1117 if (f == 0) return;
1118 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1119
Adam Cohen7bfc9792011-01-28 13:52:37 -08001120 // Clamp this factor, f, to -1 < f < 1
1121 if (Math.abs(f) >= 1) {
1122 f /= Math.abs(f);
1123 }
1124
Adam Cohene0f66b52010-11-23 15:06:07 -08001125 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001126 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001127 mOverScrollX = overScrollAmount;
1128 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001129 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001130 mOverScrollX = mMaxScrollX + overScrollAmount;
1131 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001132 }
1133 invalidate();
1134 }
1135
Adam Cohenb5ba0972011-09-07 18:02:31 -07001136 protected void overScroll(float amount) {
1137 dampedOverScroll(amount);
1138 }
1139
Michael Jurkac5b262c2011-01-12 20:24:50 -08001140 protected float maxOverScroll() {
1141 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001142 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001143 float f = 1.0f;
1144 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1145 return OVERSCROLL_DAMP_FACTOR * f;
1146 }
1147
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 @Override
1149 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001150 // Skip touch handling if there are no pages to swipe
1151 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1152
Michael Jurkab8f06722010-10-10 15:58:46 -07001153 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001154
1155 final int action = ev.getAction();
1156
1157 switch (action & MotionEvent.ACTION_MASK) {
1158 case MotionEvent.ACTION_DOWN:
1159 /*
1160 * If being flinged and user touches, stop the fling. isFinished
1161 * will be false if being flinged.
1162 */
1163 if (!mScroller.isFinished()) {
1164 mScroller.abortAnimation();
1165 }
1166
1167 // Remember where the motion event started
1168 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001169 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001170 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001171 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001172 if (mTouchState == TOUCH_STATE_SCROLLING) {
1173 pageBeginMoving();
1174 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 break;
1176
1177 case MotionEvent.ACTION_MOVE:
1178 if (mTouchState == TOUCH_STATE_SCROLLING) {
1179 // Scroll to follow the motion event
1180 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1181 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001182 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001183
Adam Cohenaefd4e12011-02-14 16:39:38 -08001184 mTotalMotionX += Math.abs(deltaX);
1185
Winson Chungc0844aa2011-02-02 15:25:58 -08001186 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1187 // keep the remainder because we are actually testing if we've moved from the last
1188 // scrolled position (which is discrete).
1189 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001190 mTouchX += deltaX;
1191 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1192 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001193 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001194 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001195 } else {
1196 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001197 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001198 mLastMotionX = x;
1199 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 } else {
1201 awakenScrollBars();
1202 }
Adam Cohen564976a2010-10-13 18:52:07 -07001203 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 determineScrollingStart(ev);
1205 }
1206 break;
1207
1208 case MotionEvent.ACTION_UP:
1209 if (mTouchState == TOUCH_STATE_SCROLLING) {
1210 final int activePointerId = mActivePointerId;
1211 final int pointerIndex = ev.findPointerIndex(activePointerId);
1212 final float x = ev.getX(pointerIndex);
1213 final VelocityTracker velocityTracker = mVelocityTracker;
1214 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1215 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001216 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001217 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1218 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1219 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001220
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001221 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1222
Adam Cohen00481b32011-11-18 12:03:48 -08001223 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001224 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001225
Adam Cohenaefd4e12011-02-14 16:39:38 -08001226 // In the case that the page is moved far to one direction and then is flung
1227 // in the opposite direction, we use a threshold to determine whether we should
1228 // just return to the starting page, or if we should skip one further.
1229 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001230 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001231 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001232 returnToOriginalPage = true;
1233 }
1234
Adam Cohenaefd4e12011-02-14 16:39:38 -08001235 int finalPage;
1236 // We give flings precedence over large moves, which is why we short-circuit our
1237 // test for a large move if a fling has been registered. That is, a large
1238 // move to the left and fling to the right will register as a fling to the right.
1239 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1240 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1241 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1242 snapToPageWithVelocity(finalPage, velocityX);
1243 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1244 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001245 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001246 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1247 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 } else {
1249 snapToDestination();
1250 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001251 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 // at this point we have not moved beyond the touch slop
1253 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1254 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001255 int nextPage = Math.max(0, mCurrentPage - 1);
1256 if (nextPage != mCurrentPage) {
1257 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001258 } else {
1259 snapToDestination();
1260 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001261 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001262 // at this point we have not moved beyond the touch slop
1263 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1264 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001265 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1266 if (nextPage != mCurrentPage) {
1267 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 } else {
1269 snapToDestination();
1270 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001271 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001272 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 }
1274 mTouchState = TOUCH_STATE_REST;
1275 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001276 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 break;
1278
1279 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001280 if (mTouchState == TOUCH_STATE_SCROLLING) {
1281 snapToDestination();
1282 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 mTouchState = TOUCH_STATE_REST;
1284 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001285 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 break;
1287
1288 case MotionEvent.ACTION_POINTER_UP:
1289 onSecondaryPointerUp(ev);
1290 break;
1291 }
1292
1293 return true;
1294 }
1295
Winson Chung185d7162011-02-28 13:47:29 -08001296 @Override
1297 public boolean onGenericMotionEvent(MotionEvent event) {
1298 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1299 switch (event.getAction()) {
1300 case MotionEvent.ACTION_SCROLL: {
1301 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1302 final float vscroll;
1303 final float hscroll;
1304 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1305 vscroll = 0;
1306 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1307 } else {
1308 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1309 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1310 }
1311 if (hscroll != 0 || vscroll != 0) {
1312 if (hscroll > 0 || vscroll > 0) {
1313 scrollRight();
1314 } else {
1315 scrollLeft();
1316 }
1317 return true;
1318 }
1319 }
1320 }
1321 }
1322 return super.onGenericMotionEvent(event);
1323 }
1324
Michael Jurkab8f06722010-10-10 15:58:46 -07001325 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1326 if (mVelocityTracker == null) {
1327 mVelocityTracker = VelocityTracker.obtain();
1328 }
1329 mVelocityTracker.addMovement(ev);
1330 }
1331
1332 private void releaseVelocityTracker() {
1333 if (mVelocityTracker != null) {
1334 mVelocityTracker.recycle();
1335 mVelocityTracker = null;
1336 }
1337 }
1338
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 private void onSecondaryPointerUp(MotionEvent ev) {
1340 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1341 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1342 final int pointerId = ev.getPointerId(pointerIndex);
1343 if (pointerId == mActivePointerId) {
1344 // This was our active pointer going up. Choose a new
1345 // active pointer and adjust accordingly.
1346 // TODO: Make this decision more intelligent.
1347 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1348 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1349 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001350 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 mActivePointerId = ev.getPointerId(newPointerIndex);
1352 if (mVelocityTracker != null) {
1353 mVelocityTracker.clear();
1354 }
1355 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001356 }
1357
Michael Jurkad771c962011-08-09 15:00:48 -07001358 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001359
1360 @Override
1361 public void requestChildFocus(View child, View focused) {
1362 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001363 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001364 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001365 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 }
1367 }
1368
Winson Chunge3193b92010-09-10 11:44:42 -07001369 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1370 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001371 int left;
1372 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001373 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001374 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001375 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001376 if (left <= relativeOffset && relativeOffset <= right) {
1377 return i;
1378 }
Winson Chunge3193b92010-09-10 11:44:42 -07001379 }
1380 return -1;
1381 }
1382
Winson Chung1908d072011-02-24 18:09:44 -08001383 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001384 // This functions are called enough times that it actually makes a difference in the
1385 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001386 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001387 final int minWidth = mMinimumWidth;
1388 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001389 }
1390
Adam Cohend19d3ca2010-09-15 14:43:42 -07001391 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001392 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 int minDistanceFromScreenCenterIndex = -1;
1394 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1395 final int childCount = getChildCount();
1396 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001397 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001398 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 int halfChildWidth = (childWidth / 2);
1400 int childCenter = getChildOffset(i) + halfChildWidth;
1401 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1402 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1403 minDistanceFromScreenCenter = distanceFromScreenCenter;
1404 minDistanceFromScreenCenterIndex = i;
1405 }
1406 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001407 return minDistanceFromScreenCenterIndex;
1408 }
1409
1410 protected void snapToDestination() {
1411 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 }
1413
Adam Cohene0f66b52010-11-23 15:06:07 -08001414 private static class ScrollInterpolator implements Interpolator {
1415 public ScrollInterpolator() {
1416 }
1417
1418 public float getInterpolation(float t) {
1419 t -= 1.0f;
1420 return t*t*t*t*t + 1;
1421 }
1422 }
1423
1424 // We want the duration of the page snap animation to be influenced by the distance that
1425 // the screen has to travel, however, we don't want this duration to be effected in a
1426 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1427 // of travel has on the overall snap duration.
1428 float distanceInfluenceForSnapDuration(float f) {
1429 f -= 0.5f; // center the values about 0.
1430 f *= 0.3f * Math.PI / 2.0f;
1431 return (float) Math.sin(f);
1432 }
1433
Michael Jurka0142d492010-08-25 17:46:15 -07001434 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001435 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1436 int halfScreenSize = getMeasuredWidth() / 2;
1437
Winson Chung785d2eb2011-04-14 16:08:02 -07001438 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1439 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1440 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001441 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1442 int delta = newX - mUnboundedScrollX;
1443 int duration = 0;
1444
Adam Cohen265b9a62011-12-07 14:37:18 -08001445 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001446 // If the velocity is low enough, then treat this more as an automatic page advance
1447 // as opposed to an apparent physical response to flinging
1448 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1449 return;
1450 }
1451
1452 // Here we compute a "distance" that will be used in the computation of the overall
1453 // snap duration. This is a function of the actual distance that needs to be traveled;
1454 // we keep this value close to half screen size in order to reduce the variance in snap
1455 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001456 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001457 float distance = halfScreenSize + halfScreenSize *
1458 distanceInfluenceForSnapDuration(distanceRatio);
1459
1460 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001461 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001462
1463 // we want the page's snap velocity to approximately match the velocity at which the
1464 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001465 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1466 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001467
1468 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001469 }
1470
1471 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001472 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 }
1474
Michael Jurka0142d492010-08-25 17:46:15 -07001475 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001476 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001477
Winson Chung785d2eb2011-04-14 16:08:02 -07001478 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1479 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1480 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001481 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001482 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001484 snapToPage(whichPage, delta, duration);
1485 }
1486
1487 protected void snapToPage(int whichPage, int delta, int duration) {
1488 mNextPage = whichPage;
1489
1490 View focusedChild = getFocusedChild();
1491 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001492 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001493 focusedChild.clearFocus();
1494 }
1495
1496 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 awakenScrollBars(duration);
1498 if (duration == 0) {
1499 duration = Math.abs(delta);
1500 }
1501
1502 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001503 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001504
Winson Chungb44b5242011-06-13 11:32:14 -07001505 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1506 // loading associated pages until the scroll settles
1507 if (mDeferScrollUpdate) {
1508 loadAssociatedPages(mNextPage);
1509 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001510 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001511 }
Michael Jurka0142d492010-08-25 17:46:15 -07001512 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001513 invalidate();
1514 }
1515
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 public void scrollLeft() {
1517 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001518 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001519 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001520 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 }
1522 }
1523
1524 public void scrollRight() {
1525 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001526 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001528 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001529 }
1530 }
1531
Winson Chung86f77532010-08-24 11:08:22 -07001532 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001533 int result = -1;
1534 if (v != null) {
1535 ViewParent vp = v.getParent();
1536 int count = getChildCount();
1537 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001538 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 return i;
1540 }
1541 }
1542 }
1543 return result;
1544 }
1545
1546 /**
1547 * @return True is long presses are still allowed for the current touch
1548 */
1549 public boolean allowLongPress() {
1550 return mAllowLongPress;
1551 }
1552
Michael Jurka0142d492010-08-25 17:46:15 -07001553 /**
1554 * Set true to allow long-press events to be triggered, usually checked by
1555 * {@link Launcher} to accept or block dpad-initiated long-presses.
1556 */
1557 public void setAllowLongPress(boolean allowLongPress) {
1558 mAllowLongPress = allowLongPress;
1559 }
1560
Winson Chung321e9ee2010-08-09 13:37:56 -07001561 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001562 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001563
1564 SavedState(Parcelable superState) {
1565 super(superState);
1566 }
1567
1568 private SavedState(Parcel in) {
1569 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001570 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 }
1572
1573 @Override
1574 public void writeToParcel(Parcel out, int flags) {
1575 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001576 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001577 }
1578
1579 public static final Parcelable.Creator<SavedState> CREATOR =
1580 new Parcelable.Creator<SavedState>() {
1581 public SavedState createFromParcel(Parcel in) {
1582 return new SavedState(in);
1583 }
1584
1585 public SavedState[] newArray(int size) {
1586 return new SavedState[size];
1587 }
1588 };
1589 }
1590
Winson Chungf314b0e2011-08-16 11:54:27 -07001591 protected void loadAssociatedPages(int page) {
1592 loadAssociatedPages(page, false);
1593 }
1594 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001595 if (mContentIsRefreshable) {
1596 final int count = getChildCount();
1597 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001598 int lowerPageBound = getAssociatedLowerPageBound(page);
1599 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1601 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001602 // First, clear any pages that should no longer be loaded
1603 for (int i = 0; i < count; ++i) {
1604 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001605 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001606 if (layout.getPageChildCount() > 0) {
1607 layout.removeAllViewsOnPage();
1608 }
1609 mDirtyPageContent.set(i, true);
1610 }
1611 }
1612 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001613 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001614 if ((i != page) && immediateAndOnly) {
1615 continue;
1616 }
Michael Jurka0142d492010-08-25 17:46:15 -07001617 if (lowerPageBound <= i && i <= upperPageBound) {
1618 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001619 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001620 mDirtyPageContent.set(i, false);
1621 }
Winson Chung86f77532010-08-24 11:08:22 -07001622 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001623 }
1624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001625 }
1626 }
1627
Winson Chunge3193b92010-09-10 11:44:42 -07001628 protected int getAssociatedLowerPageBound(int page) {
1629 return Math.max(0, page - 1);
1630 }
1631 protected int getAssociatedUpperPageBound(int page) {
1632 final int count = getChildCount();
1633 return Math.min(page + 1, count - 1);
1634 }
1635
Winson Chung86f77532010-08-24 11:08:22 -07001636 /**
1637 * This method is called ONLY to synchronize the number of pages that the paged view has.
1638 * To actually fill the pages with information, implement syncPageItems() below. It is
1639 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1640 * and therefore, individual page items do not need to be updated in this method.
1641 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001642 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001643
1644 /**
1645 * This method is called to synchronize the items that are on a particular page. If views on
1646 * the page can be reused, then they should be updated within this method.
1647 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001648 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001649
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001650 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001651 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001652 }
1653 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001654 invalidatePageData(currentPage, false);
1655 }
1656 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001657 if (!mIsDataReady) {
1658 return;
1659 }
1660
Michael Jurka0142d492010-08-25 17:46:15 -07001661 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001662 // Force all scrolling-related behavior to end
1663 mScroller.forceFinished(true);
1664 mNextPage = INVALID_PAGE;
1665
Michael Jurka0142d492010-08-25 17:46:15 -07001666 // Update all the pages
1667 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001668
Winson Chung5a808352011-06-27 19:08:49 -07001669 // We must force a measure after we've loaded the pages to update the content width and
1670 // to determine the full scroll width
1671 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1672 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1673
1674 // Set a new page as the current page if necessary
1675 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001676 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001677 }
1678
Michael Jurka0142d492010-08-25 17:46:15 -07001679 // Mark each of the pages as dirty
1680 final int count = getChildCount();
1681 mDirtyPageContent.clear();
1682 for (int i = 0; i < count; ++i) {
1683 mDirtyPageContent.add(true);
1684 }
1685
1686 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001687 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001688 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001689 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001690 }
Winson Chung007c6982011-06-14 13:27:53 -07001691
Michael Jurkaafaa0502011-12-13 18:22:50 -08001692 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001693 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1694 // found
1695 if (mHasScrollIndicator && mScrollIndicator == null) {
1696 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001697 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001698 mHasScrollIndicator = mScrollIndicator != null;
1699 if (mHasScrollIndicator) {
1700 mScrollIndicator.setVisibility(View.VISIBLE);
1701 }
1702 }
1703 return mScrollIndicator;
1704 }
1705
1706 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001707 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001708 }
1709
Winson Chung5a808352011-06-27 19:08:49 -07001710 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1711 @Override
1712 public void run() {
1713 hideScrollingIndicator(false);
1714 }
1715 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001716 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001717 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001718 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001719 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001720 }
1721
Michael Jurka430e8a52011-08-08 15:52:14 -07001722 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001723 mShouldShowScrollIndicator = true;
1724 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001725 if (getChildCount() <= 1) return;
1726 if (!isScrollingIndicatorEnabled()) return;
1727
Michael Jurkabed61d22012-02-14 22:51:29 -08001728 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001729 getScrollingIndicator();
1730 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001731 // Fade the indicator in
1732 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001733 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001734 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001735 if (immediately) {
1736 mScrollIndicator.setAlpha(1f);
1737 } else {
1738 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1739 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1740 mScrollIndicatorAnimator.start();
1741 }
Winson Chung007c6982011-06-14 13:27:53 -07001742 }
1743 }
1744
Adam Cohen21b41102011-11-01 17:29:52 -07001745 protected void cancelScrollingIndicatorAnimations() {
1746 if (mScrollIndicatorAnimator != null) {
1747 mScrollIndicatorAnimator.cancel();
1748 }
1749 }
1750
Winson Chung007c6982011-06-14 13:27:53 -07001751 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001752 if (getChildCount() <= 1) return;
1753 if (!isScrollingIndicatorEnabled()) return;
1754
1755 getScrollingIndicator();
1756 if (mScrollIndicator != null) {
1757 // Fade the indicator out
1758 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001759 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001760 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001761 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001762 mScrollIndicator.setAlpha(0f);
1763 } else {
1764 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1765 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1766 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1767 private boolean cancelled = false;
1768 @Override
1769 public void onAnimationCancel(android.animation.Animator animation) {
1770 cancelled = true;
1771 }
1772 @Override
1773 public void onAnimationEnd(Animator animation) {
1774 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001775 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001776 }
1777 }
1778 });
1779 mScrollIndicatorAnimator.start();
1780 }
Winson Chung007c6982011-06-14 13:27:53 -07001781 }
1782 }
1783
Winson Chung32174c82011-07-19 15:47:55 -07001784 /**
1785 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1786 * fill its space on the track or not.
1787 */
1788 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001789 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001790 }
1791
Winson Chung007c6982011-06-14 13:27:53 -07001792 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001793 if (getChildCount() <= 1) return;
1794 if (!isScrollingIndicatorEnabled()) return;
1795
1796 getScrollingIndicator();
1797 if (mScrollIndicator != null) {
1798 updateScrollingIndicatorPosition();
1799 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001800 if (mShouldShowScrollIndicator) {
1801 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1802 }
Winson Chung007c6982011-06-14 13:27:53 -07001803 }
1804
Winson Chung007c6982011-06-14 13:27:53 -07001805 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001806 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001807 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001808 int numPages = getChildCount();
1809 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001810 int lastChildIndex = Math.max(0, getChildCount() - 1);
1811 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001812 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001813 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1814 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001815
Winson Chungae890b82011-09-13 18:08:54 -07001816 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001817 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001818 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001819 if (hasElasticScrollIndicator()) {
1820 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1821 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1822 mScrollIndicator.requestLayout();
1823 }
1824 } else {
1825 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1826 indicatorPos += indicatorCenterOffset;
1827 }
Winson Chung007c6982011-06-14 13:27:53 -07001828 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001829 }
1830
Winson Chung3ac74c52011-06-30 17:39:37 -07001831 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001832 }
1833
1834 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001835 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001836
1837 /* Accessibility */
1838 @Override
1839 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1840 super.onInitializeAccessibilityNodeInfo(info);
1841 info.setScrollable(true);
1842 }
1843
1844 @Override
1845 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1846 super.onInitializeAccessibilityEvent(event);
1847 event.setScrollable(true);
1848 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1849 event.setFromIndex(mCurrentPage);
1850 event.setToIndex(mCurrentPage);
1851 event.setItemCount(getChildCount());
1852 }
1853 }
1854
Winson Chung6a0f57d2011-06-29 20:10:49 -07001855 protected String getCurrentPageDescription() {
1856 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1857 return String.format(mContext.getString(R.string.default_scroll_format),
1858 page + 1, getChildCount());
1859 }
Winson Chungd11265e2011-08-30 13:37:23 -07001860
1861 @Override
1862 public boolean onHoverEvent(android.view.MotionEvent event) {
1863 return true;
1864 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001865}