blob: 494534c16a767723a2f202c9b1754d36e2cfcb1a [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;
Winson Chungbb6f6a52011-07-25 17:55:44 -070021import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.graphics.Canvas;
25import android.graphics.Rect;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070026import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080031import android.view.InputDevice;
32import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070039import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070040import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import java.util.ArrayList;
48
Winson Chung321e9ee2010-08-09 13:37:56 -070049/**
50 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070051 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070052 */
Michael Jurka8b805b12012-04-18 14:23:14 -070053public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070054 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070055 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chung86f77532010-08-24 11:08:22 -070058 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070059 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Winson Chungf0c6ae02012-03-21 16:10:31 -070061 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Chet Haase97687ef2012-10-25 16:01:11 -070062 protected static final int MAX_PAGE_SNAP_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070063 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;
Chet Haasebc2f0822012-10-26 17:59:53 -0700185 private boolean mScrollingPaused = false;
Winson Chung007c6982011-06-14 13:27:53 -0700186
Winson Chungb44b5242011-06-13 11:32:14 -0700187 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700188 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700189
Winson Chung86f77532010-08-24 11:08:22 -0700190 public interface PageSwitchListener {
191 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 }
193
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 public PagedView(Context context) {
195 this(context, null);
196 }
197
198 public PagedView(Context context, AttributeSet attrs) {
199 this(context, attrs, 0);
200 }
201
202 public PagedView(Context context, AttributeSet attrs, int defStyle) {
203 super(context, attrs, defStyle);
204
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 TypedArray a = context.obtainStyledAttributes(attrs,
206 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800207 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700214 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800215 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700216 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700218 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700220 mScrollIndicatorPaddingLeft =
221 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
222 mScrollIndicatorPaddingRight =
223 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700224 a.recycle();
225
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700227 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
230 /**
231 * Initializes various states for this workspace.
232 */
Michael Jurka0142d492010-08-25 17:46:15 -0700233 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700234 mDirtyPageContent = new ArrayList<Boolean>();
235 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800236 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700237 mCurrentPage = 0;
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 Chung52aee602013-01-30 12:01:02 -0800260 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
261 */
262 public boolean isLayoutRtl() {
263 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
264 }
265
266 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700267 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
268 * out pages.
269 */
270 protected void setDataIsReady() {
271 mIsDataReady = true;
272 }
273 protected boolean isDataReady() {
274 return mIsDataReady;
275 }
276
277 /**
Winson Chung86f77532010-08-24 11:08:22 -0700278 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 *
Winson Chung86f77532010-08-24 11:08:22 -0700280 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 */
Winson Chung86f77532010-08-24 11:08:22 -0700282 int getCurrentPage() {
283 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700284 }
Winson Chung360e63f2012-04-27 13:48:05 -0700285 int getNextPage() {
286 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
287 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700288
Winson Chung86f77532010-08-24 11:08:22 -0700289 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 return getChildCount();
291 }
292
Winson Chung86f77532010-08-24 11:08:22 -0700293 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 return getChildAt(index);
295 }
296
Adam Cohenae4f1552011-10-20 00:15:42 -0700297 protected int indexToPage(int index) {
298 return index;
299 }
300
Winson Chung321e9ee2010-08-09 13:37:56 -0700301 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800302 * Updates the scroll of the current page immediately to its final scroll position. We use this
303 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
304 * the previous tab page.
305 */
306 protected void updateCurrentPageScroll() {
Winson Chung1ef21232012-11-26 14:08:38 -0800307 // If the current page is invalid, just reset the scroll position to zero
308 int newX = 0;
309 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
310 int offset = getChildOffset(mCurrentPage);
311 int relOffset = getRelativeChildOffset(mCurrentPage);
312 newX = offset - relOffset;
313 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800314 scrollTo(newX, 0);
315 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800316 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800317 }
318
319 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700320 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
321 * ends, {@link #resumeScrolling()} should be called, along with
322 * {@link #updateCurrentPageScroll()} to correctly set the final state and re-enable scrolling.
323 */
324 void pauseScrolling() {
325 mScroller.forceFinished(true);
326 cancelScrollingIndicatorAnimations();
327 mScrollingPaused = true;
328 }
329
330 /**
331 * Enables scrolling again.
332 * @see #pauseScrolling()
333 */
334 void resumeScrolling() {
335 mScrollingPaused = false;
336 }
337 /**
Winson Chung86f77532010-08-24 11:08:22 -0700338 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 */
Winson Chung86f77532010-08-24 11:08:22 -0700340 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800341 if (!mScroller.isFinished()) {
342 mScroller.abortAnimation();
343 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800344 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
345 // the default
346 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800347 return;
348 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700349
Winson Chungbfeac062012-06-06 15:56:08 -0700350
Winson Chung86f77532010-08-24 11:08:22 -0700351 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800352 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700353 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700354 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800355 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
357
Michael Jurka0142d492010-08-25 17:46:15 -0700358 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700359 if (mPageSwitchListener != null) {
360 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 }
362 }
363
Michael Jurkace7e05f2011-02-01 22:02:35 -0800364 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700365 if (!mIsPageMoving) {
366 mIsPageMoving = true;
367 onPageBeginMoving();
368 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700369 }
370
Michael Jurkace7e05f2011-02-01 22:02:35 -0800371 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700372 if (mIsPageMoving) {
373 mIsPageMoving = false;
374 onPageEndMoving();
375 }
Michael Jurka0142d492010-08-25 17:46:15 -0700376 }
377
Adam Cohen26976d92011-03-22 15:33:33 -0700378 protected boolean isPageMoving() {
379 return mIsPageMoving;
380 }
381
Michael Jurka0142d492010-08-25 17:46:15 -0700382 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700383 protected void onPageBeginMoving() {
384 }
385
386 // a method that subclasses can override to add behavior
387 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700388 }
389
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 /**
Winson Chung86f77532010-08-24 11:08:22 -0700391 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 *
393 * @param l The listener used to respond to long clicks.
394 */
395 @Override
396 public void setOnLongClickListener(OnLongClickListener l) {
397 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700398 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700400 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 }
402 }
403
404 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800405 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700406 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800407 }
408
409 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700410 public void scrollTo(int x, int y) {
Winson Chung52aee602013-01-30 12:01:02 -0800411 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800412 mUnboundedScrollX = x;
413
Winson Chung52aee602013-01-30 12:01:02 -0800414 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
415 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
416 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800417 super.scrollTo(0, y);
418 if (mAllowOverScroll) {
Winson Chung52aee602013-01-30 12:01:02 -0800419 if (isRtl) {
420 overScroll(x - mMaxScrollX);
421 } else {
422 overScroll(x);
423 }
Adam Cohen68d73932010-11-15 10:50:58 -0800424 }
Winson Chung52aee602013-01-30 12:01:02 -0800425 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800426 super.scrollTo(mMaxScrollX, y);
427 if (mAllowOverScroll) {
Winson Chung52aee602013-01-30 12:01:02 -0800428 if (isRtl) {
429 overScroll(x);
430 } else {
431 overScroll(x - mMaxScrollX);
432 }
Adam Cohen68d73932010-11-15 10:50:58 -0800433 }
434 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800435 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800436 super.scrollTo(x, y);
437 }
438
Michael Jurka0142d492010-08-25 17:46:15 -0700439 mTouchX = x;
440 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
441 }
442
443 // we moved this functionality to a helper function so SmoothPagedView can reuse it
444 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700446 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700447 if (getScrollX() != mScroller.getCurrX()
448 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700449 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700450 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
451 }
Michael Jurka0142d492010-08-25 17:46:15 -0700452 invalidate();
453 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700454 } else if (mNextPage != INVALID_PAGE) {
455 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700456 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700457 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700458
459 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700460 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700461 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700462 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700463 }
464
Adam Cohen73aa9752010-11-24 16:26:58 -0800465 // We don't want to trigger a page end moving unless the page has settled
466 // and the user has stopped scrolling
467 if (mTouchState == TOUCH_STATE_REST) {
468 pageEndMoving();
469 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700470
471 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700472 AccessibilityManager accessibilityManager = (AccessibilityManager)
473 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
474 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700475 AccessibilityEvent ev =
476 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
477 ev.getText().add(getCurrentPageDescription());
478 sendAccessibilityEventUnchecked(ev);
479 }
Michael Jurka0142d492010-08-25 17:46:15 -0700480 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 }
Michael Jurka0142d492010-08-25 17:46:15 -0700482 return false;
483 }
484
485 @Override
486 public void computeScroll() {
487 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 }
489
490 @Override
491 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700492 if (!mIsDataReady) {
493 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
494 return;
495 }
496
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
498 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700499 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
500 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 if (widthMode != MeasureSpec.EXACTLY) {
502 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
503 }
504
Winson Chung8aad6102012-05-11 16:27:49 -0700505 // Return early if we aren't given a proper dimension
506 if (widthSize <= 0 || heightSize <= 0) {
507 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
508 return;
509 }
510
Adam Lesinski6b879f02010-11-04 16:15:23 -0700511 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
512 * of the All apps view on XLarge displays to not take up more space then it needs. Width
513 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
514 * each page to have the same width.
515 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700516 int maxChildHeight = 0;
517
Michael Jurka8b805b12012-04-18 14:23:14 -0700518 final int verticalPadding = getPaddingTop() + getPaddingBottom();
519 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700520
Michael Jurka36fcb742011-04-06 17:08:58 -0700521
Winson Chung321e9ee2010-08-09 13:37:56 -0700522 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700523 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700524 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 final int childCount = getChildCount();
526 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700527 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700528 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700529 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
530
531 int childWidthMode;
532 if (lp.width == LayoutParams.WRAP_CONTENT) {
533 childWidthMode = MeasureSpec.AT_MOST;
534 } else {
535 childWidthMode = MeasureSpec.EXACTLY;
536 }
537
538 int childHeightMode;
539 if (lp.height == LayoutParams.WRAP_CONTENT) {
540 childHeightMode = MeasureSpec.AT_MOST;
541 } else {
542 childHeightMode = MeasureSpec.EXACTLY;
543 }
544
545 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700546 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700547 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700548 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700549
550 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700551 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700552 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
553 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700554 }
555
556 if (heightMode == MeasureSpec.AT_MOST) {
557 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 }
Winson Chungae890b82011-09-13 18:08:54 -0700559
Winson Chungae890b82011-09-13 18:08:54 -0700560 setMeasuredDimension(widthSize, heightSize);
561
Winson Chung8aad6102012-05-11 16:27:49 -0700562 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
563 // We also wait until we set the measured dimensions before flushing the cache as well, to
564 // ensure that the cache is filled with good values.
565 invalidateCachedOffsets();
566
Winson Chunga128a7b2012-04-30 15:23:15 -0700567 if (childCount > 0) {
568 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
569 + getChildWidth(0));
570
571 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700572 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700573 // The gap between pages in the PagedView should be equal to the gap from the page
574 // to the edge of the screen (so it is not visible in the current screen). To
575 // account for unequal padding on each side of the paged view, we take the maximum
576 // of the left/right gap and use that as the gap between each page.
577 int offset = getRelativeChildOffset(0);
578 int spacing = Math.max(offset, widthSize - offset -
579 getChildAt(0).getMeasuredWidth());
580 setPageSpacing(spacing);
581 }
582 }
583
Adam Cohen25b29952011-11-02 14:49:06 -0700584 updateScrollingIndicatorPosition();
585
Adam Cohenfaa28302010-11-19 12:02:18 -0800586 if (childCount > 0) {
Winson Chung52aee602013-01-30 12:01:02 -0800587 final int index = isLayoutRtl() ? 0 : childCount - 1;
588 mMaxScrollX = getChildOffset(index) - getRelativeChildOffset(index);
Adam Cohenfaa28302010-11-19 12:02:18 -0800589 } else {
590 mMaxScrollX = 0;
591 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700592 }
593
Michael Jurka8c920dd2011-01-20 14:16:56 -0800594 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800595 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700596 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800597
Michael Jurka8c920dd2011-01-20 14:16:56 -0800598 final int pageCount = getChildCount();
599 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700600 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800601 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800602 }
603 setCurrentPage(newCurrentPage);
604 }
605
Michael Jurka8c920dd2011-01-20 14:16:56 -0800606 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
607 // 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 -0800608 // tightens the layout accordingly
609 public void setLayoutScale(float childrenScale) {
610 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700611 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800612
613 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
614 int childCount = getChildCount();
615 float childrenX[] = new float[childCount];
616 float childrenY[] = new float[childCount];
617 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700618 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800619 childrenX[i] = child.getX();
620 childrenY[i] = child.getY();
621 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700622 // Trigger a full re-layout (never just call onLayout directly!)
623 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
624 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
625 requestLayout();
626 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800628 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700629 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800630 child.setX(childrenX[i]);
631 child.setY(childrenY[i]);
632 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700633
Michael Jurkad3ef3062010-11-23 16:23:58 -0800634 // Also, the page offset has changed (since the pages are now smaller);
635 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800636 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800637 }
638
Adam Cohen60b07122011-11-14 17:26:06 -0800639 public void setPageSpacing(int pageSpacing) {
640 mPageSpacing = pageSpacing;
641 invalidateCachedOffsets();
642 }
643
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700645 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700646 if (!mIsDataReady) {
647 return;
648 }
649
Winson Chung785d2eb2011-04-14 16:08:02 -0700650 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700651 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700652 final int childCount = getChildCount();
Winson Chung52aee602013-01-30 12:01:02 -0800653 final boolean isRtl = isLayoutRtl();
Winson Chung321e9ee2010-08-09 13:37:56 -0700654
Winson Chung52aee602013-01-30 12:01:02 -0800655 final int startIndex = isRtl ? childCount - 1 : 0;
656 final int endIndex = isRtl ? -1 : childCount;
657 final int delta = isRtl ? -1 : 1;
658 int childLeft = getRelativeChildOffset(startIndex);
659 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700660 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700661 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800662 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700663 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700664 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700665 if (mCenterPagesVertically) {
666 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
667 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800668
Winson Chung785d2eb2011-04-14 16:08:02 -0700669 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700670 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800671 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700672 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 }
674 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700675
676 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
677 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800678 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700679 setHorizontalScrollBarEnabled(true);
680 mFirstLayout = false;
681 }
Winson Chunge3193b92010-09-10 11:44:42 -0700682 }
683
Adam Cohenf34bab52010-09-30 14:11:56 -0700684 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700685 if (isScrollingIndicatorEnabled()) {
686 updateScrollingIndicator();
687 }
Michael Jurka869390b2012-05-06 15:55:19 -0700688 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
689
690 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700691 for (int i = 0; i < getChildCount(); i++) {
692 View child = getChildAt(i);
693 if (child != null) {
694 float scrollProgress = getScrollProgress(screenCenter, child, i);
695 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800696 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700697 }
698 }
699 invalidate();
700 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700701 }
702
Winson Chunge3193b92010-09-10 11:44:42 -0700703 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700704 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700705 // This ensures that when children are added, they get the correct transforms / alphas
706 // in accordance with any scroll effects.
707 mForceScreenScrolled = true;
708 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700709 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700710 }
711
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 @Override
713 public void onChildViewRemoved(View parent, View child) {
714 }
715
Adam Cohen73894962011-10-31 13:17:17 -0700716 protected void invalidateCachedOffsets() {
717 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700718 if (count == 0) {
719 mChildOffsets = null;
720 mChildRelativeOffsets = null;
721 mChildOffsetsWithLayoutScale = null;
722 return;
723 }
Adam Cohen73894962011-10-31 13:17:17 -0700724
725 mChildOffsets = new int[count];
726 mChildRelativeOffsets = new int[count];
727 mChildOffsetsWithLayoutScale = new int[count];
728 for (int i = 0; i < count; i++) {
729 mChildOffsets[i] = -1;
730 mChildRelativeOffsets[i] = -1;
731 mChildOffsetsWithLayoutScale[i] = -1;
732 }
733 }
734
735 protected int getChildOffset(int index) {
Winson Chung52aee602013-01-30 12:01:02 -0800736 final boolean isRtl = isLayoutRtl();
Adam Cohen73894962011-10-31 13:17:17 -0700737 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
738 mChildOffsets : mChildOffsetsWithLayoutScale;
739
740 if (childOffsets != null && childOffsets[index] != -1) {
741 return childOffsets[index];
742 } else {
743 if (getChildCount() == 0)
744 return 0;
745
Winson Chung52aee602013-01-30 12:01:02 -0800746 final int startIndex = isRtl ? getChildCount() - 1 : 0;
747 final int endIndex = isRtl ? index : index;
748 final int delta = isRtl ? -1 : 1;
749 int offset = getRelativeChildOffset(startIndex);
750 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen73894962011-10-31 13:17:17 -0700751 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
752 }
753 if (childOffsets != null) {
754 childOffsets[index] = offset;
755 }
756 return offset;
757 }
758 }
759
760 protected int getRelativeChildOffset(int index) {
761 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
762 return mChildRelativeOffsets[index];
763 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 final int padding = getPaddingLeft() + getPaddingRight();
765 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700766 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
767 if (mChildRelativeOffsets != null) {
768 mChildRelativeOffsets[index] = offset;
769 }
770 return offset;
771 }
772 }
773
Adam Cohen73894962011-10-31 13:17:17 -0700774 protected int getScaledMeasuredWidth(View child) {
775 // This functions are called enough times that it actually makes a difference in the
776 // profiler -- so just inline the max() here
777 final int measuredWidth = child.getMeasuredWidth();
778 final int minWidth = mMinimumWidth;
779 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
780 return (int) (maxWidth * mLayoutScale + 0.5f);
781 }
782
Michael Jurkadde558b2011-11-09 22:09:06 -0800783 protected void getVisiblePages(int[] range) {
Winson Chung52aee602013-01-30 12:01:02 -0800784 final boolean isRtl = isLayoutRtl();
Michael Jurka0142d492010-08-25 17:46:15 -0700785 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700786
Michael Jurkac4fb9172010-09-02 17:19:20 -0700787 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700788 final int screenWidth = getMeasuredWidth();
Winson Chung52aee602013-01-30 12:01:02 -0800789 int leftScreen = isRtl ? pageCount - 1 : 0;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700790 int rightScreen = 0;
Winson Chung52aee602013-01-30 12:01:02 -0800791 int endIndex = isRtl ? 0 : pageCount - 1;
792 int delta = isRtl ? -1 : 1;
Michael Jurka47f74742012-03-02 13:39:13 -0800793 View currPage = getPageAt(leftScreen);
Winson Chung52aee602013-01-30 12:01:02 -0800794 while (leftScreen != endIndex &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700795 currPage.getX() + currPage.getWidth() -
796 currPage.getPaddingRight() < getScrollX()) {
Winson Chung52aee602013-01-30 12:01:02 -0800797 leftScreen += delta;
Michael Jurka47f74742012-03-02 13:39:13 -0800798 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700799 }
800 rightScreen = leftScreen;
Winson Chung52aee602013-01-30 12:01:02 -0800801 currPage = getPageAt(rightScreen + delta);
802 while (rightScreen != endIndex &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700803 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Winson Chung52aee602013-01-30 12:01:02 -0800804 rightScreen += delta;
805 currPage = getPageAt(rightScreen + delta);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700806 }
Winson Chung52aee602013-01-30 12:01:02 -0800807 range[0] = Math.min(leftScreen, rightScreen);
808 range[1] = Math.max(leftScreen, rightScreen);
Michael Jurkadde558b2011-11-09 22:09:06 -0800809 } else {
810 range[0] = -1;
811 range[1] = -1;
812 }
813 }
814
Michael Jurka920d7f42012-05-14 16:29:55 -0700815 protected boolean shouldDrawChild(View child) {
816 return child.getAlpha() > 0;
817 }
818
Michael Jurkadde558b2011-11-09 22:09:06 -0800819 @Override
820 protected void dispatchDraw(Canvas canvas) {
821 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700822 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
823 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800824 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800825
826 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700827 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
828 // set it for the next frame
829 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800830 screenScrolled(screenCenter);
831 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800832 }
833
834 // Find out which screens are visible; as an optimization we only call draw on them
835 final int pageCount = getChildCount();
836 if (pageCount > 0) {
837 getVisiblePages(mTempVisiblePagesRange);
838 final int leftScreen = mTempVisiblePagesRange[0];
839 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800840 if (leftScreen != -1 && rightScreen != -1) {
841 final long drawingTime = getDrawingTime();
842 // Clip to the bounds
843 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700844 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
845 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700846
Michael Jurka80c69852011-12-16 14:16:32 -0800847 for (int i = getChildCount() - 1; i >= 0; i--) {
848 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700849 if (mForceDrawAllChildrenNextFrame ||
850 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800851 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -0800852 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800853 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700854 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800855 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700856 }
Michael Jurka0142d492010-08-25 17:46:15 -0700857 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 }
859
860 @Override
861 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700862 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700863 if (page != mCurrentPage || !mScroller.isFinished()) {
864 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 return true;
866 }
867 return false;
868 }
869
870 @Override
871 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700872 int focusablePage;
873 if (mNextPage != INVALID_PAGE) {
874 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700876 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 }
Winson Chung86f77532010-08-24 11:08:22 -0700878 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700880 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 }
882 return false;
883 }
884
885 @Override
886 public boolean dispatchUnhandledMove(View focused, int direction) {
Winson Chung52aee602013-01-30 12:01:02 -0800887 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700889 if (getCurrentPage() > 0) {
890 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 return true;
892 }
893 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700894 if (getCurrentPage() < getPageCount() - 1) {
895 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 return true;
897 }
898 }
899 return super.dispatchUnhandledMove(focused, direction);
900 }
901
902 @Override
903 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung52aee602013-01-30 12:01:02 -0800904 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700905 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700906 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700907 }
908 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700909 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700910 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 }
912 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700913 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700914 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 }
916 }
917 }
918
919 /**
920 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700921 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 *
Winson Chung86f77532010-08-24 11:08:22 -0700923 * This happens when live folders requery, and if they're off page, they
924 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 */
926 @Override
927 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700928 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 View v = focused;
930 while (true) {
931 if (v == current) {
932 super.focusableViewAvailable(focused);
933 return;
934 }
935 if (v == this) {
936 return;
937 }
938 ViewParent parent = v.getParent();
939 if (parent instanceof View) {
940 v = (View)v.getParent();
941 } else {
942 return;
943 }
944 }
945 }
946
947 /**
948 * {@inheritDoc}
949 */
950 @Override
951 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
952 if (disallowIntercept) {
953 // We need to make sure to cancel our long press if
954 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700955 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700956 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 }
958 super.requestDisallowInterceptTouchEvent(disallowIntercept);
959 }
960
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800961 /**
962 * Return true if a tap at (x, y) should trigger a flip to the previous page.
963 */
964 protected boolean hitsPreviousPage(float x, float y) {
Winson Chung52aee602013-01-30 12:01:02 -0800965 if (isLayoutRtl()) {
966 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
967 } else {
968 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
969 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800970 }
971
972 /**
973 * Return true if a tap at (x, y) should trigger a flip to the next page.
974 */
975 protected boolean hitsNextPage(float x, float y) {
Winson Chung52aee602013-01-30 12:01:02 -0800976 if (isLayoutRtl()) {
977 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
978 } else {
979 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
980 }
981
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800982 }
983
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 @Override
985 public boolean onInterceptTouchEvent(MotionEvent ev) {
986 /*
987 * This method JUST determines whether we want to intercept the motion.
988 * If we return true, onTouchEvent will be called and we do the actual
989 * scrolling there.
990 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800991 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700992
Winson Chung45e1d6e2010-11-09 17:19:49 -0800993 // Skip touch handling if there are no pages to swipe
994 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
995
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 /*
997 * Shortcut the most recurring case: the user is in the dragging
998 * state and he is moving his finger. We want to intercept this
999 * motion.
1000 */
1001 final int action = ev.getAction();
1002 if ((action == MotionEvent.ACTION_MOVE) &&
1003 (mTouchState == TOUCH_STATE_SCROLLING)) {
1004 return true;
1005 }
1006
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 switch (action & MotionEvent.ACTION_MASK) {
1008 case MotionEvent.ACTION_MOVE: {
1009 /*
1010 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1011 * whether the user has moved far enough from his original down touch.
1012 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001013 if (mActivePointerId != INVALID_POINTER) {
1014 determineScrollingStart(ev);
1015 break;
1016 }
1017 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1018 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1019 // i.e. fall through to the next case (don't break)
1020 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1021 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 }
1023
1024 case MotionEvent.ACTION_DOWN: {
1025 final float x = ev.getX();
1026 final float y = ev.getY();
1027 // Remember location of down touch
1028 mDownMotionX = x;
1029 mLastMotionX = x;
1030 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -08001031 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001032 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 mActivePointerId = ev.getPointerId(0);
1034 mAllowLongPress = true;
1035
1036 /*
1037 * If being flinged and user touches the screen, initiate drag;
1038 * otherwise don't. mScroller.isFinished should be false when
1039 * being flinged.
1040 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001041 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001042 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
1043 if (finishedScrolling) {
1044 mTouchState = TOUCH_STATE_REST;
1045 mScroller.abortAnimation();
1046 } else {
1047 mTouchState = TOUCH_STATE_SCROLLING;
1048 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001049
Winson Chung86f77532010-08-24 11:08:22 -07001050 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001052 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001054 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001056 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 mTouchState = TOUCH_STATE_NEXT_PAGE;
1058 }
1059 }
1060 }
1061 break;
1062 }
1063
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001065 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 mTouchState = TOUCH_STATE_REST;
1067 mAllowLongPress = false;
1068 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001069 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 break;
1071
1072 case MotionEvent.ACTION_POINTER_UP:
1073 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001074 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 break;
1076 }
1077
1078 /*
1079 * The only time we want to intercept motion events is if we are in the
1080 * drag mode.
1081 */
1082 return mTouchState != TOUCH_STATE_REST;
1083 }
1084
Adam Cohenf8d28232011-02-01 21:47:00 -08001085 protected void determineScrollingStart(MotionEvent ev) {
1086 determineScrollingStart(ev, 1.0f);
1087 }
1088
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 /*
1090 * Determines if we should change the touch state to start scrolling after the
1091 * user moves their touch point too far.
1092 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001093 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 /*
1095 * Locally do absolute value. mLastMotionX is set to the y value
1096 * of the down event.
1097 */
1098 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001099 if (pointerIndex == -1) {
1100 return;
1101 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 final float x = ev.getX(pointerIndex);
1103 final float y = ev.getY(pointerIndex);
1104 final int xDiff = (int) Math.abs(x - mLastMotionX);
1105 final int yDiff = (int) Math.abs(y - mLastMotionY);
1106
Adam Cohenf8d28232011-02-01 21:47:00 -08001107 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001108 boolean xPaged = xDiff > mPagingTouchSlop;
1109 boolean xMoved = xDiff > touchSlop;
1110 boolean yMoved = yDiff > touchSlop;
1111
Adam Cohenf8d28232011-02-01 21:47:00 -08001112 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001113 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001114 // Scroll if the user moved far enough along the X axis
1115 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001116 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001118 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001119 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001120 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1121 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 }
1123 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001124 cancelCurrentPageLongPress();
1125 }
1126 }
1127
1128 protected void cancelCurrentPageLongPress() {
1129 if (mAllowLongPress) {
1130 mAllowLongPress = false;
1131 // Try canceling the long press. It could also have been scheduled
1132 // by a distant descendant, so use the mAllowLongPress flag to block
1133 // everything
1134 final View currentPage = getPageAt(mCurrentPage);
1135 if (currentPage != null) {
1136 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 }
1138 }
1139 }
1140
Adam Cohenb5ba0972011-09-07 18:02:31 -07001141 protected float getScrollProgress(int screenCenter, View v, int page) {
1142 final int halfScreenSize = getMeasuredWidth() / 2;
1143
1144 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1145 int delta = screenCenter - (getChildOffset(page) -
1146 getRelativeChildOffset(page) + halfScreenSize);
1147
1148 float scrollProgress = delta / (totalDistance * 1.0f);
1149 scrollProgress = Math.min(scrollProgress, 1.0f);
1150 scrollProgress = Math.max(scrollProgress, -1.0f);
1151 return scrollProgress;
1152 }
1153
Adam Cohene0f66b52010-11-23 15:06:07 -08001154 // This curve determines how the effect of scrolling over the limits of the page dimishes
1155 // as the user pulls further and further from the bounds
1156 private float overScrollInfluenceCurve(float f) {
1157 f -= 1.0f;
1158 return f * f * f + 1.0f;
1159 }
1160
Adam Cohenb5ba0972011-09-07 18:02:31 -07001161 protected void acceleratedOverScroll(float amount) {
1162 int screenSize = getMeasuredWidth();
1163
1164 // We want to reach the max over scroll effect when the user has
1165 // over scrolled half the size of the screen
1166 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1167
1168 if (f == 0) return;
1169
1170 // Clamp this factor, f, to -1 < f < 1
1171 if (Math.abs(f) >= 1) {
1172 f /= Math.abs(f);
1173 }
1174
1175 int overScrollAmount = (int) Math.round(f * screenSize);
1176 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001177 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001178 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001179 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001180 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001181 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001182 }
1183 invalidate();
1184 }
1185
1186 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001187 int screenSize = getMeasuredWidth();
1188
1189 float f = (amount / screenSize);
1190
1191 if (f == 0) return;
1192 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1193
Adam Cohen7bfc9792011-01-28 13:52:37 -08001194 // Clamp this factor, f, to -1 < f < 1
1195 if (Math.abs(f) >= 1) {
1196 f /= Math.abs(f);
1197 }
1198
Adam Cohene0f66b52010-11-23 15:06:07 -08001199 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001200 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001201 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001202 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001203 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001204 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001205 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001206 }
1207 invalidate();
1208 }
1209
Adam Cohenb5ba0972011-09-07 18:02:31 -07001210 protected void overScroll(float amount) {
1211 dampedOverScroll(amount);
1212 }
1213
Michael Jurkac5b262c2011-01-12 20:24:50 -08001214 protected float maxOverScroll() {
1215 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001216 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001217 float f = 1.0f;
1218 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1219 return OVERSCROLL_DAMP_FACTOR * f;
1220 }
1221
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 @Override
1223 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001224 // Skip touch handling if there are no pages to swipe
1225 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1226
Michael Jurkab8f06722010-10-10 15:58:46 -07001227 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001228
1229 final int action = ev.getAction();
1230
1231 switch (action & MotionEvent.ACTION_MASK) {
1232 case MotionEvent.ACTION_DOWN:
1233 /*
1234 * If being flinged and user touches, stop the fling. isFinished
1235 * will be false if being flinged.
1236 */
1237 if (!mScroller.isFinished()) {
1238 mScroller.abortAnimation();
1239 }
1240
1241 // Remember where the motion event started
1242 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001243 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001244 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001246 if (mTouchState == TOUCH_STATE_SCROLLING) {
1247 pageBeginMoving();
1248 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 break;
1250
1251 case MotionEvent.ACTION_MOVE:
1252 if (mTouchState == TOUCH_STATE_SCROLLING) {
1253 // Scroll to follow the motion event
1254 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1255 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001256 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001257
Adam Cohenaefd4e12011-02-14 16:39:38 -08001258 mTotalMotionX += Math.abs(deltaX);
1259
Winson Chungc0844aa2011-02-02 15:25:58 -08001260 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1261 // keep the remainder because we are actually testing if we've moved from the last
1262 // scrolled position (which is discrete).
1263 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001264 mTouchX += deltaX;
1265 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1266 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001267 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001268 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001269 } else {
1270 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001272 mLastMotionX = x;
1273 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 } else {
1275 awakenScrollBars();
1276 }
Adam Cohen564976a2010-10-13 18:52:07 -07001277 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 determineScrollingStart(ev);
1279 }
1280 break;
1281
1282 case MotionEvent.ACTION_UP:
1283 if (mTouchState == TOUCH_STATE_SCROLLING) {
1284 final int activePointerId = mActivePointerId;
1285 final int pointerIndex = ev.findPointerIndex(activePointerId);
1286 final float x = ev.getX(pointerIndex);
1287 final VelocityTracker velocityTracker = mVelocityTracker;
1288 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1289 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001290 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001291 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1292 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1293 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001294
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001295 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1296
Adam Cohen00481b32011-11-18 12:03:48 -08001297 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001298 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001299
Adam Cohenaefd4e12011-02-14 16:39:38 -08001300 // In the case that the page is moved far to one direction and then is flung
1301 // in the opposite direction, we use a threshold to determine whether we should
1302 // just return to the starting page, or if we should skip one further.
1303 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001304 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001305 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001306 returnToOriginalPage = true;
1307 }
1308
Adam Cohenaefd4e12011-02-14 16:39:38 -08001309 int finalPage;
1310 // We give flings precedence over large moves, which is why we short-circuit our
1311 // test for a large move if a fling has been registered. That is, a large
1312 // move to the left and fling to the right will register as a fling to the right.
Winson Chung52aee602013-01-30 12:01:02 -08001313 final boolean isRtl = isLayoutRtl();
1314 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1315 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1316 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1317 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001318 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1319 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung52aee602013-01-30 12:01:02 -08001320 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1321 (isFling && isVelocityXLeft)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001322 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001323 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1324 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 } else {
1326 snapToDestination();
1327 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001328 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 // at this point we have not moved beyond the touch slop
1330 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1331 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001332 int nextPage = Math.max(0, mCurrentPage - 1);
1333 if (nextPage != mCurrentPage) {
1334 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001335 } else {
1336 snapToDestination();
1337 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001338 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 // at this point we have not moved beyond the touch slop
1340 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1341 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001342 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1343 if (nextPage != mCurrentPage) {
1344 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001345 } else {
1346 snapToDestination();
1347 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001348 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001349 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 }
1351 mTouchState = TOUCH_STATE_REST;
1352 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001353 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 break;
1355
1356 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001357 if (mTouchState == TOUCH_STATE_SCROLLING) {
1358 snapToDestination();
1359 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001360 mTouchState = TOUCH_STATE_REST;
1361 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001362 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001363 break;
1364
1365 case MotionEvent.ACTION_POINTER_UP:
1366 onSecondaryPointerUp(ev);
1367 break;
1368 }
1369
1370 return true;
1371 }
1372
Winson Chung185d7162011-02-28 13:47:29 -08001373 @Override
1374 public boolean onGenericMotionEvent(MotionEvent event) {
1375 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1376 switch (event.getAction()) {
1377 case MotionEvent.ACTION_SCROLL: {
1378 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1379 final float vscroll;
1380 final float hscroll;
1381 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1382 vscroll = 0;
1383 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1384 } else {
1385 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1386 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1387 }
1388 if (hscroll != 0 || vscroll != 0) {
Winson Chung52aee602013-01-30 12:01:02 -08001389 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
1390 : (hscroll > 0 || vscroll > 0);
1391 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001392 scrollRight();
1393 } else {
1394 scrollLeft();
1395 }
1396 return true;
1397 }
1398 }
1399 }
1400 }
1401 return super.onGenericMotionEvent(event);
1402 }
1403
Michael Jurkab8f06722010-10-10 15:58:46 -07001404 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1405 if (mVelocityTracker == null) {
1406 mVelocityTracker = VelocityTracker.obtain();
1407 }
1408 mVelocityTracker.addMovement(ev);
1409 }
1410
1411 private void releaseVelocityTracker() {
1412 if (mVelocityTracker != null) {
1413 mVelocityTracker.recycle();
1414 mVelocityTracker = null;
1415 }
1416 }
1417
Winson Chung321e9ee2010-08-09 13:37:56 -07001418 private void onSecondaryPointerUp(MotionEvent ev) {
1419 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1420 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1421 final int pointerId = ev.getPointerId(pointerIndex);
1422 if (pointerId == mActivePointerId) {
1423 // This was our active pointer going up. Choose a new
1424 // active pointer and adjust accordingly.
1425 // TODO: Make this decision more intelligent.
1426 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1427 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1428 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001429 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 mActivePointerId = ev.getPointerId(newPointerIndex);
1431 if (mVelocityTracker != null) {
1432 mVelocityTracker.clear();
1433 }
1434 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001435 }
1436
Michael Jurkad771c962011-08-09 15:00:48 -07001437 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001438
1439 @Override
1440 public void requestChildFocus(View child, View focused) {
1441 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001442 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001443 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001444 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001445 }
1446 }
1447
Winson Chunge3193b92010-09-10 11:44:42 -07001448 protected int getChildIndexForRelativeOffset(int relativeOffset) {
Winson Chung52aee602013-01-30 12:01:02 -08001449 final boolean isRtl = isLayoutRtl();
Winson Chunge3193b92010-09-10 11:44:42 -07001450 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001451 int left;
1452 int right;
Winson Chung52aee602013-01-30 12:01:02 -08001453 final int startIndex = isRtl ? childCount - 1 : 0;
1454 final int endIndex = isRtl ? -1 : childCount;
1455 final int delta = isRtl ? -1 : 1;
1456 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001457 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001458 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001459 if (left <= relativeOffset && relativeOffset <= right) {
1460 return i;
1461 }
Winson Chunge3193b92010-09-10 11:44:42 -07001462 }
1463 return -1;
1464 }
1465
Winson Chung1908d072011-02-24 18:09:44 -08001466 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001467 // This functions are called enough times that it actually makes a difference in the
1468 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001469 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001470 final int minWidth = mMinimumWidth;
1471 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001472 }
1473
Adam Cohend19d3ca2010-09-15 14:43:42 -07001474 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001475 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001477 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 final int childCount = getChildCount();
1479 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001480 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001481 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 int halfChildWidth = (childWidth / 2);
1483 int childCenter = getChildOffset(i) + halfChildWidth;
1484 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1485 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1486 minDistanceFromScreenCenter = distanceFromScreenCenter;
1487 minDistanceFromScreenCenterIndex = i;
1488 }
1489 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001490 return minDistanceFromScreenCenterIndex;
1491 }
1492
1493 protected void snapToDestination() {
1494 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001495 }
1496
Adam Cohene0f66b52010-11-23 15:06:07 -08001497 private static class ScrollInterpolator implements Interpolator {
1498 public ScrollInterpolator() {
1499 }
1500
1501 public float getInterpolation(float t) {
1502 t -= 1.0f;
1503 return t*t*t*t*t + 1;
1504 }
1505 }
1506
1507 // We want the duration of the page snap animation to be influenced by the distance that
1508 // the screen has to travel, however, we don't want this duration to be effected in a
1509 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1510 // of travel has on the overall snap duration.
1511 float distanceInfluenceForSnapDuration(float f) {
1512 f -= 0.5f; // center the values about 0.
1513 f *= 0.3f * Math.PI / 2.0f;
1514 return (float) Math.sin(f);
1515 }
1516
Michael Jurka0142d492010-08-25 17:46:15 -07001517 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001518 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1519 int halfScreenSize = getMeasuredWidth() / 2;
1520
Winson Chung785d2eb2011-04-14 16:08:02 -07001521 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1522 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1523 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001524 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1525 int delta = newX - mUnboundedScrollX;
1526 int duration = 0;
1527
Adam Cohen265b9a62011-12-07 14:37:18 -08001528 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001529 // If the velocity is low enough, then treat this more as an automatic page advance
1530 // as opposed to an apparent physical response to flinging
1531 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1532 return;
1533 }
1534
1535 // Here we compute a "distance" that will be used in the computation of the overall
1536 // snap duration. This is a function of the actual distance that needs to be traveled;
1537 // we keep this value close to half screen size in order to reduce the variance in snap
1538 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001539 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001540 float distance = halfScreenSize + halfScreenSize *
1541 distanceInfluenceForSnapDuration(distanceRatio);
1542
1543 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001544 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001545
1546 // we want the page's snap velocity to approximately match the velocity at which the
1547 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001548 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1549 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Chet Haase97687ef2012-10-25 16:01:11 -07001550 duration = Math.min(duration, MAX_PAGE_SNAP_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001551
1552 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001553 }
1554
1555 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001556 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001557 }
1558
Michael Jurka0142d492010-08-25 17:46:15 -07001559 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001560 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001561
Winson Chung785d2eb2011-04-14 16:08:02 -07001562 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1563 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1564 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001565 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001566 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001567 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001568 snapToPage(whichPage, delta, duration);
1569 }
1570
1571 protected void snapToPage(int whichPage, int delta, int duration) {
1572 mNextPage = whichPage;
1573
1574 View focusedChild = getFocusedChild();
1575 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001576 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001577 focusedChild.clearFocus();
1578 }
1579
1580 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001581 awakenScrollBars(duration);
1582 if (duration == 0) {
1583 duration = Math.abs(delta);
1584 }
1585
1586 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001587 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001588
Winson Chungb44b5242011-06-13 11:32:14 -07001589 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1590 // loading associated pages until the scroll settles
1591 if (mDeferScrollUpdate) {
1592 loadAssociatedPages(mNextPage);
1593 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001594 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001595 }
Michael Jurka0142d492010-08-25 17:46:15 -07001596 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001597 invalidate();
1598 }
1599
Winson Chung321e9ee2010-08-09 13:37:56 -07001600 public void scrollLeft() {
1601 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001602 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001603 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001604 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001605 }
1606 }
1607
1608 public void scrollRight() {
1609 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001610 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001611 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001612 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001613 }
1614 }
1615
Winson Chung86f77532010-08-24 11:08:22 -07001616 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 int result = -1;
1618 if (v != null) {
1619 ViewParent vp = v.getParent();
1620 int count = getChildCount();
1621 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001622 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001623 return i;
1624 }
1625 }
1626 }
1627 return result;
1628 }
1629
1630 /**
1631 * @return True is long presses are still allowed for the current touch
1632 */
1633 public boolean allowLongPress() {
1634 return mAllowLongPress;
1635 }
1636
Michael Jurka0142d492010-08-25 17:46:15 -07001637 /**
1638 * Set true to allow long-press events to be triggered, usually checked by
1639 * {@link Launcher} to accept or block dpad-initiated long-presses.
1640 */
1641 public void setAllowLongPress(boolean allowLongPress) {
1642 mAllowLongPress = allowLongPress;
1643 }
1644
Winson Chung321e9ee2010-08-09 13:37:56 -07001645 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001646 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001647
1648 SavedState(Parcelable superState) {
1649 super(superState);
1650 }
1651
1652 private SavedState(Parcel in) {
1653 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001654 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001655 }
1656
1657 @Override
1658 public void writeToParcel(Parcel out, int flags) {
1659 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001660 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001661 }
1662
1663 public static final Parcelable.Creator<SavedState> CREATOR =
1664 new Parcelable.Creator<SavedState>() {
1665 public SavedState createFromParcel(Parcel in) {
1666 return new SavedState(in);
1667 }
1668
1669 public SavedState[] newArray(int size) {
1670 return new SavedState[size];
1671 }
1672 };
1673 }
1674
Winson Chungf314b0e2011-08-16 11:54:27 -07001675 protected void loadAssociatedPages(int page) {
1676 loadAssociatedPages(page, false);
1677 }
1678 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001679 if (mContentIsRefreshable) {
1680 final int count = getChildCount();
1681 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001682 int lowerPageBound = getAssociatedLowerPageBound(page);
1683 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001684 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1685 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001686 // First, clear any pages that should no longer be loaded
1687 for (int i = 0; i < count; ++i) {
1688 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001689 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001690 if (layout.getPageChildCount() > 0) {
1691 layout.removeAllViewsOnPage();
1692 }
1693 mDirtyPageContent.set(i, true);
1694 }
1695 }
1696 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001697 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001698 if ((i != page) && immediateAndOnly) {
1699 continue;
1700 }
Michael Jurka0142d492010-08-25 17:46:15 -07001701 if (lowerPageBound <= i && i <= upperPageBound) {
1702 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001703 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001704 mDirtyPageContent.set(i, false);
1705 }
Winson Chung86f77532010-08-24 11:08:22 -07001706 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001707 }
1708 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001709 }
1710 }
1711
Winson Chunge3193b92010-09-10 11:44:42 -07001712 protected int getAssociatedLowerPageBound(int page) {
1713 return Math.max(0, page - 1);
1714 }
1715 protected int getAssociatedUpperPageBound(int page) {
1716 final int count = getChildCount();
1717 return Math.min(page + 1, count - 1);
1718 }
1719
Winson Chung86f77532010-08-24 11:08:22 -07001720 /**
1721 * This method is called ONLY to synchronize the number of pages that the paged view has.
1722 * To actually fill the pages with information, implement syncPageItems() below. It is
1723 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1724 * and therefore, individual page items do not need to be updated in this method.
1725 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001726 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001727
1728 /**
1729 * This method is called to synchronize the items that are on a particular page. If views on
1730 * the page can be reused, then they should be updated within this method.
1731 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001732 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001733
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001734 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001735 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001736 }
1737 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001738 invalidatePageData(currentPage, false);
1739 }
1740 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001741 if (!mIsDataReady) {
1742 return;
1743 }
1744
Michael Jurka0142d492010-08-25 17:46:15 -07001745 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001746 // Force all scrolling-related behavior to end
1747 mScroller.forceFinished(true);
1748 mNextPage = INVALID_PAGE;
1749
Michael Jurka0142d492010-08-25 17:46:15 -07001750 // Update all the pages
1751 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001752
Winson Chung5a808352011-06-27 19:08:49 -07001753 // We must force a measure after we've loaded the pages to update the content width and
1754 // to determine the full scroll width
1755 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1756 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1757
1758 // Set a new page as the current page if necessary
1759 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001760 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001761 }
1762
Michael Jurka0142d492010-08-25 17:46:15 -07001763 // Mark each of the pages as dirty
1764 final int count = getChildCount();
1765 mDirtyPageContent.clear();
1766 for (int i = 0; i < count; ++i) {
1767 mDirtyPageContent.add(true);
1768 }
1769
1770 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001771 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001772 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001773 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001774 }
Winson Chung007c6982011-06-14 13:27:53 -07001775
Michael Jurkaafaa0502011-12-13 18:22:50 -08001776 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001777 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1778 // found
1779 if (mHasScrollIndicator && mScrollIndicator == null) {
1780 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001781 if (parent != null) {
1782 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1783 mHasScrollIndicator = mScrollIndicator != null;
1784 if (mHasScrollIndicator) {
1785 mScrollIndicator.setVisibility(View.VISIBLE);
1786 }
Winson Chung007c6982011-06-14 13:27:53 -07001787 }
1788 }
1789 return mScrollIndicator;
1790 }
1791
1792 protected boolean isScrollingIndicatorEnabled() {
Michael Jurkab1dfe252012-09-26 10:53:55 -07001793 return true;
Winson Chung007c6982011-06-14 13:27:53 -07001794 }
1795
Winson Chung5a808352011-06-27 19:08:49 -07001796 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1797 @Override
1798 public void run() {
1799 hideScrollingIndicator(false);
1800 }
1801 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001802 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001803 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001804 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001805 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001806 }
1807
Michael Jurka430e8a52011-08-08 15:52:14 -07001808 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001809 mShouldShowScrollIndicator = true;
1810 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001811 if (getChildCount() <= 1) return;
1812 if (!isScrollingIndicatorEnabled()) return;
1813
Michael Jurkabed61d22012-02-14 22:51:29 -08001814 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001815 getScrollingIndicator();
1816 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001817 // Fade the indicator in
1818 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001819 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001820 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001821 if (immediately || mScrollingPaused) {
Michael Jurka430e8a52011-08-08 15:52:14 -07001822 mScrollIndicator.setAlpha(1f);
1823 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001824 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f);
Michael Jurka430e8a52011-08-08 15:52:14 -07001825 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1826 mScrollIndicatorAnimator.start();
1827 }
Winson Chung007c6982011-06-14 13:27:53 -07001828 }
1829 }
1830
Adam Cohen21b41102011-11-01 17:29:52 -07001831 protected void cancelScrollingIndicatorAnimations() {
1832 if (mScrollIndicatorAnimator != null) {
1833 mScrollIndicatorAnimator.cancel();
1834 }
1835 }
1836
Winson Chung007c6982011-06-14 13:27:53 -07001837 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001838 if (getChildCount() <= 1) return;
1839 if (!isScrollingIndicatorEnabled()) return;
1840
1841 getScrollingIndicator();
1842 if (mScrollIndicator != null) {
1843 // Fade the indicator out
1844 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001845 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001846 if (immediately || mScrollingPaused) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001847 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001848 mScrollIndicator.setAlpha(0f);
1849 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001850 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f);
Winson Chung32174c82011-07-19 15:47:55 -07001851 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1852 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1853 private boolean cancelled = false;
1854 @Override
1855 public void onAnimationCancel(android.animation.Animator animation) {
1856 cancelled = true;
1857 }
1858 @Override
1859 public void onAnimationEnd(Animator animation) {
1860 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001861 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001862 }
1863 }
1864 });
1865 mScrollIndicatorAnimator.start();
1866 }
Winson Chung007c6982011-06-14 13:27:53 -07001867 }
1868 }
1869
Winson Chung32174c82011-07-19 15:47:55 -07001870 /**
1871 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1872 * fill its space on the track or not.
1873 */
1874 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001875 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001876 }
1877
Winson Chung007c6982011-06-14 13:27:53 -07001878 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001879 if (getChildCount() <= 1) return;
1880 if (!isScrollingIndicatorEnabled()) return;
1881
1882 getScrollingIndicator();
1883 if (mScrollIndicator != null) {
1884 updateScrollingIndicatorPosition();
1885 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001886 if (mShouldShowScrollIndicator) {
1887 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1888 }
Winson Chung007c6982011-06-14 13:27:53 -07001889 }
1890
Winson Chung007c6982011-06-14 13:27:53 -07001891 private void updateScrollingIndicatorPosition() {
Winson Chung52aee602013-01-30 12:01:02 -08001892 final boolean isRtl = isLayoutRtl();
Winson Chung649723c2011-07-06 20:41:23 -07001893 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001894 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001895 int numPages = getChildCount();
1896 int pageWidth = getMeasuredWidth();
Winson Chungf5f8cef2011-07-22 11:16:13 -07001897 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001898 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1899 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001900
Winson Chung52aee602013-01-30 12:01:02 -08001901 float scrollPos = isRtl ? mMaxScrollX - getScrollX() : getScrollX();
1902 float offset = Math.max(0f, Math.min(1f, (float) scrollPos / mMaxScrollX));
1903 if (isRtl) {
1904 offset = 1f - offset;
1905 }
Winson Chung32174c82011-07-19 15:47:55 -07001906 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001907 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001908 if (hasElasticScrollIndicator()) {
1909 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1910 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1911 mScrollIndicator.requestLayout();
1912 }
1913 } else {
1914 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1915 indicatorPos += indicatorCenterOffset;
1916 }
Winson Chung007c6982011-06-14 13:27:53 -07001917 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001918 }
1919
Winson Chung3ac74c52011-06-30 17:39:37 -07001920 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001921 }
1922
1923 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001924 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001925
1926 /* Accessibility */
1927 @Override
1928 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1929 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001930 info.setScrollable(getPageCount() > 1);
1931 if (getCurrentPage() < getPageCount() - 1) {
1932 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1933 }
1934 if (getCurrentPage() > 0) {
1935 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1936 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001937 }
1938
1939 @Override
1940 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1941 super.onInitializeAccessibilityEvent(event);
1942 event.setScrollable(true);
1943 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1944 event.setFromIndex(mCurrentPage);
1945 event.setToIndex(mCurrentPage);
1946 event.setItemCount(getChildCount());
1947 }
1948 }
1949
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001950 @Override
1951 public boolean performAccessibilityAction(int action, Bundle arguments) {
1952 if (super.performAccessibilityAction(action, arguments)) {
1953 return true;
1954 }
1955 switch (action) {
1956 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1957 if (getCurrentPage() < getPageCount() - 1) {
1958 scrollRight();
1959 return true;
1960 }
1961 } break;
1962 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1963 if (getCurrentPage() > 0) {
1964 scrollLeft();
1965 return true;
1966 }
1967 } break;
1968 }
1969 return false;
1970 }
1971
Winson Chung6a0f57d2011-06-29 20:10:49 -07001972 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001973 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001974 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001975 }
Winson Chungd11265e2011-08-30 13:37:23 -07001976
1977 @Override
1978 public boolean onHoverEvent(android.view.MotionEvent event) {
1979 return true;
1980 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001981}