blob: 02f1c22b89efe80784de68c4e3ced7885e86452c [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;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070027import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080032import android.view.InputDevice;
33import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.MotionEvent;
35import android.view.VelocityTracker;
36import android.view.View;
37import android.view.ViewConfiguration;
38import android.view.ViewGroup;
39import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070040import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070041import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070042import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080043import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.widget.Scroller;
45
Winson Chung04998342011-01-05 13:54:43 -080046import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070047
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import java.util.ArrayList;
49
Winson Chung321e9ee2010-08-09 13:37:56 -070050/**
51 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070052 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070053 */
Michael Jurka8b805b12012-04-18 14:23:14 -070054public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070055 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070056 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070057 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070058
Winson Chung86f77532010-08-24 11:08:22 -070059 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070060 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chungf0c6ae02012-03-21 16:10:31 -070062 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
63 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Adam Cohenb5ba0972011-09-07 18:02:31 -070066 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070067 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080068
Adam Cohenb64cb5a2011-02-15 13:53:42 -080069 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080070 // The page is moved more than halfway, automatically move to the next page on touch up.
71 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080072
Adam Cohen265b9a62011-12-07 14:37:18 -080073 // The following constants need to be scaled based on density. The scaled versions will be
74 // assigned to the corresponding member variables below.
75 private static final int FLING_THRESHOLD_VELOCITY = 500;
76 private static final int MIN_SNAP_VELOCITY = 1500;
77 private static final int MIN_FLING_VELOCITY = 250;
78
Winson Chung8aad6102012-05-11 16:27:49 -070079 static final int AUTOMATIC_PAGE_SPACING = -1;
80
Adam Cohen265b9a62011-12-07 14:37:18 -080081 protected int mFlingThresholdVelocity;
82 protected int mMinFlingVelocity;
83 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070084
Adam Cohenb5ba0972011-09-07 18:02:31 -070085 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070086 protected float mSmoothingTime;
87 protected float mTouchX;
88
89 protected boolean mFirstLayout = true;
90
91 protected int mCurrentPage;
92 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080093 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070095 private VelocityTracker mVelocityTracker;
96
97 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080099 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800101 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700102 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700103 private int[] mChildOffsets;
104 private int[] mChildRelativeOffsets;
105 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected final static int TOUCH_STATE_REST = 0;
108 protected final static int TOUCH_STATE_SCROLLING = 1;
109 protected final static int TOUCH_STATE_PREV_PAGE = 2;
110 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700111 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700114 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Michael Jurka0142d492010-08-25 17:46:15 -0700116 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka7426c422010-11-11 15:23:47 -0800118 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka7426c422010-11-11 15:23:47 -0800120 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121 private int mPagingTouchSlop;
122 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800123 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700124 protected int mPageSpacing;
125 protected int mPageLayoutPaddingTop;
126 protected int mPageLayoutPaddingBottom;
127 protected int mPageLayoutPaddingLeft;
128 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700129 protected int mPageLayoutWidthGap;
130 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700131 protected int mCellCountX = 0;
132 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700133 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800134 protected boolean mAllowOverScroll = true;
135 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800136 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Michael Jurka8b805b12012-04-18 14:23:14 -0700138 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800139 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
140 // the screens from continuing to translate beyond the normal bounds.
141 protected int mOverScrollX;
142
Michael Jurka8c920dd2011-01-20 14:16:56 -0800143 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800144 protected float mLayoutScale = 1.0f;
145
Michael Jurka5f1c5092010-09-03 14:15:02 -0700146 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700147
Michael Jurka5f1c5092010-09-03 14:15:02 -0700148 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700149
Winson Chung86f77532010-08-24 11:08:22 -0700150 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700151
Michael Jurkae326f182011-11-21 14:05:46 -0800152 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700153
Michael Jurka0142d492010-08-25 17:46:15 -0700154 // If true, syncPages and syncPageItems will be called to refresh pages
155 protected boolean mContentIsRefreshable = true;
156
157 // If true, modify alpha of neighboring pages as user scrolls left/right
158 protected boolean mFadeInAdjacentScreens = true;
159
160 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
161 // to switch to a new page
162 protected boolean mUsePagingTouchSlop = true;
163
Michael Jurka8b805b12012-04-18 14:23:14 -0700164 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700165 // (SmoothPagedView does this)
166 protected boolean mDeferScrollUpdate = false;
167
Patrick Dubroy1262e362010-10-06 15:49:50 -0700168 protected boolean mIsPageMoving = false;
169
Winson Chungf0ea4d32011-06-06 14:27:16 -0700170 // All syncs and layout passes are deferred until data is ready.
171 protected boolean mIsDataReady = false;
172
Winson Chung007c6982011-06-14 13:27:53 -0700173 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700174 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800175 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700176 private int mScrollIndicatorPaddingLeft;
177 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700178 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800179 private boolean mShouldShowScrollIndicator = false;
180 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700181 protected static final int sScrollIndicatorFadeInDuration = 150;
182 protected static final int sScrollIndicatorFadeOutDuration = 650;
183 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700184
Winson Chungb44b5242011-06-13 11:32:14 -0700185 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700186 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700187
Winson Chung86f77532010-08-24 11:08:22 -0700188 public interface PageSwitchListener {
189 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 }
191
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 public PagedView(Context context) {
193 this(context, null);
194 }
195
196 public PagedView(Context context, AttributeSet attrs) {
197 this(context, attrs, 0);
198 }
199
200 public PagedView(Context context, AttributeSet attrs, int defStyle) {
201 super(context, attrs, defStyle);
202
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 TypedArray a = context.obtainStyledAttributes(attrs,
204 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800205 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800207 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700214 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700215 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700216 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700218 mScrollIndicatorPaddingLeft =
219 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
220 mScrollIndicatorPaddingRight =
221 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700222 a.recycle();
223
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700225 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 }
227
228 /**
229 * Initializes various states for this workspace.
230 */
Michael Jurka0142d492010-08-25 17:46:15 -0700231 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700232 mDirtyPageContent = new ArrayList<Boolean>();
233 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800234 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700235 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700236 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700237
238 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
239 mTouchSlop = configuration.getScaledTouchSlop();
240 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
241 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700242 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800243
244 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
245 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
246 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700247 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 }
249
Winson Chung86f77532010-08-24 11:08:22 -0700250 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
251 mPageSwitchListener = pageSwitchListener;
252 if (mPageSwitchListener != null) {
253 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 }
255 }
256
257 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700258 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
259 * out pages.
260 */
261 protected void setDataIsReady() {
262 mIsDataReady = true;
263 }
264 protected boolean isDataReady() {
265 return mIsDataReady;
266 }
267
268 /**
Winson Chung86f77532010-08-24 11:08:22 -0700269 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 *
Winson Chung86f77532010-08-24 11:08:22 -0700271 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 */
Winson Chung86f77532010-08-24 11:08:22 -0700273 int getCurrentPage() {
274 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 }
Winson Chung360e63f2012-04-27 13:48:05 -0700276 int getNextPage() {
277 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
278 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700279
Winson Chung86f77532010-08-24 11:08:22 -0700280 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 return getChildCount();
282 }
283
Winson Chung86f77532010-08-24 11:08:22 -0700284 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 return getChildAt(index);
286 }
287
Adam Cohenae4f1552011-10-20 00:15:42 -0700288 protected int indexToPage(int index) {
289 return index;
290 }
291
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800293 * Updates the scroll of the current page immediately to its final scroll position. We use this
294 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
295 * the previous tab page.
296 */
297 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700298 int offset = getChildOffset(mCurrentPage);
299 int relOffset = getRelativeChildOffset(mCurrentPage);
300 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800301 scrollTo(newX, 0);
302 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800303 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800304 }
305
306 /**
Winson Chung86f77532010-08-24 11:08:22 -0700307 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 */
Winson Chung86f77532010-08-24 11:08:22 -0700309 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800310 if (!mScroller.isFinished()) {
311 mScroller.abortAnimation();
312 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800313 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
314 // the default
315 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800316 return;
317 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700318
Winson Chung86f77532010-08-24 11:08:22 -0700319 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800320 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700321 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700322 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800323 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 }
325
Michael Jurka0142d492010-08-25 17:46:15 -0700326 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700327 if (mPageSwitchListener != null) {
328 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 }
330 }
331
Michael Jurkace7e05f2011-02-01 22:02:35 -0800332 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700333 if (!mIsPageMoving) {
334 mIsPageMoving = true;
335 onPageBeginMoving();
336 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700337 }
338
Michael Jurkace7e05f2011-02-01 22:02:35 -0800339 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700340 if (mIsPageMoving) {
341 mIsPageMoving = false;
342 onPageEndMoving();
343 }
Michael Jurka0142d492010-08-25 17:46:15 -0700344 }
345
Adam Cohen26976d92011-03-22 15:33:33 -0700346 protected boolean isPageMoving() {
347 return mIsPageMoving;
348 }
349
Michael Jurka0142d492010-08-25 17:46:15 -0700350 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700351 protected void onPageBeginMoving() {
352 }
353
354 // a method that subclasses can override to add behavior
355 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700356 }
357
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 /**
Winson Chung86f77532010-08-24 11:08:22 -0700359 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 *
361 * @param l The listener used to respond to long clicks.
362 */
363 @Override
364 public void setOnLongClickListener(OnLongClickListener l) {
365 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700366 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700367 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700368 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700369 }
370 }
371
372 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800373 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700374 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800375 }
376
377 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700378 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800379 mUnboundedScrollX = x;
380
381 if (x < 0) {
382 super.scrollTo(0, y);
383 if (mAllowOverScroll) {
384 overScroll(x);
385 }
386 } else if (x > mMaxScrollX) {
387 super.scrollTo(mMaxScrollX, y);
388 if (mAllowOverScroll) {
389 overScroll(x - mMaxScrollX);
390 }
391 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800392 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800393 super.scrollTo(x, y);
394 }
395
Michael Jurka0142d492010-08-25 17:46:15 -0700396 mTouchX = x;
397 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
398 }
399
400 // we moved this functionality to a helper function so SmoothPagedView can reuse it
401 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700403 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700404 if (getScrollX() != mScroller.getCurrX()
405 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700406 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700407 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
408 }
Michael Jurka0142d492010-08-25 17:46:15 -0700409 invalidate();
410 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700411 } else if (mNextPage != INVALID_PAGE) {
412 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700413 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700414 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700415
416 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700417 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700418 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700419 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700420 }
421
Adam Cohen73aa9752010-11-24 16:26:58 -0800422 // We don't want to trigger a page end moving unless the page has settled
423 // and the user has stopped scrolling
424 if (mTouchState == TOUCH_STATE_REST) {
425 pageEndMoving();
426 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700427
428 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700429 AccessibilityManager accessibilityManager = (AccessibilityManager)
430 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
431 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700432 AccessibilityEvent ev =
433 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
434 ev.getText().add(getCurrentPageDescription());
435 sendAccessibilityEventUnchecked(ev);
436 }
Michael Jurka0142d492010-08-25 17:46:15 -0700437 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
Michael Jurka0142d492010-08-25 17:46:15 -0700439 return false;
440 }
441
442 @Override
443 public void computeScroll() {
444 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 }
446
447 @Override
448 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 if (!mIsDataReady) {
450 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
451 return;
452 }
453
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
455 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700456 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
457 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 if (widthMode != MeasureSpec.EXACTLY) {
459 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
460 }
461
Winson Chung8aad6102012-05-11 16:27:49 -0700462 // Return early if we aren't given a proper dimension
463 if (widthSize <= 0 || heightSize <= 0) {
464 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
465 return;
466 }
467
Adam Lesinski6b879f02010-11-04 16:15:23 -0700468 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
469 * of the All apps view on XLarge displays to not take up more space then it needs. Width
470 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
471 * each page to have the same width.
472 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700473 int maxChildHeight = 0;
474
Michael Jurka8b805b12012-04-18 14:23:14 -0700475 final int verticalPadding = getPaddingTop() + getPaddingBottom();
476 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700477
Michael Jurka36fcb742011-04-06 17:08:58 -0700478
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700480 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700481 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700482 final int childCount = getChildCount();
483 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700484 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700485 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700486 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
487
488 int childWidthMode;
489 if (lp.width == LayoutParams.WRAP_CONTENT) {
490 childWidthMode = MeasureSpec.AT_MOST;
491 } else {
492 childWidthMode = MeasureSpec.EXACTLY;
493 }
494
495 int childHeightMode;
496 if (lp.height == LayoutParams.WRAP_CONTENT) {
497 childHeightMode = MeasureSpec.AT_MOST;
498 } else {
499 childHeightMode = MeasureSpec.EXACTLY;
500 }
501
502 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700503 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700504 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700505 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700506
507 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700508 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700509 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
510 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700511 }
512
513 if (heightMode == MeasureSpec.AT_MOST) {
514 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
Winson Chungae890b82011-09-13 18:08:54 -0700516
Winson Chungae890b82011-09-13 18:08:54 -0700517 setMeasuredDimension(widthSize, heightSize);
518
Winson Chung8aad6102012-05-11 16:27:49 -0700519 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
520 // We also wait until we set the measured dimensions before flushing the cache as well, to
521 // ensure that the cache is filled with good values.
522 invalidateCachedOffsets();
523
Winson Chunga128a7b2012-04-30 15:23:15 -0700524 if (childCount > 0) {
525 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
526 + getChildWidth(0));
527
528 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700529 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700530 // The gap between pages in the PagedView should be equal to the gap from the page
531 // to the edge of the screen (so it is not visible in the current screen). To
532 // account for unequal padding on each side of the paged view, we take the maximum
533 // of the left/right gap and use that as the gap between each page.
534 int offset = getRelativeChildOffset(0);
535 int spacing = Math.max(offset, widthSize - offset -
536 getChildAt(0).getMeasuredWidth());
537 setPageSpacing(spacing);
538 }
539 }
540
Adam Cohen25b29952011-11-02 14:49:06 -0700541 updateScrollingIndicatorPosition();
542
Adam Cohenfaa28302010-11-19 12:02:18 -0800543 if (childCount > 0) {
544 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
545 } else {
546 mMaxScrollX = 0;
547 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 }
549
Michael Jurka8c920dd2011-01-20 14:16:56 -0800550 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800551 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700552 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800553
Michael Jurka8c920dd2011-01-20 14:16:56 -0800554 final int pageCount = getChildCount();
555 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700556 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800557 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800558 }
559 setCurrentPage(newCurrentPage);
560 }
561
Michael Jurka8c920dd2011-01-20 14:16:56 -0800562 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
563 // 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 -0800564 // tightens the layout accordingly
565 public void setLayoutScale(float childrenScale) {
566 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700567 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800568
569 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
570 int childCount = getChildCount();
571 float childrenX[] = new float[childCount];
572 float childrenY[] = new float[childCount];
573 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700574 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800575 childrenX[i] = child.getX();
576 childrenY[i] = child.getY();
577 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700578 // Trigger a full re-layout (never just call onLayout directly!)
579 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
580 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
581 requestLayout();
582 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800584 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700585 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800586 child.setX(childrenX[i]);
587 child.setY(childrenY[i]);
588 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700589
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590 // Also, the page offset has changed (since the pages are now smaller);
591 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800592 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800593 }
594
Adam Cohen60b07122011-11-14 17:26:06 -0800595 public void setPageSpacing(int pageSpacing) {
596 mPageSpacing = pageSpacing;
597 invalidateCachedOffsets();
598 }
599
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700601 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700602 if (!mIsDataReady) {
603 return;
604 }
605
Winson Chung785d2eb2011-04-14 16:08:02 -0700606 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700607 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700609 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700610
611 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700612 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700613 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800614 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700615 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700616 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700617 if (mCenterPagesVertically) {
618 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
619 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800620
Winson Chung785d2eb2011-04-14 16:08:02 -0700621 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700622 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800623 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700624 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700625 }
626 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700627
628 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
629 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800630 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700631 setHorizontalScrollBarEnabled(true);
632 mFirstLayout = false;
633 }
Winson Chunge3193b92010-09-10 11:44:42 -0700634 }
635
Adam Cohenf34bab52010-09-30 14:11:56 -0700636 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700637 if (isScrollingIndicatorEnabled()) {
638 updateScrollingIndicator();
639 }
Michael Jurka869390b2012-05-06 15:55:19 -0700640 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
641
642 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700643 for (int i = 0; i < getChildCount(); i++) {
644 View child = getChildAt(i);
645 if (child != null) {
646 float scrollProgress = getScrollProgress(screenCenter, child, i);
647 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800648 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700649 }
650 }
651 invalidate();
652 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700653 }
654
Winson Chunge3193b92010-09-10 11:44:42 -0700655 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700656 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700657 // This ensures that when children are added, they get the correct transforms / alphas
658 // in accordance with any scroll effects.
659 mForceScreenScrolled = true;
660 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700661 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700662 }
663
Michael Jurka8b805b12012-04-18 14:23:14 -0700664 @Override
665 public void onChildViewRemoved(View parent, View child) {
666 }
667
Adam Cohen73894962011-10-31 13:17:17 -0700668 protected void invalidateCachedOffsets() {
669 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700670 if (count == 0) {
671 mChildOffsets = null;
672 mChildRelativeOffsets = null;
673 mChildOffsetsWithLayoutScale = null;
674 return;
675 }
Adam Cohen73894962011-10-31 13:17:17 -0700676
677 mChildOffsets = new int[count];
678 mChildRelativeOffsets = new int[count];
679 mChildOffsetsWithLayoutScale = new int[count];
680 for (int i = 0; i < count; i++) {
681 mChildOffsets[i] = -1;
682 mChildRelativeOffsets[i] = -1;
683 mChildOffsetsWithLayoutScale[i] = -1;
684 }
685 }
686
687 protected int getChildOffset(int index) {
688 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
689 mChildOffsets : mChildOffsetsWithLayoutScale;
690
691 if (childOffsets != null && childOffsets[index] != -1) {
692 return childOffsets[index];
693 } else {
694 if (getChildCount() == 0)
695 return 0;
696
697 int offset = getRelativeChildOffset(0);
698 for (int i = 0; i < index; ++i) {
699 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
700 }
701 if (childOffsets != null) {
702 childOffsets[index] = offset;
703 }
704 return offset;
705 }
706 }
707
708 protected int getRelativeChildOffset(int index) {
709 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
710 return mChildRelativeOffsets[index];
711 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 final int padding = getPaddingLeft() + getPaddingRight();
713 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700714 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
715 if (mChildRelativeOffsets != null) {
716 mChildRelativeOffsets[index] = offset;
717 }
718 return offset;
719 }
720 }
721
Adam Cohen73894962011-10-31 13:17:17 -0700722 protected int getScaledMeasuredWidth(View child) {
723 // This functions are called enough times that it actually makes a difference in the
724 // profiler -- so just inline the max() here
725 final int measuredWidth = child.getMeasuredWidth();
726 final int minWidth = mMinimumWidth;
727 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
728 return (int) (maxWidth * mLayoutScale + 0.5f);
729 }
730
Michael Jurkadde558b2011-11-09 22:09:06 -0800731 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700732 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700733
Michael Jurkac4fb9172010-09-02 17:19:20 -0700734 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700735 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700736 int leftScreen = 0;
737 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800738 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800739 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 currPage.getX() + currPage.getWidth() -
741 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700742 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800743 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700744 }
745 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800746 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800747 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700749 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800750 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700751 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800752 range[0] = leftScreen;
753 range[1] = rightScreen;
754 } else {
755 range[0] = -1;
756 range[1] = -1;
757 }
758 }
759
Michael Jurka920d7f42012-05-14 16:29:55 -0700760 protected boolean shouldDrawChild(View child) {
761 return child.getAlpha() > 0;
762 }
763
Michael Jurkadde558b2011-11-09 22:09:06 -0800764 @Override
765 protected void dispatchDraw(Canvas canvas) {
766 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
768 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800769 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800770
771 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700772 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
773 // set it for the next frame
774 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800775 screenScrolled(screenCenter);
776 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800777 }
778
779 // Find out which screens are visible; as an optimization we only call draw on them
780 final int pageCount = getChildCount();
781 if (pageCount > 0) {
782 getVisiblePages(mTempVisiblePagesRange);
783 final int leftScreen = mTempVisiblePagesRange[0];
784 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800785 if (leftScreen != -1 && rightScreen != -1) {
786 final long drawingTime = getDrawingTime();
787 // Clip to the bounds
788 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
790 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700791
Michael Jurka80c69852011-12-16 14:16:32 -0800792 // On certain graphics drivers, if you draw to a off-screen buffer that's not
793 // used, it can lead to poor performance. We were running into this when
794 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
795 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
796 // As a fix, below we set pages that aren't going to be rendered are to be
797 // View.INVISIBLE, preventing re-drawing of their hardware layer
798 for (int i = getChildCount() - 1; i >= 0; i--) {
799 final View v = getPageAt(i);
Michael Jurka920d7f42012-05-14 16:29:55 -0700800 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -0800801 v.setVisibility(VISIBLE);
802 drawChild(canvas, v, drawingTime);
803 } else {
804 v.setVisibility(INVISIBLE);
805 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800806 }
807 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700808 }
Michael Jurka0142d492010-08-25 17:46:15 -0700809 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 }
811
812 @Override
813 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700814 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700815 if (page != mCurrentPage || !mScroller.isFinished()) {
816 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 return true;
818 }
819 return false;
820 }
821
822 @Override
823 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700824 int focusablePage;
825 if (mNextPage != INVALID_PAGE) {
826 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700828 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 }
Winson Chung86f77532010-08-24 11:08:22 -0700830 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700832 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 }
834 return false;
835 }
836
837 @Override
838 public boolean dispatchUnhandledMove(View focused, int direction) {
839 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700840 if (getCurrentPage() > 0) {
841 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 return true;
843 }
844 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700845 if (getCurrentPage() < getPageCount() - 1) {
846 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 return true;
848 }
849 }
850 return super.dispatchUnhandledMove(focused, direction);
851 }
852
853 @Override
854 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700855 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700856 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 }
858 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700859 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700860 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 }
862 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700863 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700864 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866 }
867 }
868
869 /**
870 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700871 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 *
Winson Chung86f77532010-08-24 11:08:22 -0700873 * This happens when live folders requery, and if they're off page, they
874 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 */
876 @Override
877 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700878 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 View v = focused;
880 while (true) {
881 if (v == current) {
882 super.focusableViewAvailable(focused);
883 return;
884 }
885 if (v == this) {
886 return;
887 }
888 ViewParent parent = v.getParent();
889 if (parent instanceof View) {
890 v = (View)v.getParent();
891 } else {
892 return;
893 }
894 }
895 }
896
897 /**
898 * {@inheritDoc}
899 */
900 @Override
901 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
902 if (disallowIntercept) {
903 // We need to make sure to cancel our long press if
904 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700905 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700906 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 }
908 super.requestDisallowInterceptTouchEvent(disallowIntercept);
909 }
910
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800911 /**
912 * Return true if a tap at (x, y) should trigger a flip to the previous page.
913 */
914 protected boolean hitsPreviousPage(float x, float y) {
915 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
916 }
917
918 /**
919 * Return true if a tap at (x, y) should trigger a flip to the next page.
920 */
921 protected boolean hitsNextPage(float x, float y) {
922 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
923 }
924
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 @Override
926 public boolean onInterceptTouchEvent(MotionEvent ev) {
927 /*
928 * This method JUST determines whether we want to intercept the motion.
929 * If we return true, onTouchEvent will be called and we do the actual
930 * scrolling there.
931 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800932 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700933
Winson Chung45e1d6e2010-11-09 17:19:49 -0800934 // Skip touch handling if there are no pages to swipe
935 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
936
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 /*
938 * Shortcut the most recurring case: the user is in the dragging
939 * state and he is moving his finger. We want to intercept this
940 * motion.
941 */
942 final int action = ev.getAction();
943 if ((action == MotionEvent.ACTION_MOVE) &&
944 (mTouchState == TOUCH_STATE_SCROLLING)) {
945 return true;
946 }
947
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 switch (action & MotionEvent.ACTION_MASK) {
949 case MotionEvent.ACTION_MOVE: {
950 /*
951 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
952 * whether the user has moved far enough from his original down touch.
953 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700954 if (mActivePointerId != INVALID_POINTER) {
955 determineScrollingStart(ev);
956 break;
957 }
958 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
959 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
960 // i.e. fall through to the next case (don't break)
961 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
962 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 }
964
965 case MotionEvent.ACTION_DOWN: {
966 final float x = ev.getX();
967 final float y = ev.getY();
968 // Remember location of down touch
969 mDownMotionX = x;
970 mLastMotionX = x;
971 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800972 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800973 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700974 mActivePointerId = ev.getPointerId(0);
975 mAllowLongPress = true;
976
977 /*
978 * If being flinged and user touches the screen, initiate drag;
979 * otherwise don't. mScroller.isFinished should be false when
980 * being flinged.
981 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700982 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700983 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
984 if (finishedScrolling) {
985 mTouchState = TOUCH_STATE_REST;
986 mScroller.abortAnimation();
987 } else {
988 mTouchState = TOUCH_STATE_SCROLLING;
989 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700990
Winson Chung86f77532010-08-24 11:08:22 -0700991 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800993 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800995 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800997 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 mTouchState = TOUCH_STATE_NEXT_PAGE;
999 }
1000 }
1001 }
1002 break;
1003 }
1004
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001006 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 mTouchState = TOUCH_STATE_REST;
1008 mAllowLongPress = false;
1009 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001010 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 break;
1012
1013 case MotionEvent.ACTION_POINTER_UP:
1014 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001015 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 break;
1017 }
1018
1019 /*
1020 * The only time we want to intercept motion events is if we are in the
1021 * drag mode.
1022 */
1023 return mTouchState != TOUCH_STATE_REST;
1024 }
1025
Adam Cohenf8d28232011-02-01 21:47:00 -08001026 protected void determineScrollingStart(MotionEvent ev) {
1027 determineScrollingStart(ev, 1.0f);
1028 }
1029
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 /*
1031 * Determines if we should change the touch state to start scrolling after the
1032 * user moves their touch point too far.
1033 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001034 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 /*
1036 * Locally do absolute value. mLastMotionX is set to the y value
1037 * of the down event.
1038 */
1039 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001040 if (pointerIndex == -1) {
1041 return;
1042 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 final float x = ev.getX(pointerIndex);
1044 final float y = ev.getY(pointerIndex);
1045 final int xDiff = (int) Math.abs(x - mLastMotionX);
1046 final int yDiff = (int) Math.abs(y - mLastMotionY);
1047
Adam Cohenf8d28232011-02-01 21:47:00 -08001048 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 boolean xPaged = xDiff > mPagingTouchSlop;
1050 boolean xMoved = xDiff > touchSlop;
1051 boolean yMoved = yDiff > touchSlop;
1052
Adam Cohenf8d28232011-02-01 21:47:00 -08001053 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001054 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 // Scroll if the user moved far enough along the X axis
1056 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001057 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001059 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001060 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001061 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1062 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 }
1064 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001065 cancelCurrentPageLongPress();
1066 }
1067 }
1068
1069 protected void cancelCurrentPageLongPress() {
1070 if (mAllowLongPress) {
1071 mAllowLongPress = false;
1072 // Try canceling the long press. It could also have been scheduled
1073 // by a distant descendant, so use the mAllowLongPress flag to block
1074 // everything
1075 final View currentPage = getPageAt(mCurrentPage);
1076 if (currentPage != null) {
1077 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 }
1079 }
1080 }
1081
Adam Cohenb5ba0972011-09-07 18:02:31 -07001082 protected float getScrollProgress(int screenCenter, View v, int page) {
1083 final int halfScreenSize = getMeasuredWidth() / 2;
1084
1085 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1086 int delta = screenCenter - (getChildOffset(page) -
1087 getRelativeChildOffset(page) + halfScreenSize);
1088
1089 float scrollProgress = delta / (totalDistance * 1.0f);
1090 scrollProgress = Math.min(scrollProgress, 1.0f);
1091 scrollProgress = Math.max(scrollProgress, -1.0f);
1092 return scrollProgress;
1093 }
1094
Adam Cohene0f66b52010-11-23 15:06:07 -08001095 // This curve determines how the effect of scrolling over the limits of the page dimishes
1096 // as the user pulls further and further from the bounds
1097 private float overScrollInfluenceCurve(float f) {
1098 f -= 1.0f;
1099 return f * f * f + 1.0f;
1100 }
1101
Adam Cohenb5ba0972011-09-07 18:02:31 -07001102 protected void acceleratedOverScroll(float amount) {
1103 int screenSize = getMeasuredWidth();
1104
1105 // We want to reach the max over scroll effect when the user has
1106 // over scrolled half the size of the screen
1107 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1108
1109 if (f == 0) return;
1110
1111 // Clamp this factor, f, to -1 < f < 1
1112 if (Math.abs(f) >= 1) {
1113 f /= Math.abs(f);
1114 }
1115
1116 int overScrollAmount = (int) Math.round(f * screenSize);
1117 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001118 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001119 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001120 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001121 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001122 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001123 }
1124 invalidate();
1125 }
1126
1127 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001128 int screenSize = getMeasuredWidth();
1129
1130 float f = (amount / screenSize);
1131
1132 if (f == 0) return;
1133 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1134
Adam Cohen7bfc9792011-01-28 13:52:37 -08001135 // Clamp this factor, f, to -1 < f < 1
1136 if (Math.abs(f) >= 1) {
1137 f /= Math.abs(f);
1138 }
1139
Adam Cohene0f66b52010-11-23 15:06:07 -08001140 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001141 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001142 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001143 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001144 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001145 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001146 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001147 }
1148 invalidate();
1149 }
1150
Adam Cohenb5ba0972011-09-07 18:02:31 -07001151 protected void overScroll(float amount) {
1152 dampedOverScroll(amount);
1153 }
1154
Michael Jurkac5b262c2011-01-12 20:24:50 -08001155 protected float maxOverScroll() {
1156 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001157 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001158 float f = 1.0f;
1159 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1160 return OVERSCROLL_DAMP_FACTOR * f;
1161 }
1162
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 @Override
1164 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001165 // Skip touch handling if there are no pages to swipe
1166 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1167
Michael Jurkab8f06722010-10-10 15:58:46 -07001168 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001169
1170 final int action = ev.getAction();
1171
1172 switch (action & MotionEvent.ACTION_MASK) {
1173 case MotionEvent.ACTION_DOWN:
1174 /*
1175 * If being flinged and user touches, stop the fling. isFinished
1176 * will be false if being flinged.
1177 */
1178 if (!mScroller.isFinished()) {
1179 mScroller.abortAnimation();
1180 }
1181
1182 // Remember where the motion event started
1183 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001184 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001185 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001187 if (mTouchState == TOUCH_STATE_SCROLLING) {
1188 pageBeginMoving();
1189 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 break;
1191
1192 case MotionEvent.ACTION_MOVE:
1193 if (mTouchState == TOUCH_STATE_SCROLLING) {
1194 // Scroll to follow the motion event
1195 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1196 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001197 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001198
Adam Cohenaefd4e12011-02-14 16:39:38 -08001199 mTotalMotionX += Math.abs(deltaX);
1200
Winson Chungc0844aa2011-02-02 15:25:58 -08001201 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1202 // keep the remainder because we are actually testing if we've moved from the last
1203 // scrolled position (which is discrete).
1204 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001205 mTouchX += deltaX;
1206 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1207 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001208 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001209 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001210 } else {
1211 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001213 mLastMotionX = x;
1214 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 } else {
1216 awakenScrollBars();
1217 }
Adam Cohen564976a2010-10-13 18:52:07 -07001218 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001219 determineScrollingStart(ev);
1220 }
1221 break;
1222
1223 case MotionEvent.ACTION_UP:
1224 if (mTouchState == TOUCH_STATE_SCROLLING) {
1225 final int activePointerId = mActivePointerId;
1226 final int pointerIndex = ev.findPointerIndex(activePointerId);
1227 final float x = ev.getX(pointerIndex);
1228 final VelocityTracker velocityTracker = mVelocityTracker;
1229 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1230 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001231 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001232 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1233 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1234 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001235
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001236 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1237
Adam Cohen00481b32011-11-18 12:03:48 -08001238 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001239 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001240
Adam Cohenaefd4e12011-02-14 16:39:38 -08001241 // In the case that the page is moved far to one direction and then is flung
1242 // in the opposite direction, we use a threshold to determine whether we should
1243 // just return to the starting page, or if we should skip one further.
1244 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001245 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001246 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001247 returnToOriginalPage = true;
1248 }
1249
Adam Cohenaefd4e12011-02-14 16:39:38 -08001250 int finalPage;
1251 // We give flings precedence over large moves, which is why we short-circuit our
1252 // test for a large move if a fling has been registered. That is, a large
1253 // move to the left and fling to the right will register as a fling to the right.
1254 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1255 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1256 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1257 snapToPageWithVelocity(finalPage, velocityX);
1258 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1259 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001260 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001261 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1262 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 } else {
1264 snapToDestination();
1265 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001266 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 // at this point we have not moved beyond the touch slop
1268 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1269 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001270 int nextPage = Math.max(0, mCurrentPage - 1);
1271 if (nextPage != mCurrentPage) {
1272 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 } else {
1274 snapToDestination();
1275 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001276 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 // at this point we have not moved beyond the touch slop
1278 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1279 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001280 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1281 if (nextPage != mCurrentPage) {
1282 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 } else {
1284 snapToDestination();
1285 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001286 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001287 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001288 }
1289 mTouchState = TOUCH_STATE_REST;
1290 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001291 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001292 break;
1293
1294 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001295 if (mTouchState == TOUCH_STATE_SCROLLING) {
1296 snapToDestination();
1297 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 mTouchState = TOUCH_STATE_REST;
1299 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001300 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001301 break;
1302
1303 case MotionEvent.ACTION_POINTER_UP:
1304 onSecondaryPointerUp(ev);
1305 break;
1306 }
1307
1308 return true;
1309 }
1310
Winson Chung185d7162011-02-28 13:47:29 -08001311 @Override
1312 public boolean onGenericMotionEvent(MotionEvent event) {
1313 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1314 switch (event.getAction()) {
1315 case MotionEvent.ACTION_SCROLL: {
1316 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1317 final float vscroll;
1318 final float hscroll;
1319 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1320 vscroll = 0;
1321 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1322 } else {
1323 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1324 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1325 }
1326 if (hscroll != 0 || vscroll != 0) {
1327 if (hscroll > 0 || vscroll > 0) {
1328 scrollRight();
1329 } else {
1330 scrollLeft();
1331 }
1332 return true;
1333 }
1334 }
1335 }
1336 }
1337 return super.onGenericMotionEvent(event);
1338 }
1339
Michael Jurkab8f06722010-10-10 15:58:46 -07001340 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1341 if (mVelocityTracker == null) {
1342 mVelocityTracker = VelocityTracker.obtain();
1343 }
1344 mVelocityTracker.addMovement(ev);
1345 }
1346
1347 private void releaseVelocityTracker() {
1348 if (mVelocityTracker != null) {
1349 mVelocityTracker.recycle();
1350 mVelocityTracker = null;
1351 }
1352 }
1353
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 private void onSecondaryPointerUp(MotionEvent ev) {
1355 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1356 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1357 final int pointerId = ev.getPointerId(pointerIndex);
1358 if (pointerId == mActivePointerId) {
1359 // This was our active pointer going up. Choose a new
1360 // active pointer and adjust accordingly.
1361 // TODO: Make this decision more intelligent.
1362 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1363 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1364 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001365 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 mActivePointerId = ev.getPointerId(newPointerIndex);
1367 if (mVelocityTracker != null) {
1368 mVelocityTracker.clear();
1369 }
1370 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001371 }
1372
Michael Jurkad771c962011-08-09 15:00:48 -07001373 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001374
1375 @Override
1376 public void requestChildFocus(View child, View focused) {
1377 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001378 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001379 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001380 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001381 }
1382 }
1383
Winson Chunge3193b92010-09-10 11:44:42 -07001384 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1385 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001386 int left;
1387 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001388 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001389 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001390 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001391 if (left <= relativeOffset && relativeOffset <= right) {
1392 return i;
1393 }
Winson Chunge3193b92010-09-10 11:44:42 -07001394 }
1395 return -1;
1396 }
1397
Winson Chung1908d072011-02-24 18:09:44 -08001398 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001399 // This functions are called enough times that it actually makes a difference in the
1400 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001401 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001402 final int minWidth = mMinimumWidth;
1403 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001404 }
1405
Adam Cohend19d3ca2010-09-15 14:43:42 -07001406 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001407 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001409 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 final int childCount = getChildCount();
1411 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001412 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001413 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 int halfChildWidth = (childWidth / 2);
1415 int childCenter = getChildOffset(i) + halfChildWidth;
1416 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1417 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1418 minDistanceFromScreenCenter = distanceFromScreenCenter;
1419 minDistanceFromScreenCenterIndex = i;
1420 }
1421 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001422 return minDistanceFromScreenCenterIndex;
1423 }
1424
1425 protected void snapToDestination() {
1426 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 }
1428
Adam Cohene0f66b52010-11-23 15:06:07 -08001429 private static class ScrollInterpolator implements Interpolator {
1430 public ScrollInterpolator() {
1431 }
1432
1433 public float getInterpolation(float t) {
1434 t -= 1.0f;
1435 return t*t*t*t*t + 1;
1436 }
1437 }
1438
1439 // We want the duration of the page snap animation to be influenced by the distance that
1440 // the screen has to travel, however, we don't want this duration to be effected in a
1441 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1442 // of travel has on the overall snap duration.
1443 float distanceInfluenceForSnapDuration(float f) {
1444 f -= 0.5f; // center the values about 0.
1445 f *= 0.3f * Math.PI / 2.0f;
1446 return (float) Math.sin(f);
1447 }
1448
Michael Jurka0142d492010-08-25 17:46:15 -07001449 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001450 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1451 int halfScreenSize = getMeasuredWidth() / 2;
1452
Winson Chung785d2eb2011-04-14 16:08:02 -07001453 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1454 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1455 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001456 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1457 int delta = newX - mUnboundedScrollX;
1458 int duration = 0;
1459
Adam Cohen265b9a62011-12-07 14:37:18 -08001460 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001461 // If the velocity is low enough, then treat this more as an automatic page advance
1462 // as opposed to an apparent physical response to flinging
1463 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1464 return;
1465 }
1466
1467 // Here we compute a "distance" that will be used in the computation of the overall
1468 // snap duration. This is a function of the actual distance that needs to be traveled;
1469 // we keep this value close to half screen size in order to reduce the variance in snap
1470 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001471 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001472 float distance = halfScreenSize + halfScreenSize *
1473 distanceInfluenceForSnapDuration(distanceRatio);
1474
1475 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001476 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001477
1478 // we want the page's snap velocity to approximately match the velocity at which the
1479 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001480 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1481 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001482
1483 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001484 }
1485
1486 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001487 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001488 }
1489
Michael Jurka0142d492010-08-25 17:46:15 -07001490 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001491 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001492
Winson Chung785d2eb2011-04-14 16:08:02 -07001493 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1494 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1495 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001496 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001497 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001499 snapToPage(whichPage, delta, duration);
1500 }
1501
1502 protected void snapToPage(int whichPage, int delta, int duration) {
1503 mNextPage = whichPage;
1504
1505 View focusedChild = getFocusedChild();
1506 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001507 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001508 focusedChild.clearFocus();
1509 }
1510
1511 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 awakenScrollBars(duration);
1513 if (duration == 0) {
1514 duration = Math.abs(delta);
1515 }
1516
1517 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001518 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001519
Winson Chungb44b5242011-06-13 11:32:14 -07001520 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1521 // loading associated pages until the scroll settles
1522 if (mDeferScrollUpdate) {
1523 loadAssociatedPages(mNextPage);
1524 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001525 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001526 }
Michael Jurka0142d492010-08-25 17:46:15 -07001527 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 invalidate();
1529 }
1530
Winson Chung321e9ee2010-08-09 13:37:56 -07001531 public void scrollLeft() {
1532 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001533 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001535 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001536 }
1537 }
1538
1539 public void scrollRight() {
1540 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001541 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001543 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 }
1545 }
1546
Winson Chung86f77532010-08-24 11:08:22 -07001547 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001548 int result = -1;
1549 if (v != null) {
1550 ViewParent vp = v.getParent();
1551 int count = getChildCount();
1552 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001553 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001554 return i;
1555 }
1556 }
1557 }
1558 return result;
1559 }
1560
1561 /**
1562 * @return True is long presses are still allowed for the current touch
1563 */
1564 public boolean allowLongPress() {
1565 return mAllowLongPress;
1566 }
1567
Michael Jurka0142d492010-08-25 17:46:15 -07001568 /**
1569 * Set true to allow long-press events to be triggered, usually checked by
1570 * {@link Launcher} to accept or block dpad-initiated long-presses.
1571 */
1572 public void setAllowLongPress(boolean allowLongPress) {
1573 mAllowLongPress = allowLongPress;
1574 }
1575
Winson Chung321e9ee2010-08-09 13:37:56 -07001576 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001577 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001578
1579 SavedState(Parcelable superState) {
1580 super(superState);
1581 }
1582
1583 private SavedState(Parcel in) {
1584 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001585 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001586 }
1587
1588 @Override
1589 public void writeToParcel(Parcel out, int flags) {
1590 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001591 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001592 }
1593
1594 public static final Parcelable.Creator<SavedState> CREATOR =
1595 new Parcelable.Creator<SavedState>() {
1596 public SavedState createFromParcel(Parcel in) {
1597 return new SavedState(in);
1598 }
1599
1600 public SavedState[] newArray(int size) {
1601 return new SavedState[size];
1602 }
1603 };
1604 }
1605
Winson Chungf314b0e2011-08-16 11:54:27 -07001606 protected void loadAssociatedPages(int page) {
1607 loadAssociatedPages(page, false);
1608 }
1609 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001610 if (mContentIsRefreshable) {
1611 final int count = getChildCount();
1612 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001613 int lowerPageBound = getAssociatedLowerPageBound(page);
1614 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001615 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1616 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001617 // First, clear any pages that should no longer be loaded
1618 for (int i = 0; i < count; ++i) {
1619 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001620 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001621 if (layout.getPageChildCount() > 0) {
1622 layout.removeAllViewsOnPage();
1623 }
1624 mDirtyPageContent.set(i, true);
1625 }
1626 }
1627 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001628 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001629 if ((i != page) && immediateAndOnly) {
1630 continue;
1631 }
Michael Jurka0142d492010-08-25 17:46:15 -07001632 if (lowerPageBound <= i && i <= upperPageBound) {
1633 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001634 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001635 mDirtyPageContent.set(i, false);
1636 }
Winson Chung86f77532010-08-24 11:08:22 -07001637 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001638 }
1639 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001640 }
1641 }
1642
Winson Chunge3193b92010-09-10 11:44:42 -07001643 protected int getAssociatedLowerPageBound(int page) {
1644 return Math.max(0, page - 1);
1645 }
1646 protected int getAssociatedUpperPageBound(int page) {
1647 final int count = getChildCount();
1648 return Math.min(page + 1, count - 1);
1649 }
1650
Winson Chung86f77532010-08-24 11:08:22 -07001651 /**
1652 * This method is called ONLY to synchronize the number of pages that the paged view has.
1653 * To actually fill the pages with information, implement syncPageItems() below. It is
1654 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1655 * and therefore, individual page items do not need to be updated in this method.
1656 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001657 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001658
1659 /**
1660 * This method is called to synchronize the items that are on a particular page. If views on
1661 * the page can be reused, then they should be updated within this method.
1662 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001663 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001664
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001665 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001666 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001667 }
1668 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001669 invalidatePageData(currentPage, false);
1670 }
1671 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001672 if (!mIsDataReady) {
1673 return;
1674 }
1675
Michael Jurka0142d492010-08-25 17:46:15 -07001676 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001677 // Force all scrolling-related behavior to end
1678 mScroller.forceFinished(true);
1679 mNextPage = INVALID_PAGE;
1680
Michael Jurka0142d492010-08-25 17:46:15 -07001681 // Update all the pages
1682 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001683
Winson Chung5a808352011-06-27 19:08:49 -07001684 // We must force a measure after we've loaded the pages to update the content width and
1685 // to determine the full scroll width
1686 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1687 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1688
1689 // Set a new page as the current page if necessary
1690 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001691 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001692 }
1693
Michael Jurka0142d492010-08-25 17:46:15 -07001694 // Mark each of the pages as dirty
1695 final int count = getChildCount();
1696 mDirtyPageContent.clear();
1697 for (int i = 0; i < count; ++i) {
1698 mDirtyPageContent.add(true);
1699 }
1700
1701 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001702 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001703 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001704 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001705 }
Winson Chung007c6982011-06-14 13:27:53 -07001706
Michael Jurkaafaa0502011-12-13 18:22:50 -08001707 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001708 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1709 // found
1710 if (mHasScrollIndicator && mScrollIndicator == null) {
1711 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001712 if (parent != null) {
1713 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1714 mHasScrollIndicator = mScrollIndicator != null;
1715 if (mHasScrollIndicator) {
1716 mScrollIndicator.setVisibility(View.VISIBLE);
1717 }
Winson Chung007c6982011-06-14 13:27:53 -07001718 }
1719 }
1720 return mScrollIndicator;
1721 }
1722
1723 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001724 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001725 }
1726
Winson Chung5a808352011-06-27 19:08:49 -07001727 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1728 @Override
1729 public void run() {
1730 hideScrollingIndicator(false);
1731 }
1732 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001733 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001734 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001735 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001736 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001737 }
1738
Michael Jurka430e8a52011-08-08 15:52:14 -07001739 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001740 mShouldShowScrollIndicator = true;
1741 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001742 if (getChildCount() <= 1) return;
1743 if (!isScrollingIndicatorEnabled()) return;
1744
Michael Jurkabed61d22012-02-14 22:51:29 -08001745 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001746 getScrollingIndicator();
1747 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001748 // Fade the indicator in
1749 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001750 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001751 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001752 if (immediately) {
1753 mScrollIndicator.setAlpha(1f);
1754 } else {
1755 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1756 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1757 mScrollIndicatorAnimator.start();
1758 }
Winson Chung007c6982011-06-14 13:27:53 -07001759 }
1760 }
1761
Adam Cohen21b41102011-11-01 17:29:52 -07001762 protected void cancelScrollingIndicatorAnimations() {
1763 if (mScrollIndicatorAnimator != null) {
1764 mScrollIndicatorAnimator.cancel();
1765 }
1766 }
1767
Winson Chung007c6982011-06-14 13:27:53 -07001768 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001769 if (getChildCount() <= 1) return;
1770 if (!isScrollingIndicatorEnabled()) return;
1771
1772 getScrollingIndicator();
1773 if (mScrollIndicator != null) {
1774 // Fade the indicator out
1775 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001776 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001777 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001778 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001779 mScrollIndicator.setAlpha(0f);
1780 } else {
1781 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1782 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1783 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1784 private boolean cancelled = false;
1785 @Override
1786 public void onAnimationCancel(android.animation.Animator animation) {
1787 cancelled = true;
1788 }
1789 @Override
1790 public void onAnimationEnd(Animator animation) {
1791 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001792 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001793 }
1794 }
1795 });
1796 mScrollIndicatorAnimator.start();
1797 }
Winson Chung007c6982011-06-14 13:27:53 -07001798 }
1799 }
1800
Winson Chung32174c82011-07-19 15:47:55 -07001801 /**
1802 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1803 * fill its space on the track or not.
1804 */
1805 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001806 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001807 }
1808
Winson Chung007c6982011-06-14 13:27:53 -07001809 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001810 if (getChildCount() <= 1) return;
1811 if (!isScrollingIndicatorEnabled()) return;
1812
1813 getScrollingIndicator();
1814 if (mScrollIndicator != null) {
1815 updateScrollingIndicatorPosition();
1816 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001817 if (mShouldShowScrollIndicator) {
1818 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1819 }
Winson Chung007c6982011-06-14 13:27:53 -07001820 }
1821
Winson Chung007c6982011-06-14 13:27:53 -07001822 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001823 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001824 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001825 int numPages = getChildCount();
1826 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001827 int lastChildIndex = Math.max(0, getChildCount() - 1);
1828 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001829 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001830 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1831 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001832
Winson Chungae890b82011-09-13 18:08:54 -07001833 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001834 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001835 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001836 if (hasElasticScrollIndicator()) {
1837 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1838 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1839 mScrollIndicator.requestLayout();
1840 }
1841 } else {
1842 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1843 indicatorPos += indicatorCenterOffset;
1844 }
Winson Chung007c6982011-06-14 13:27:53 -07001845 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001846 }
1847
Winson Chung3ac74c52011-06-30 17:39:37 -07001848 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001849 }
1850
1851 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001852 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001853
1854 /* Accessibility */
1855 @Override
1856 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1857 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001858 info.setScrollable(getPageCount() > 1);
1859 if (getCurrentPage() < getPageCount() - 1) {
1860 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1861 }
1862 if (getCurrentPage() > 0) {
1863 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1864 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001865 }
1866
1867 @Override
1868 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1869 super.onInitializeAccessibilityEvent(event);
1870 event.setScrollable(true);
1871 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1872 event.setFromIndex(mCurrentPage);
1873 event.setToIndex(mCurrentPage);
1874 event.setItemCount(getChildCount());
1875 }
1876 }
1877
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001878 @Override
1879 public boolean performAccessibilityAction(int action, Bundle arguments) {
1880 if (super.performAccessibilityAction(action, arguments)) {
1881 return true;
1882 }
1883 switch (action) {
1884 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1885 if (getCurrentPage() < getPageCount() - 1) {
1886 scrollRight();
1887 return true;
1888 }
1889 } break;
1890 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1891 if (getCurrentPage() > 0) {
1892 scrollLeft();
1893 return true;
1894 }
1895 } break;
1896 }
1897 return false;
1898 }
1899
Winson Chung6a0f57d2011-06-29 20:10:49 -07001900 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001901 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001902 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001903 }
Winson Chungd11265e2011-08-30 13:37:23 -07001904
1905 @Override
1906 public boolean onHoverEvent(android.view.MotionEvent event) {
1907 return true;
1908 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001909}