blob: 604e73c1d6e0e21c07717e6f1b2839ec1cc91705 [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
Michael Jurkab06d95f2012-04-02 06:26:53 -0700399 if (mScrollX != mScroller.getCurrX()
400 || mScrollY != mScroller.getCurrY()
401 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700402 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
403 }
Michael Jurka0142d492010-08-25 17:46:15 -0700404 invalidate();
405 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700406 } else if (mNextPage != INVALID_PAGE) {
407 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700408 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700409 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700410
411 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700412 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700413 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700414 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700415 }
416
Adam Cohen73aa9752010-11-24 16:26:58 -0800417 // We don't want to trigger a page end moving unless the page has settled
418 // and the user has stopped scrolling
419 if (mTouchState == TOUCH_STATE_REST) {
420 pageEndMoving();
421 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700422
423 // Notify the user when the page changes
424 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
425 AccessibilityEvent ev =
426 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
427 ev.getText().add(getCurrentPageDescription());
428 sendAccessibilityEventUnchecked(ev);
429 }
Michael Jurka0142d492010-08-25 17:46:15 -0700430 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 }
Michael Jurka0142d492010-08-25 17:46:15 -0700432 return false;
433 }
434
435 @Override
436 public void computeScroll() {
437 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
439
440 @Override
441 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 if (!mIsDataReady) {
443 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
444 return;
445 }
446
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
448 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
449 if (widthMode != MeasureSpec.EXACTLY) {
450 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
451 }
452
Adam Lesinski6b879f02010-11-04 16:15:23 -0700453 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
454 * of the All apps view on XLarge displays to not take up more space then it needs. Width
455 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
456 * each page to have the same width.
457 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700459 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
460 int maxChildHeight = 0;
461
462 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700463 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700464
Michael Jurka36fcb742011-04-06 17:08:58 -0700465
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700467 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700468 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700469 final int childCount = getChildCount();
470 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700471 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700472 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700473 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
474
475 int childWidthMode;
476 if (lp.width == LayoutParams.WRAP_CONTENT) {
477 childWidthMode = MeasureSpec.AT_MOST;
478 } else {
479 childWidthMode = MeasureSpec.EXACTLY;
480 }
481
482 int childHeightMode;
483 if (lp.height == LayoutParams.WRAP_CONTENT) {
484 childHeightMode = MeasureSpec.AT_MOST;
485 } else {
486 childHeightMode = MeasureSpec.EXACTLY;
487 }
488
489 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700490 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700491 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700492 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700493
494 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700495 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700496 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
497 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700498 }
499
500 if (heightMode == MeasureSpec.AT_MOST) {
501 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 }
Winson Chungae890b82011-09-13 18:08:54 -0700503
Winson Chungae890b82011-09-13 18:08:54 -0700504 setMeasuredDimension(widthSize, heightSize);
505
Adam Cohen25b29952011-11-02 14:49:06 -0700506 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
507 // We also wait until we set the measured dimensions before flushing the cache as well, to
508 // ensure that the cache is filled with good values.
509 invalidateCachedOffsets();
510 updateScrollingIndicatorPosition();
511
Adam Cohenfaa28302010-11-19 12:02:18 -0800512 if (childCount > 0) {
513 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
514 } else {
515 mMaxScrollX = 0;
516 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 }
518
Michael Jurka8c920dd2011-01-20 14:16:56 -0800519 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800520 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
521 int delta = newX - mScrollX;
522
Michael Jurka8c920dd2011-01-20 14:16:56 -0800523 final int pageCount = getChildCount();
524 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700525 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800526 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800527 }
528 setCurrentPage(newCurrentPage);
529 }
530
Michael Jurka8c920dd2011-01-20 14:16:56 -0800531 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
532 // 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 -0800533 // tightens the layout accordingly
534 public void setLayoutScale(float childrenScale) {
535 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700536 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800537
538 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
539 int childCount = getChildCount();
540 float childrenX[] = new float[childCount];
541 float childrenY[] = new float[childCount];
542 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700543 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800544 childrenX[i] = child.getX();
545 childrenY[i] = child.getY();
546 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700547 // Trigger a full re-layout (never just call onLayout directly!)
548 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
549 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
550 requestLayout();
551 measure(widthSpec, heightSpec);
552 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800553 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700554 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 child.setX(childrenX[i]);
556 child.setY(childrenY[i]);
557 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700558
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 // Also, the page offset has changed (since the pages are now smaller);
560 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800561 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800562 }
563
Adam Cohen60b07122011-11-14 17:26:06 -0800564 public void setPageSpacing(int pageSpacing) {
565 mPageSpacing = pageSpacing;
566 invalidateCachedOffsets();
567 }
568
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700570 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700571 if (!mIsDataReady) {
572 return;
573 }
574
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700576 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 final int childCount = getChildCount();
578 int childLeft = 0;
579 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700580 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
581 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700582 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700583
584 // Calculate the variable page spacing if necessary
585 if (mPageSpacing < 0) {
Winson Chungeecf02d2012-03-02 17:14:58 -0800586 // The gap between pages in the PagedView should be equal to the gap from the page
587 // to the edge of the screen (so it is not visible in the current screen). To
588 // account for unequal padding on each side of the paged view, we take the maximum
589 // of the left/right gap and use that as the gap between each page.
590 int offset = getRelativeChildOffset(0);
591 int spacing = Math.max(offset, (right - left) - offset -
592 getChildAt(0).getMeasuredWidth());
593 setPageSpacing(spacing);
Winson Chungae890b82011-09-13 18:08:54 -0700594 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 }
596
597 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700598 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800600 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700601 final int childHeight = child.getMeasuredHeight();
602 int childTop = mPaddingTop;
603 if (mCenterPagesVertically) {
604 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
605 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800606
Winson Chung785d2eb2011-04-14 16:08:02 -0700607 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700608 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800609 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700610 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 }
612 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700613
614 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
615 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800616 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700617 setHorizontalScrollBarEnabled(true);
618 mFirstLayout = false;
619 }
620
Michael Jurkad3ef3062010-11-23 16:23:58 -0800621 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
622 mFirstLayout = false;
623 }
Winson Chunge3193b92010-09-10 11:44:42 -0700624 }
625
Adam Cohenf34bab52010-09-30 14:11:56 -0700626 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700627 if (isScrollingIndicatorEnabled()) {
628 updateScrollingIndicator();
629 }
630 if (mFadeInAdjacentScreens) {
631 for (int i = 0; i < getChildCount(); i++) {
632 View child = getChildAt(i);
633 if (child != null) {
634 float scrollProgress = getScrollProgress(screenCenter, child, i);
635 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800636 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700637 }
638 }
639 invalidate();
640 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700641 }
642
Winson Chunge3193b92010-09-10 11:44:42 -0700643 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700644 protected void onViewAdded(View child) {
645 super.onViewAdded(child);
646
647 // This ensures that when children are added, they get the correct transforms / alphas
648 // in accordance with any scroll effects.
649 mForceScreenScrolled = true;
650 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700651 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700652 }
653
Adam Cohen73894962011-10-31 13:17:17 -0700654 protected void invalidateCachedOffsets() {
655 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700656 if (count == 0) {
657 mChildOffsets = null;
658 mChildRelativeOffsets = null;
659 mChildOffsetsWithLayoutScale = null;
660 return;
661 }
Adam Cohen73894962011-10-31 13:17:17 -0700662
663 mChildOffsets = new int[count];
664 mChildRelativeOffsets = new int[count];
665 mChildOffsetsWithLayoutScale = new int[count];
666 for (int i = 0; i < count; i++) {
667 mChildOffsets[i] = -1;
668 mChildRelativeOffsets[i] = -1;
669 mChildOffsetsWithLayoutScale[i] = -1;
670 }
671 }
672
673 protected int getChildOffset(int index) {
674 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
675 mChildOffsets : mChildOffsetsWithLayoutScale;
676
677 if (childOffsets != null && childOffsets[index] != -1) {
678 return childOffsets[index];
679 } else {
680 if (getChildCount() == 0)
681 return 0;
682
683 int offset = getRelativeChildOffset(0);
684 for (int i = 0; i < index; ++i) {
685 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
686 }
687 if (childOffsets != null) {
688 childOffsets[index] = offset;
689 }
690 return offset;
691 }
692 }
693
694 protected int getRelativeChildOffset(int index) {
695 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
696 return mChildRelativeOffsets[index];
697 } else {
698 final int padding = mPaddingLeft + mPaddingRight;
699 final int offset = mPaddingLeft +
700 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
701 if (mChildRelativeOffsets != null) {
702 mChildRelativeOffsets[index] = offset;
703 }
704 return offset;
705 }
706 }
707
708 protected int getScaledRelativeChildOffset(int index) {
709 final int padding = mPaddingLeft + mPaddingRight;
710 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
711 getScaledMeasuredWidth(getPageAt(index))) / 2;
712 return offset;
713 }
714
715 protected int getScaledMeasuredWidth(View child) {
716 // This functions are called enough times that it actually makes a difference in the
717 // profiler -- so just inline the max() here
718 final int measuredWidth = child.getMeasuredWidth();
719 final int minWidth = mMinimumWidth;
720 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
721 return (int) (maxWidth * mLayoutScale + 0.5f);
722 }
723
Michael Jurkadde558b2011-11-09 22:09:06 -0800724 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700725 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700726
Michael Jurkac4fb9172010-09-02 17:19:20 -0700727 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700728 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700729 int leftScreen = 0;
730 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800731 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800732 while (leftScreen < pageCount - 1 &&
Michael Jurka4ff7d792012-04-02 03:46:50 -0700733 currPage.getX() + currPage.getWidth() - currPage.getPaddingRight() < mScrollX) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700734 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800735 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700736 }
737 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800738 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800739 while (rightScreen < pageCount - 1 &&
Michael Jurka4ff7d792012-04-02 03:46:50 -0700740 currPage.getX() - currPage.getPaddingLeft() < mScrollX + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700741 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800742 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700743 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800744 range[0] = leftScreen;
745 range[1] = rightScreen;
746 } else {
747 range[0] = -1;
748 range[1] = -1;
749 }
750 }
751
752 @Override
753 protected void dispatchDraw(Canvas canvas) {
754 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800755 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
756 // it is equal to the scaled overscroll position.
757 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800758
759 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700760 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
761 // set it for the next frame
762 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800763 screenScrolled(screenCenter);
764 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800765 }
766
767 // Find out which screens are visible; as an optimization we only call draw on them
768 final int pageCount = getChildCount();
769 if (pageCount > 0) {
770 getVisiblePages(mTempVisiblePagesRange);
771 final int leftScreen = mTempVisiblePagesRange[0];
772 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800773 if (leftScreen != -1 && rightScreen != -1) {
774 final long drawingTime = getDrawingTime();
775 // Clip to the bounds
776 canvas.save();
777 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
778 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700779
Michael Jurka80c69852011-12-16 14:16:32 -0800780 // On certain graphics drivers, if you draw to a off-screen buffer that's not
781 // used, it can lead to poor performance. We were running into this when
782 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
783 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
784 // As a fix, below we set pages that aren't going to be rendered are to be
785 // View.INVISIBLE, preventing re-drawing of their hardware layer
786 for (int i = getChildCount() - 1; i >= 0; i--) {
787 final View v = getPageAt(i);
Winson Chungb33e05f2012-01-30 12:18:04 -0800788
Michael Jurkabed61d22012-02-14 22:51:29 -0800789 if (leftScreen <= i && i <= rightScreen &&
790 v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
Michael Jurka80c69852011-12-16 14:16:32 -0800791 v.setVisibility(VISIBLE);
792 drawChild(canvas, v, drawingTime);
793 } else {
794 v.setVisibility(INVISIBLE);
795 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800796 }
797 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700798 }
Michael Jurka0142d492010-08-25 17:46:15 -0700799 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 }
801
802 @Override
803 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700804 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700805 if (page != mCurrentPage || !mScroller.isFinished()) {
806 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 return true;
808 }
809 return false;
810 }
811
812 @Override
813 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700814 int focusablePage;
815 if (mNextPage != INVALID_PAGE) {
816 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700818 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 }
Winson Chung86f77532010-08-24 11:08:22 -0700820 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700822 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 }
824 return false;
825 }
826
827 @Override
828 public boolean dispatchUnhandledMove(View focused, int direction) {
829 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700830 if (getCurrentPage() > 0) {
831 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 return true;
833 }
834 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700835 if (getCurrentPage() < getPageCount() - 1) {
836 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 return true;
838 }
839 }
840 return super.dispatchUnhandledMove(focused, direction);
841 }
842
843 @Override
844 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700845 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
846 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700849 if (mCurrentPage > 0) {
850 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 }
852 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700853 if (mCurrentPage < getPageCount() - 1) {
854 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 }
856 }
857 }
858
859 /**
860 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700861 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 *
Winson Chung86f77532010-08-24 11:08:22 -0700863 * This happens when live folders requery, and if they're off page, they
864 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 */
866 @Override
867 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700868 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 View v = focused;
870 while (true) {
871 if (v == current) {
872 super.focusableViewAvailable(focused);
873 return;
874 }
875 if (v == this) {
876 return;
877 }
878 ViewParent parent = v.getParent();
879 if (parent instanceof View) {
880 v = (View)v.getParent();
881 } else {
882 return;
883 }
884 }
885 }
886
887 /**
888 * {@inheritDoc}
889 */
890 @Override
891 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
892 if (disallowIntercept) {
893 // We need to make sure to cancel our long press if
894 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700895 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700896 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 }
898 super.requestDisallowInterceptTouchEvent(disallowIntercept);
899 }
900
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800901 /**
902 * Return true if a tap at (x, y) should trigger a flip to the previous page.
903 */
904 protected boolean hitsPreviousPage(float x, float y) {
905 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
906 }
907
908 /**
909 * Return true if a tap at (x, y) should trigger a flip to the next page.
910 */
911 protected boolean hitsNextPage(float x, float y) {
912 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
913 }
914
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 @Override
916 public boolean onInterceptTouchEvent(MotionEvent ev) {
917 /*
918 * This method JUST determines whether we want to intercept the motion.
919 * If we return true, onTouchEvent will be called and we do the actual
920 * scrolling there.
921 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800922 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700923
Winson Chung45e1d6e2010-11-09 17:19:49 -0800924 // Skip touch handling if there are no pages to swipe
925 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
926
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 /*
928 * Shortcut the most recurring case: the user is in the dragging
929 * state and he is moving his finger. We want to intercept this
930 * motion.
931 */
932 final int action = ev.getAction();
933 if ((action == MotionEvent.ACTION_MOVE) &&
934 (mTouchState == TOUCH_STATE_SCROLLING)) {
935 return true;
936 }
937
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 switch (action & MotionEvent.ACTION_MASK) {
939 case MotionEvent.ACTION_MOVE: {
940 /*
941 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
942 * whether the user has moved far enough from his original down touch.
943 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700944 if (mActivePointerId != INVALID_POINTER) {
945 determineScrollingStart(ev);
946 break;
947 }
948 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
949 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
950 // i.e. fall through to the next case (don't break)
951 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
952 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 }
954
955 case MotionEvent.ACTION_DOWN: {
956 final float x = ev.getX();
957 final float y = ev.getY();
958 // Remember location of down touch
959 mDownMotionX = x;
960 mLastMotionX = x;
961 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800962 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800963 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 mActivePointerId = ev.getPointerId(0);
965 mAllowLongPress = true;
966
967 /*
968 * If being flinged and user touches the screen, initiate drag;
969 * otherwise don't. mScroller.isFinished should be false when
970 * being flinged.
971 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700972 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700973 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
974 if (finishedScrolling) {
975 mTouchState = TOUCH_STATE_REST;
976 mScroller.abortAnimation();
977 } else {
978 mTouchState = TOUCH_STATE_SCROLLING;
979 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700980
Winson Chung86f77532010-08-24 11:08:22 -0700981 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800983 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800985 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800987 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 mTouchState = TOUCH_STATE_NEXT_PAGE;
989 }
990 }
991 }
992 break;
993 }
994
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800996 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 mTouchState = TOUCH_STATE_REST;
998 mAllowLongPress = false;
999 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001000 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 break;
1002
1003 case MotionEvent.ACTION_POINTER_UP:
1004 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001005 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 break;
1007 }
1008
1009 /*
1010 * The only time we want to intercept motion events is if we are in the
1011 * drag mode.
1012 */
1013 return mTouchState != TOUCH_STATE_REST;
1014 }
1015
Adam Cohenf8d28232011-02-01 21:47:00 -08001016 protected void determineScrollingStart(MotionEvent ev) {
1017 determineScrollingStart(ev, 1.0f);
1018 }
1019
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 /*
1021 * Determines if we should change the touch state to start scrolling after the
1022 * user moves their touch point too far.
1023 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001024 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 /*
1026 * Locally do absolute value. mLastMotionX is set to the y value
1027 * of the down event.
1028 */
1029 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001030 if (pointerIndex == -1) {
1031 return;
1032 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 final float x = ev.getX(pointerIndex);
1034 final float y = ev.getY(pointerIndex);
1035 final int xDiff = (int) Math.abs(x - mLastMotionX);
1036 final int yDiff = (int) Math.abs(y - mLastMotionY);
1037
Adam Cohenf8d28232011-02-01 21:47:00 -08001038 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 boolean xPaged = xDiff > mPagingTouchSlop;
1040 boolean xMoved = xDiff > touchSlop;
1041 boolean yMoved = yDiff > touchSlop;
1042
Adam Cohenf8d28232011-02-01 21:47:00 -08001043 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001044 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 // Scroll if the user moved far enough along the X axis
1046 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001047 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001049 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001050 mTouchX = mScrollX;
1051 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1052 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 }
1054 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001055 cancelCurrentPageLongPress();
1056 }
1057 }
1058
1059 protected void cancelCurrentPageLongPress() {
1060 if (mAllowLongPress) {
1061 mAllowLongPress = false;
1062 // Try canceling the long press. It could also have been scheduled
1063 // by a distant descendant, so use the mAllowLongPress flag to block
1064 // everything
1065 final View currentPage = getPageAt(mCurrentPage);
1066 if (currentPage != null) {
1067 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001068 }
1069 }
1070 }
1071
Adam Cohenb5ba0972011-09-07 18:02:31 -07001072 protected float getScrollProgress(int screenCenter, View v, int page) {
1073 final int halfScreenSize = getMeasuredWidth() / 2;
1074
1075 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1076 int delta = screenCenter - (getChildOffset(page) -
1077 getRelativeChildOffset(page) + halfScreenSize);
1078
1079 float scrollProgress = delta / (totalDistance * 1.0f);
1080 scrollProgress = Math.min(scrollProgress, 1.0f);
1081 scrollProgress = Math.max(scrollProgress, -1.0f);
1082 return scrollProgress;
1083 }
1084
Adam Cohene0f66b52010-11-23 15:06:07 -08001085 // This curve determines how the effect of scrolling over the limits of the page dimishes
1086 // as the user pulls further and further from the bounds
1087 private float overScrollInfluenceCurve(float f) {
1088 f -= 1.0f;
1089 return f * f * f + 1.0f;
1090 }
1091
Adam Cohenb5ba0972011-09-07 18:02:31 -07001092 protected void acceleratedOverScroll(float amount) {
1093 int screenSize = getMeasuredWidth();
1094
1095 // We want to reach the max over scroll effect when the user has
1096 // over scrolled half the size of the screen
1097 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1098
1099 if (f == 0) return;
1100
1101 // Clamp this factor, f, to -1 < f < 1
1102 if (Math.abs(f) >= 1) {
1103 f /= Math.abs(f);
1104 }
1105
1106 int overScrollAmount = (int) Math.round(f * screenSize);
1107 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001108 mOverScrollX = overScrollAmount;
1109 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001110 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001111 mOverScrollX = mMaxScrollX + overScrollAmount;
1112 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001113 }
1114 invalidate();
1115 }
1116
1117 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001118 int screenSize = getMeasuredWidth();
1119
1120 float f = (amount / screenSize);
1121
1122 if (f == 0) return;
1123 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1124
Adam Cohen7bfc9792011-01-28 13:52:37 -08001125 // Clamp this factor, f, to -1 < f < 1
1126 if (Math.abs(f) >= 1) {
1127 f /= Math.abs(f);
1128 }
1129
Adam Cohene0f66b52010-11-23 15:06:07 -08001130 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001131 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001132 mOverScrollX = overScrollAmount;
1133 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001134 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001135 mOverScrollX = mMaxScrollX + overScrollAmount;
1136 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001137 }
1138 invalidate();
1139 }
1140
Adam Cohenb5ba0972011-09-07 18:02:31 -07001141 protected void overScroll(float amount) {
1142 dampedOverScroll(amount);
1143 }
1144
Michael Jurkac5b262c2011-01-12 20:24:50 -08001145 protected float maxOverScroll() {
1146 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001147 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001148 float f = 1.0f;
1149 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1150 return OVERSCROLL_DAMP_FACTOR * f;
1151 }
1152
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 @Override
1154 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001155 // Skip touch handling if there are no pages to swipe
1156 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1157
Michael Jurkab8f06722010-10-10 15:58:46 -07001158 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001159
1160 final int action = ev.getAction();
1161
1162 switch (action & MotionEvent.ACTION_MASK) {
1163 case MotionEvent.ACTION_DOWN:
1164 /*
1165 * If being flinged and user touches, stop the fling. isFinished
1166 * will be false if being flinged.
1167 */
1168 if (!mScroller.isFinished()) {
1169 mScroller.abortAnimation();
1170 }
1171
1172 // Remember where the motion event started
1173 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001174 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001175 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001177 if (mTouchState == TOUCH_STATE_SCROLLING) {
1178 pageBeginMoving();
1179 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 break;
1181
1182 case MotionEvent.ACTION_MOVE:
1183 if (mTouchState == TOUCH_STATE_SCROLLING) {
1184 // Scroll to follow the motion event
1185 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1186 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001187 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001188
Adam Cohenaefd4e12011-02-14 16:39:38 -08001189 mTotalMotionX += Math.abs(deltaX);
1190
Winson Chungc0844aa2011-02-02 15:25:58 -08001191 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1192 // keep the remainder because we are actually testing if we've moved from the last
1193 // scrolled position (which is discrete).
1194 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001195 mTouchX += deltaX;
1196 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1197 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001198 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001199 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001200 } else {
1201 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001203 mLastMotionX = x;
1204 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 } else {
1206 awakenScrollBars();
1207 }
Adam Cohen564976a2010-10-13 18:52:07 -07001208 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001209 determineScrollingStart(ev);
1210 }
1211 break;
1212
1213 case MotionEvent.ACTION_UP:
1214 if (mTouchState == TOUCH_STATE_SCROLLING) {
1215 final int activePointerId = mActivePointerId;
1216 final int pointerIndex = ev.findPointerIndex(activePointerId);
1217 final float x = ev.getX(pointerIndex);
1218 final VelocityTracker velocityTracker = mVelocityTracker;
1219 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1220 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001221 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001222 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1223 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1224 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001225
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001226 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1227
Adam Cohen00481b32011-11-18 12:03:48 -08001228 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001229 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001230
Adam Cohenaefd4e12011-02-14 16:39:38 -08001231 // In the case that the page is moved far to one direction and then is flung
1232 // in the opposite direction, we use a threshold to determine whether we should
1233 // just return to the starting page, or if we should skip one further.
1234 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001235 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001236 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001237 returnToOriginalPage = true;
1238 }
1239
Adam Cohenaefd4e12011-02-14 16:39:38 -08001240 int finalPage;
1241 // We give flings precedence over large moves, which is why we short-circuit our
1242 // test for a large move if a fling has been registered. That is, a large
1243 // move to the left and fling to the right will register as a fling to the right.
1244 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1245 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1246 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1247 snapToPageWithVelocity(finalPage, velocityX);
1248 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1249 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001250 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001251 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1252 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 } else {
1254 snapToDestination();
1255 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001256 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 // at this point we have not moved beyond the touch slop
1258 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1259 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001260 int nextPage = Math.max(0, mCurrentPage - 1);
1261 if (nextPage != mCurrentPage) {
1262 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 } else {
1264 snapToDestination();
1265 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001266 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 // at this point we have not moved beyond the touch slop
1268 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1269 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001270 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1271 if (nextPage != mCurrentPage) {
1272 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 } else {
1274 snapToDestination();
1275 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001276 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001277 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 }
1279 mTouchState = TOUCH_STATE_REST;
1280 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001281 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 break;
1283
1284 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001285 if (mTouchState == TOUCH_STATE_SCROLLING) {
1286 snapToDestination();
1287 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 mTouchState = TOUCH_STATE_REST;
1289 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001290 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 break;
1292
1293 case MotionEvent.ACTION_POINTER_UP:
1294 onSecondaryPointerUp(ev);
1295 break;
1296 }
1297
1298 return true;
1299 }
1300
Winson Chung185d7162011-02-28 13:47:29 -08001301 @Override
1302 public boolean onGenericMotionEvent(MotionEvent event) {
1303 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1304 switch (event.getAction()) {
1305 case MotionEvent.ACTION_SCROLL: {
1306 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1307 final float vscroll;
1308 final float hscroll;
1309 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1310 vscroll = 0;
1311 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1312 } else {
1313 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1314 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1315 }
1316 if (hscroll != 0 || vscroll != 0) {
1317 if (hscroll > 0 || vscroll > 0) {
1318 scrollRight();
1319 } else {
1320 scrollLeft();
1321 }
1322 return true;
1323 }
1324 }
1325 }
1326 }
1327 return super.onGenericMotionEvent(event);
1328 }
1329
Michael Jurkab8f06722010-10-10 15:58:46 -07001330 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1331 if (mVelocityTracker == null) {
1332 mVelocityTracker = VelocityTracker.obtain();
1333 }
1334 mVelocityTracker.addMovement(ev);
1335 }
1336
1337 private void releaseVelocityTracker() {
1338 if (mVelocityTracker != null) {
1339 mVelocityTracker.recycle();
1340 mVelocityTracker = null;
1341 }
1342 }
1343
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 private void onSecondaryPointerUp(MotionEvent ev) {
1345 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1346 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1347 final int pointerId = ev.getPointerId(pointerIndex);
1348 if (pointerId == mActivePointerId) {
1349 // This was our active pointer going up. Choose a new
1350 // active pointer and adjust accordingly.
1351 // TODO: Make this decision more intelligent.
1352 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1353 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1354 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001355 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 mActivePointerId = ev.getPointerId(newPointerIndex);
1357 if (mVelocityTracker != null) {
1358 mVelocityTracker.clear();
1359 }
1360 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001361 }
1362
Michael Jurkad771c962011-08-09 15:00:48 -07001363 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001364
1365 @Override
1366 public void requestChildFocus(View child, View focused) {
1367 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001368 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001369 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001370 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 }
1372 }
1373
Winson Chunge3193b92010-09-10 11:44:42 -07001374 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1375 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001376 int left;
1377 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001378 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001379 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001380 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001381 if (left <= relativeOffset && relativeOffset <= right) {
1382 return i;
1383 }
Winson Chunge3193b92010-09-10 11:44:42 -07001384 }
1385 return -1;
1386 }
1387
Winson Chung1908d072011-02-24 18:09:44 -08001388 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001389 // This functions are called enough times that it actually makes a difference in the
1390 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001391 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001392 final int minWidth = mMinimumWidth;
1393 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001394 }
1395
Adam Cohend19d3ca2010-09-15 14:43:42 -07001396 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001397 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 int minDistanceFromScreenCenterIndex = -1;
1399 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1400 final int childCount = getChildCount();
1401 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001402 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001403 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 int halfChildWidth = (childWidth / 2);
1405 int childCenter = getChildOffset(i) + halfChildWidth;
1406 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1407 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1408 minDistanceFromScreenCenter = distanceFromScreenCenter;
1409 minDistanceFromScreenCenterIndex = i;
1410 }
1411 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001412 return minDistanceFromScreenCenterIndex;
1413 }
1414
1415 protected void snapToDestination() {
1416 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001417 }
1418
Adam Cohene0f66b52010-11-23 15:06:07 -08001419 private static class ScrollInterpolator implements Interpolator {
1420 public ScrollInterpolator() {
1421 }
1422
1423 public float getInterpolation(float t) {
1424 t -= 1.0f;
1425 return t*t*t*t*t + 1;
1426 }
1427 }
1428
1429 // We want the duration of the page snap animation to be influenced by the distance that
1430 // the screen has to travel, however, we don't want this duration to be effected in a
1431 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1432 // of travel has on the overall snap duration.
1433 float distanceInfluenceForSnapDuration(float f) {
1434 f -= 0.5f; // center the values about 0.
1435 f *= 0.3f * Math.PI / 2.0f;
1436 return (float) Math.sin(f);
1437 }
1438
Michael Jurka0142d492010-08-25 17:46:15 -07001439 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001440 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1441 int halfScreenSize = getMeasuredWidth() / 2;
1442
Winson Chung785d2eb2011-04-14 16:08:02 -07001443 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1444 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1445 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001446 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1447 int delta = newX - mUnboundedScrollX;
1448 int duration = 0;
1449
Adam Cohen265b9a62011-12-07 14:37:18 -08001450 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001451 // If the velocity is low enough, then treat this more as an automatic page advance
1452 // as opposed to an apparent physical response to flinging
1453 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1454 return;
1455 }
1456
1457 // Here we compute a "distance" that will be used in the computation of the overall
1458 // snap duration. This is a function of the actual distance that needs to be traveled;
1459 // we keep this value close to half screen size in order to reduce the variance in snap
1460 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001461 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001462 float distance = halfScreenSize + halfScreenSize *
1463 distanceInfluenceForSnapDuration(distanceRatio);
1464
1465 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001466 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001467
1468 // we want the page's snap velocity to approximately match the velocity at which the
1469 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001470 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1471 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001472
1473 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001474 }
1475
1476 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001477 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 }
1479
Michael Jurka0142d492010-08-25 17:46:15 -07001480 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001481 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001482
Winson Chung785d2eb2011-04-14 16:08:02 -07001483 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1484 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1485 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001486 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001487 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001488 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001489 snapToPage(whichPage, delta, duration);
1490 }
1491
1492 protected void snapToPage(int whichPage, int delta, int duration) {
1493 mNextPage = whichPage;
1494
1495 View focusedChild = getFocusedChild();
1496 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001497 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001498 focusedChild.clearFocus();
1499 }
1500
1501 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 awakenScrollBars(duration);
1503 if (duration == 0) {
1504 duration = Math.abs(delta);
1505 }
1506
1507 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001508 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001509
Winson Chungb44b5242011-06-13 11:32:14 -07001510 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1511 // loading associated pages until the scroll settles
1512 if (mDeferScrollUpdate) {
1513 loadAssociatedPages(mNextPage);
1514 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001515 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001516 }
Michael Jurka0142d492010-08-25 17:46:15 -07001517 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 invalidate();
1519 }
1520
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 public void scrollLeft() {
1522 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001523 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001525 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001526 }
1527 }
1528
1529 public void scrollRight() {
1530 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001531 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001532 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001533 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 }
1535 }
1536
Winson Chung86f77532010-08-24 11:08:22 -07001537 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001538 int result = -1;
1539 if (v != null) {
1540 ViewParent vp = v.getParent();
1541 int count = getChildCount();
1542 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001543 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 return i;
1545 }
1546 }
1547 }
1548 return result;
1549 }
1550
1551 /**
1552 * @return True is long presses are still allowed for the current touch
1553 */
1554 public boolean allowLongPress() {
1555 return mAllowLongPress;
1556 }
1557
Michael Jurka0142d492010-08-25 17:46:15 -07001558 /**
1559 * Set true to allow long-press events to be triggered, usually checked by
1560 * {@link Launcher} to accept or block dpad-initiated long-presses.
1561 */
1562 public void setAllowLongPress(boolean allowLongPress) {
1563 mAllowLongPress = allowLongPress;
1564 }
1565
Winson Chung321e9ee2010-08-09 13:37:56 -07001566 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001567 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001568
1569 SavedState(Parcelable superState) {
1570 super(superState);
1571 }
1572
1573 private SavedState(Parcel in) {
1574 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001575 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001576 }
1577
1578 @Override
1579 public void writeToParcel(Parcel out, int flags) {
1580 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001581 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001582 }
1583
1584 public static final Parcelable.Creator<SavedState> CREATOR =
1585 new Parcelable.Creator<SavedState>() {
1586 public SavedState createFromParcel(Parcel in) {
1587 return new SavedState(in);
1588 }
1589
1590 public SavedState[] newArray(int size) {
1591 return new SavedState[size];
1592 }
1593 };
1594 }
1595
Winson Chungf314b0e2011-08-16 11:54:27 -07001596 protected void loadAssociatedPages(int page) {
1597 loadAssociatedPages(page, false);
1598 }
1599 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001600 if (mContentIsRefreshable) {
1601 final int count = getChildCount();
1602 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001603 int lowerPageBound = getAssociatedLowerPageBound(page);
1604 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001605 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1606 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001607 // First, clear any pages that should no longer be loaded
1608 for (int i = 0; i < count; ++i) {
1609 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001610 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001611 if (layout.getPageChildCount() > 0) {
1612 layout.removeAllViewsOnPage();
1613 }
1614 mDirtyPageContent.set(i, true);
1615 }
1616 }
1617 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001618 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001619 if ((i != page) && immediateAndOnly) {
1620 continue;
1621 }
Michael Jurka0142d492010-08-25 17:46:15 -07001622 if (lowerPageBound <= i && i <= upperPageBound) {
1623 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001624 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001625 mDirtyPageContent.set(i, false);
1626 }
Winson Chung86f77532010-08-24 11:08:22 -07001627 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001628 }
1629 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001630 }
1631 }
1632
Winson Chunge3193b92010-09-10 11:44:42 -07001633 protected int getAssociatedLowerPageBound(int page) {
1634 return Math.max(0, page - 1);
1635 }
1636 protected int getAssociatedUpperPageBound(int page) {
1637 final int count = getChildCount();
1638 return Math.min(page + 1, count - 1);
1639 }
1640
Winson Chung86f77532010-08-24 11:08:22 -07001641 /**
1642 * This method is called ONLY to synchronize the number of pages that the paged view has.
1643 * To actually fill the pages with information, implement syncPageItems() below. It is
1644 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1645 * and therefore, individual page items do not need to be updated in this method.
1646 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001647 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001648
1649 /**
1650 * This method is called to synchronize the items that are on a particular page. If views on
1651 * the page can be reused, then they should be updated within this method.
1652 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001653 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001654
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001655 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001656 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001657 }
1658 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001659 invalidatePageData(currentPage, false);
1660 }
1661 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001662 if (!mIsDataReady) {
1663 return;
1664 }
1665
Michael Jurka0142d492010-08-25 17:46:15 -07001666 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001667 // Force all scrolling-related behavior to end
1668 mScroller.forceFinished(true);
1669 mNextPage = INVALID_PAGE;
1670
Michael Jurka0142d492010-08-25 17:46:15 -07001671 // Update all the pages
1672 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001673
Winson Chung5a808352011-06-27 19:08:49 -07001674 // We must force a measure after we've loaded the pages to update the content width and
1675 // to determine the full scroll width
1676 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1677 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1678
1679 // Set a new page as the current page if necessary
1680 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001681 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001682 }
1683
Michael Jurka0142d492010-08-25 17:46:15 -07001684 // Mark each of the pages as dirty
1685 final int count = getChildCount();
1686 mDirtyPageContent.clear();
1687 for (int i = 0; i < count; ++i) {
1688 mDirtyPageContent.add(true);
1689 }
1690
1691 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001692 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001693 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001694 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001695 }
Winson Chung007c6982011-06-14 13:27:53 -07001696
Michael Jurkaafaa0502011-12-13 18:22:50 -08001697 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001698 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1699 // found
1700 if (mHasScrollIndicator && mScrollIndicator == null) {
1701 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001702 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001703 mHasScrollIndicator = mScrollIndicator != null;
1704 if (mHasScrollIndicator) {
1705 mScrollIndicator.setVisibility(View.VISIBLE);
1706 }
1707 }
1708 return mScrollIndicator;
1709 }
1710
1711 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001712 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001713 }
1714
Winson Chung5a808352011-06-27 19:08:49 -07001715 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1716 @Override
1717 public void run() {
1718 hideScrollingIndicator(false);
1719 }
1720 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001721 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001722 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001723 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001724 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001725 }
1726
Michael Jurka430e8a52011-08-08 15:52:14 -07001727 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001728 mShouldShowScrollIndicator = true;
1729 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001730 if (getChildCount() <= 1) return;
1731 if (!isScrollingIndicatorEnabled()) return;
1732
Michael Jurkabed61d22012-02-14 22:51:29 -08001733 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001734 getScrollingIndicator();
1735 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001736 // Fade the indicator in
1737 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001738 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001739 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001740 if (immediately) {
1741 mScrollIndicator.setAlpha(1f);
1742 } else {
1743 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1744 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1745 mScrollIndicatorAnimator.start();
1746 }
Winson Chung007c6982011-06-14 13:27:53 -07001747 }
1748 }
1749
Adam Cohen21b41102011-11-01 17:29:52 -07001750 protected void cancelScrollingIndicatorAnimations() {
1751 if (mScrollIndicatorAnimator != null) {
1752 mScrollIndicatorAnimator.cancel();
1753 }
1754 }
1755
Winson Chung007c6982011-06-14 13:27:53 -07001756 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001757 if (getChildCount() <= 1) return;
1758 if (!isScrollingIndicatorEnabled()) return;
1759
1760 getScrollingIndicator();
1761 if (mScrollIndicator != null) {
1762 // Fade the indicator out
1763 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001764 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001765 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001766 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001767 mScrollIndicator.setAlpha(0f);
1768 } else {
1769 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1770 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1771 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1772 private boolean cancelled = false;
1773 @Override
1774 public void onAnimationCancel(android.animation.Animator animation) {
1775 cancelled = true;
1776 }
1777 @Override
1778 public void onAnimationEnd(Animator animation) {
1779 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001780 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001781 }
1782 }
1783 });
1784 mScrollIndicatorAnimator.start();
1785 }
Winson Chung007c6982011-06-14 13:27:53 -07001786 }
1787 }
1788
Winson Chung32174c82011-07-19 15:47:55 -07001789 /**
1790 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1791 * fill its space on the track or not.
1792 */
1793 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001794 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001795 }
1796
Winson Chung007c6982011-06-14 13:27:53 -07001797 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001798 if (getChildCount() <= 1) return;
1799 if (!isScrollingIndicatorEnabled()) return;
1800
1801 getScrollingIndicator();
1802 if (mScrollIndicator != null) {
1803 updateScrollingIndicatorPosition();
1804 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001805 if (mShouldShowScrollIndicator) {
1806 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1807 }
Winson Chung007c6982011-06-14 13:27:53 -07001808 }
1809
Winson Chung007c6982011-06-14 13:27:53 -07001810 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001811 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001812 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001813 int numPages = getChildCount();
1814 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001815 int lastChildIndex = Math.max(0, getChildCount() - 1);
1816 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001817 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001818 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1819 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001820
Winson Chungae890b82011-09-13 18:08:54 -07001821 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001822 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001823 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001824 if (hasElasticScrollIndicator()) {
1825 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1826 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1827 mScrollIndicator.requestLayout();
1828 }
1829 } else {
1830 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1831 indicatorPos += indicatorCenterOffset;
1832 }
Winson Chung007c6982011-06-14 13:27:53 -07001833 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001834 }
1835
Winson Chung3ac74c52011-06-30 17:39:37 -07001836 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001837 }
1838
1839 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001840 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001841
1842 /* Accessibility */
1843 @Override
1844 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1845 super.onInitializeAccessibilityNodeInfo(info);
1846 info.setScrollable(true);
1847 }
1848
1849 @Override
1850 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1851 super.onInitializeAccessibilityEvent(event);
1852 event.setScrollable(true);
1853 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1854 event.setFromIndex(mCurrentPage);
1855 event.setToIndex(mCurrentPage);
1856 event.setItemCount(getChildCount());
1857 }
1858 }
1859
Winson Chung6a0f57d2011-06-29 20:10:49 -07001860 protected String getCurrentPageDescription() {
1861 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1862 return String.format(mContext.getString(R.string.default_scroll_format),
1863 page + 1, getChildCount());
1864 }
Winson Chungd11265e2011-08-30 13:37:23 -07001865
1866 @Override
1867 public boolean onHoverEvent(android.view.MotionEvent event) {
1868 return true;
1869 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001870}