blob: 19f6a62a7959dba57373befb730177a3f5507f22 [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];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700137 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800140 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
141 // the screens from continuing to translate beyond the normal bounds.
142 protected int mOverScrollX;
143
Michael Jurka8c920dd2011-01-20 14:16:56 -0800144 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800145 protected float mLayoutScale = 1.0f;
146
Michael Jurka5f1c5092010-09-03 14:15:02 -0700147 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurka5f1c5092010-09-03 14:15:02 -0700149 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Winson Chung86f77532010-08-24 11:08:22 -0700151 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Michael Jurkae326f182011-11-21 14:05:46 -0800153 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700154
Michael Jurka0142d492010-08-25 17:46:15 -0700155 // If true, syncPages and syncPageItems will be called to refresh pages
156 protected boolean mContentIsRefreshable = true;
157
158 // If true, modify alpha of neighboring pages as user scrolls left/right
159 protected boolean mFadeInAdjacentScreens = true;
160
161 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
162 // to switch to a new page
163 protected boolean mUsePagingTouchSlop = true;
164
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700166 // (SmoothPagedView does this)
167 protected boolean mDeferScrollUpdate = false;
168
Patrick Dubroy1262e362010-10-06 15:49:50 -0700169 protected boolean mIsPageMoving = false;
170
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 // All syncs and layout passes are deferred until data is ready.
172 protected boolean mIsDataReady = false;
173
Winson Chung007c6982011-06-14 13:27:53 -0700174 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700175 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800176 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700177 private int mScrollIndicatorPaddingLeft;
178 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700179 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800180 private boolean mShouldShowScrollIndicator = false;
181 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700182 protected static final int sScrollIndicatorFadeInDuration = 150;
183 protected static final int sScrollIndicatorFadeOutDuration = 650;
184 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700185
Winson Chungb44b5242011-06-13 11:32:14 -0700186 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700187 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700188
Winson Chung86f77532010-08-24 11:08:22 -0700189 public interface PageSwitchListener {
190 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 public PagedView(Context context) {
194 this(context, null);
195 }
196
197 public PagedView(Context context, AttributeSet attrs) {
198 this(context, attrs, 0);
199 }
200
201 public PagedView(Context context, AttributeSet attrs, int defStyle) {
202 super(context, attrs, defStyle);
203
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 TypedArray a = context.obtainStyledAttributes(attrs,
205 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800206 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800212 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800214 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700215 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700216 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700217 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700218 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700219 mScrollIndicatorPaddingLeft =
220 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
221 mScrollIndicatorPaddingRight =
222 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700223 a.recycle();
224
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700226 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 }
228
229 /**
230 * Initializes various states for this workspace.
231 */
Michael Jurka0142d492010-08-25 17:46:15 -0700232 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700233 mDirtyPageContent = new ArrayList<Boolean>();
234 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800235 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700236 mCurrentPage = 0;
Winson Chungbfeac062012-06-06 15:56:08 -0700237 if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 init() mCurrentPage: " + mCurrentPage);
Winson Chung7da10252010-10-28 16:07:04 -0700238 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700239
240 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
241 mTouchSlop = configuration.getScaledTouchSlop();
242 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
243 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700244 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800245
246 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
247 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
248 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700249 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 }
251
Winson Chung86f77532010-08-24 11:08:22 -0700252 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
253 mPageSwitchListener = pageSwitchListener;
254 if (mPageSwitchListener != null) {
255 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 }
257 }
258
259 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700260 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
261 * out pages.
262 */
263 protected void setDataIsReady() {
264 mIsDataReady = true;
265 }
266 protected boolean isDataReady() {
267 return mIsDataReady;
268 }
269
270 /**
Winson Chung86f77532010-08-24 11:08:22 -0700271 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 *
Winson Chung86f77532010-08-24 11:08:22 -0700273 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 */
Winson Chung86f77532010-08-24 11:08:22 -0700275 int getCurrentPage() {
276 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 }
Winson Chung360e63f2012-04-27 13:48:05 -0700278 int getNextPage() {
279 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
280 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700281
Winson Chung86f77532010-08-24 11:08:22 -0700282 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 return getChildCount();
284 }
285
Winson Chung86f77532010-08-24 11:08:22 -0700286 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 return getChildAt(index);
288 }
289
Adam Cohenae4f1552011-10-20 00:15:42 -0700290 protected int indexToPage(int index) {
291 return index;
292 }
293
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800295 * Updates the scroll of the current page immediately to its final scroll position. We use this
296 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
297 * the previous tab page.
298 */
299 protected void updateCurrentPageScroll() {
Winson Chunga128a7b2012-04-30 15:23:15 -0700300 int offset = getChildOffset(mCurrentPage);
301 int relOffset = getRelativeChildOffset(mCurrentPage);
302 int newX = offset - relOffset;
Winson Chungbbc60d82010-11-11 16:34:41 -0800303 scrollTo(newX, 0);
304 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800305 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800306 }
307
308 /**
Winson Chung86f77532010-08-24 11:08:22 -0700309 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 */
Winson Chung86f77532010-08-24 11:08:22 -0700311 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800312 if (!mScroller.isFinished()) {
313 mScroller.abortAnimation();
314 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800315 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
316 // the default
317 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800318 return;
319 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700320
Winson Chungbfeac062012-06-06 15:56:08 -0700321
Winson Chung86f77532010-08-24 11:08:22 -0700322 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbfeac062012-06-06 15:56:08 -0700323 if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 setCurrentPage mCurrentPage: " + mCurrentPage);
Winson Chungbbc60d82010-11-11 16:34:41 -0800324 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700325 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700326 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800327 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 }
329
Michael Jurka0142d492010-08-25 17:46:15 -0700330 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700331 if (mPageSwitchListener != null) {
332 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 }
334 }
335
Michael Jurkace7e05f2011-02-01 22:02:35 -0800336 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700337 if (!mIsPageMoving) {
338 mIsPageMoving = true;
339 onPageBeginMoving();
340 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700341 }
342
Michael Jurkace7e05f2011-02-01 22:02:35 -0800343 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700344 if (mIsPageMoving) {
345 mIsPageMoving = false;
346 onPageEndMoving();
347 }
Michael Jurka0142d492010-08-25 17:46:15 -0700348 }
349
Adam Cohen26976d92011-03-22 15:33:33 -0700350 protected boolean isPageMoving() {
351 return mIsPageMoving;
352 }
353
Michael Jurka0142d492010-08-25 17:46:15 -0700354 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700355 protected void onPageBeginMoving() {
356 }
357
358 // a method that subclasses can override to add behavior
359 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700360 }
361
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 /**
Winson Chung86f77532010-08-24 11:08:22 -0700363 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 *
365 * @param l The listener used to respond to long clicks.
366 */
367 @Override
368 public void setOnLongClickListener(OnLongClickListener l) {
369 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700370 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700372 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700373 }
374 }
375
376 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800377 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700378 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800379 }
380
381 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700382 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800383 mUnboundedScrollX = x;
384
385 if (x < 0) {
386 super.scrollTo(0, y);
387 if (mAllowOverScroll) {
388 overScroll(x);
389 }
390 } else if (x > mMaxScrollX) {
391 super.scrollTo(mMaxScrollX, y);
392 if (mAllowOverScroll) {
393 overScroll(x - mMaxScrollX);
394 }
395 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800396 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800397 super.scrollTo(x, y);
398 }
399
Michael Jurka0142d492010-08-25 17:46:15 -0700400 mTouchX = x;
401 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
402 }
403
404 // we moved this functionality to a helper function so SmoothPagedView can reuse it
405 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700407 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700408 if (getScrollX() != mScroller.getCurrX()
409 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700410 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700411 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
412 }
Michael Jurka0142d492010-08-25 17:46:15 -0700413 invalidate();
414 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700415 } else if (mNextPage != INVALID_PAGE) {
416 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700417 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700418 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700419
420 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700421 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700422 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700423 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700424 }
425
Adam Cohen73aa9752010-11-24 16:26:58 -0800426 // We don't want to trigger a page end moving unless the page has settled
427 // and the user has stopped scrolling
428 if (mTouchState == TOUCH_STATE_REST) {
429 pageEndMoving();
430 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700431
432 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700433 AccessibilityManager accessibilityManager = (AccessibilityManager)
434 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
435 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700436 AccessibilityEvent ev =
437 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
438 ev.getText().add(getCurrentPageDescription());
439 sendAccessibilityEventUnchecked(ev);
440 }
Michael Jurka0142d492010-08-25 17:46:15 -0700441 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700442 }
Michael Jurka0142d492010-08-25 17:46:15 -0700443 return false;
444 }
445
446 @Override
447 public void computeScroll() {
448 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700449 }
450
451 @Override
452 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 if (!mIsDataReady) {
454 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
455 return;
456 }
457
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
459 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700460 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
461 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 if (widthMode != MeasureSpec.EXACTLY) {
463 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
464 }
465
Winson Chung8aad6102012-05-11 16:27:49 -0700466 // Return early if we aren't given a proper dimension
467 if (widthSize <= 0 || heightSize <= 0) {
468 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
469 return;
470 }
471
Adam Lesinski6b879f02010-11-04 16:15:23 -0700472 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
473 * of the All apps view on XLarge displays to not take up more space then it needs. Width
474 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
475 * each page to have the same width.
476 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700477 int maxChildHeight = 0;
478
Michael Jurka8b805b12012-04-18 14:23:14 -0700479 final int verticalPadding = getPaddingTop() + getPaddingBottom();
480 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700481
Michael Jurka36fcb742011-04-06 17:08:58 -0700482
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700484 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700485 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 final int childCount = getChildCount();
487 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700488 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700489 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700490 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
491
492 int childWidthMode;
493 if (lp.width == LayoutParams.WRAP_CONTENT) {
494 childWidthMode = MeasureSpec.AT_MOST;
495 } else {
496 childWidthMode = MeasureSpec.EXACTLY;
497 }
498
499 int childHeightMode;
500 if (lp.height == LayoutParams.WRAP_CONTENT) {
501 childHeightMode = MeasureSpec.AT_MOST;
502 } else {
503 childHeightMode = MeasureSpec.EXACTLY;
504 }
505
506 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700507 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700508 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700509 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700510
511 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700512 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700513 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
514 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700515 }
516
517 if (heightMode == MeasureSpec.AT_MOST) {
518 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
Winson Chungae890b82011-09-13 18:08:54 -0700520
Winson Chungae890b82011-09-13 18:08:54 -0700521 setMeasuredDimension(widthSize, heightSize);
522
Winson Chung8aad6102012-05-11 16:27:49 -0700523 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
524 // We also wait until we set the measured dimensions before flushing the cache as well, to
525 // ensure that the cache is filled with good values.
526 invalidateCachedOffsets();
527
Winson Chunga128a7b2012-04-30 15:23:15 -0700528 if (childCount > 0) {
529 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
530 + getChildWidth(0));
531
532 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700533 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700534 // The gap between pages in the PagedView should be equal to the gap from the page
535 // to the edge of the screen (so it is not visible in the current screen). To
536 // account for unequal padding on each side of the paged view, we take the maximum
537 // of the left/right gap and use that as the gap between each page.
538 int offset = getRelativeChildOffset(0);
539 int spacing = Math.max(offset, widthSize - offset -
540 getChildAt(0).getMeasuredWidth());
541 setPageSpacing(spacing);
542 }
543 }
544
Adam Cohen25b29952011-11-02 14:49:06 -0700545 updateScrollingIndicatorPosition();
546
Adam Cohenfaa28302010-11-19 12:02:18 -0800547 if (childCount > 0) {
548 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
549 } else {
550 mMaxScrollX = 0;
551 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
553
Michael Jurka8c920dd2011-01-20 14:16:56 -0800554 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800555 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800557
Michael Jurka8c920dd2011-01-20 14:16:56 -0800558 final int pageCount = getChildCount();
559 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700560 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800561 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800562 }
563 setCurrentPage(newCurrentPage);
564 }
565
Michael Jurka8c920dd2011-01-20 14:16:56 -0800566 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
567 // 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 -0800568 // tightens the layout accordingly
569 public void setLayoutScale(float childrenScale) {
570 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700571 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800572
573 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
574 int childCount = getChildCount();
575 float childrenX[] = new float[childCount];
576 float childrenY[] = new float[childCount];
577 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700578 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800579 childrenX[i] = child.getX();
580 childrenY[i] = child.getY();
581 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700582 // Trigger a full re-layout (never just call onLayout directly!)
583 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
584 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
585 requestLayout();
586 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700587 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800588 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700589 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590 child.setX(childrenX[i]);
591 child.setY(childrenY[i]);
592 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700593
Michael Jurkad3ef3062010-11-23 16:23:58 -0800594 // Also, the page offset has changed (since the pages are now smaller);
595 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800596 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800597 }
598
Adam Cohen60b07122011-11-14 17:26:06 -0800599 public void setPageSpacing(int pageSpacing) {
600 mPageSpacing = pageSpacing;
601 invalidateCachedOffsets();
602 }
603
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700605 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700606 if (!mIsDataReady) {
607 return;
608 }
609
Winson Chung785d2eb2011-04-14 16:08:02 -0700610 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700611 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700612 final int childCount = getChildCount();
Winson Chunga128a7b2012-04-30 15:23:15 -0700613 int childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700614
615 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700616 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800618 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700619 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700621 if (mCenterPagesVertically) {
622 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
623 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800624
Winson Chung785d2eb2011-04-14 16:08:02 -0700625 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700626 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800627 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700628 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700629 }
630 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700631
632 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
633 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800634 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700635 setHorizontalScrollBarEnabled(true);
636 mFirstLayout = false;
637 }
Winson Chunge3193b92010-09-10 11:44:42 -0700638 }
639
Adam Cohenf34bab52010-09-30 14:11:56 -0700640 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700641 if (isScrollingIndicatorEnabled()) {
642 updateScrollingIndicator();
643 }
Michael Jurka869390b2012-05-06 15:55:19 -0700644 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
645
646 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700647 for (int i = 0; i < getChildCount(); i++) {
648 View child = getChildAt(i);
649 if (child != null) {
650 float scrollProgress = getScrollProgress(screenCenter, child, i);
651 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800652 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700653 }
654 }
655 invalidate();
656 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700657 }
658
Winson Chunge3193b92010-09-10 11:44:42 -0700659 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700660 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700661 // This ensures that when children are added, they get the correct transforms / alphas
662 // in accordance with any scroll effects.
663 mForceScreenScrolled = true;
664 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700665 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700666 }
667
Michael Jurka8b805b12012-04-18 14:23:14 -0700668 @Override
669 public void onChildViewRemoved(View parent, View child) {
670 }
671
Adam Cohen73894962011-10-31 13:17:17 -0700672 protected void invalidateCachedOffsets() {
673 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700674 if (count == 0) {
675 mChildOffsets = null;
676 mChildRelativeOffsets = null;
677 mChildOffsetsWithLayoutScale = null;
678 return;
679 }
Adam Cohen73894962011-10-31 13:17:17 -0700680
681 mChildOffsets = new int[count];
682 mChildRelativeOffsets = new int[count];
683 mChildOffsetsWithLayoutScale = new int[count];
684 for (int i = 0; i < count; i++) {
685 mChildOffsets[i] = -1;
686 mChildRelativeOffsets[i] = -1;
687 mChildOffsetsWithLayoutScale[i] = -1;
688 }
689 }
690
691 protected int getChildOffset(int index) {
692 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
693 mChildOffsets : mChildOffsetsWithLayoutScale;
694
695 if (childOffsets != null && childOffsets[index] != -1) {
696 return childOffsets[index];
697 } else {
698 if (getChildCount() == 0)
699 return 0;
700
701 int offset = getRelativeChildOffset(0);
702 for (int i = 0; i < index; ++i) {
703 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
704 }
705 if (childOffsets != null) {
706 childOffsets[index] = offset;
707 }
708 return offset;
709 }
710 }
711
712 protected int getRelativeChildOffset(int index) {
713 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
714 return mChildRelativeOffsets[index];
715 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 final int padding = getPaddingLeft() + getPaddingRight();
717 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700718 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
719 if (mChildRelativeOffsets != null) {
720 mChildRelativeOffsets[index] = offset;
721 }
722 return offset;
723 }
724 }
725
Adam Cohen73894962011-10-31 13:17:17 -0700726 protected int getScaledMeasuredWidth(View child) {
727 // This functions are called enough times that it actually makes a difference in the
728 // profiler -- so just inline the max() here
729 final int measuredWidth = child.getMeasuredWidth();
730 final int minWidth = mMinimumWidth;
731 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
732 return (int) (maxWidth * mLayoutScale + 0.5f);
733 }
734
Michael Jurkadde558b2011-11-09 22:09:06 -0800735 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700736 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700737
Michael Jurkac4fb9172010-09-02 17:19:20 -0700738 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700739 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700740 int leftScreen = 0;
741 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800742 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800743 while (leftScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 currPage.getX() + currPage.getWidth() -
745 currPage.getPaddingRight() < getScrollX()) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700746 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800747 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700748 }
749 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800750 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800751 while (rightScreen < pageCount - 1 &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700753 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800754 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700755 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800756 range[0] = leftScreen;
757 range[1] = rightScreen;
758 } else {
759 range[0] = -1;
760 range[1] = -1;
761 }
762 }
763
Michael Jurka920d7f42012-05-14 16:29:55 -0700764 protected boolean shouldDrawChild(View child) {
765 return child.getAlpha() > 0;
766 }
767
Michael Jurkadde558b2011-11-09 22:09:06 -0800768 @Override
769 protected void dispatchDraw(Canvas canvas) {
770 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
772 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800773 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800774
775 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700776 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
777 // set it for the next frame
778 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800779 screenScrolled(screenCenter);
780 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800781 }
782
783 // Find out which screens are visible; as an optimization we only call draw on them
784 final int pageCount = getChildCount();
785 if (pageCount > 0) {
786 getVisiblePages(mTempVisiblePagesRange);
787 final int leftScreen = mTempVisiblePagesRange[0];
788 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800789 if (leftScreen != -1 && rightScreen != -1) {
790 final long drawingTime = getDrawingTime();
791 // Clip to the bounds
792 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
794 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700795
Michael Jurka80c69852011-12-16 14:16:32 -0800796 // On certain graphics drivers, if you draw to a off-screen buffer that's not
797 // used, it can lead to poor performance. We were running into this when
798 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
799 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
800 // As a fix, below we set pages that aren't going to be rendered are to be
801 // View.INVISIBLE, preventing re-drawing of their hardware layer
802 for (int i = getChildCount() - 1; i >= 0; i--) {
803 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700804 if (mForceDrawAllChildrenNextFrame ||
805 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800806 v.setVisibility(VISIBLE);
807 drawChild(canvas, v, drawingTime);
808 } else {
809 v.setVisibility(INVISIBLE);
810 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800811 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700812 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800813 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700814 }
Michael Jurka0142d492010-08-25 17:46:15 -0700815 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 }
817
818 @Override
819 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700820 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700821 if (page != mCurrentPage || !mScroller.isFinished()) {
822 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 return true;
824 }
825 return false;
826 }
827
828 @Override
829 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700830 int focusablePage;
831 if (mNextPage != INVALID_PAGE) {
832 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700834 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 }
Winson Chung86f77532010-08-24 11:08:22 -0700836 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700838 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 }
840 return false;
841 }
842
843 @Override
844 public boolean dispatchUnhandledMove(View focused, int direction) {
845 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700846 if (getCurrentPage() > 0) {
847 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 return true;
849 }
850 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700851 if (getCurrentPage() < getPageCount() - 1) {
852 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 return true;
854 }
855 }
856 return super.dispatchUnhandledMove(focused, direction);
857 }
858
859 @Override
860 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700861 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700862 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 }
864 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700865 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700866 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 }
868 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700869 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700870 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 }
872 }
873 }
874
875 /**
876 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700877 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 *
Winson Chung86f77532010-08-24 11:08:22 -0700879 * This happens when live folders requery, and if they're off page, they
880 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 */
882 @Override
883 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700884 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 View v = focused;
886 while (true) {
887 if (v == current) {
888 super.focusableViewAvailable(focused);
889 return;
890 }
891 if (v == this) {
892 return;
893 }
894 ViewParent parent = v.getParent();
895 if (parent instanceof View) {
896 v = (View)v.getParent();
897 } else {
898 return;
899 }
900 }
901 }
902
903 /**
904 * {@inheritDoc}
905 */
906 @Override
907 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
908 if (disallowIntercept) {
909 // We need to make sure to cancel our long press if
910 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700911 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700912 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700913 }
914 super.requestDisallowInterceptTouchEvent(disallowIntercept);
915 }
916
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800917 /**
918 * Return true if a tap at (x, y) should trigger a flip to the previous page.
919 */
920 protected boolean hitsPreviousPage(float x, float y) {
921 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
922 }
923
924 /**
925 * Return true if a tap at (x, y) should trigger a flip to the next page.
926 */
927 protected boolean hitsNextPage(float x, float y) {
928 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
929 }
930
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 @Override
932 public boolean onInterceptTouchEvent(MotionEvent ev) {
933 /*
934 * This method JUST determines whether we want to intercept the motion.
935 * If we return true, onTouchEvent will be called and we do the actual
936 * scrolling there.
937 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800938 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700939
Winson Chung45e1d6e2010-11-09 17:19:49 -0800940 // Skip touch handling if there are no pages to swipe
941 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
942
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 /*
944 * Shortcut the most recurring case: the user is in the dragging
945 * state and he is moving his finger. We want to intercept this
946 * motion.
947 */
948 final int action = ev.getAction();
949 if ((action == MotionEvent.ACTION_MOVE) &&
950 (mTouchState == TOUCH_STATE_SCROLLING)) {
951 return true;
952 }
953
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 switch (action & MotionEvent.ACTION_MASK) {
955 case MotionEvent.ACTION_MOVE: {
956 /*
957 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
958 * whether the user has moved far enough from his original down touch.
959 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700960 if (mActivePointerId != INVALID_POINTER) {
961 determineScrollingStart(ev);
962 break;
963 }
964 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
965 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
966 // i.e. fall through to the next case (don't break)
967 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
968 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 }
970
971 case MotionEvent.ACTION_DOWN: {
972 final float x = ev.getX();
973 final float y = ev.getY();
974 // Remember location of down touch
975 mDownMotionX = x;
976 mLastMotionX = x;
977 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800978 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800979 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 mActivePointerId = ev.getPointerId(0);
981 mAllowLongPress = true;
982
983 /*
984 * If being flinged and user touches the screen, initiate drag;
985 * otherwise don't. mScroller.isFinished should be false when
986 * being flinged.
987 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700988 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700989 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
990 if (finishedScrolling) {
991 mTouchState = TOUCH_STATE_REST;
992 mScroller.abortAnimation();
993 } else {
994 mTouchState = TOUCH_STATE_SCROLLING;
995 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700996
Winson Chung86f77532010-08-24 11:08:22 -0700997 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800999 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001001 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001003 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 mTouchState = TOUCH_STATE_NEXT_PAGE;
1005 }
1006 }
1007 }
1008 break;
1009 }
1010
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001012 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 mTouchState = TOUCH_STATE_REST;
1014 mAllowLongPress = false;
1015 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001016 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 break;
1018
1019 case MotionEvent.ACTION_POINTER_UP:
1020 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001021 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 break;
1023 }
1024
1025 /*
1026 * The only time we want to intercept motion events is if we are in the
1027 * drag mode.
1028 */
1029 return mTouchState != TOUCH_STATE_REST;
1030 }
1031
Adam Cohenf8d28232011-02-01 21:47:00 -08001032 protected void determineScrollingStart(MotionEvent ev) {
1033 determineScrollingStart(ev, 1.0f);
1034 }
1035
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 /*
1037 * Determines if we should change the touch state to start scrolling after the
1038 * user moves their touch point too far.
1039 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001040 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001041 /*
1042 * Locally do absolute value. mLastMotionX is set to the y value
1043 * of the down event.
1044 */
1045 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001046 if (pointerIndex == -1) {
1047 return;
1048 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 final float x = ev.getX(pointerIndex);
1050 final float y = ev.getY(pointerIndex);
1051 final int xDiff = (int) Math.abs(x - mLastMotionX);
1052 final int yDiff = (int) Math.abs(y - mLastMotionY);
1053
Adam Cohenf8d28232011-02-01 21:47:00 -08001054 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 boolean xPaged = xDiff > mPagingTouchSlop;
1056 boolean xMoved = xDiff > touchSlop;
1057 boolean yMoved = yDiff > touchSlop;
1058
Adam Cohenf8d28232011-02-01 21:47:00 -08001059 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001060 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 // Scroll if the user moved far enough along the X axis
1062 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001063 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001065 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001066 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001067 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1068 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 }
1070 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001071 cancelCurrentPageLongPress();
1072 }
1073 }
1074
1075 protected void cancelCurrentPageLongPress() {
1076 if (mAllowLongPress) {
1077 mAllowLongPress = false;
1078 // Try canceling the long press. It could also have been scheduled
1079 // by a distant descendant, so use the mAllowLongPress flag to block
1080 // everything
1081 final View currentPage = getPageAt(mCurrentPage);
1082 if (currentPage != null) {
1083 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 }
1085 }
1086 }
1087
Adam Cohenb5ba0972011-09-07 18:02:31 -07001088 protected float getScrollProgress(int screenCenter, View v, int page) {
1089 final int halfScreenSize = getMeasuredWidth() / 2;
1090
1091 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1092 int delta = screenCenter - (getChildOffset(page) -
1093 getRelativeChildOffset(page) + halfScreenSize);
1094
1095 float scrollProgress = delta / (totalDistance * 1.0f);
1096 scrollProgress = Math.min(scrollProgress, 1.0f);
1097 scrollProgress = Math.max(scrollProgress, -1.0f);
1098 return scrollProgress;
1099 }
1100
Adam Cohene0f66b52010-11-23 15:06:07 -08001101 // This curve determines how the effect of scrolling over the limits of the page dimishes
1102 // as the user pulls further and further from the bounds
1103 private float overScrollInfluenceCurve(float f) {
1104 f -= 1.0f;
1105 return f * f * f + 1.0f;
1106 }
1107
Adam Cohenb5ba0972011-09-07 18:02:31 -07001108 protected void acceleratedOverScroll(float amount) {
1109 int screenSize = getMeasuredWidth();
1110
1111 // We want to reach the max over scroll effect when the user has
1112 // over scrolled half the size of the screen
1113 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1114
1115 if (f == 0) return;
1116
1117 // Clamp this factor, f, to -1 < f < 1
1118 if (Math.abs(f) >= 1) {
1119 f /= Math.abs(f);
1120 }
1121
1122 int overScrollAmount = (int) Math.round(f * screenSize);
1123 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001124 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001125 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001126 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001127 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001128 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001129 }
1130 invalidate();
1131 }
1132
1133 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001134 int screenSize = getMeasuredWidth();
1135
1136 float f = (amount / screenSize);
1137
1138 if (f == 0) return;
1139 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1140
Adam Cohen7bfc9792011-01-28 13:52:37 -08001141 // Clamp this factor, f, to -1 < f < 1
1142 if (Math.abs(f) >= 1) {
1143 f /= Math.abs(f);
1144 }
1145
Adam Cohene0f66b52010-11-23 15:06:07 -08001146 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001147 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001148 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001149 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001150 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001151 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001152 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001153 }
1154 invalidate();
1155 }
1156
Adam Cohenb5ba0972011-09-07 18:02:31 -07001157 protected void overScroll(float amount) {
1158 dampedOverScroll(amount);
1159 }
1160
Michael Jurkac5b262c2011-01-12 20:24:50 -08001161 protected float maxOverScroll() {
1162 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001163 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001164 float f = 1.0f;
1165 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1166 return OVERSCROLL_DAMP_FACTOR * f;
1167 }
1168
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 @Override
1170 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001171 // Skip touch handling if there are no pages to swipe
1172 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1173
Michael Jurkab8f06722010-10-10 15:58:46 -07001174 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001175
1176 final int action = ev.getAction();
1177
1178 switch (action & MotionEvent.ACTION_MASK) {
1179 case MotionEvent.ACTION_DOWN:
1180 /*
1181 * If being flinged and user touches, stop the fling. isFinished
1182 * will be false if being flinged.
1183 */
1184 if (!mScroller.isFinished()) {
1185 mScroller.abortAnimation();
1186 }
1187
1188 // Remember where the motion event started
1189 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001190 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001191 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001192 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001193 if (mTouchState == TOUCH_STATE_SCROLLING) {
1194 pageBeginMoving();
1195 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 break;
1197
1198 case MotionEvent.ACTION_MOVE:
1199 if (mTouchState == TOUCH_STATE_SCROLLING) {
1200 // Scroll to follow the motion event
1201 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1202 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001203 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001204
Adam Cohenaefd4e12011-02-14 16:39:38 -08001205 mTotalMotionX += Math.abs(deltaX);
1206
Winson Chungc0844aa2011-02-02 15:25:58 -08001207 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1208 // keep the remainder because we are actually testing if we've moved from the last
1209 // scrolled position (which is discrete).
1210 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001211 mTouchX += deltaX;
1212 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1213 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001214 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001216 } else {
1217 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001219 mLastMotionX = x;
1220 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 } else {
1222 awakenScrollBars();
1223 }
Adam Cohen564976a2010-10-13 18:52:07 -07001224 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 determineScrollingStart(ev);
1226 }
1227 break;
1228
1229 case MotionEvent.ACTION_UP:
1230 if (mTouchState == TOUCH_STATE_SCROLLING) {
1231 final int activePointerId = mActivePointerId;
1232 final int pointerIndex = ev.findPointerIndex(activePointerId);
1233 final float x = ev.getX(pointerIndex);
1234 final VelocityTracker velocityTracker = mVelocityTracker;
1235 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1236 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001237 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001238 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1239 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1240 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001241
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001242 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1243
Adam Cohen00481b32011-11-18 12:03:48 -08001244 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001245 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001246
Adam Cohenaefd4e12011-02-14 16:39:38 -08001247 // In the case that the page is moved far to one direction and then is flung
1248 // in the opposite direction, we use a threshold to determine whether we should
1249 // just return to the starting page, or if we should skip one further.
1250 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001251 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001252 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001253 returnToOriginalPage = true;
1254 }
1255
Adam Cohenaefd4e12011-02-14 16:39:38 -08001256 int finalPage;
1257 // We give flings precedence over large moves, which is why we short-circuit our
1258 // test for a large move if a fling has been registered. That is, a large
1259 // move to the left and fling to the right will register as a fling to the right.
1260 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1261 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1262 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1263 snapToPageWithVelocity(finalPage, velocityX);
1264 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1265 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001266 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001267 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1268 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 } else {
1270 snapToDestination();
1271 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001272 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001273 // at this point we have not moved beyond the touch slop
1274 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1275 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001276 int nextPage = Math.max(0, mCurrentPage - 1);
1277 if (nextPage != mCurrentPage) {
1278 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 } else {
1280 snapToDestination();
1281 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001282 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 // at this point we have not moved beyond the touch slop
1284 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1285 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001286 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1287 if (nextPage != mCurrentPage) {
1288 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001289 } else {
1290 snapToDestination();
1291 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001292 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001293 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001294 }
1295 mTouchState = TOUCH_STATE_REST;
1296 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001297 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 break;
1299
1300 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001301 if (mTouchState == TOUCH_STATE_SCROLLING) {
1302 snapToDestination();
1303 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 mTouchState = TOUCH_STATE_REST;
1305 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001306 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001307 break;
1308
1309 case MotionEvent.ACTION_POINTER_UP:
1310 onSecondaryPointerUp(ev);
1311 break;
1312 }
1313
1314 return true;
1315 }
1316
Winson Chung185d7162011-02-28 13:47:29 -08001317 @Override
1318 public boolean onGenericMotionEvent(MotionEvent event) {
1319 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1320 switch (event.getAction()) {
1321 case MotionEvent.ACTION_SCROLL: {
1322 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1323 final float vscroll;
1324 final float hscroll;
1325 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1326 vscroll = 0;
1327 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1328 } else {
1329 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1330 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1331 }
1332 if (hscroll != 0 || vscroll != 0) {
1333 if (hscroll > 0 || vscroll > 0) {
1334 scrollRight();
1335 } else {
1336 scrollLeft();
1337 }
1338 return true;
1339 }
1340 }
1341 }
1342 }
1343 return super.onGenericMotionEvent(event);
1344 }
1345
Michael Jurkab8f06722010-10-10 15:58:46 -07001346 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1347 if (mVelocityTracker == null) {
1348 mVelocityTracker = VelocityTracker.obtain();
1349 }
1350 mVelocityTracker.addMovement(ev);
1351 }
1352
1353 private void releaseVelocityTracker() {
1354 if (mVelocityTracker != null) {
1355 mVelocityTracker.recycle();
1356 mVelocityTracker = null;
1357 }
1358 }
1359
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 private void onSecondaryPointerUp(MotionEvent ev) {
1361 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1362 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1363 final int pointerId = ev.getPointerId(pointerIndex);
1364 if (pointerId == mActivePointerId) {
1365 // This was our active pointer going up. Choose a new
1366 // active pointer and adjust accordingly.
1367 // TODO: Make this decision more intelligent.
1368 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1369 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1370 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001371 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 mActivePointerId = ev.getPointerId(newPointerIndex);
1373 if (mVelocityTracker != null) {
1374 mVelocityTracker.clear();
1375 }
1376 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001377 }
1378
Michael Jurkad771c962011-08-09 15:00:48 -07001379 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001380
1381 @Override
1382 public void requestChildFocus(View child, View focused) {
1383 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001384 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001385 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001386 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001387 }
1388 }
1389
Winson Chunge3193b92010-09-10 11:44:42 -07001390 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1391 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001392 int left;
1393 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001394 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001395 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001396 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001397 if (left <= relativeOffset && relativeOffset <= right) {
1398 return i;
1399 }
Winson Chunge3193b92010-09-10 11:44:42 -07001400 }
1401 return -1;
1402 }
1403
Winson Chung1908d072011-02-24 18:09:44 -08001404 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001405 // This functions are called enough times that it actually makes a difference in the
1406 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001407 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001408 final int minWidth = mMinimumWidth;
1409 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001410 }
1411
Adam Cohend19d3ca2010-09-15 14:43:42 -07001412 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001413 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001415 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 final int childCount = getChildCount();
1417 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001418 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001419 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 int halfChildWidth = (childWidth / 2);
1421 int childCenter = getChildOffset(i) + halfChildWidth;
1422 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1423 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1424 minDistanceFromScreenCenter = distanceFromScreenCenter;
1425 minDistanceFromScreenCenterIndex = i;
1426 }
1427 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001428 return minDistanceFromScreenCenterIndex;
1429 }
1430
1431 protected void snapToDestination() {
1432 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 }
1434
Adam Cohene0f66b52010-11-23 15:06:07 -08001435 private static class ScrollInterpolator implements Interpolator {
1436 public ScrollInterpolator() {
1437 }
1438
1439 public float getInterpolation(float t) {
1440 t -= 1.0f;
1441 return t*t*t*t*t + 1;
1442 }
1443 }
1444
1445 // We want the duration of the page snap animation to be influenced by the distance that
1446 // the screen has to travel, however, we don't want this duration to be effected in a
1447 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1448 // of travel has on the overall snap duration.
1449 float distanceInfluenceForSnapDuration(float f) {
1450 f -= 0.5f; // center the values about 0.
1451 f *= 0.3f * Math.PI / 2.0f;
1452 return (float) Math.sin(f);
1453 }
1454
Michael Jurka0142d492010-08-25 17:46:15 -07001455 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001456 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1457 int halfScreenSize = getMeasuredWidth() / 2;
1458
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1460 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1461 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001462 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1463 int delta = newX - mUnboundedScrollX;
1464 int duration = 0;
1465
Adam Cohen265b9a62011-12-07 14:37:18 -08001466 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001467 // If the velocity is low enough, then treat this more as an automatic page advance
1468 // as opposed to an apparent physical response to flinging
1469 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1470 return;
1471 }
1472
1473 // Here we compute a "distance" that will be used in the computation of the overall
1474 // snap duration. This is a function of the actual distance that needs to be traveled;
1475 // we keep this value close to half screen size in order to reduce the variance in snap
1476 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001477 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001478 float distance = halfScreenSize + halfScreenSize *
1479 distanceInfluenceForSnapDuration(distanceRatio);
1480
1481 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001482 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001483
1484 // we want the page's snap velocity to approximately match the velocity at which the
1485 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001486 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1487 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001488
1489 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001490 }
1491
1492 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001493 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 }
1495
Michael Jurka0142d492010-08-25 17:46:15 -07001496 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001497 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001498
Winson Chung785d2eb2011-04-14 16:08:02 -07001499 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1500 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1501 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001502 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001503 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001504 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001505 snapToPage(whichPage, delta, duration);
1506 }
1507
1508 protected void snapToPage(int whichPage, int delta, int duration) {
1509 mNextPage = whichPage;
1510
1511 View focusedChild = getFocusedChild();
1512 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001513 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001514 focusedChild.clearFocus();
1515 }
1516
1517 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 awakenScrollBars(duration);
1519 if (duration == 0) {
1520 duration = Math.abs(delta);
1521 }
1522
1523 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001524 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001525
Winson Chungb44b5242011-06-13 11:32:14 -07001526 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1527 // loading associated pages until the scroll settles
1528 if (mDeferScrollUpdate) {
1529 loadAssociatedPages(mNextPage);
1530 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001531 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001532 }
Michael Jurka0142d492010-08-25 17:46:15 -07001533 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 invalidate();
1535 }
1536
Winson Chung321e9ee2010-08-09 13:37:56 -07001537 public void scrollLeft() {
1538 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001539 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001541 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001542 }
1543 }
1544
1545 public void scrollRight() {
1546 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001547 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001548 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001549 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 }
1551 }
1552
Winson Chung86f77532010-08-24 11:08:22 -07001553 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001554 int result = -1;
1555 if (v != null) {
1556 ViewParent vp = v.getParent();
1557 int count = getChildCount();
1558 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001559 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001560 return i;
1561 }
1562 }
1563 }
1564 return result;
1565 }
1566
1567 /**
1568 * @return True is long presses are still allowed for the current touch
1569 */
1570 public boolean allowLongPress() {
1571 return mAllowLongPress;
1572 }
1573
Michael Jurka0142d492010-08-25 17:46:15 -07001574 /**
1575 * Set true to allow long-press events to be triggered, usually checked by
1576 * {@link Launcher} to accept or block dpad-initiated long-presses.
1577 */
1578 public void setAllowLongPress(boolean allowLongPress) {
1579 mAllowLongPress = allowLongPress;
1580 }
1581
Winson Chung321e9ee2010-08-09 13:37:56 -07001582 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001583 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001584
1585 SavedState(Parcelable superState) {
1586 super(superState);
1587 }
1588
1589 private SavedState(Parcel in) {
1590 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001591 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001592 }
1593
1594 @Override
1595 public void writeToParcel(Parcel out, int flags) {
1596 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001597 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 }
1599
1600 public static final Parcelable.Creator<SavedState> CREATOR =
1601 new Parcelable.Creator<SavedState>() {
1602 public SavedState createFromParcel(Parcel in) {
1603 return new SavedState(in);
1604 }
1605
1606 public SavedState[] newArray(int size) {
1607 return new SavedState[size];
1608 }
1609 };
1610 }
1611
Winson Chungf314b0e2011-08-16 11:54:27 -07001612 protected void loadAssociatedPages(int page) {
1613 loadAssociatedPages(page, false);
1614 }
1615 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001616 if (mContentIsRefreshable) {
1617 final int count = getChildCount();
1618 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001619 int lowerPageBound = getAssociatedLowerPageBound(page);
1620 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001621 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1622 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001623 // First, clear any pages that should no longer be loaded
1624 for (int i = 0; i < count; ++i) {
1625 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001626 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001627 if (layout.getPageChildCount() > 0) {
1628 layout.removeAllViewsOnPage();
1629 }
1630 mDirtyPageContent.set(i, true);
1631 }
1632 }
1633 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001634 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001635 if ((i != page) && immediateAndOnly) {
1636 continue;
1637 }
Michael Jurka0142d492010-08-25 17:46:15 -07001638 if (lowerPageBound <= i && i <= upperPageBound) {
1639 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001640 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001641 mDirtyPageContent.set(i, false);
1642 }
Winson Chung86f77532010-08-24 11:08:22 -07001643 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001644 }
1645 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001646 }
1647 }
1648
Winson Chunge3193b92010-09-10 11:44:42 -07001649 protected int getAssociatedLowerPageBound(int page) {
1650 return Math.max(0, page - 1);
1651 }
1652 protected int getAssociatedUpperPageBound(int page) {
1653 final int count = getChildCount();
1654 return Math.min(page + 1, count - 1);
1655 }
1656
Winson Chung86f77532010-08-24 11:08:22 -07001657 /**
1658 * This method is called ONLY to synchronize the number of pages that the paged view has.
1659 * To actually fill the pages with information, implement syncPageItems() below. It is
1660 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1661 * and therefore, individual page items do not need to be updated in this method.
1662 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001663 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001664
1665 /**
1666 * This method is called to synchronize the items that are on a particular page. If views on
1667 * the page can be reused, then they should be updated within this method.
1668 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001669 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001670
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001671 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001672 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001673 }
1674 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001675 invalidatePageData(currentPage, false);
1676 }
1677 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001678 if (!mIsDataReady) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001679 if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 invalidatePageData page: " + currentPage + " not data ready");
Winson Chungf0ea4d32011-06-06 14:27:16 -07001680 return;
1681 }
1682
Michael Jurka0142d492010-08-25 17:46:15 -07001683 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001684 // Force all scrolling-related behavior to end
1685 mScroller.forceFinished(true);
1686 mNextPage = INVALID_PAGE;
1687
Michael Jurka0142d492010-08-25 17:46:15 -07001688 // Update all the pages
1689 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001690
Winson Chung5a808352011-06-27 19:08:49 -07001691 // We must force a measure after we've loaded the pages to update the content width and
1692 // to determine the full scroll width
1693 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1694 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1695
1696 // Set a new page as the current page if necessary
1697 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001698 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001699 }
1700
Michael Jurka0142d492010-08-25 17:46:15 -07001701 // Mark each of the pages as dirty
1702 final int count = getChildCount();
1703 mDirtyPageContent.clear();
1704 for (int i = 0; i < count; ++i) {
1705 mDirtyPageContent.add(true);
1706 }
1707
1708 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001709 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001710 requestLayout();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001711
1712 if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 invalidatePageData page: " + currentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001713 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001714 }
Winson Chung007c6982011-06-14 13:27:53 -07001715
Michael Jurkaafaa0502011-12-13 18:22:50 -08001716 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001717 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1718 // found
1719 if (mHasScrollIndicator && mScrollIndicator == null) {
1720 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001721 if (parent != null) {
1722 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1723 mHasScrollIndicator = mScrollIndicator != null;
1724 if (mHasScrollIndicator) {
1725 mScrollIndicator.setVisibility(View.VISIBLE);
1726 }
Winson Chung007c6982011-06-14 13:27:53 -07001727 }
1728 }
1729 return mScrollIndicator;
1730 }
1731
1732 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001733 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001734 }
1735
Winson Chung5a808352011-06-27 19:08:49 -07001736 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1737 @Override
1738 public void run() {
1739 hideScrollingIndicator(false);
1740 }
1741 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001742 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001743 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001744 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001745 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001746 }
1747
Michael Jurka430e8a52011-08-08 15:52:14 -07001748 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001749 mShouldShowScrollIndicator = true;
1750 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001751 if (getChildCount() <= 1) return;
1752 if (!isScrollingIndicatorEnabled()) return;
1753
Michael Jurkabed61d22012-02-14 22:51:29 -08001754 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001755 getScrollingIndicator();
1756 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001757 // Fade the indicator in
1758 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001759 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001760 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001761 if (immediately) {
1762 mScrollIndicator.setAlpha(1f);
1763 } else {
1764 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1765 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1766 mScrollIndicatorAnimator.start();
1767 }
Winson Chung007c6982011-06-14 13:27:53 -07001768 }
1769 }
1770
Adam Cohen21b41102011-11-01 17:29:52 -07001771 protected void cancelScrollingIndicatorAnimations() {
1772 if (mScrollIndicatorAnimator != null) {
1773 mScrollIndicatorAnimator.cancel();
1774 }
1775 }
1776
Winson Chung007c6982011-06-14 13:27:53 -07001777 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001778 if (getChildCount() <= 1) return;
1779 if (!isScrollingIndicatorEnabled()) return;
1780
1781 getScrollingIndicator();
1782 if (mScrollIndicator != null) {
1783 // Fade the indicator out
1784 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001785 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001786 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001787 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001788 mScrollIndicator.setAlpha(0f);
1789 } else {
1790 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1791 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1792 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1793 private boolean cancelled = false;
1794 @Override
1795 public void onAnimationCancel(android.animation.Animator animation) {
1796 cancelled = true;
1797 }
1798 @Override
1799 public void onAnimationEnd(Animator animation) {
1800 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001801 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001802 }
1803 }
1804 });
1805 mScrollIndicatorAnimator.start();
1806 }
Winson Chung007c6982011-06-14 13:27:53 -07001807 }
1808 }
1809
Winson Chung32174c82011-07-19 15:47:55 -07001810 /**
1811 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1812 * fill its space on the track or not.
1813 */
1814 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001815 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001816 }
1817
Winson Chung007c6982011-06-14 13:27:53 -07001818 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001819 if (getChildCount() <= 1) return;
1820 if (!isScrollingIndicatorEnabled()) return;
1821
1822 getScrollingIndicator();
1823 if (mScrollIndicator != null) {
1824 updateScrollingIndicatorPosition();
1825 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001826 if (mShouldShowScrollIndicator) {
1827 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1828 }
Winson Chung007c6982011-06-14 13:27:53 -07001829 }
1830
Winson Chung007c6982011-06-14 13:27:53 -07001831 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001832 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001833 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001834 int numPages = getChildCount();
1835 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001836 int lastChildIndex = Math.max(0, getChildCount() - 1);
1837 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001838 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001839 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1840 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001841
Winson Chungae890b82011-09-13 18:08:54 -07001842 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001843 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001844 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001845 if (hasElasticScrollIndicator()) {
1846 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1847 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1848 mScrollIndicator.requestLayout();
1849 }
1850 } else {
1851 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1852 indicatorPos += indicatorCenterOffset;
1853 }
Winson Chung007c6982011-06-14 13:27:53 -07001854 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001855 }
1856
Winson Chung3ac74c52011-06-30 17:39:37 -07001857 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001858 }
1859
1860 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001861 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001862
1863 /* Accessibility */
1864 @Override
1865 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1866 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001867 info.setScrollable(getPageCount() > 1);
1868 if (getCurrentPage() < getPageCount() - 1) {
1869 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1870 }
1871 if (getCurrentPage() > 0) {
1872 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1873 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001874 }
1875
1876 @Override
1877 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1878 super.onInitializeAccessibilityEvent(event);
1879 event.setScrollable(true);
1880 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1881 event.setFromIndex(mCurrentPage);
1882 event.setToIndex(mCurrentPage);
1883 event.setItemCount(getChildCount());
1884 }
1885 }
1886
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001887 @Override
1888 public boolean performAccessibilityAction(int action, Bundle arguments) {
1889 if (super.performAccessibilityAction(action, arguments)) {
1890 return true;
1891 }
1892 switch (action) {
1893 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1894 if (getCurrentPage() < getPageCount() - 1) {
1895 scrollRight();
1896 return true;
1897 }
1898 } break;
1899 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1900 if (getCurrentPage() > 0) {
1901 scrollLeft();
1902 return true;
1903 }
1904 } break;
1905 }
1906 return false;
1907 }
1908
Winson Chung6a0f57d2011-06-29 20:10:49 -07001909 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001910 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001911 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001912 }
Winson Chungd11265e2011-08-30 13:37:23 -07001913
1914 @Override
1915 public boolean onHoverEvent(android.view.MotionEvent event) {
1916 return true;
1917 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001918}