blob: abf8bbd140b680bdeedb0b6aea8fa35cab5d7ffb [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
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
Daniel Sandler325dc232013-06-05 22:57:57 -040045import com.android.launcher3.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;
Adam Cohene61a9a22013-06-11 15:45:31 -070092 protected int mChildCountOnLastMeasure;
93
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080095 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070097 private VelocityTracker mVelocityTracker;
98
99 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -0800101 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -0800102 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800103 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700104 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700105 private int[] mChildOffsets;
106 private int[] mChildRelativeOffsets;
107 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Michael Jurka0142d492010-08-25 17:46:15 -0700109 protected final static int TOUCH_STATE_REST = 0;
110 protected final static int TOUCH_STATE_SCROLLING = 1;
111 protected final static int TOUCH_STATE_PREV_PAGE = 2;
112 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700113 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka0142d492010-08-25 17:46:15 -0700115 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700116 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka0142d492010-08-25 17:46:15 -0700118 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka7426c422010-11-11 15:23:47 -0800120 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka7426c422010-11-11 15:23:47 -0800122 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123 private int mPagingTouchSlop;
124 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800125 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700126 protected int mPageSpacing;
127 protected int mPageLayoutPaddingTop;
128 protected int mPageLayoutPaddingBottom;
129 protected int mPageLayoutPaddingLeft;
130 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700131 protected int mPageLayoutWidthGap;
132 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700133 protected int mCellCountX = 0;
134 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700135 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800136 protected boolean mAllowOverScroll = true;
137 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800138 protected int[] mTempVisiblePagesRange = new int[2];
Michael Jurka5e368ff2012-05-14 23:13:15 -0700139 protected boolean mForceDrawAllChildrenNextFrame;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise
Adam Cohenebea84d2011-11-09 17:20:41 -0800142 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
143 // the screens from continuing to translate beyond the normal bounds.
144 protected int mOverScrollX;
145
Michael Jurka8c920dd2011-01-20 14:16:56 -0800146 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800147 protected float mLayoutScale = 1.0f;
148
Michael Jurka5f1c5092010-09-03 14:15:02 -0700149 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Michael Jurka5f1c5092010-09-03 14:15:02 -0700151 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700152
Winson Chung86f77532010-08-24 11:08:22 -0700153 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700154
Michael Jurkae326f182011-11-21 14:05:46 -0800155 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700156
Michael Jurka0142d492010-08-25 17:46:15 -0700157 // If true, syncPages and syncPageItems will be called to refresh pages
158 protected boolean mContentIsRefreshable = true;
159
160 // If true, modify alpha of neighboring pages as user scrolls left/right
161 protected boolean mFadeInAdjacentScreens = true;
162
163 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
164 // to switch to a new page
165 protected boolean mUsePagingTouchSlop = true;
166
Michael Jurka8b805b12012-04-18 14:23:14 -0700167 // If true, the subclass should directly update scrollX itself in its computeScroll method
Michael Jurka0142d492010-08-25 17:46:15 -0700168 // (SmoothPagedView does this)
169 protected boolean mDeferScrollUpdate = false;
170
Patrick Dubroy1262e362010-10-06 15:49:50 -0700171 protected boolean mIsPageMoving = false;
172
Winson Chungf0ea4d32011-06-06 14:27:16 -0700173 // All syncs and layout passes are deferred until data is ready.
174 protected boolean mIsDataReady = false;
175
Winson Chung007c6982011-06-14 13:27:53 -0700176 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700177 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800178 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700179 private int mScrollIndicatorPaddingLeft;
180 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700181 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800182 private boolean mShouldShowScrollIndicator = false;
183 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700184 protected static final int sScrollIndicatorFadeInDuration = 150;
185 protected static final int sScrollIndicatorFadeOutDuration = 650;
186 protected static final int sScrollIndicatorFlashDuration = 650;
Chet Haasebc2f0822012-10-26 17:59:53 -0700187 private boolean mScrollingPaused = false;
Winson Chung007c6982011-06-14 13:27:53 -0700188
Winson Chungb44b5242011-06-13 11:32:14 -0700189 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700190 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700191
Winson Chung86f77532010-08-24 11:08:22 -0700192 public interface PageSwitchListener {
193 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 }
195
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 public PagedView(Context context) {
197 this(context, null);
198 }
199
200 public PagedView(Context context, AttributeSet attrs) {
201 this(context, attrs, 0);
202 }
203
204 public PagedView(Context context, AttributeSet attrs, int defStyle) {
205 super(context, attrs, defStyle);
206
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 TypedArray a = context.obtainStyledAttributes(attrs,
208 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800209 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700210 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800211 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800213 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700214 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800215 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700216 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800217 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700218 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700220 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700221 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700222 mScrollIndicatorPaddingLeft =
223 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
224 mScrollIndicatorPaddingRight =
225 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700226 a.recycle();
227
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700229 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 }
231
232 /**
233 * Initializes various states for this workspace.
234 */
Michael Jurka0142d492010-08-25 17:46:15 -0700235 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700236 mDirtyPageContent = new ArrayList<Boolean>();
237 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800238 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700239 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700240 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700241
242 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
243 mTouchSlop = configuration.getScaledTouchSlop();
244 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
245 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700246 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800247
248 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
249 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
250 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Michael Jurka8b805b12012-04-18 14:23:14 -0700251 setOnHierarchyChangeListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 }
253
Winson Chung86f77532010-08-24 11:08:22 -0700254 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
255 mPageSwitchListener = pageSwitchListener;
256 if (mPageSwitchListener != null) {
257 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 }
259 }
260
261 /**
Winson Chung52aee602013-01-30 12:01:02 -0800262 * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api.
263 */
264 public boolean isLayoutRtl() {
265 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
266 }
267
268 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
270 * out pages.
271 */
272 protected void setDataIsReady() {
273 mIsDataReady = true;
274 }
275 protected boolean isDataReady() {
276 return mIsDataReady;
277 }
278
279 /**
Winson Chung86f77532010-08-24 11:08:22 -0700280 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 *
Winson Chung86f77532010-08-24 11:08:22 -0700282 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 */
Winson Chung86f77532010-08-24 11:08:22 -0700284 int getCurrentPage() {
285 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 }
Winson Chung360e63f2012-04-27 13:48:05 -0700287 int getNextPage() {
288 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
289 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700290
Winson Chung86f77532010-08-24 11:08:22 -0700291 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 return getChildCount();
293 }
294
Winson Chung86f77532010-08-24 11:08:22 -0700295 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 return getChildAt(index);
297 }
298
Adam Cohenae4f1552011-10-20 00:15:42 -0700299 protected int indexToPage(int index) {
300 return index;
301 }
302
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800304 * Updates the scroll of the current page immediately to its final scroll position. We use this
305 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
306 * the previous tab page.
307 */
308 protected void updateCurrentPageScroll() {
Winson Chung1ef21232012-11-26 14:08:38 -0800309 // If the current page is invalid, just reset the scroll position to zero
310 int newX = 0;
311 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
312 int offset = getChildOffset(mCurrentPage);
313 int relOffset = getRelativeChildOffset(mCurrentPage);
314 newX = offset - relOffset;
315 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800316 scrollTo(newX, 0);
317 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800318 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800319 }
320
321 /**
Chet Haasebc2f0822012-10-26 17:59:53 -0700322 * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation
323 * ends, {@link #resumeScrolling()} should be called, along with
324 * {@link #updateCurrentPageScroll()} to correctly set the final state and re-enable scrolling.
325 */
326 void pauseScrolling() {
327 mScroller.forceFinished(true);
328 cancelScrollingIndicatorAnimations();
329 mScrollingPaused = true;
330 }
331
332 /**
333 * Enables scrolling again.
334 * @see #pauseScrolling()
335 */
336 void resumeScrolling() {
337 mScrollingPaused = false;
338 }
339 /**
Winson Chung86f77532010-08-24 11:08:22 -0700340 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700341 */
Winson Chung86f77532010-08-24 11:08:22 -0700342 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800343 if (!mScroller.isFinished()) {
344 mScroller.abortAnimation();
345 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800346 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
347 // the default
348 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800349 return;
350 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700351
Adam Cohene61a9a22013-06-11 15:45:31 -0700352 mForceScreenScrolled = true;
Winson Chung86f77532010-08-24 11:08:22 -0700353 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800354 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700355 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700356 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800357 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 }
359
Michael Jurka0142d492010-08-25 17:46:15 -0700360 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700361 if (mPageSwitchListener != null) {
362 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
364 }
365
Michael Jurkace7e05f2011-02-01 22:02:35 -0800366 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700367 if (!mIsPageMoving) {
368 mIsPageMoving = true;
369 onPageBeginMoving();
370 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700371 }
372
Michael Jurkace7e05f2011-02-01 22:02:35 -0800373 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700374 if (mIsPageMoving) {
375 mIsPageMoving = false;
376 onPageEndMoving();
377 }
Michael Jurka0142d492010-08-25 17:46:15 -0700378 }
379
Adam Cohen26976d92011-03-22 15:33:33 -0700380 protected boolean isPageMoving() {
381 return mIsPageMoving;
382 }
383
Michael Jurka0142d492010-08-25 17:46:15 -0700384 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700385 protected void onPageBeginMoving() {
386 }
387
388 // a method that subclasses can override to add behavior
389 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700390 }
391
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 /**
Winson Chung86f77532010-08-24 11:08:22 -0700393 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 *
395 * @param l The listener used to respond to long clicks.
396 */
397 @Override
398 public void setOnLongClickListener(OnLongClickListener l) {
399 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700400 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700402 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 }
404 }
405
406 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800407 public void scrollBy(int x, int y) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700408 scrollTo(mUnboundedScrollX + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800409 }
410
411 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700412 public void scrollTo(int x, int y) {
Winson Chung52aee602013-01-30 12:01:02 -0800413 final boolean isRtl = isLayoutRtl();
Adam Cohen68d73932010-11-15 10:50:58 -0800414 mUnboundedScrollX = x;
415
Winson Chung52aee602013-01-30 12:01:02 -0800416 boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0);
417 boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX);
418 if (isXBeforeFirstPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800419 super.scrollTo(0, y);
420 if (mAllowOverScroll) {
Winson Chung52aee602013-01-30 12:01:02 -0800421 if (isRtl) {
422 overScroll(x - mMaxScrollX);
423 } else {
424 overScroll(x);
425 }
Adam Cohen68d73932010-11-15 10:50:58 -0800426 }
Winson Chung52aee602013-01-30 12:01:02 -0800427 } else if (isXAfterLastPage) {
Adam Cohen68d73932010-11-15 10:50:58 -0800428 super.scrollTo(mMaxScrollX, y);
429 if (mAllowOverScroll) {
Winson Chung52aee602013-01-30 12:01:02 -0800430 if (isRtl) {
431 overScroll(x);
432 } else {
433 overScroll(x - mMaxScrollX);
434 }
Adam Cohen68d73932010-11-15 10:50:58 -0800435 }
436 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800437 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800438 super.scrollTo(x, y);
439 }
440
Michael Jurka0142d492010-08-25 17:46:15 -0700441 mTouchX = x;
442 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
443 }
444
445 // we moved this functionality to a helper function so SmoothPagedView can reuse it
446 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700448 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700449 if (getScrollX() != mScroller.getCurrX()
450 || getScrollY() != mScroller.getCurrY()
Michael Jurkab06d95f2012-04-02 06:26:53 -0700451 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700452 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
453 }
Michael Jurka0142d492010-08-25 17:46:15 -0700454 invalidate();
455 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700456 } else if (mNextPage != INVALID_PAGE) {
457 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700458 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700459 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700460
461 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700462 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700463 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700464 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700465 }
466
Adam Cohen73aa9752010-11-24 16:26:58 -0800467 // We don't want to trigger a page end moving unless the page has settled
468 // and the user has stopped scrolling
469 if (mTouchState == TOUCH_STATE_REST) {
470 pageEndMoving();
471 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700472
473 // Notify the user when the page changes
Michael Jurka8b805b12012-04-18 14:23:14 -0700474 AccessibilityManager accessibilityManager = (AccessibilityManager)
475 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
476 if (accessibilityManager.isEnabled()) {
Winson Chungc27d1bb2011-09-29 12:07:42 -0700477 AccessibilityEvent ev =
478 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
479 ev.getText().add(getCurrentPageDescription());
480 sendAccessibilityEventUnchecked(ev);
481 }
Michael Jurka0142d492010-08-25 17:46:15 -0700482 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
Michael Jurka0142d492010-08-25 17:46:15 -0700484 return false;
485 }
486
487 @Override
488 public void computeScroll() {
489 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 }
491
492 @Override
493 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700494 if (!mIsDataReady) {
495 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
496 return;
497 }
498
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
500 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700501 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
502 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 if (widthMode != MeasureSpec.EXACTLY) {
504 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
505 }
506
Winson Chung8aad6102012-05-11 16:27:49 -0700507 // Return early if we aren't given a proper dimension
508 if (widthSize <= 0 || heightSize <= 0) {
509 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
510 return;
511 }
512
Adam Lesinski6b879f02010-11-04 16:15:23 -0700513 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
514 * of the All apps view on XLarge displays to not take up more space then it needs. Width
515 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
516 * each page to have the same width.
517 */
Adam Lesinski6b879f02010-11-04 16:15:23 -0700518 int maxChildHeight = 0;
519
Michael Jurka8b805b12012-04-18 14:23:14 -0700520 final int verticalPadding = getPaddingTop() + getPaddingBottom();
521 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700522
Michael Jurka36fcb742011-04-06 17:08:58 -0700523
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700525 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700526 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 final int childCount = getChildCount();
528 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700529 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700530 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700531 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
532
533 int childWidthMode;
534 if (lp.width == LayoutParams.WRAP_CONTENT) {
535 childWidthMode = MeasureSpec.AT_MOST;
536 } else {
537 childWidthMode = MeasureSpec.EXACTLY;
538 }
539
540 int childHeightMode;
541 if (lp.height == LayoutParams.WRAP_CONTENT) {
542 childHeightMode = MeasureSpec.AT_MOST;
543 } else {
544 childHeightMode = MeasureSpec.EXACTLY;
545 }
546
547 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700548 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700549 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700550 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700551
552 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700553 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700554 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
555 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700556 }
557
558 if (heightMode == MeasureSpec.AT_MOST) {
559 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 }
Winson Chungae890b82011-09-13 18:08:54 -0700561
Winson Chungae890b82011-09-13 18:08:54 -0700562 setMeasuredDimension(widthSize, heightSize);
563
Winson Chung8aad6102012-05-11 16:27:49 -0700564 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
565 // We also wait until we set the measured dimensions before flushing the cache as well, to
566 // ensure that the cache is filled with good values.
567 invalidateCachedOffsets();
568
Adam Cohene61a9a22013-06-11 15:45:31 -0700569 if (mChildCountOnLastMeasure != getChildCount()) {
570 setCurrentPage(mCurrentPage);
571 }
572 mChildCountOnLastMeasure = getChildCount();
573
Winson Chunga128a7b2012-04-30 15:23:15 -0700574 if (childCount > 0) {
575 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
576 + getChildWidth(0));
577
578 // Calculate the variable page spacing if necessary
Winson Chung8aad6102012-05-11 16:27:49 -0700579 if (mPageSpacing == AUTOMATIC_PAGE_SPACING) {
Winson Chunga128a7b2012-04-30 15:23:15 -0700580 // The gap between pages in the PagedView should be equal to the gap from the page
581 // to the edge of the screen (so it is not visible in the current screen). To
582 // account for unequal padding on each side of the paged view, we take the maximum
583 // of the left/right gap and use that as the gap between each page.
584 int offset = getRelativeChildOffset(0);
585 int spacing = Math.max(offset, widthSize - offset -
586 getChildAt(0).getMeasuredWidth());
587 setPageSpacing(spacing);
588 }
589 }
590
Adam Cohen25b29952011-11-02 14:49:06 -0700591 updateScrollingIndicatorPosition();
592
Adam Cohenfaa28302010-11-19 12:02:18 -0800593 if (childCount > 0) {
Winson Chung52aee602013-01-30 12:01:02 -0800594 final int index = isLayoutRtl() ? 0 : childCount - 1;
595 mMaxScrollX = getChildOffset(index) - getRelativeChildOffset(index);
Adam Cohenfaa28302010-11-19 12:02:18 -0800596 } else {
597 mMaxScrollX = 0;
598 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700599 }
600
Michael Jurka8c920dd2011-01-20 14:16:56 -0800601 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800602 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
Michael Jurka8b805b12012-04-18 14:23:14 -0700603 int delta = newX - getScrollX();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800604
Michael Jurka8c920dd2011-01-20 14:16:56 -0800605 final int pageCount = getChildCount();
606 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700607 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800608 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800609 }
610 setCurrentPage(newCurrentPage);
611 }
612
Michael Jurka8c920dd2011-01-20 14:16:56 -0800613 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
614 // 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 -0800615 // tightens the layout accordingly
616 public void setLayoutScale(float childrenScale) {
617 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700618 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800619
620 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
621 int childCount = getChildCount();
622 float childrenX[] = new float[childCount];
623 float childrenY[] = new float[childCount];
624 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700625 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800626 childrenX[i] = child.getX();
627 childrenY[i] = child.getY();
628 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700629 // Trigger a full re-layout (never just call onLayout directly!)
630 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
631 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
632 requestLayout();
633 measure(widthSpec, heightSpec);
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 layout(getLeft(), getTop(), getRight(), getBottom());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800635 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700636 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800637 child.setX(childrenX[i]);
638 child.setY(childrenY[i]);
639 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700640
Michael Jurkad3ef3062010-11-23 16:23:58 -0800641 // Also, the page offset has changed (since the pages are now smaller);
642 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800643 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800644 }
645
Adam Cohen60b07122011-11-14 17:26:06 -0800646 public void setPageSpacing(int pageSpacing) {
647 mPageSpacing = pageSpacing;
648 invalidateCachedOffsets();
649 }
650
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700652 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700653 if (!mIsDataReady) {
654 return;
655 }
656
Winson Chung785d2eb2011-04-14 16:08:02 -0700657 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurka8b805b12012-04-18 14:23:14 -0700658 final int verticalPadding = getPaddingTop() + getPaddingBottom();
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 final int childCount = getChildCount();
Winson Chung52aee602013-01-30 12:01:02 -0800660 final boolean isRtl = isLayoutRtl();
Winson Chung321e9ee2010-08-09 13:37:56 -0700661
Winson Chung52aee602013-01-30 12:01:02 -0800662 final int startIndex = isRtl ? childCount - 1 : 0;
663 final int endIndex = isRtl ? -1 : childCount;
664 final int delta = isRtl ? -1 : 1;
665 int childLeft = getRelativeChildOffset(startIndex);
666 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700667 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700668 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800669 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700670 final int childHeight = child.getMeasuredHeight();
Michael Jurka8b805b12012-04-18 14:23:14 -0700671 int childTop = getPaddingTop();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700672 if (mCenterPagesVertically) {
673 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
674 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800675
Winson Chung785d2eb2011-04-14 16:08:02 -0700676 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700677 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800678 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700679 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 }
681 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700682
683 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
684 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800685 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700686 setHorizontalScrollBarEnabled(true);
687 mFirstLayout = false;
688 }
Winson Chunge3193b92010-09-10 11:44:42 -0700689 }
690
Adam Cohenf34bab52010-09-30 14:11:56 -0700691 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700692 if (isScrollingIndicatorEnabled()) {
693 updateScrollingIndicator();
694 }
Michael Jurka869390b2012-05-06 15:55:19 -0700695 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
696
697 if (mFadeInAdjacentScreens && !isInOverscroll) {
Adam Cohen73894962011-10-31 13:17:17 -0700698 for (int i = 0; i < getChildCount(); i++) {
699 View child = getChildAt(i);
700 if (child != null) {
701 float scrollProgress = getScrollProgress(screenCenter, child, i);
702 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800703 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700704 }
705 }
706 invalidate();
707 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700708 }
709
Winson Chunge3193b92010-09-10 11:44:42 -0700710 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 public void onChildViewAdded(View parent, View child) {
Adam Cohen2591f6a2011-10-25 14:36:40 -0700712 // This ensures that when children are added, they get the correct transforms / alphas
713 // in accordance with any scroll effects.
714 mForceScreenScrolled = true;
715 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700716 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700717 }
718
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 @Override
720 public void onChildViewRemoved(View parent, View child) {
721 }
722
Adam Cohen73894962011-10-31 13:17:17 -0700723 protected void invalidateCachedOffsets() {
724 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700725 if (count == 0) {
726 mChildOffsets = null;
727 mChildRelativeOffsets = null;
728 mChildOffsetsWithLayoutScale = null;
729 return;
730 }
Adam Cohen73894962011-10-31 13:17:17 -0700731
732 mChildOffsets = new int[count];
733 mChildRelativeOffsets = new int[count];
734 mChildOffsetsWithLayoutScale = new int[count];
735 for (int i = 0; i < count; i++) {
736 mChildOffsets[i] = -1;
737 mChildRelativeOffsets[i] = -1;
738 mChildOffsetsWithLayoutScale[i] = -1;
739 }
740 }
741
742 protected int getChildOffset(int index) {
Winson Chung52aee602013-01-30 12:01:02 -0800743 final boolean isRtl = isLayoutRtl();
Adam Cohen73894962011-10-31 13:17:17 -0700744 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
745 mChildOffsets : mChildOffsetsWithLayoutScale;
746
747 if (childOffsets != null && childOffsets[index] != -1) {
748 return childOffsets[index];
749 } else {
750 if (getChildCount() == 0)
751 return 0;
752
Winson Chung52aee602013-01-30 12:01:02 -0800753 final int startIndex = isRtl ? getChildCount() - 1 : 0;
754 final int endIndex = isRtl ? index : index;
755 final int delta = isRtl ? -1 : 1;
756 int offset = getRelativeChildOffset(startIndex);
757 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen73894962011-10-31 13:17:17 -0700758 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
759 }
760 if (childOffsets != null) {
761 childOffsets[index] = offset;
762 }
763 return offset;
764 }
765 }
766
767 protected int getRelativeChildOffset(int index) {
768 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
769 return mChildRelativeOffsets[index];
770 } else {
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 final int padding = getPaddingLeft() + getPaddingRight();
772 final int offset = getPaddingLeft() +
Adam Cohen73894962011-10-31 13:17:17 -0700773 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
774 if (mChildRelativeOffsets != null) {
775 mChildRelativeOffsets[index] = offset;
776 }
777 return offset;
778 }
779 }
780
Adam Cohen73894962011-10-31 13:17:17 -0700781 protected int getScaledMeasuredWidth(View child) {
782 // This functions are called enough times that it actually makes a difference in the
783 // profiler -- so just inline the max() here
784 final int measuredWidth = child.getMeasuredWidth();
785 final int minWidth = mMinimumWidth;
786 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
787 return (int) (maxWidth * mLayoutScale + 0.5f);
788 }
789
Michael Jurkadde558b2011-11-09 22:09:06 -0800790 protected void getVisiblePages(int[] range) {
Winson Chung52aee602013-01-30 12:01:02 -0800791 final boolean isRtl = isLayoutRtl();
Michael Jurka0142d492010-08-25 17:46:15 -0700792 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700793
Michael Jurkac4fb9172010-09-02 17:19:20 -0700794 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700795 final int screenWidth = getMeasuredWidth();
Winson Chung52aee602013-01-30 12:01:02 -0800796 int leftScreen = isRtl ? pageCount - 1 : 0;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700797 int rightScreen = 0;
Winson Chung52aee602013-01-30 12:01:02 -0800798 int endIndex = isRtl ? 0 : pageCount - 1;
799 int delta = isRtl ? -1 : 1;
Michael Jurka47f74742012-03-02 13:39:13 -0800800 View currPage = getPageAt(leftScreen);
Winson Chung52aee602013-01-30 12:01:02 -0800801 while (leftScreen != endIndex &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700802 currPage.getX() + currPage.getWidth() -
803 currPage.getPaddingRight() < getScrollX()) {
Winson Chung52aee602013-01-30 12:01:02 -0800804 leftScreen += delta;
Michael Jurka47f74742012-03-02 13:39:13 -0800805 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700806 }
807 rightScreen = leftScreen;
Winson Chung52aee602013-01-30 12:01:02 -0800808 currPage = getPageAt(rightScreen + delta);
809 while (rightScreen != endIndex &&
Michael Jurka8b805b12012-04-18 14:23:14 -0700810 currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) {
Winson Chung52aee602013-01-30 12:01:02 -0800811 rightScreen += delta;
812 currPage = getPageAt(rightScreen + delta);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700813 }
Winson Chung52aee602013-01-30 12:01:02 -0800814 range[0] = Math.min(leftScreen, rightScreen);
815 range[1] = Math.max(leftScreen, rightScreen);
Michael Jurkadde558b2011-11-09 22:09:06 -0800816 } else {
817 range[0] = -1;
818 range[1] = -1;
819 }
820 }
821
Michael Jurka920d7f42012-05-14 16:29:55 -0700822 protected boolean shouldDrawChild(View child) {
823 return child.getAlpha() > 0;
824 }
825
Michael Jurkadde558b2011-11-09 22:09:06 -0800826 @Override
827 protected void dispatchDraw(Canvas canvas) {
828 int halfScreenSize = getMeasuredWidth() / 2;
Michael Jurka8b805b12012-04-18 14:23:14 -0700829 // mOverScrollX is equal to getScrollX() when we're within the normal scroll range.
830 // Otherwise it is equal to the scaled overscroll position.
Adam Cohenebea84d2011-11-09 17:20:41 -0800831 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800832
833 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700834 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
835 // set it for the next frame
836 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800837 screenScrolled(screenCenter);
838 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800839 }
840
841 // Find out which screens are visible; as an optimization we only call draw on them
842 final int pageCount = getChildCount();
843 if (pageCount > 0) {
844 getVisiblePages(mTempVisiblePagesRange);
845 final int leftScreen = mTempVisiblePagesRange[0];
846 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800847 if (leftScreen != -1 && rightScreen != -1) {
848 final long drawingTime = getDrawingTime();
849 // Clip to the bounds
850 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -0700851 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
852 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -0700853
Michael Jurka80c69852011-12-16 14:16:32 -0800854 for (int i = getChildCount() - 1; i >= 0; i--) {
855 final View v = getPageAt(i);
Michael Jurka5e368ff2012-05-14 23:13:15 -0700856 if (mForceDrawAllChildrenNextFrame ||
857 (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) {
Michael Jurka80c69852011-12-16 14:16:32 -0800858 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -0800859 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800860 }
Michael Jurka5e368ff2012-05-14 23:13:15 -0700861 mForceDrawAllChildrenNextFrame = false;
Winson Chungc6f10b92011-11-14 11:39:07 -0800862 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700863 }
Michael Jurka0142d492010-08-25 17:46:15 -0700864 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 }
866
867 @Override
868 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700869 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700870 if (page != mCurrentPage || !mScroller.isFinished()) {
871 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 return true;
873 }
874 return false;
875 }
876
877 @Override
878 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700879 int focusablePage;
880 if (mNextPage != INVALID_PAGE) {
881 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700883 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 }
Winson Chung86f77532010-08-24 11:08:22 -0700885 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700887 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 }
889 return false;
890 }
891
892 @Override
893 public boolean dispatchUnhandledMove(View focused, int direction) {
Winson Chung52aee602013-01-30 12:01:02 -0800894 // XXX-RTL: This will be fixed in a future CL
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700896 if (getCurrentPage() > 0) {
897 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 return true;
899 }
900 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700901 if (getCurrentPage() < getPageCount() - 1) {
902 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700903 return true;
904 }
905 }
906 return super.dispatchUnhandledMove(focused, direction);
907 }
908
909 @Override
910 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung52aee602013-01-30 12:01:02 -0800911 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -0700912 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -0700913 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 }
915 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700916 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -0700917 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 }
919 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700920 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -0700921 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 }
923 }
924 }
925
926 /**
927 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700928 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 *
Winson Chung86f77532010-08-24 11:08:22 -0700930 * This happens when live folders requery, and if they're off page, they
931 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 */
933 @Override
934 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700935 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 View v = focused;
937 while (true) {
938 if (v == current) {
939 super.focusableViewAvailable(focused);
940 return;
941 }
942 if (v == this) {
943 return;
944 }
945 ViewParent parent = v.getParent();
946 if (parent instanceof View) {
947 v = (View)v.getParent();
948 } else {
949 return;
950 }
951 }
952 }
953
954 /**
955 * {@inheritDoc}
956 */
957 @Override
958 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
959 if (disallowIntercept) {
960 // We need to make sure to cancel our long press if
961 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700962 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700963 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 }
965 super.requestDisallowInterceptTouchEvent(disallowIntercept);
966 }
967
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800968 /**
969 * Return true if a tap at (x, y) should trigger a flip to the previous page.
970 */
971 protected boolean hitsPreviousPage(float x, float y) {
Winson Chung52aee602013-01-30 12:01:02 -0800972 if (isLayoutRtl()) {
973 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
974 } else {
975 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
976 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800977 }
978
979 /**
980 * Return true if a tap at (x, y) should trigger a flip to the next page.
981 */
982 protected boolean hitsNextPage(float x, float y) {
Winson Chung52aee602013-01-30 12:01:02 -0800983 if (isLayoutRtl()) {
984 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
985 } else {
986 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
987 }
988
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800989 }
990
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 @Override
992 public boolean onInterceptTouchEvent(MotionEvent ev) {
993 /*
994 * This method JUST determines whether we want to intercept the motion.
995 * If we return true, onTouchEvent will be called and we do the actual
996 * scrolling there.
997 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800998 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700999
Winson Chung45e1d6e2010-11-09 17:19:49 -08001000 // Skip touch handling if there are no pages to swipe
1001 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1002
Winson Chung321e9ee2010-08-09 13:37:56 -07001003 /*
1004 * Shortcut the most recurring case: the user is in the dragging
1005 * state and he is moving his finger. We want to intercept this
1006 * motion.
1007 */
1008 final int action = ev.getAction();
1009 if ((action == MotionEvent.ACTION_MOVE) &&
1010 (mTouchState == TOUCH_STATE_SCROLLING)) {
1011 return true;
1012 }
1013
Winson Chung321e9ee2010-08-09 13:37:56 -07001014 switch (action & MotionEvent.ACTION_MASK) {
1015 case MotionEvent.ACTION_MOVE: {
1016 /*
1017 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1018 * whether the user has moved far enough from his original down touch.
1019 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001020 if (mActivePointerId != INVALID_POINTER) {
1021 determineScrollingStart(ev);
1022 break;
1023 }
1024 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1025 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1026 // i.e. fall through to the next case (don't break)
1027 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1028 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 }
1030
1031 case MotionEvent.ACTION_DOWN: {
1032 final float x = ev.getX();
1033 final float y = ev.getY();
1034 // Remember location of down touch
1035 mDownMotionX = x;
1036 mLastMotionX = x;
1037 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -08001038 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001039 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 mActivePointerId = ev.getPointerId(0);
1041 mAllowLongPress = true;
1042
1043 /*
1044 * If being flinged and user touches the screen, initiate drag;
1045 * otherwise don't. mScroller.isFinished should be false when
1046 * being flinged.
1047 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001048 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001049 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
1050 if (finishedScrolling) {
1051 mTouchState = TOUCH_STATE_REST;
1052 mScroller.abortAnimation();
1053 } else {
1054 mTouchState = TOUCH_STATE_SCROLLING;
1055 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001056
Winson Chung86f77532010-08-24 11:08:22 -07001057 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001059 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001061 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001063 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 mTouchState = TOUCH_STATE_NEXT_PAGE;
1065 }
1066 }
1067 }
1068 break;
1069 }
1070
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001072 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 mTouchState = TOUCH_STATE_REST;
1074 mAllowLongPress = false;
1075 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -08001076 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001077 break;
1078
1079 case MotionEvent.ACTION_POINTER_UP:
1080 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001081 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 break;
1083 }
1084
1085 /*
1086 * The only time we want to intercept motion events is if we are in the
1087 * drag mode.
1088 */
1089 return mTouchState != TOUCH_STATE_REST;
1090 }
1091
Adam Cohenf8d28232011-02-01 21:47:00 -08001092 protected void determineScrollingStart(MotionEvent ev) {
1093 determineScrollingStart(ev, 1.0f);
1094 }
1095
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 /*
1097 * Determines if we should change the touch state to start scrolling after the
1098 * user moves their touch point too far.
1099 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001100 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001101 /*
1102 * Locally do absolute value. mLastMotionX is set to the y value
1103 * of the down event.
1104 */
1105 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001106 if (pointerIndex == -1) {
1107 return;
1108 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001109 final float x = ev.getX(pointerIndex);
1110 final float y = ev.getY(pointerIndex);
1111 final int xDiff = (int) Math.abs(x - mLastMotionX);
1112 final int yDiff = (int) Math.abs(y - mLastMotionY);
1113
Adam Cohenf8d28232011-02-01 21:47:00 -08001114 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 boolean xPaged = xDiff > mPagingTouchSlop;
1116 boolean xMoved = xDiff > touchSlop;
1117 boolean yMoved = yDiff > touchSlop;
1118
Adam Cohenf8d28232011-02-01 21:47:00 -08001119 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001120 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 // Scroll if the user moved far enough along the X axis
1122 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001123 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001125 mLastMotionXRemainder = 0;
Michael Jurka8b805b12012-04-18 14:23:14 -07001126 mTouchX = getScrollX();
Michael Jurka0142d492010-08-25 17:46:15 -07001127 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1128 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001129 }
1130 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001131 cancelCurrentPageLongPress();
1132 }
1133 }
1134
1135 protected void cancelCurrentPageLongPress() {
1136 if (mAllowLongPress) {
1137 mAllowLongPress = false;
1138 // Try canceling the long press. It could also have been scheduled
1139 // by a distant descendant, so use the mAllowLongPress flag to block
1140 // everything
1141 final View currentPage = getPageAt(mCurrentPage);
1142 if (currentPage != null) {
1143 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001144 }
1145 }
1146 }
1147
Adam Cohenb5ba0972011-09-07 18:02:31 -07001148 protected float getScrollProgress(int screenCenter, View v, int page) {
1149 final int halfScreenSize = getMeasuredWidth() / 2;
1150
1151 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1152 int delta = screenCenter - (getChildOffset(page) -
1153 getRelativeChildOffset(page) + halfScreenSize);
1154
1155 float scrollProgress = delta / (totalDistance * 1.0f);
1156 scrollProgress = Math.min(scrollProgress, 1.0f);
1157 scrollProgress = Math.max(scrollProgress, -1.0f);
1158 return scrollProgress;
1159 }
1160
Adam Cohene0f66b52010-11-23 15:06:07 -08001161 // This curve determines how the effect of scrolling over the limits of the page dimishes
1162 // as the user pulls further and further from the bounds
1163 private float overScrollInfluenceCurve(float f) {
1164 f -= 1.0f;
1165 return f * f * f + 1.0f;
1166 }
1167
Adam Cohenb5ba0972011-09-07 18:02:31 -07001168 protected void acceleratedOverScroll(float amount) {
1169 int screenSize = getMeasuredWidth();
1170
1171 // We want to reach the max over scroll effect when the user has
1172 // over scrolled half the size of the screen
1173 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1174
1175 if (f == 0) return;
1176
1177 // Clamp this factor, f, to -1 < f < 1
1178 if (Math.abs(f) >= 1) {
1179 f /= Math.abs(f);
1180 }
1181
1182 int overScrollAmount = (int) Math.round(f * screenSize);
1183 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001184 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001185 super.scrollTo(0, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001186 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001187 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001188 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohenb5ba0972011-09-07 18:02:31 -07001189 }
1190 invalidate();
1191 }
1192
1193 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001194 int screenSize = getMeasuredWidth();
1195
1196 float f = (amount / screenSize);
1197
1198 if (f == 0) return;
1199 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1200
Adam Cohen7bfc9792011-01-28 13:52:37 -08001201 // Clamp this factor, f, to -1 < f < 1
1202 if (Math.abs(f) >= 1) {
1203 f /= Math.abs(f);
1204 }
1205
Adam Cohene0f66b52010-11-23 15:06:07 -08001206 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001207 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001208 mOverScrollX = overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001209 super.scrollTo(0, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001210 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001211 mOverScrollX = mMaxScrollX + overScrollAmount;
Michael Jurka8b805b12012-04-18 14:23:14 -07001212 super.scrollTo(mMaxScrollX, getScrollY());
Adam Cohen68d73932010-11-15 10:50:58 -08001213 }
1214 invalidate();
1215 }
1216
Adam Cohenb5ba0972011-09-07 18:02:31 -07001217 protected void overScroll(float amount) {
1218 dampedOverScroll(amount);
1219 }
1220
Michael Jurkac5b262c2011-01-12 20:24:50 -08001221 protected float maxOverScroll() {
1222 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001223 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001224 float f = 1.0f;
1225 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1226 return OVERSCROLL_DAMP_FACTOR * f;
1227 }
1228
Winson Chung321e9ee2010-08-09 13:37:56 -07001229 @Override
1230 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001231 // Skip touch handling if there are no pages to swipe
1232 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1233
Michael Jurkab8f06722010-10-10 15:58:46 -07001234 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001235
1236 final int action = ev.getAction();
1237
1238 switch (action & MotionEvent.ACTION_MASK) {
1239 case MotionEvent.ACTION_DOWN:
1240 /*
1241 * If being flinged and user touches, stop the fling. isFinished
1242 * will be false if being flinged.
1243 */
1244 if (!mScroller.isFinished()) {
1245 mScroller.abortAnimation();
1246 }
1247
1248 // Remember where the motion event started
1249 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001250 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001251 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001253 if (mTouchState == TOUCH_STATE_SCROLLING) {
1254 pageBeginMoving();
1255 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 break;
1257
1258 case MotionEvent.ACTION_MOVE:
1259 if (mTouchState == TOUCH_STATE_SCROLLING) {
1260 // Scroll to follow the motion event
1261 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1262 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001263 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001264
Adam Cohenaefd4e12011-02-14 16:39:38 -08001265 mTotalMotionX += Math.abs(deltaX);
1266
Winson Chungc0844aa2011-02-02 15:25:58 -08001267 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1268 // keep the remainder because we are actually testing if we've moved from the last
1269 // scrolled position (which is discrete).
1270 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001271 mTouchX += deltaX;
1272 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1273 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001274 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001275 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001276 } else {
1277 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001279 mLastMotionX = x;
1280 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 } else {
1282 awakenScrollBars();
1283 }
Adam Cohen564976a2010-10-13 18:52:07 -07001284 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 determineScrollingStart(ev);
1286 }
1287 break;
1288
1289 case MotionEvent.ACTION_UP:
1290 if (mTouchState == TOUCH_STATE_SCROLLING) {
1291 final int activePointerId = mActivePointerId;
1292 final int pointerIndex = ev.findPointerIndex(activePointerId);
1293 final float x = ev.getX(pointerIndex);
1294 final VelocityTracker velocityTracker = mVelocityTracker;
1295 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1296 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001297 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001298 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1299 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1300 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001301
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001302 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1303
Adam Cohen00481b32011-11-18 12:03:48 -08001304 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001305 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001306
Adam Cohenaefd4e12011-02-14 16:39:38 -08001307 // In the case that the page is moved far to one direction and then is flung
1308 // in the opposite direction, we use a threshold to determine whether we should
1309 // just return to the starting page, or if we should skip one further.
1310 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001311 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001312 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001313 returnToOriginalPage = true;
1314 }
1315
Adam Cohenaefd4e12011-02-14 16:39:38 -08001316 int finalPage;
1317 // We give flings precedence over large moves, which is why we short-circuit our
1318 // test for a large move if a fling has been registered. That is, a large
1319 // move to the left and fling to the right will register as a fling to the right.
Winson Chung52aee602013-01-30 12:01:02 -08001320 final boolean isRtl = isLayoutRtl();
1321 boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
1322 boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
1323 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1324 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001325 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1326 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung52aee602013-01-30 12:01:02 -08001327 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1328 (isFling && isVelocityXLeft)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001329 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001330 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1331 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 } else {
1333 snapToDestination();
1334 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001335 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 // at this point we have not moved beyond the touch slop
1337 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1338 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001339 int nextPage = Math.max(0, mCurrentPage - 1);
1340 if (nextPage != mCurrentPage) {
1341 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001342 } else {
1343 snapToDestination();
1344 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001345 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 // at this point we have not moved beyond the touch slop
1347 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1348 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001349 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1350 if (nextPage != mCurrentPage) {
1351 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 } else {
1353 snapToDestination();
1354 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001355 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001356 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001357 }
1358 mTouchState = TOUCH_STATE_REST;
1359 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001360 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 break;
1362
1363 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001364 if (mTouchState == TOUCH_STATE_SCROLLING) {
1365 snapToDestination();
1366 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 mTouchState = TOUCH_STATE_REST;
1368 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001369 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001370 break;
1371
1372 case MotionEvent.ACTION_POINTER_UP:
1373 onSecondaryPointerUp(ev);
1374 break;
1375 }
1376
1377 return true;
1378 }
1379
Winson Chung185d7162011-02-28 13:47:29 -08001380 @Override
1381 public boolean onGenericMotionEvent(MotionEvent event) {
1382 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1383 switch (event.getAction()) {
1384 case MotionEvent.ACTION_SCROLL: {
1385 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1386 final float vscroll;
1387 final float hscroll;
1388 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1389 vscroll = 0;
1390 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1391 } else {
1392 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1393 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1394 }
1395 if (hscroll != 0 || vscroll != 0) {
Winson Chung52aee602013-01-30 12:01:02 -08001396 boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
1397 : (hscroll > 0 || vscroll > 0);
1398 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001399 scrollRight();
1400 } else {
1401 scrollLeft();
1402 }
1403 return true;
1404 }
1405 }
1406 }
1407 }
1408 return super.onGenericMotionEvent(event);
1409 }
1410
Michael Jurkab8f06722010-10-10 15:58:46 -07001411 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1412 if (mVelocityTracker == null) {
1413 mVelocityTracker = VelocityTracker.obtain();
1414 }
1415 mVelocityTracker.addMovement(ev);
1416 }
1417
1418 private void releaseVelocityTracker() {
1419 if (mVelocityTracker != null) {
1420 mVelocityTracker.recycle();
1421 mVelocityTracker = null;
1422 }
1423 }
1424
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 private void onSecondaryPointerUp(MotionEvent ev) {
1426 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1427 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1428 final int pointerId = ev.getPointerId(pointerIndex);
1429 if (pointerId == mActivePointerId) {
1430 // This was our active pointer going up. Choose a new
1431 // active pointer and adjust accordingly.
1432 // TODO: Make this decision more intelligent.
1433 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1434 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1435 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001436 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 mActivePointerId = ev.getPointerId(newPointerIndex);
1438 if (mVelocityTracker != null) {
1439 mVelocityTracker.clear();
1440 }
1441 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001442 }
1443
Michael Jurkad771c962011-08-09 15:00:48 -07001444 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001445
1446 @Override
1447 public void requestChildFocus(View child, View focused) {
1448 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001449 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001450 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001451 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 }
1453 }
1454
Winson Chunge3193b92010-09-10 11:44:42 -07001455 protected int getChildIndexForRelativeOffset(int relativeOffset) {
Winson Chung52aee602013-01-30 12:01:02 -08001456 final boolean isRtl = isLayoutRtl();
Winson Chunge3193b92010-09-10 11:44:42 -07001457 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001458 int left;
1459 int right;
Winson Chung52aee602013-01-30 12:01:02 -08001460 final int startIndex = isRtl ? childCount - 1 : 0;
1461 final int endIndex = isRtl ? -1 : childCount;
1462 final int delta = isRtl ? -1 : 1;
1463 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001464 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001465 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001466 if (left <= relativeOffset && relativeOffset <= right) {
1467 return i;
1468 }
Winson Chunge3193b92010-09-10 11:44:42 -07001469 }
1470 return -1;
1471 }
1472
Winson Chung1908d072011-02-24 18:09:44 -08001473 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001474 // This functions are called enough times that it actually makes a difference in the
1475 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001476 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001477 final int minWidth = mMinimumWidth;
1478 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001479 }
1480
Adam Cohend19d3ca2010-09-15 14:43:42 -07001481 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001482 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 int minDistanceFromScreenCenterIndex = -1;
Michael Jurka8b805b12012-04-18 14:23:14 -07001484 int screenCenter = getScrollX() + (getMeasuredWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001485 final int childCount = getChildCount();
1486 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001487 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001488 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 int halfChildWidth = (childWidth / 2);
1490 int childCenter = getChildOffset(i) + halfChildWidth;
1491 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1492 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1493 minDistanceFromScreenCenter = distanceFromScreenCenter;
1494 minDistanceFromScreenCenterIndex = i;
1495 }
1496 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001497 return minDistanceFromScreenCenterIndex;
1498 }
1499
1500 protected void snapToDestination() {
1501 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 }
1503
Adam Cohene0f66b52010-11-23 15:06:07 -08001504 private static class ScrollInterpolator implements Interpolator {
1505 public ScrollInterpolator() {
1506 }
1507
1508 public float getInterpolation(float t) {
1509 t -= 1.0f;
1510 return t*t*t*t*t + 1;
1511 }
1512 }
1513
1514 // We want the duration of the page snap animation to be influenced by the distance that
1515 // the screen has to travel, however, we don't want this duration to be effected in a
1516 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1517 // of travel has on the overall snap duration.
1518 float distanceInfluenceForSnapDuration(float f) {
1519 f -= 0.5f; // center the values about 0.
1520 f *= 0.3f * Math.PI / 2.0f;
1521 return (float) Math.sin(f);
1522 }
1523
Michael Jurka0142d492010-08-25 17:46:15 -07001524 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001525 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1526 int halfScreenSize = getMeasuredWidth() / 2;
1527
Winson Chung785d2eb2011-04-14 16:08:02 -07001528 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1529 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1530 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001531 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1532 int delta = newX - mUnboundedScrollX;
1533 int duration = 0;
1534
Adam Cohen265b9a62011-12-07 14:37:18 -08001535 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001536 // If the velocity is low enough, then treat this more as an automatic page advance
1537 // as opposed to an apparent physical response to flinging
1538 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1539 return;
1540 }
1541
1542 // Here we compute a "distance" that will be used in the computation of the overall
1543 // snap duration. This is a function of the actual distance that needs to be traveled;
1544 // we keep this value close to half screen size in order to reduce the variance in snap
1545 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001546 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001547 float distance = halfScreenSize + halfScreenSize *
1548 distanceInfluenceForSnapDuration(distanceRatio);
1549
1550 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001551 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001552
1553 // we want the page's snap velocity to approximately match the velocity at which the
1554 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001555 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1556 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Chet Haase97687ef2012-10-25 16:01:11 -07001557 duration = Math.min(duration, MAX_PAGE_SNAP_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08001558
1559 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001560 }
1561
1562 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001563 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001564 }
1565
Michael Jurka0142d492010-08-25 17:46:15 -07001566 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001567 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001568
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1570 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1571 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001572 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001573 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001574 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001575 snapToPage(whichPage, delta, duration);
1576 }
1577
1578 protected void snapToPage(int whichPage, int delta, int duration) {
1579 mNextPage = whichPage;
1580
1581 View focusedChild = getFocusedChild();
1582 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001583 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001584 focusedChild.clearFocus();
1585 }
1586
1587 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001588 awakenScrollBars(duration);
1589 if (duration == 0) {
1590 duration = Math.abs(delta);
1591 }
1592
1593 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001594 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001595
Winson Chungb44b5242011-06-13 11:32:14 -07001596 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1597 // loading associated pages until the scroll settles
1598 if (mDeferScrollUpdate) {
1599 loadAssociatedPages(mNextPage);
1600 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001601 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001602 }
Michael Jurka0142d492010-08-25 17:46:15 -07001603 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001604 invalidate();
1605 }
1606
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 public void scrollLeft() {
1608 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001609 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001610 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001611 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001612 }
1613 }
1614
1615 public void scrollRight() {
1616 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001617 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001618 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001619 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001620 }
1621 }
1622
Winson Chung86f77532010-08-24 11:08:22 -07001623 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001624 int result = -1;
1625 if (v != null) {
1626 ViewParent vp = v.getParent();
1627 int count = getChildCount();
1628 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001629 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001630 return i;
1631 }
1632 }
1633 }
1634 return result;
1635 }
1636
1637 /**
1638 * @return True is long presses are still allowed for the current touch
1639 */
1640 public boolean allowLongPress() {
1641 return mAllowLongPress;
1642 }
1643
Michael Jurka0142d492010-08-25 17:46:15 -07001644 /**
1645 * Set true to allow long-press events to be triggered, usually checked by
1646 * {@link Launcher} to accept or block dpad-initiated long-presses.
1647 */
1648 public void setAllowLongPress(boolean allowLongPress) {
1649 mAllowLongPress = allowLongPress;
1650 }
1651
Winson Chung321e9ee2010-08-09 13:37:56 -07001652 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001653 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001654
1655 SavedState(Parcelable superState) {
1656 super(superState);
1657 }
1658
1659 private SavedState(Parcel in) {
1660 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001661 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001662 }
1663
1664 @Override
1665 public void writeToParcel(Parcel out, int flags) {
1666 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001667 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001668 }
1669
1670 public static final Parcelable.Creator<SavedState> CREATOR =
1671 new Parcelable.Creator<SavedState>() {
1672 public SavedState createFromParcel(Parcel in) {
1673 return new SavedState(in);
1674 }
1675
1676 public SavedState[] newArray(int size) {
1677 return new SavedState[size];
1678 }
1679 };
1680 }
1681
Winson Chungf314b0e2011-08-16 11:54:27 -07001682 protected void loadAssociatedPages(int page) {
1683 loadAssociatedPages(page, false);
1684 }
1685 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001686 if (mContentIsRefreshable) {
1687 final int count = getChildCount();
1688 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001689 int lowerPageBound = getAssociatedLowerPageBound(page);
1690 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001691 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1692 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001693 // First, clear any pages that should no longer be loaded
1694 for (int i = 0; i < count; ++i) {
1695 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001696 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001697 if (layout.getPageChildCount() > 0) {
1698 layout.removeAllViewsOnPage();
1699 }
1700 mDirtyPageContent.set(i, true);
1701 }
1702 }
1703 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001704 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001705 if ((i != page) && immediateAndOnly) {
1706 continue;
1707 }
Michael Jurka0142d492010-08-25 17:46:15 -07001708 if (lowerPageBound <= i && i <= upperPageBound) {
1709 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001710 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001711 mDirtyPageContent.set(i, false);
1712 }
Winson Chung86f77532010-08-24 11:08:22 -07001713 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001714 }
1715 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001716 }
1717 }
1718
Winson Chunge3193b92010-09-10 11:44:42 -07001719 protected int getAssociatedLowerPageBound(int page) {
1720 return Math.max(0, page - 1);
1721 }
1722 protected int getAssociatedUpperPageBound(int page) {
1723 final int count = getChildCount();
1724 return Math.min(page + 1, count - 1);
1725 }
1726
Winson Chung86f77532010-08-24 11:08:22 -07001727 /**
1728 * This method is called ONLY to synchronize the number of pages that the paged view has.
1729 * To actually fill the pages with information, implement syncPageItems() below. It is
1730 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1731 * and therefore, individual page items do not need to be updated in this method.
1732 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001733 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001734
1735 /**
1736 * This method is called to synchronize the items that are on a particular page. If views on
1737 * the page can be reused, then they should be updated within this method.
1738 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001739 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001740
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001741 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001742 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001743 }
1744 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001745 invalidatePageData(currentPage, false);
1746 }
1747 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001748 if (!mIsDataReady) {
1749 return;
1750 }
1751
Michael Jurka0142d492010-08-25 17:46:15 -07001752 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001753 // Force all scrolling-related behavior to end
1754 mScroller.forceFinished(true);
1755 mNextPage = INVALID_PAGE;
1756
Michael Jurka0142d492010-08-25 17:46:15 -07001757 // Update all the pages
1758 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001759
Winson Chung5a808352011-06-27 19:08:49 -07001760 // We must force a measure after we've loaded the pages to update the content width and
1761 // to determine the full scroll width
1762 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1763 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1764
1765 // Set a new page as the current page if necessary
1766 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001767 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001768 }
1769
Michael Jurka0142d492010-08-25 17:46:15 -07001770 // Mark each of the pages as dirty
1771 final int count = getChildCount();
1772 mDirtyPageContent.clear();
1773 for (int i = 0; i < count; ++i) {
1774 mDirtyPageContent.add(true);
1775 }
1776
1777 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001778 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001779 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001780 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001781 }
Winson Chung007c6982011-06-14 13:27:53 -07001782
Michael Jurkaafaa0502011-12-13 18:22:50 -08001783 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001784 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1785 // found
1786 if (mHasScrollIndicator && mScrollIndicator == null) {
1787 ViewGroup parent = (ViewGroup) getParent();
Winson Chunga128a7b2012-04-30 15:23:15 -07001788 if (parent != null) {
1789 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
1790 mHasScrollIndicator = mScrollIndicator != null;
1791 if (mHasScrollIndicator) {
1792 mScrollIndicator.setVisibility(View.VISIBLE);
1793 }
Winson Chung007c6982011-06-14 13:27:53 -07001794 }
1795 }
1796 return mScrollIndicator;
1797 }
1798
1799 protected boolean isScrollingIndicatorEnabled() {
Michael Jurkab1dfe252012-09-26 10:53:55 -07001800 return true;
Winson Chung007c6982011-06-14 13:27:53 -07001801 }
1802
Winson Chung5a808352011-06-27 19:08:49 -07001803 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1804 @Override
1805 public void run() {
1806 hideScrollingIndicator(false);
1807 }
1808 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001809 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001810 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001811 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001812 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001813 }
1814
Michael Jurka430e8a52011-08-08 15:52:14 -07001815 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001816 mShouldShowScrollIndicator = true;
1817 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001818 if (getChildCount() <= 1) return;
1819 if (!isScrollingIndicatorEnabled()) return;
1820
Michael Jurkabed61d22012-02-14 22:51:29 -08001821 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001822 getScrollingIndicator();
1823 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001824 // Fade the indicator in
1825 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001826 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001827 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001828 if (immediately || mScrollingPaused) {
Michael Jurka430e8a52011-08-08 15:52:14 -07001829 mScrollIndicator.setAlpha(1f);
1830 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001831 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f);
Michael Jurka430e8a52011-08-08 15:52:14 -07001832 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1833 mScrollIndicatorAnimator.start();
1834 }
Winson Chung007c6982011-06-14 13:27:53 -07001835 }
1836 }
1837
Adam Cohen21b41102011-11-01 17:29:52 -07001838 protected void cancelScrollingIndicatorAnimations() {
1839 if (mScrollIndicatorAnimator != null) {
1840 mScrollIndicatorAnimator.cancel();
1841 }
1842 }
1843
Winson Chung007c6982011-06-14 13:27:53 -07001844 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001845 if (getChildCount() <= 1) return;
1846 if (!isScrollingIndicatorEnabled()) return;
1847
1848 getScrollingIndicator();
1849 if (mScrollIndicator != null) {
1850 // Fade the indicator out
1851 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001852 cancelScrollingIndicatorAnimations();
Chet Haasebc2f0822012-10-26 17:59:53 -07001853 if (immediately || mScrollingPaused) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001854 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001855 mScrollIndicator.setAlpha(0f);
1856 } else {
Michael Jurka2ecf9952012-06-18 12:52:28 -07001857 mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f);
Winson Chung32174c82011-07-19 15:47:55 -07001858 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1859 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1860 private boolean cancelled = false;
1861 @Override
1862 public void onAnimationCancel(android.animation.Animator animation) {
1863 cancelled = true;
1864 }
1865 @Override
1866 public void onAnimationEnd(Animator animation) {
1867 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001868 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001869 }
1870 }
1871 });
1872 mScrollIndicatorAnimator.start();
1873 }
Winson Chung007c6982011-06-14 13:27:53 -07001874 }
1875 }
1876
Winson Chung32174c82011-07-19 15:47:55 -07001877 /**
1878 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1879 * fill its space on the track or not.
1880 */
1881 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001882 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001883 }
1884
Winson Chung007c6982011-06-14 13:27:53 -07001885 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001886 if (getChildCount() <= 1) return;
1887 if (!isScrollingIndicatorEnabled()) return;
1888
1889 getScrollingIndicator();
1890 if (mScrollIndicator != null) {
1891 updateScrollingIndicatorPosition();
1892 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001893 if (mShouldShowScrollIndicator) {
1894 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1895 }
Winson Chung007c6982011-06-14 13:27:53 -07001896 }
1897
Winson Chung007c6982011-06-14 13:27:53 -07001898 private void updateScrollingIndicatorPosition() {
Winson Chung52aee602013-01-30 12:01:02 -08001899 final boolean isRtl = isLayoutRtl();
Winson Chung649723c2011-07-06 20:41:23 -07001900 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001901 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001902 int numPages = getChildCount();
1903 int pageWidth = getMeasuredWidth();
Winson Chungf5f8cef2011-07-22 11:16:13 -07001904 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001905 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1906 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001907
Winson Chung52aee602013-01-30 12:01:02 -08001908 float scrollPos = isRtl ? mMaxScrollX - getScrollX() : getScrollX();
1909 float offset = Math.max(0f, Math.min(1f, (float) scrollPos / mMaxScrollX));
1910 if (isRtl) {
1911 offset = 1f - offset;
1912 }
Winson Chung32174c82011-07-19 15:47:55 -07001913 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001914 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001915 if (hasElasticScrollIndicator()) {
1916 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1917 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1918 mScrollIndicator.requestLayout();
1919 }
1920 } else {
1921 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1922 indicatorPos += indicatorCenterOffset;
1923 }
Winson Chung007c6982011-06-14 13:27:53 -07001924 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001925 }
1926
Winson Chung3ac74c52011-06-30 17:39:37 -07001927 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001928 }
1929
1930 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001931 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001932
1933 /* Accessibility */
1934 @Override
1935 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1936 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001937 info.setScrollable(getPageCount() > 1);
1938 if (getCurrentPage() < getPageCount() - 1) {
1939 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1940 }
1941 if (getCurrentPage() > 0) {
1942 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1943 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001944 }
1945
1946 @Override
1947 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1948 super.onInitializeAccessibilityEvent(event);
1949 event.setScrollable(true);
1950 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1951 event.setFromIndex(mCurrentPage);
1952 event.setToIndex(mCurrentPage);
1953 event.setItemCount(getChildCount());
1954 }
1955 }
1956
Svetoslav Ganov08055f62012-05-15 11:06:36 -07001957 @Override
1958 public boolean performAccessibilityAction(int action, Bundle arguments) {
1959 if (super.performAccessibilityAction(action, arguments)) {
1960 return true;
1961 }
1962 switch (action) {
1963 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
1964 if (getCurrentPage() < getPageCount() - 1) {
1965 scrollRight();
1966 return true;
1967 }
1968 } break;
1969 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
1970 if (getCurrentPage() > 0) {
1971 scrollLeft();
1972 return true;
1973 }
1974 } break;
1975 }
1976 return false;
1977 }
1978
Winson Chung6a0f57d2011-06-29 20:10:49 -07001979 protected String getCurrentPageDescription() {
Michael Jurka8b805b12012-04-18 14:23:14 -07001980 return String.format(getContext().getString(R.string.default_scroll_format),
Winson Chung360e63f2012-04-27 13:48:05 -07001981 getNextPage() + 1, getChildCount());
Winson Chung6a0f57d2011-06-29 20:10:49 -07001982 }
Winson Chungd11265e2011-08-30 13:37:23 -07001983
1984 @Override
1985 public boolean onHoverEvent(android.view.MotionEvent event) {
1986 return true;
1987 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001988}