blob: 4bcb4c7d4539c39bfdeb7d220a29e3ca31da2fcc [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070022import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070024import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080031import android.view.InputDevice;
32import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070039import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070040import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import java.util.ArrayList;
48
Winson Chung321e9ee2010-08-09 13:37:56 -070049/**
50 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070051 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070052 */
Michael Jurka8b805b12012-04-18 14:23:14 -070053public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070054 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070055 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chung86f77532010-08-24 11:08:22 -070058 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070059 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Winson Chungf0c6ae02012-03-21 16:10:31 -070061 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
62 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohenb5ba0972011-09-07 18:02:31 -070065 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070066 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080067
Adam Cohenb64cb5a2011-02-15 13:53:42 -080068 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080069 // The page is moved more than halfway, automatically move to the next page on touch up.
70 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080071
Adam Cohen265b9a62011-12-07 14:37:18 -080072 // The following constants need to be scaled based on density. The scaled versions will be
73 // assigned to the corresponding member variables below.
74 private static final int FLING_THRESHOLD_VELOCITY = 500;
75 private static final int MIN_SNAP_VELOCITY = 1500;
76 private static final int MIN_FLING_VELOCITY = 250;
77
78 protected int mFlingThresholdVelocity;
79 protected int mMinFlingVelocity;
80 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070081
Adam Cohenb5ba0972011-09-07 18:02:31 -070082 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070083 protected float mSmoothingTime;
84 protected float mTouchX;
85
86 protected boolean mFirstLayout = true;
87
88 protected int mCurrentPage;
89 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080090 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070092 private VelocityTracker mVelocityTracker;
93
94 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080095 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080096 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080098 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070099 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700100 private int[] mChildOffsets;
101 private int[] mChildRelativeOffsets;
102 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected final static int TOUCH_STATE_REST = 0;
105 protected final static int TOUCH_STATE_SCROLLING = 1;
106 protected final static int TOUCH_STATE_PREV_PAGE = 2;
107 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700108 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700111 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka7426c422010-11-11 15:23:47 -0800115 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 private int mPagingTouchSlop;
119 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800120 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700121 protected int mPageSpacing;
122 protected int mPageLayoutPaddingTop;
123 protected int mPageLayoutPaddingBottom;
124 protected int mPageLayoutPaddingLeft;
125 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700126 protected int mPageLayoutWidthGap;
127 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700128 protected int mCellCountX = 0;
129 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700130 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800131 protected boolean mAllowOverScroll = true;
132 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800133 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Michael Jurka8b805b12012-04-18 14:23:14 -0700135 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800136 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
137 // the screens from continuing to translate beyond the normal bounds.
138 protected int mOverScrollX;
139
Michael Jurka8c920dd2011-01-20 14:16:56 -0800140 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800141 protected float mLayoutScale = 1.0f;
142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka5f1c5092010-09-03 14:15:02 -0700145 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Winson Chung86f77532010-08-24 11:08:22 -0700147 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurkae326f182011-11-21 14:05:46 -0800149 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Michael Jurka0142d492010-08-25 17:46:15 -0700151 // If true, syncPages and syncPageItems will be called to refresh pages
152 protected boolean mContentIsRefreshable = true;
153
154 // If true, modify alpha of neighboring pages as user scrolls left/right
155 protected boolean mFadeInAdjacentScreens = true;
156
157 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
158 // to switch to a new page
159 protected boolean mUsePagingTouchSlop = true;
160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700162 // (SmoothPagedView does this)
163 protected boolean mDeferScrollUpdate = false;
164
Patrick Dubroy1262e362010-10-06 15:49:50 -0700165 protected boolean mIsPageMoving = false;
166
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 // All syncs and layout passes are deferred until data is ready.
168 protected boolean mIsDataReady = false;
169
Winson Chung007c6982011-06-14 13:27:53 -0700170 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700171 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800172 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700173 private int mScrollIndicatorPaddingLeft;
174 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700175 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800176 private boolean mShouldShowScrollIndicator = false;
177 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700178 protected static final int sScrollIndicatorFadeInDuration = 150;
179 protected static final int sScrollIndicatorFadeOutDuration = 650;
180 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700181
Winson Chungb44b5242011-06-13 11:32:14 -0700182 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700183 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700184
Winson Chung86f77532010-08-24 11:08:22 -0700185 public interface PageSwitchListener {
186 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 }
188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 public PagedView(Context context) {
190 this(context, null);
191 }
192
193 public PagedView(Context context, AttributeSet attrs) {
194 this(context, attrs, 0);
195 }
196
197 public PagedView(Context context, AttributeSet attrs, int defStyle) {
198 super(context, attrs, defStyle);
199
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800202 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800204 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800206 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700211 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700213 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700214 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700215 mScrollIndicatorPaddingLeft =
216 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
217 mScrollIndicatorPaddingRight =
218 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700219 a.recycle();
220
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700222 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224
225 /**
226 * Initializes various states for this workspace.
227 */
Michael Jurka0142d492010-08-25 17:46:15 -0700228 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700229 mDirtyPageContent = new ArrayList<Boolean>();
230 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800231 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700232 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700233 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700234
235 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
236 mTouchSlop = configuration.getScaledTouchSlop();
237 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
238 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700239 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800240
241 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
242 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
243 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700244 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246
Winson Chung86f77532010-08-24 11:08:22 -0700247 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
248 mPageSwitchListener = pageSwitchListener;
249 if (mPageSwitchListener != null) {
250 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 }
252 }
253
254 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700255 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
256 * out pages.
257 */
258 protected void setDataIsReady() {
259 mIsDataReady = true;
260 }
261 protected boolean isDataReady() {
262 return mIsDataReady;
263 }
264
265 /**
Winson Chung86f77532010-08-24 11:08:22 -0700266 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 *
Winson Chung86f77532010-08-24 11:08:22 -0700268 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 */
Winson Chung86f77532010-08-24 11:08:22 -0700270 int getCurrentPage() {
271 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 }
Winson Chung360e63f2012-04-27 13:48:05 -0700273 int getNextPage() {
274 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
275 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700276
Winson Chung86f77532010-08-24 11:08:22 -0700277 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 return getChildCount();
279 }
280
Winson Chung86f77532010-08-24 11:08:22 -0700281 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 return getChildAt(index);
283 }
284
Adam Cohenae4f1552011-10-20 00:15:42 -0700285 protected int indexToPage(int index) {
286 return index;
287 }
288
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800290 * Updates the scroll of the current page immediately to its final scroll position. We use this
291 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
292 * the previous tab page.
293 */
294 protected void updateCurrentPageScroll() {
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) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700369 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800370 }
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 Jurka8b805b12012-04-18 14:23:14 -0700399 if (getScrollX() != mScroller.getCurrX()
400 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700401 || 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
Michael Jurka8b805b12012-04-18 14:23:14 -0700424 AccessibilityManager accessibilityManager = (AccessibilityManager)
425 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
426 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700427 AccessibilityEvent ev =
428 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
429 ev.getText().add(getCurrentPageDescription());
430 sendAccessibilityEventUnchecked(ev);
431 }
Michael Jurka0142d492010-08-25 17:46:15 -0700432 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 }
Michael Jurka0142d492010-08-25 17:46:15 -0700434 return false;
435 }
436
437 @Override
438 public void computeScroll() {
439 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700440 }
441
442 @Override
443 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700444 if (!mIsDataReady) {
445 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
446 return;
447 }
448
Winson Chung321e9ee2010-08-09 13:37:56 -0700449 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
450 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
451 if (widthMode != MeasureSpec.EXACTLY) {
452 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
453 }
454
Adam Lesinski6b879f02010-11-04 16:15:23 -0700455 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
456 * of the All apps view on XLarge displays to not take up more space then it needs. Width
457 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
458 * each page to have the same width.
459 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700461 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
462 int maxChildHeight = 0;
463
Michael Jurka8b805b12012-04-18 14:23:14 -0700464 final int verticalPadding = getPaddingTop() + getPaddingBottom();
465 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700466
Michael Jurka36fcb742011-04-06 17:08:58 -0700467
Winson Chung321e9ee2010-08-09 13:37:56 -0700468 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700469 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700470 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700471 final int childCount = getChildCount();
472 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700473 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700474 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700475 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
476
477 int childWidthMode;
478 if (lp.width == LayoutParams.WRAP_CONTENT) {
479 childWidthMode = MeasureSpec.AT_MOST;
480 } else {
481 childWidthMode = MeasureSpec.EXACTLY;
482 }
483
484 int childHeightMode;
485 if (lp.height == LayoutParams.WRAP_CONTENT) {
486 childHeightMode = MeasureSpec.AT_MOST;
487 } else {
488 childHeightMode = MeasureSpec.EXACTLY;
489 }
490
491 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700492 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700493 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700494 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700495
496 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700497 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700498 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
499 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700500 }
501
502 if (heightMode == MeasureSpec.AT_MOST) {
503 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700504 }
Winson Chungae890b82011-09-13 18:08:54 -0700505
Winson Chungae890b82011-09-13 18:08:54 -0700506 setMeasuredDimension(widthSize, heightSize);
507
Adam Cohen25b29952011-11-02 14:49:06 -0700508 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
509 // We also wait until we set the measured dimensions before flushing the cache as well, to
510 // ensure that the cache is filled with good values.
511 invalidateCachedOffsets();
512 updateScrollingIndicatorPosition();
513
Adam Cohenfaa28302010-11-19 12:02:18 -0800514 if (childCount > 0) {
515 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
516 } else {
517 mMaxScrollX = 0;
518 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
520
Michael Jurka8c920dd2011-01-20 14:16:56 -0800521 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800522 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700523 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800524
Michael Jurka8c920dd2011-01-20 14:16:56 -0800525 final int pageCount = getChildCount();
526 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700527 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800528 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800529 }
530 setCurrentPage(newCurrentPage);
531 }
532
Michael Jurka8c920dd2011-01-20 14:16:56 -0800533 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
534 // 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 -0800535 // tightens the layout accordingly
536 public void setLayoutScale(float childrenScale) {
537 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700538 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539
540 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
541 int childCount = getChildCount();
542 float childrenX[] = new float[childCount];
543 float childrenY[] = new float[childCount];
544 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700545 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800546 childrenX[i] = child.getX();
547 childrenY[i] = child.getY();
548 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700549 // Trigger a full re-layout (never just call onLayout directly!)
550 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
551 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
552 requestLayout();
553 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700554 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700556 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800557 child.setX(childrenX[i]);
558 child.setY(childrenY[i]);
559 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700560
Michael Jurkad3ef3062010-11-23 16:23:58 -0800561 // Also, the page offset has changed (since the pages are now smaller);
562 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800563 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800564 }
565
Adam Cohen60b07122011-11-14 17:26:06 -0800566 public void setPageSpacing(int pageSpacing) {
567 mPageSpacing = pageSpacing;
568 invalidateCachedOffsets();
569 }
570
Winson Chung321e9ee2010-08-09 13:37:56 -0700571 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700572 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700573 if (!mIsDataReady) {
574 return;
575 }
576
Winson Chung785d2eb2011-04-14 16:08:02 -0700577 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700578 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 final int childCount = getChildCount();
580 int childLeft = 0;
581 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700582 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
583 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700584 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700585
586 // Calculate the variable page spacing if necessary
587 if (mPageSpacing < 0) {
Winson Chungeecf02d2012-03-02 17:14:58 -0800588 // The gap between pages in the PagedView should be equal to the gap from the page
589 // to the edge of the screen (so it is not visible in the current screen). To
590 // account for unequal padding on each side of the paged view, we take the maximum
591 // of the left/right gap and use that as the gap between each page.
592 int offset = getRelativeChildOffset(0);
593 int spacing = Math.max(offset, (right - left) - offset -
594 getChildAt(0).getMeasuredWidth());
595 setPageSpacing(spacing);
Winson Chungae890b82011-09-13 18:08:54 -0700596 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
598
599 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700600 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700601 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800602 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700603 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700604 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700605 if (mCenterPagesVertically) {
606 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
607 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800608
Winson Chung785d2eb2011-04-14 16:08:02 -0700609 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700610 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800611 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700612 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 }
614 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700615
616 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
617 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800618 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700619 setHorizontalScrollBarEnabled(true);
620 mFirstLayout = false;
621 }
622
Michael Jurkad3ef3062010-11-23 16:23:58 -0800623 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
624 mFirstLayout = false;
625 }
Winson Chunge3193b92010-09-10 11:44:42 -0700626 }
627
Adam Cohenf34bab52010-09-30 14:11:56 -0700628 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700629 if (isScrollingIndicatorEnabled()) {
630 updateScrollingIndicator();
631 }
632 if (mFadeInAdjacentScreens) {
633 for (int i = 0; i < getChildCount(); i++) {
634 View child = getChildAt(i);
635 if (child != null) {
636 float scrollProgress = getScrollProgress(screenCenter, child, i);
637 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800638 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700639 }
640 }
641 invalidate();
642 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700643 }
644
Winson Chunge3193b92010-09-10 11:44:42 -0700645 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700647 // 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
Michael Jurka8b805b12012-04-18 14:23:14 -0700654 @Override
655 public void onChildViewRemoved(View parent, View child) {
656 }
657
Adam Cohen73894962011-10-31 13:17:17 -0700658 protected void invalidateCachedOffsets() {
659 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700660 if (count == 0) {
661 mChildOffsets = null;
662 mChildRelativeOffsets = null;
663 mChildOffsetsWithLayoutScale = null;
664 return;
665 }
Adam Cohen73894962011-10-31 13:17:17 -0700666
667 mChildOffsets = new int[count];
668 mChildRelativeOffsets = new int[count];
669 mChildOffsetsWithLayoutScale = new int[count];
670 for (int i = 0; i < count; i++) {
671 mChildOffsets[i] = -1;
672 mChildRelativeOffsets[i] = -1;
673 mChildOffsetsWithLayoutScale[i] = -1;
674 }
675 }
676
677 protected int getChildOffset(int index) {
678 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
679 mChildOffsets : mChildOffsetsWithLayoutScale;
680
681 if (childOffsets != null && childOffsets[index] != -1) {
682 return childOffsets[index];
683 } else {
684 if (getChildCount() == 0)
685 return 0;
686
687 int offset = getRelativeChildOffset(0);
688 for (int i = 0; i < index; ++i) {
689 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
690 }
691 if (childOffsets != null) {
692 childOffsets[index] = offset;
693 }
694 return offset;
695 }
696 }
697
698 protected int getRelativeChildOffset(int index) {
699 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
700 return mChildRelativeOffsets[index];
701 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 final int padding = getPaddingLeft() + getPaddingRight();
703 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700704 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
705 if (mChildRelativeOffsets != null) {
706 mChildRelativeOffsets[index] = offset;
707 }
708 return offset;
709 }
710 }
711
712 protected int getScaledRelativeChildOffset(int index) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 final int padding = getPaddingLeft() + getPaddingRight();
714 final int offset = getPaddingLeft() + (getMeasuredWidth() - padding -
Adam Cohen73894962011-10-31 13:17:17 -0700715 getScaledMeasuredWidth(getPageAt(index))) / 2;
716 return offset;
717 }
718
719 protected int getScaledMeasuredWidth(View child) {
720 // This functions are called enough times that it actually makes a difference in the
721 // profiler -- so just inline the max() here
722 final int measuredWidth = child.getMeasuredWidth();
723 final int minWidth = mMinimumWidth;
724 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
725 return (int) (maxWidth * mLayoutScale + 0.5f);
726 }
727
Michael Jurkadde558b2011-11-09 22:09:06 -0800728 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700729 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700730
Michael Jurkac4fb9172010-09-02 17:19:20 -0700731 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700732 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700733 int leftScreen = 0;
734 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800735 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800736 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 currPage.getX() + currPage.getWidth() -
738 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700739 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800740 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700741 }
742 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800743 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800744 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700746 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800747 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700748 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800749 range[0] = leftScreen;
750 range[1] = rightScreen;
751 } else {
752 range[0] = -1;
753 range[1] = -1;
754 }
755 }
756
757 @Override
758 protected void dispatchDraw(Canvas canvas) {
759 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
761 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800762 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800763
764 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700765 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
766 // set it for the next frame
767 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800768 screenScrolled(screenCenter);
769 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800770 }
771
772 // Find out which screens are visible; as an optimization we only call draw on them
773 final int pageCount = getChildCount();
774 if (pageCount > 0) {
775 getVisiblePages(mTempVisiblePagesRange);
776 final int leftScreen = mTempVisiblePagesRange[0];
777 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800778 if (leftScreen != -1 && rightScreen != -1) {
779 final long drawingTime = getDrawingTime();
780 // Clip to the bounds
781 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
783 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700784
Michael Jurka80c69852011-12-16 14:16:32 -0800785 // On certain graphics drivers, if you draw to a off-screen buffer that's not
786 // used, it can lead to poor performance. We were running into this when
787 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
788 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
789 // As a fix, below we set pages that aren't going to be rendered are to be
790 // View.INVISIBLE, preventing re-drawing of their hardware layer
791 for (int i = getChildCount() - 1; i >= 0; i--) {
792 final View v = getPageAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 if (leftScreen <= i && i <= rightScreen && v.getAlpha() > 0) {
Michael Jurka80c69852011-12-16 14:16:32 -0800794 v.setVisibility(VISIBLE);
795 drawChild(canvas, v, drawingTime);
796 } else {
797 v.setVisibility(INVISIBLE);
798 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800799 }
800 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700801 }
Michael Jurka0142d492010-08-25 17:46:15 -0700802 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 }
804
805 @Override
806 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700807 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700808 if (page != mCurrentPage || !mScroller.isFinished()) {
809 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 return true;
811 }
812 return false;
813 }
814
815 @Override
816 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700817 int focusablePage;
818 if (mNextPage != INVALID_PAGE) {
819 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700820 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700821 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 }
Winson Chung86f77532010-08-24 11:08:22 -0700823 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700825 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 }
827 return false;
828 }
829
830 @Override
831 public boolean dispatchUnhandledMove(View focused, int direction) {
832 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700833 if (getCurrentPage() > 0) {
834 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 return true;
836 }
837 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700838 if (getCurrentPage() < getPageCount() - 1) {
839 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 return true;
841 }
842 }
843 return super.dispatchUnhandledMove(focused, direction);
844 }
845
846 @Override
847 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700848 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
849 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 }
851 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700852 if (mCurrentPage > 0) {
853 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 }
855 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700856 if (mCurrentPage < getPageCount() - 1) {
857 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 }
859 }
860 }
861
862 /**
863 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700864 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 *
Winson Chung86f77532010-08-24 11:08:22 -0700866 * This happens when live folders requery, and if they're off page, they
867 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 */
869 @Override
870 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700871 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 View v = focused;
873 while (true) {
874 if (v == current) {
875 super.focusableViewAvailable(focused);
876 return;
877 }
878 if (v == this) {
879 return;
880 }
881 ViewParent parent = v.getParent();
882 if (parent instanceof View) {
883 v = (View)v.getParent();
884 } else {
885 return;
886 }
887 }
888 }
889
890 /**
891 * {@inheritDoc}
892 */
893 @Override
894 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
895 if (disallowIntercept) {
896 // We need to make sure to cancel our long press if
897 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700898 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700899 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 }
901 super.requestDisallowInterceptTouchEvent(disallowIntercept);
902 }
903
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800904 /**
905 * Return true if a tap at (x, y) should trigger a flip to the previous page.
906 */
907 protected boolean hitsPreviousPage(float x, float y) {
908 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
909 }
910
911 /**
912 * Return true if a tap at (x, y) should trigger a flip to the next page.
913 */
914 protected boolean hitsNextPage(float x, float y) {
915 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
916 }
917
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 @Override
919 public boolean onInterceptTouchEvent(MotionEvent ev) {
920 /*
921 * This method JUST determines whether we want to intercept the motion.
922 * If we return true, onTouchEvent will be called and we do the actual
923 * scrolling there.
924 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800925 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700926
Winson Chung45e1d6e2010-11-09 17:19:49 -0800927 // Skip touch handling if there are no pages to swipe
928 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
929
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 /*
931 * Shortcut the most recurring case: the user is in the dragging
932 * state and he is moving his finger. We want to intercept this
933 * motion.
934 */
935 final int action = ev.getAction();
936 if ((action == MotionEvent.ACTION_MOVE) &&
937 (mTouchState == TOUCH_STATE_SCROLLING)) {
938 return true;
939 }
940
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 switch (action & MotionEvent.ACTION_MASK) {
942 case MotionEvent.ACTION_MOVE: {
943 /*
944 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
945 * whether the user has moved far enough from his original down touch.
946 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700947 if (mActivePointerId != INVALID_POINTER) {
948 determineScrollingStart(ev);
949 break;
950 }
951 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
952 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
953 // i.e. fall through to the next case (don't break)
954 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
955 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 }
957
958 case MotionEvent.ACTION_DOWN: {
959 final float x = ev.getX();
960 final float y = ev.getY();
961 // Remember location of down touch
962 mDownMotionX = x;
963 mLastMotionX = x;
964 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800965 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800966 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 mActivePointerId = ev.getPointerId(0);
968 mAllowLongPress = true;
969
970 /*
971 * If being flinged and user touches the screen, initiate drag;
972 * otherwise don't. mScroller.isFinished should be false when
973 * being flinged.
974 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700975 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700976 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
977 if (finishedScrolling) {
978 mTouchState = TOUCH_STATE_REST;
979 mScroller.abortAnimation();
980 } else {
981 mTouchState = TOUCH_STATE_SCROLLING;
982 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700983
Winson Chung86f77532010-08-24 11:08:22 -0700984 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800986 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800988 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800990 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 mTouchState = TOUCH_STATE_NEXT_PAGE;
992 }
993 }
994 }
995 break;
996 }
997
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800999 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 mTouchState = TOUCH_STATE_REST;
1001 mAllowLongPress = false;
1002 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001003 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 break;
1005
1006 case MotionEvent.ACTION_POINTER_UP:
1007 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001008 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 break;
1010 }
1011
1012 /*
1013 * The only time we want to intercept motion events is if we are in the
1014 * drag mode.
1015 */
1016 return mTouchState != TOUCH_STATE_REST;
1017 }
1018
Adam Cohenf8d28232011-02-01 21:47:00 -08001019 protected void determineScrollingStart(MotionEvent ev) {
1020 determineScrollingStart(ev, 1.0f);
1021 }
1022
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 /*
1024 * Determines if we should change the touch state to start scrolling after the
1025 * user moves their touch point too far.
1026 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001027 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001028 /*
1029 * Locally do absolute value. mLastMotionX is set to the y value
1030 * of the down event.
1031 */
1032 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001033 if (pointerIndex == -1) {
1034 return;
1035 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 final float x = ev.getX(pointerIndex);
1037 final float y = ev.getY(pointerIndex);
1038 final int xDiff = (int) Math.abs(x - mLastMotionX);
1039 final int yDiff = (int) Math.abs(y - mLastMotionY);
1040
Adam Cohenf8d28232011-02-01 21:47:00 -08001041 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 boolean xPaged = xDiff > mPagingTouchSlop;
1043 boolean xMoved = xDiff > touchSlop;
1044 boolean yMoved = yDiff > touchSlop;
1045
Adam Cohenf8d28232011-02-01 21:47:00 -08001046 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001047 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 // Scroll if the user moved far enough along the X axis
1049 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001050 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001052 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001053 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001054 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1055 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 }
1057 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001058 cancelCurrentPageLongPress();
1059 }
1060 }
1061
1062 protected void cancelCurrentPageLongPress() {
1063 if (mAllowLongPress) {
1064 mAllowLongPress = false;
1065 // Try canceling the long press. It could also have been scheduled
1066 // by a distant descendant, so use the mAllowLongPress flag to block
1067 // everything
1068 final View currentPage = getPageAt(mCurrentPage);
1069 if (currentPage != null) {
1070 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 }
1072 }
1073 }
1074
Adam Cohenb5ba0972011-09-07 18:02:31 -07001075 protected float getScrollProgress(int screenCenter, View v, int page) {
1076 final int halfScreenSize = getMeasuredWidth() / 2;
1077
1078 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1079 int delta = screenCenter - (getChildOffset(page) -
1080 getRelativeChildOffset(page) + halfScreenSize);
1081
1082 float scrollProgress = delta / (totalDistance * 1.0f);
1083 scrollProgress = Math.min(scrollProgress, 1.0f);
1084 scrollProgress = Math.max(scrollProgress, -1.0f);
1085 return scrollProgress;
1086 }
1087
Adam Cohene0f66b52010-11-23 15:06:07 -08001088 // This curve determines how the effect of scrolling over the limits of the page dimishes
1089 // as the user pulls further and further from the bounds
1090 private float overScrollInfluenceCurve(float f) {
1091 f -= 1.0f;
1092 return f * f * f + 1.0f;
1093 }
1094
Adam Cohenb5ba0972011-09-07 18:02:31 -07001095 protected void acceleratedOverScroll(float amount) {
1096 int screenSize = getMeasuredWidth();
1097
1098 // We want to reach the max over scroll effect when the user has
1099 // over scrolled half the size of the screen
1100 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1101
1102 if (f == 0) return;
1103
1104 // Clamp this factor, f, to -1 < f < 1
1105 if (Math.abs(f) >= 1) {
1106 f /= Math.abs(f);
1107 }
1108
1109 int overScrollAmount = (int) Math.round(f * screenSize);
1110 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001111 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001112 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001113 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001114 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001115 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001116 }
1117 invalidate();
1118 }
1119
1120 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001121 int screenSize = getMeasuredWidth();
1122
1123 float f = (amount / screenSize);
1124
1125 if (f == 0) return;
1126 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1127
Adam Cohen7bfc9792011-01-28 13:52:37 -08001128 // Clamp this factor, f, to -1 < f < 1
1129 if (Math.abs(f) >= 1) {
1130 f /= Math.abs(f);
1131 }
1132
Adam Cohene0f66b52010-11-23 15:06:07 -08001133 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001134 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001135 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001136 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001137 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001138 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001139 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001140 }
1141 invalidate();
1142 }
1143
Adam Cohenb5ba0972011-09-07 18:02:31 -07001144 protected void overScroll(float amount) {
1145 dampedOverScroll(amount);
1146 }
1147
Michael Jurkac5b262c2011-01-12 20:24:50 -08001148 protected float maxOverScroll() {
1149 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001150 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001151 float f = 1.0f;
1152 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1153 return OVERSCROLL_DAMP_FACTOR * f;
1154 }
1155
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 @Override
1157 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001158 // Skip touch handling if there are no pages to swipe
1159 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1160
Michael Jurkab8f06722010-10-10 15:58:46 -07001161 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001162
1163 final int action = ev.getAction();
1164
1165 switch (action & MotionEvent.ACTION_MASK) {
1166 case MotionEvent.ACTION_DOWN:
1167 /*
1168 * If being flinged and user touches, stop the fling. isFinished
1169 * will be false if being flinged.
1170 */
1171 if (!mScroller.isFinished()) {
1172 mScroller.abortAnimation();
1173 }
1174
1175 // Remember where the motion event started
1176 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001177 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001178 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001180 if (mTouchState == TOUCH_STATE_SCROLLING) {
1181 pageBeginMoving();
1182 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 break;
1184
1185 case MotionEvent.ACTION_MOVE:
1186 if (mTouchState == TOUCH_STATE_SCROLLING) {
1187 // Scroll to follow the motion event
1188 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1189 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001190 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001191
Adam Cohenaefd4e12011-02-14 16:39:38 -08001192 mTotalMotionX += Math.abs(deltaX);
1193
Winson Chungc0844aa2011-02-02 15:25:58 -08001194 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1195 // keep the remainder because we are actually testing if we've moved from the last
1196 // scrolled position (which is discrete).
1197 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001198 mTouchX += deltaX;
1199 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1200 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001201 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001202 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001203 } else {
1204 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001206 mLastMotionX = x;
1207 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 } else {
1209 awakenScrollBars();
1210 }
Adam Cohen564976a2010-10-13 18:52:07 -07001211 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 determineScrollingStart(ev);
1213 }
1214 break;
1215
1216 case MotionEvent.ACTION_UP:
1217 if (mTouchState == TOUCH_STATE_SCROLLING) {
1218 final int activePointerId = mActivePointerId;
1219 final int pointerIndex = ev.findPointerIndex(activePointerId);
1220 final float x = ev.getX(pointerIndex);
1221 final VelocityTracker velocityTracker = mVelocityTracker;
1222 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1223 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001224 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001225 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1226 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1227 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001228
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001229 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1230
Adam Cohen00481b32011-11-18 12:03:48 -08001231 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001232 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001233
Adam Cohenaefd4e12011-02-14 16:39:38 -08001234 // In the case that the page is moved far to one direction and then is flung
1235 // in the opposite direction, we use a threshold to determine whether we should
1236 // just return to the starting page, or if we should skip one further.
1237 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001238 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001239 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001240 returnToOriginalPage = true;
1241 }
1242
Adam Cohenaefd4e12011-02-14 16:39:38 -08001243 int finalPage;
1244 // We give flings precedence over large moves, which is why we short-circuit our
1245 // test for a large move if a fling has been registered. That is, a large
1246 // move to the left and fling to the right will register as a fling to the right.
1247 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1248 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1249 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1250 snapToPageWithVelocity(finalPage, velocityX);
1251 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1252 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001253 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001254 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1255 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 } else {
1257 snapToDestination();
1258 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001259 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 // at this point we have not moved beyond the touch slop
1261 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1262 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001263 int nextPage = Math.max(0, mCurrentPage - 1);
1264 if (nextPage != mCurrentPage) {
1265 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 } else {
1267 snapToDestination();
1268 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001269 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 // at this point we have not moved beyond the touch slop
1271 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1272 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001273 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1274 if (nextPage != mCurrentPage) {
1275 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 } else {
1277 snapToDestination();
1278 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001279 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001280 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 }
1282 mTouchState = TOUCH_STATE_REST;
1283 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001284 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 break;
1286
1287 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001288 if (mTouchState == TOUCH_STATE_SCROLLING) {
1289 snapToDestination();
1290 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 mTouchState = TOUCH_STATE_REST;
1292 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001293 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 break;
1295
1296 case MotionEvent.ACTION_POINTER_UP:
1297 onSecondaryPointerUp(ev);
1298 break;
1299 }
1300
1301 return true;
1302 }
1303
Winson Chung185d7162011-02-28 13:47:29 -08001304 @Override
1305 public boolean onGenericMotionEvent(MotionEvent event) {
1306 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1307 switch (event.getAction()) {
1308 case MotionEvent.ACTION_SCROLL: {
1309 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1310 final float vscroll;
1311 final float hscroll;
1312 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1313 vscroll = 0;
1314 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1315 } else {
1316 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1317 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1318 }
1319 if (hscroll != 0 || vscroll != 0) {
1320 if (hscroll > 0 || vscroll > 0) {
1321 scrollRight();
1322 } else {
1323 scrollLeft();
1324 }
1325 return true;
1326 }
1327 }
1328 }
1329 }
1330 return super.onGenericMotionEvent(event);
1331 }
1332
Michael Jurkab8f06722010-10-10 15:58:46 -07001333 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1334 if (mVelocityTracker == null) {
1335 mVelocityTracker = VelocityTracker.obtain();
1336 }
1337 mVelocityTracker.addMovement(ev);
1338 }
1339
1340 private void releaseVelocityTracker() {
1341 if (mVelocityTracker != null) {
1342 mVelocityTracker.recycle();
1343 mVelocityTracker = null;
1344 }
1345 }
1346
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 private void onSecondaryPointerUp(MotionEvent ev) {
1348 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1349 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1350 final int pointerId = ev.getPointerId(pointerIndex);
1351 if (pointerId == mActivePointerId) {
1352 // This was our active pointer going up. Choose a new
1353 // active pointer and adjust accordingly.
1354 // TODO: Make this decision more intelligent.
1355 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1356 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1357 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001358 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 mActivePointerId = ev.getPointerId(newPointerIndex);
1360 if (mVelocityTracker != null) {
1361 mVelocityTracker.clear();
1362 }
1363 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001364 }
1365
Michael Jurkad771c962011-08-09 15:00:48 -07001366 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001367
1368 @Override
1369 public void requestChildFocus(View child, View focused) {
1370 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001371 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001372 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001373 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001374 }
1375 }
1376
Winson Chunge3193b92010-09-10 11:44:42 -07001377 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1378 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001379 int left;
1380 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001381 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001382 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001383 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001384 if (left <= relativeOffset && relativeOffset <= right) {
1385 return i;
1386 }
Winson Chunge3193b92010-09-10 11:44:42 -07001387 }
1388 return -1;
1389 }
1390
Winson Chung1908d072011-02-24 18:09:44 -08001391 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001392 // This functions are called enough times that it actually makes a difference in the
1393 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001394 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001395 final int minWidth = mMinimumWidth;
1396 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001397 }
1398
Adam Cohend19d3ca2010-09-15 14:43:42 -07001399 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001400 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001402 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 final int childCount = getChildCount();
1404 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001405 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001406 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 int halfChildWidth = (childWidth / 2);
1408 int childCenter = getChildOffset(i) + halfChildWidth;
1409 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1410 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1411 minDistanceFromScreenCenter = distanceFromScreenCenter;
1412 minDistanceFromScreenCenterIndex = i;
1413 }
1414 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001415 return minDistanceFromScreenCenterIndex;
1416 }
1417
1418 protected void snapToDestination() {
1419 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 }
1421
Adam Cohene0f66b52010-11-23 15:06:07 -08001422 private static class ScrollInterpolator implements Interpolator {
1423 public ScrollInterpolator() {
1424 }
1425
1426 public float getInterpolation(float t) {
1427 t -= 1.0f;
1428 return t*t*t*t*t + 1;
1429 }
1430 }
1431
1432 // We want the duration of the page snap animation to be influenced by the distance that
1433 // the screen has to travel, however, we don't want this duration to be effected in a
1434 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1435 // of travel has on the overall snap duration.
1436 float distanceInfluenceForSnapDuration(float f) {
1437 f -= 0.5f; // center the values about 0.
1438 f *= 0.3f * Math.PI / 2.0f;
1439 return (float) Math.sin(f);
1440 }
1441
Michael Jurka0142d492010-08-25 17:46:15 -07001442 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001443 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1444 int halfScreenSize = getMeasuredWidth() / 2;
1445
Winson Chung785d2eb2011-04-14 16:08:02 -07001446 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1447 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1448 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001449 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1450 int delta = newX - mUnboundedScrollX;
1451 int duration = 0;
1452
Adam Cohen265b9a62011-12-07 14:37:18 -08001453 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001454 // If the velocity is low enough, then treat this more as an automatic page advance
1455 // as opposed to an apparent physical response to flinging
1456 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1457 return;
1458 }
1459
1460 // Here we compute a "distance" that will be used in the computation of the overall
1461 // snap duration. This is a function of the actual distance that needs to be traveled;
1462 // we keep this value close to half screen size in order to reduce the variance in snap
1463 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001464 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001465 float distance = halfScreenSize + halfScreenSize *
1466 distanceInfluenceForSnapDuration(distanceRatio);
1467
1468 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001469 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001470
1471 // we want the page's snap velocity to approximately match the velocity at which the
1472 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001473 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1474 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001475
1476 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001477 }
1478
1479 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001480 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 }
1482
Michael Jurka0142d492010-08-25 17:46:15 -07001483 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001484 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001485
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1487 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1488 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001489 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001490 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001492 snapToPage(whichPage, delta, duration);
1493 }
1494
1495 protected void snapToPage(int whichPage, int delta, int duration) {
1496 mNextPage = whichPage;
1497
1498 View focusedChild = getFocusedChild();
1499 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001500 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001501 focusedChild.clearFocus();
1502 }
1503
1504 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 awakenScrollBars(duration);
1506 if (duration == 0) {
1507 duration = Math.abs(delta);
1508 }
1509
1510 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001511 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001512
Winson Chungb44b5242011-06-13 11:32:14 -07001513 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1514 // loading associated pages until the scroll settles
1515 if (mDeferScrollUpdate) {
1516 loadAssociatedPages(mNextPage);
1517 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001518 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001519 }
Michael Jurka0142d492010-08-25 17:46:15 -07001520 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 invalidate();
1522 }
1523
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 public void scrollLeft() {
1525 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001526 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001527 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001528 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001529 }
1530 }
1531
1532 public void scrollRight() {
1533 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001534 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001535 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001536 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001537 }
1538 }
1539
Winson Chung86f77532010-08-24 11:08:22 -07001540 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001541 int result = -1;
1542 if (v != null) {
1543 ViewParent vp = v.getParent();
1544 int count = getChildCount();
1545 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001546 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001547 return i;
1548 }
1549 }
1550 }
1551 return result;
1552 }
1553
1554 /**
1555 * @return True is long presses are still allowed for the current touch
1556 */
1557 public boolean allowLongPress() {
1558 return mAllowLongPress;
1559 }
1560
Michael Jurka0142d492010-08-25 17:46:15 -07001561 /**
1562 * Set true to allow long-press events to be triggered, usually checked by
1563 * {@link Launcher} to accept or block dpad-initiated long-presses.
1564 */
1565 public void setAllowLongPress(boolean allowLongPress) {
1566 mAllowLongPress = allowLongPress;
1567 }
1568
Winson Chung321e9ee2010-08-09 13:37:56 -07001569 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001570 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001571
1572 SavedState(Parcelable superState) {
1573 super(superState);
1574 }
1575
1576 private SavedState(Parcel in) {
1577 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001578 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001579 }
1580
1581 @Override
1582 public void writeToParcel(Parcel out, int flags) {
1583 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001584 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001585 }
1586
1587 public static final Parcelable.Creator<SavedState> CREATOR =
1588 new Parcelable.Creator<SavedState>() {
1589 public SavedState createFromParcel(Parcel in) {
1590 return new SavedState(in);
1591 }
1592
1593 public SavedState[] newArray(int size) {
1594 return new SavedState[size];
1595 }
1596 };
1597 }
1598
Winson Chungf314b0e2011-08-16 11:54:27 -07001599 protected void loadAssociatedPages(int page) {
1600 loadAssociatedPages(page, false);
1601 }
1602 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001603 if (mContentIsRefreshable) {
1604 final int count = getChildCount();
1605 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001606 int lowerPageBound = getAssociatedLowerPageBound(page);
1607 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001608 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1609 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001610 // First, clear any pages that should no longer be loaded
1611 for (int i = 0; i < count; ++i) {
1612 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001613 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001614 if (layout.getPageChildCount() > 0) {
1615 layout.removeAllViewsOnPage();
1616 }
1617 mDirtyPageContent.set(i, true);
1618 }
1619 }
1620 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001621 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001622 if ((i != page) && immediateAndOnly) {
1623 continue;
1624 }
Michael Jurka0142d492010-08-25 17:46:15 -07001625 if (lowerPageBound <= i && i <= upperPageBound) {
1626 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001627 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001628 mDirtyPageContent.set(i, false);
1629 }
Winson Chung86f77532010-08-24 11:08:22 -07001630 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001631 }
1632 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001633 }
1634 }
1635
Winson Chunge3193b92010-09-10 11:44:42 -07001636 protected int getAssociatedLowerPageBound(int page) {
1637 return Math.max(0, page - 1);
1638 }
1639 protected int getAssociatedUpperPageBound(int page) {
1640 final int count = getChildCount();
1641 return Math.min(page + 1, count - 1);
1642 }
1643
Winson Chung86f77532010-08-24 11:08:22 -07001644 /**
1645 * This method is called ONLY to synchronize the number of pages that the paged view has.
1646 * To actually fill the pages with information, implement syncPageItems() below. It is
1647 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1648 * and therefore, individual page items do not need to be updated in this method.
1649 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001650 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001651
1652 /**
1653 * This method is called to synchronize the items that are on a particular page. If views on
1654 * the page can be reused, then they should be updated within this method.
1655 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001656 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001657
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001658 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001659 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001660 }
1661 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001662 invalidatePageData(currentPage, false);
1663 }
1664 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001665 if (!mIsDataReady) {
1666 return;
1667 }
1668
Michael Jurka0142d492010-08-25 17:46:15 -07001669 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001670 // Force all scrolling-related behavior to end
1671 mScroller.forceFinished(true);
1672 mNextPage = INVALID_PAGE;
1673
Michael Jurka0142d492010-08-25 17:46:15 -07001674 // Update all the pages
1675 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001676
Winson Chung5a808352011-06-27 19:08:49 -07001677 // We must force a measure after we've loaded the pages to update the content width and
1678 // to determine the full scroll width
1679 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1680 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1681
1682 // Set a new page as the current page if necessary
1683 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001684 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001685 }
1686
Michael Jurka0142d492010-08-25 17:46:15 -07001687 // Mark each of the pages as dirty
1688 final int count = getChildCount();
1689 mDirtyPageContent.clear();
1690 for (int i = 0; i < count; ++i) {
1691 mDirtyPageContent.add(true);
1692 }
1693
1694 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001695 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001696 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001697 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001698 }
Winson Chung007c6982011-06-14 13:27:53 -07001699
Michael Jurkaafaa0502011-12-13 18:22:50 -08001700 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001701 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1702 // found
1703 if (mHasScrollIndicator && mScrollIndicator == null) {
1704 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001705 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001706 mHasScrollIndicator = mScrollIndicator != null;
1707 if (mHasScrollIndicator) {
1708 mScrollIndicator.setVisibility(View.VISIBLE);
1709 }
1710 }
1711 return mScrollIndicator;
1712 }
1713
1714 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001715 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001716 }
1717
Winson Chung5a808352011-06-27 19:08:49 -07001718 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1719 @Override
1720 public void run() {
1721 hideScrollingIndicator(false);
1722 }
1723 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001724 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001725 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001726 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001727 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001728 }
1729
Michael Jurka430e8a52011-08-08 15:52:14 -07001730 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001731 mShouldShowScrollIndicator = true;
1732 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001733 if (getChildCount() <= 1) return;
1734 if (!isScrollingIndicatorEnabled()) return;
1735
Michael Jurkabed61d22012-02-14 22:51:29 -08001736 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001737 getScrollingIndicator();
1738 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001739 // Fade the indicator in
1740 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001741 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001742 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001743 if (immediately) {
1744 mScrollIndicator.setAlpha(1f);
1745 } else {
1746 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1747 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1748 mScrollIndicatorAnimator.start();
1749 }
Winson Chung007c6982011-06-14 13:27:53 -07001750 }
1751 }
1752
Adam Cohen21b41102011-11-01 17:29:52 -07001753 protected void cancelScrollingIndicatorAnimations() {
1754 if (mScrollIndicatorAnimator != null) {
1755 mScrollIndicatorAnimator.cancel();
1756 }
1757 }
1758
Winson Chung007c6982011-06-14 13:27:53 -07001759 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001760 if (getChildCount() <= 1) return;
1761 if (!isScrollingIndicatorEnabled()) return;
1762
1763 getScrollingIndicator();
1764 if (mScrollIndicator != null) {
1765 // Fade the indicator out
1766 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001767 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001768 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001769 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001770 mScrollIndicator.setAlpha(0f);
1771 } else {
1772 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1773 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1774 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1775 private boolean cancelled = false;
1776 @Override
1777 public void onAnimationCancel(android.animation.Animator animation) {
1778 cancelled = true;
1779 }
1780 @Override
1781 public void onAnimationEnd(Animator animation) {
1782 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001783 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001784 }
1785 }
1786 });
1787 mScrollIndicatorAnimator.start();
1788 }
Winson Chung007c6982011-06-14 13:27:53 -07001789 }
1790 }
1791
Winson Chung32174c82011-07-19 15:47:55 -07001792 /**
1793 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1794 * fill its space on the track or not.
1795 */
1796 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001797 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001798 }
1799
Winson Chung007c6982011-06-14 13:27:53 -07001800 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001801 if (getChildCount() <= 1) return;
1802 if (!isScrollingIndicatorEnabled()) return;
1803
1804 getScrollingIndicator();
1805 if (mScrollIndicator != null) {
1806 updateScrollingIndicatorPosition();
1807 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001808 if (mShouldShowScrollIndicator) {
1809 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1810 }
Winson Chung007c6982011-06-14 13:27:53 -07001811 }
1812
Winson Chung007c6982011-06-14 13:27:53 -07001813 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001814 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001815 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001816 int numPages = getChildCount();
1817 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001818 int lastChildIndex = Math.max(0, getChildCount() - 1);
1819 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001820 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001821 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1822 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001823
Winson Chungae890b82011-09-13 18:08:54 -07001824 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001825 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001826 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001827 if (hasElasticScrollIndicator()) {
1828 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1829 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1830 mScrollIndicator.requestLayout();
1831 }
1832 } else {
1833 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1834 indicatorPos += indicatorCenterOffset;
1835 }
Winson Chung007c6982011-06-14 13:27:53 -07001836 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001837 }
1838
Winson Chung3ac74c52011-06-30 17:39:37 -07001839 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001840 }
1841
1842 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001843 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001844
1845 /* Accessibility */
1846 @Override
1847 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1848 super.onInitializeAccessibilityNodeInfo(info);
1849 info.setScrollable(true);
1850 }
1851
1852 @Override
1853 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1854 super.onInitializeAccessibilityEvent(event);
1855 event.setScrollable(true);
1856 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1857 event.setFromIndex(mCurrentPage);
1858 event.setToIndex(mCurrentPage);
1859 event.setItemCount(getChildCount());
1860 }
1861 }
1862
Winson Chung6a0f57d2011-06-29 20:10:49 -07001863 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001864 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001865 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001866 }
Winson Chungd11265e2011-08-30 13:37:23 -07001867
1868 @Override
1869 public boolean onHoverEvent(android.view.MotionEvent event) {
1870 return true;
1871 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001872}