blob: 1fc39f600f4701afe5c71b9bef2fcfeeafa44dd3 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070022import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070024import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080031import android.view.InputDevice;
32import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070039import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070040import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chung04998342011-01-05 13:54:43 -080045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung6a0f57d2011-06-29 20:10:49 -070047import java.util.ArrayList;
48
Winson Chung321e9ee2010-08-09 13:37:56 -070049/**
50 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070051 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070052 */
53public abstract class PagedView extends ViewGroup {
54 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070055 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chung86f77532010-08-24 11:08:22 -070058 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070059 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Winson Chungf0c6ae02012-03-21 16:10:31 -070061 protected static final int PAGE_SNAP_ANIMATION_DURATION = 550;
62 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohenb5ba0972011-09-07 18:02:31 -070065 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070066 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Winson Chung867ca622012-02-21 15:48:35 -080067
Adam Cohenb64cb5a2011-02-15 13:53:42 -080068 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080069 // The page is moved more than halfway, automatically move to the next page on touch up.
70 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080071
Adam Cohen265b9a62011-12-07 14:37:18 -080072 // The following constants need to be scaled based on density. The scaled versions will be
73 // assigned to the corresponding member variables below.
74 private static final int FLING_THRESHOLD_VELOCITY = 500;
75 private static final int MIN_SNAP_VELOCITY = 1500;
76 private static final int MIN_FLING_VELOCITY = 250;
77
78 protected int mFlingThresholdVelocity;
79 protected int mMinFlingVelocity;
80 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070081
Adam Cohenb5ba0972011-09-07 18:02:31 -070082 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070083 protected float mSmoothingTime;
84 protected float mTouchX;
85
86 protected boolean mFirstLayout = true;
87
88 protected int mCurrentPage;
89 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080090 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070092 private VelocityTracker mVelocityTracker;
93
94 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080095 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080096 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080098 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070099 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -0700100 private int[] mChildOffsets;
101 private int[] mChildRelativeOffsets;
102 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 protected final static int TOUCH_STATE_REST = 0;
105 protected final static int TOUCH_STATE_SCROLLING = 1;
106 protected final static int TOUCH_STATE_PREV_PAGE = 2;
107 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700108 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700111 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurka0142d492010-08-25 17:46:15 -0700113 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka7426c422010-11-11 15:23:47 -0800115 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka7426c422010-11-11 15:23:47 -0800117 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 private int mPagingTouchSlop;
119 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800120 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700121 protected int mPageSpacing;
122 protected int mPageLayoutPaddingTop;
123 protected int mPageLayoutPaddingBottom;
124 protected int mPageLayoutPaddingLeft;
125 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700126 protected int mPageLayoutWidthGap;
127 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700128 protected int mCellCountX = 0;
129 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700130 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800131 protected boolean mAllowOverScroll = true;
132 protected int mUnboundedScrollX;
Michael Jurkadde558b2011-11-09 22:09:06 -0800133 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Adam Cohenebea84d2011-11-09 17:20:41 -0800135 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
136 // it is equal to the scaled overscroll position. We use a separate value so as to prevent
137 // the screens from continuing to translate beyond the normal bounds.
138 protected int mOverScrollX;
139
Michael Jurka8c920dd2011-01-20 14:16:56 -0800140 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800141 protected float mLayoutScale = 1.0f;
142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Michael Jurka5f1c5092010-09-03 14:15:02 -0700145 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Winson Chung86f77532010-08-24 11:08:22 -0700147 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700148
Michael Jurkae326f182011-11-21 14:05:46 -0800149 protected ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150
Michael Jurka0142d492010-08-25 17:46:15 -0700151 // If true, syncPages and syncPageItems will be called to refresh pages
152 protected boolean mContentIsRefreshable = true;
153
154 // If true, modify alpha of neighboring pages as user scrolls left/right
155 protected boolean mFadeInAdjacentScreens = true;
156
157 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
158 // to switch to a new page
159 protected boolean mUsePagingTouchSlop = true;
160
161 // If true, the subclass should directly update mScrollX itself in its computeScroll method
162 // (SmoothPagedView does this)
163 protected boolean mDeferScrollUpdate = false;
164
Patrick Dubroy1262e362010-10-06 15:49:50 -0700165 protected boolean mIsPageMoving = false;
166
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 // All syncs and layout passes are deferred until data is ready.
168 protected boolean mIsDataReady = false;
169
Winson Chung007c6982011-06-14 13:27:53 -0700170 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700171 private ValueAnimator mScrollIndicatorAnimator;
Michael Jurkaafaa0502011-12-13 18:22:50 -0800172 private View mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700173 private int mScrollIndicatorPaddingLeft;
174 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700175 private boolean mHasScrollIndicator = true;
Michael Jurkabed61d22012-02-14 22:51:29 -0800176 private boolean mShouldShowScrollIndicator = false;
177 private boolean mShouldShowScrollIndicatorImmediately = false;
Winson Chunga6427b12011-07-27 10:53:39 -0700178 protected static final int sScrollIndicatorFadeInDuration = 150;
179 protected static final int sScrollIndicatorFadeOutDuration = 650;
180 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700181
Winson Chungb44b5242011-06-13 11:32:14 -0700182 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700183 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700184
Winson Chung86f77532010-08-24 11:08:22 -0700185 public interface PageSwitchListener {
186 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 }
188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 public PagedView(Context context) {
190 this(context, null);
191 }
192
193 public PagedView(Context context, AttributeSet attrs) {
194 this(context, attrs, 0);
195 }
196
197 public PagedView(Context context, AttributeSet attrs, int defStyle) {
198 super(context, attrs, defStyle);
199
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.PagedView, defStyle, 0);
Adam Cohen60b07122011-11-14 17:26:06 -0800202 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800204 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800206 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700211 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700213 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700214 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700215 mScrollIndicatorPaddingLeft =
216 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
217 mScrollIndicatorPaddingRight =
218 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700219 a.recycle();
220
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700222 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224
225 /**
226 * Initializes various states for this workspace.
227 */
Michael Jurka0142d492010-08-25 17:46:15 -0700228 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700229 mDirtyPageContent = new ArrayList<Boolean>();
230 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800231 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700232 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700233 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700234
235 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
236 mTouchSlop = configuration.getScaledTouchSlop();
237 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
238 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700239 mDensity = getResources().getDisplayMetrics().density;
Adam Cohen265b9a62011-12-07 14:37:18 -0800240
241 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
242 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity);
243 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity);
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 }
245
Winson Chung86f77532010-08-24 11:08:22 -0700246 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
247 mPageSwitchListener = pageSwitchListener;
248 if (mPageSwitchListener != null) {
249 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 }
251 }
252
253 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700254 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
255 * out pages.
256 */
257 protected void setDataIsReady() {
258 mIsDataReady = true;
259 }
260 protected boolean isDataReady() {
261 return mIsDataReady;
262 }
263
264 /**
Winson Chung86f77532010-08-24 11:08:22 -0700265 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 *
Winson Chung86f77532010-08-24 11:08:22 -0700267 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 */
Winson Chung86f77532010-08-24 11:08:22 -0700269 int getCurrentPage() {
270 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 }
272
Winson Chung86f77532010-08-24 11:08:22 -0700273 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 return getChildCount();
275 }
276
Winson Chung86f77532010-08-24 11:08:22 -0700277 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 return getChildAt(index);
279 }
280
Adam Cohenae4f1552011-10-20 00:15:42 -0700281 protected int indexToPage(int index) {
282 return index;
283 }
284
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800286 * Updates the scroll of the current page immediately to its final scroll position. We use this
287 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
288 * the previous tab page.
289 */
290 protected void updateCurrentPageScroll() {
291 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
292 scrollTo(newX, 0);
293 mScroller.setFinalX(newX);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800294 mScroller.forceFinished(true);
Winson Chungbbc60d82010-11-11 16:34:41 -0800295 }
296
297 /**
Winson Chung86f77532010-08-24 11:08:22 -0700298 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 */
Winson Chung86f77532010-08-24 11:08:22 -0700300 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800301 if (!mScroller.isFinished()) {
302 mScroller.abortAnimation();
303 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800304 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
305 // the default
306 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800307 return;
308 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700309
Winson Chung86f77532010-08-24 11:08:22 -0700310 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800311 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700312 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700313 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800314 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 }
316
Michael Jurka0142d492010-08-25 17:46:15 -0700317 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700318 if (mPageSwitchListener != null) {
319 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
321 }
322
Michael Jurkace7e05f2011-02-01 22:02:35 -0800323 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700324 if (!mIsPageMoving) {
325 mIsPageMoving = true;
326 onPageBeginMoving();
327 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700328 }
329
Michael Jurkace7e05f2011-02-01 22:02:35 -0800330 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700331 if (mIsPageMoving) {
332 mIsPageMoving = false;
333 onPageEndMoving();
334 }
Michael Jurka0142d492010-08-25 17:46:15 -0700335 }
336
Adam Cohen26976d92011-03-22 15:33:33 -0700337 protected boolean isPageMoving() {
338 return mIsPageMoving;
339 }
340
Michael Jurka0142d492010-08-25 17:46:15 -0700341 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700342 protected void onPageBeginMoving() {
343 }
344
345 // a method that subclasses can override to add behavior
346 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700347 }
348
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 /**
Winson Chung86f77532010-08-24 11:08:22 -0700350 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 *
352 * @param l The listener used to respond to long clicks.
353 */
354 @Override
355 public void setOnLongClickListener(OnLongClickListener l) {
356 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700357 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700359 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 }
361 }
362
363 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800364 public void scrollBy(int x, int y) {
365 scrollTo(mUnboundedScrollX + x, mScrollY + y);
366 }
367
368 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700369 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800370 mUnboundedScrollX = x;
371
372 if (x < 0) {
373 super.scrollTo(0, y);
374 if (mAllowOverScroll) {
375 overScroll(x);
376 }
377 } else if (x > mMaxScrollX) {
378 super.scrollTo(mMaxScrollX, y);
379 if (mAllowOverScroll) {
380 overScroll(x - mMaxScrollX);
381 }
382 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -0800383 mOverScrollX = x;
Adam Cohen68d73932010-11-15 10:50:58 -0800384 super.scrollTo(x, y);
385 }
386
Michael Jurka0142d492010-08-25 17:46:15 -0700387 mTouchX = x;
388 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
389 }
390
391 // we moved this functionality to a helper function so SmoothPagedView can reuse it
392 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700394 // Don't bother scrolling if the page does not need to be moved
Michael Jurkab06d95f2012-04-02 06:26:53 -0700395 if (mScrollX != mScroller.getCurrX()
396 || mScrollY != mScroller.getCurrY()
397 || mOverScrollX != mScroller.getCurrX()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700398 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
399 }
Michael Jurka0142d492010-08-25 17:46:15 -0700400 invalidate();
401 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700402 } else if (mNextPage != INVALID_PAGE) {
403 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700404 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700405 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700406
407 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700408 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700409 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700410 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700411 }
412
Adam Cohen73aa9752010-11-24 16:26:58 -0800413 // We don't want to trigger a page end moving unless the page has settled
414 // and the user has stopped scrolling
415 if (mTouchState == TOUCH_STATE_REST) {
416 pageEndMoving();
417 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700418
419 // Notify the user when the page changes
420 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
421 AccessibilityEvent ev =
422 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
423 ev.getText().add(getCurrentPageDescription());
424 sendAccessibilityEventUnchecked(ev);
425 }
Michael Jurka0142d492010-08-25 17:46:15 -0700426 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
Michael Jurka0142d492010-08-25 17:46:15 -0700428 return false;
429 }
430
431 @Override
432 public void computeScroll() {
433 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
435
436 @Override
437 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700438 if (!mIsDataReady) {
439 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
440 return;
441 }
442
Winson Chung321e9ee2010-08-09 13:37:56 -0700443 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
444 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
445 if (widthMode != MeasureSpec.EXACTLY) {
446 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
447 }
448
Adam Lesinski6b879f02010-11-04 16:15:23 -0700449 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
450 * of the All apps view on XLarge displays to not take up more space then it needs. Width
451 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
452 * each page to have the same width.
453 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700455 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
456 int maxChildHeight = 0;
457
458 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700459 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700460
Michael Jurka36fcb742011-04-06 17:08:58 -0700461
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 final int childCount = getChildCount();
466 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700467 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700468 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700469 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
470
471 int childWidthMode;
472 if (lp.width == LayoutParams.WRAP_CONTENT) {
473 childWidthMode = MeasureSpec.AT_MOST;
474 } else {
475 childWidthMode = MeasureSpec.EXACTLY;
476 }
477
478 int childHeightMode;
479 if (lp.height == LayoutParams.WRAP_CONTENT) {
480 childHeightMode = MeasureSpec.AT_MOST;
481 } else {
482 childHeightMode = MeasureSpec.EXACTLY;
483 }
484
485 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700486 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700487 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700488 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700489
490 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700491 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700492 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
493 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700494 }
495
496 if (heightMode == MeasureSpec.AT_MOST) {
497 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 }
Winson Chungae890b82011-09-13 18:08:54 -0700499
Winson Chungae890b82011-09-13 18:08:54 -0700500 setMeasuredDimension(widthSize, heightSize);
501
Adam Cohen25b29952011-11-02 14:49:06 -0700502 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
503 // We also wait until we set the measured dimensions before flushing the cache as well, to
504 // ensure that the cache is filled with good values.
505 invalidateCachedOffsets();
506 updateScrollingIndicatorPosition();
507
Adam Cohenfaa28302010-11-19 12:02:18 -0800508 if (childCount > 0) {
509 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
510 } else {
511 mMaxScrollX = 0;
512 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 }
514
Michael Jurka8c920dd2011-01-20 14:16:56 -0800515 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800516 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
517 int delta = newX - mScrollX;
518
Michael Jurka8c920dd2011-01-20 14:16:56 -0800519 final int pageCount = getChildCount();
520 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700521 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800522 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800523 }
524 setCurrentPage(newCurrentPage);
525 }
526
Michael Jurka8c920dd2011-01-20 14:16:56 -0800527 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
528 // 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 -0800529 // tightens the layout accordingly
530 public void setLayoutScale(float childrenScale) {
531 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700532 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533
534 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
535 int childCount = getChildCount();
536 float childrenX[] = new float[childCount];
537 float childrenY[] = new float[childCount];
538 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700539 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800540 childrenX[i] = child.getX();
541 childrenY[i] = child.getY();
542 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700543 // Trigger a full re-layout (never just call onLayout directly!)
544 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
545 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
546 requestLayout();
547 measure(widthSpec, heightSpec);
548 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800549 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700550 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800551 child.setX(childrenX[i]);
552 child.setY(childrenY[i]);
553 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700554
Michael Jurkad3ef3062010-11-23 16:23:58 -0800555 // Also, the page offset has changed (since the pages are now smaller);
556 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800557 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558 }
559
Adam Cohen60b07122011-11-14 17:26:06 -0800560 public void setPageSpacing(int pageSpacing) {
561 mPageSpacing = pageSpacing;
562 invalidateCachedOffsets();
563 }
564
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700566 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700567 if (!mIsDataReady) {
568 return;
569 }
570
Winson Chung785d2eb2011-04-14 16:08:02 -0700571 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700572 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 final int childCount = getChildCount();
574 int childLeft = 0;
575 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700576 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
577 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700578 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700579
580 // Calculate the variable page spacing if necessary
581 if (mPageSpacing < 0) {
Winson Chungeecf02d2012-03-02 17:14:58 -0800582 // The gap between pages in the PagedView should be equal to the gap from the page
583 // to the edge of the screen (so it is not visible in the current screen). To
584 // account for unequal padding on each side of the paged view, we take the maximum
585 // of the left/right gap and use that as the gap between each page.
586 int offset = getRelativeChildOffset(0);
587 int spacing = Math.max(offset, (right - left) - offset -
588 getChildAt(0).getMeasuredWidth());
589 setPageSpacing(spacing);
Winson Chungae890b82011-09-13 18:08:54 -0700590 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700591 }
592
593 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700594 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800596 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700597 final int childHeight = child.getMeasuredHeight();
598 int childTop = mPaddingTop;
599 if (mCenterPagesVertically) {
600 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
601 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800602
Winson Chung785d2eb2011-04-14 16:08:02 -0700603 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700604 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800605 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700606 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700607 }
608 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700609
610 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
611 setHorizontalScrollBarEnabled(false);
Michael Jurkadd6c0912012-01-16 06:46:20 -0800612 updateCurrentPageScroll();
Winson Chungc3665fa2011-09-14 17:56:27 -0700613 setHorizontalScrollBarEnabled(true);
614 mFirstLayout = false;
615 }
616
Michael Jurkad3ef3062010-11-23 16:23:58 -0800617 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
618 mFirstLayout = false;
619 }
Winson Chunge3193b92010-09-10 11:44:42 -0700620 }
621
Adam Cohenf34bab52010-09-30 14:11:56 -0700622 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700623 if (isScrollingIndicatorEnabled()) {
624 updateScrollingIndicator();
625 }
626 if (mFadeInAdjacentScreens) {
627 for (int i = 0; i < getChildCount(); i++) {
628 View child = getChildAt(i);
629 if (child != null) {
630 float scrollProgress = getScrollProgress(screenCenter, child, i);
631 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -0800632 child.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -0700633 }
634 }
635 invalidate();
636 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700637 }
638
Winson Chunge3193b92010-09-10 11:44:42 -0700639 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700640 protected void onViewAdded(View child) {
641 super.onViewAdded(child);
642
643 // This ensures that when children are added, they get the correct transforms / alphas
644 // in accordance with any scroll effects.
645 mForceScreenScrolled = true;
646 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700647 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700648 }
649
Adam Cohen73894962011-10-31 13:17:17 -0700650 protected void invalidateCachedOffsets() {
651 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700652 if (count == 0) {
653 mChildOffsets = null;
654 mChildRelativeOffsets = null;
655 mChildOffsetsWithLayoutScale = null;
656 return;
657 }
Adam Cohen73894962011-10-31 13:17:17 -0700658
659 mChildOffsets = new int[count];
660 mChildRelativeOffsets = new int[count];
661 mChildOffsetsWithLayoutScale = new int[count];
662 for (int i = 0; i < count; i++) {
663 mChildOffsets[i] = -1;
664 mChildRelativeOffsets[i] = -1;
665 mChildOffsetsWithLayoutScale[i] = -1;
666 }
667 }
668
669 protected int getChildOffset(int index) {
670 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
671 mChildOffsets : mChildOffsetsWithLayoutScale;
672
673 if (childOffsets != null && childOffsets[index] != -1) {
674 return childOffsets[index];
675 } else {
676 if (getChildCount() == 0)
677 return 0;
678
679 int offset = getRelativeChildOffset(0);
680 for (int i = 0; i < index; ++i) {
681 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
682 }
683 if (childOffsets != null) {
684 childOffsets[index] = offset;
685 }
686 return offset;
687 }
688 }
689
690 protected int getRelativeChildOffset(int index) {
691 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
692 return mChildRelativeOffsets[index];
693 } else {
694 final int padding = mPaddingLeft + mPaddingRight;
695 final int offset = mPaddingLeft +
696 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
697 if (mChildRelativeOffsets != null) {
698 mChildRelativeOffsets[index] = offset;
699 }
700 return offset;
701 }
702 }
703
704 protected int getScaledRelativeChildOffset(int index) {
705 final int padding = mPaddingLeft + mPaddingRight;
706 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
707 getScaledMeasuredWidth(getPageAt(index))) / 2;
708 return offset;
709 }
710
711 protected int getScaledMeasuredWidth(View child) {
712 // This functions are called enough times that it actually makes a difference in the
713 // profiler -- so just inline the max() here
714 final int measuredWidth = child.getMeasuredWidth();
715 final int minWidth = mMinimumWidth;
716 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
717 return (int) (maxWidth * mLayoutScale + 0.5f);
718 }
719
Michael Jurkadde558b2011-11-09 22:09:06 -0800720 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700721 final int pageCount = getChildCount();
Michael Jurka4ff7d792012-04-02 03:46:50 -0700722
Michael Jurkac4fb9172010-09-02 17:19:20 -0700723 if (pageCount > 0) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700724 final int screenWidth = getMeasuredWidth();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700725 int leftScreen = 0;
726 int rightScreen = 0;
Michael Jurka47f74742012-03-02 13:39:13 -0800727 View currPage = getPageAt(leftScreen);
Michael Jurka80c69852011-12-16 14:16:32 -0800728 while (leftScreen < pageCount - 1 &&
Michael Jurka4ff7d792012-04-02 03:46:50 -0700729 currPage.getX() + currPage.getWidth() - currPage.getPaddingRight() < mScrollX) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700730 leftScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800731 currPage = getPageAt(leftScreen);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700732 }
733 rightScreen = leftScreen;
Michael Jurka47f74742012-03-02 13:39:13 -0800734 currPage = getPageAt(rightScreen + 1);
Michael Jurka80c69852011-12-16 14:16:32 -0800735 while (rightScreen < pageCount - 1 &&
Michael Jurka4ff7d792012-04-02 03:46:50 -0700736 currPage.getX() - currPage.getPaddingLeft() < mScrollX + screenWidth) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700737 rightScreen++;
Michael Jurka47f74742012-03-02 13:39:13 -0800738 currPage = getPageAt(rightScreen + 1);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700739 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800740 range[0] = leftScreen;
741 range[1] = rightScreen;
742 } else {
743 range[0] = -1;
744 range[1] = -1;
745 }
746 }
747
748 @Override
749 protected void dispatchDraw(Canvas canvas) {
750 int halfScreenSize = getMeasuredWidth() / 2;
Adam Cohenebea84d2011-11-09 17:20:41 -0800751 // mOverScrollX is equal to mScrollX when we're within the normal scroll range. Otherwise
752 // it is equal to the scaled overscroll position.
753 int screenCenter = mOverScrollX + halfScreenSize;
Michael Jurkadde558b2011-11-09 22:09:06 -0800754
755 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Michael Jurkab06d95f2012-04-02 06:26:53 -0700756 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
757 // set it for the next frame
758 mForceScreenScrolled = false;
Michael Jurkadde558b2011-11-09 22:09:06 -0800759 screenScrolled(screenCenter);
760 mLastScreenCenter = screenCenter;
Michael Jurkadde558b2011-11-09 22:09:06 -0800761 }
762
763 // Find out which screens are visible; as an optimization we only call draw on them
764 final int pageCount = getChildCount();
765 if (pageCount > 0) {
766 getVisiblePages(mTempVisiblePagesRange);
767 final int leftScreen = mTempVisiblePagesRange[0];
768 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -0800769 if (leftScreen != -1 && rightScreen != -1) {
770 final long drawingTime = getDrawingTime();
771 // Clip to the bounds
772 canvas.save();
773 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
774 mScrollY + mBottom - mTop);
Michael Jurka0142d492010-08-25 17:46:15 -0700775
Michael Jurka80c69852011-12-16 14:16:32 -0800776 // On certain graphics drivers, if you draw to a off-screen buffer that's not
777 // used, it can lead to poor performance. We were running into this when
778 // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw
779 // of that CellLayout's hardware layer, even if that CellLayout wasn't visible.
780 // As a fix, below we set pages that aren't going to be rendered are to be
781 // View.INVISIBLE, preventing re-drawing of their hardware layer
782 for (int i = getChildCount() - 1; i >= 0; i--) {
783 final View v = getPageAt(i);
Winson Chungb33e05f2012-01-30 12:18:04 -0800784
Michael Jurkabed61d22012-02-14 22:51:29 -0800785 if (leftScreen <= i && i <= rightScreen &&
786 v.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
Michael Jurka80c69852011-12-16 14:16:32 -0800787 v.setVisibility(VISIBLE);
788 drawChild(canvas, v, drawingTime);
789 } else {
790 v.setVisibility(INVISIBLE);
791 }
Winson Chungc6f10b92011-11-14 11:39:07 -0800792 }
793 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700794 }
Michael Jurka0142d492010-08-25 17:46:15 -0700795 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 }
797
798 @Override
799 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700800 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700801 if (page != mCurrentPage || !mScroller.isFinished()) {
802 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 return true;
804 }
805 return false;
806 }
807
808 @Override
809 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700810 int focusablePage;
811 if (mNextPage != INVALID_PAGE) {
812 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700814 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 }
Winson Chung86f77532010-08-24 11:08:22 -0700816 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700817 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700818 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 }
820 return false;
821 }
822
823 @Override
824 public boolean dispatchUnhandledMove(View focused, int direction) {
825 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700826 if (getCurrentPage() > 0) {
827 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 return true;
829 }
830 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700831 if (getCurrentPage() < getPageCount() - 1) {
832 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 return true;
834 }
835 }
836 return super.dispatchUnhandledMove(focused, direction);
837 }
838
839 @Override
840 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700841 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
842 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700845 if (mCurrentPage > 0) {
846 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 }
848 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700849 if (mCurrentPage < getPageCount() - 1) {
850 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 }
852 }
853 }
854
855 /**
856 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700857 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 *
Winson Chung86f77532010-08-24 11:08:22 -0700859 * This happens when live folders requery, and if they're off page, they
860 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 */
862 @Override
863 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700864 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700865 View v = focused;
866 while (true) {
867 if (v == current) {
868 super.focusableViewAvailable(focused);
869 return;
870 }
871 if (v == this) {
872 return;
873 }
874 ViewParent parent = v.getParent();
875 if (parent instanceof View) {
876 v = (View)v.getParent();
877 } else {
878 return;
879 }
880 }
881 }
882
883 /**
884 * {@inheritDoc}
885 */
886 @Override
887 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
888 if (disallowIntercept) {
889 // We need to make sure to cancel our long press if
890 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700891 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700892 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700893 }
894 super.requestDisallowInterceptTouchEvent(disallowIntercept);
895 }
896
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800897 /**
898 * Return true if a tap at (x, y) should trigger a flip to the previous page.
899 */
900 protected boolean hitsPreviousPage(float x, float y) {
901 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
902 }
903
904 /**
905 * Return true if a tap at (x, y) should trigger a flip to the next page.
906 */
907 protected boolean hitsNextPage(float x, float y) {
908 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
909 }
910
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 @Override
912 public boolean onInterceptTouchEvent(MotionEvent ev) {
913 /*
914 * This method JUST determines whether we want to intercept the motion.
915 * If we return true, onTouchEvent will be called and we do the actual
916 * scrolling there.
917 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800918 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700919
Winson Chung45e1d6e2010-11-09 17:19:49 -0800920 // Skip touch handling if there are no pages to swipe
921 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
922
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 /*
924 * Shortcut the most recurring case: the user is in the dragging
925 * state and he is moving his finger. We want to intercept this
926 * motion.
927 */
928 final int action = ev.getAction();
929 if ((action == MotionEvent.ACTION_MOVE) &&
930 (mTouchState == TOUCH_STATE_SCROLLING)) {
931 return true;
932 }
933
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 switch (action & MotionEvent.ACTION_MASK) {
935 case MotionEvent.ACTION_MOVE: {
936 /*
937 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
938 * whether the user has moved far enough from his original down touch.
939 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700940 if (mActivePointerId != INVALID_POINTER) {
941 determineScrollingStart(ev);
942 break;
943 }
944 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
945 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
946 // i.e. fall through to the next case (don't break)
947 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
948 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 }
950
951 case MotionEvent.ACTION_DOWN: {
952 final float x = ev.getX();
953 final float y = ev.getY();
954 // Remember location of down touch
955 mDownMotionX = x;
956 mLastMotionX = x;
957 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800958 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800959 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 mActivePointerId = ev.getPointerId(0);
961 mAllowLongPress = true;
962
963 /*
964 * If being flinged and user touches the screen, initiate drag;
965 * otherwise don't. mScroller.isFinished should be false when
966 * being flinged.
967 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700968 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700969 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
970 if (finishedScrolling) {
971 mTouchState = TOUCH_STATE_REST;
972 mScroller.abortAnimation();
973 } else {
974 mTouchState = TOUCH_STATE_SCROLLING;
975 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700976
Winson Chung86f77532010-08-24 11:08:22 -0700977 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800979 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800981 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800983 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 mTouchState = TOUCH_STATE_NEXT_PAGE;
985 }
986 }
987 }
988 break;
989 }
990
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800992 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 mTouchState = TOUCH_STATE_REST;
994 mAllowLongPress = false;
995 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800996 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 break;
998
999 case MotionEvent.ACTION_POINTER_UP:
1000 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001001 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 break;
1003 }
1004
1005 /*
1006 * The only time we want to intercept motion events is if we are in the
1007 * drag mode.
1008 */
1009 return mTouchState != TOUCH_STATE_REST;
1010 }
1011
Adam Cohenf8d28232011-02-01 21:47:00 -08001012 protected void determineScrollingStart(MotionEvent ev) {
1013 determineScrollingStart(ev, 1.0f);
1014 }
1015
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 /*
1017 * Determines if we should change the touch state to start scrolling after the
1018 * user moves their touch point too far.
1019 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001020 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 /*
1022 * Locally do absolute value. mLastMotionX is set to the y value
1023 * of the down event.
1024 */
1025 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001026 if (pointerIndex == -1) {
1027 return;
1028 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 final float x = ev.getX(pointerIndex);
1030 final float y = ev.getY(pointerIndex);
1031 final int xDiff = (int) Math.abs(x - mLastMotionX);
1032 final int yDiff = (int) Math.abs(y - mLastMotionY);
1033
Adam Cohenf8d28232011-02-01 21:47:00 -08001034 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 boolean xPaged = xDiff > mPagingTouchSlop;
1036 boolean xMoved = xDiff > touchSlop;
1037 boolean yMoved = yDiff > touchSlop;
1038
Adam Cohenf8d28232011-02-01 21:47:00 -08001039 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001040 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001041 // Scroll if the user moved far enough along the X axis
1042 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001043 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001045 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001046 mTouchX = mScrollX;
1047 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1048 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 }
1050 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001051 cancelCurrentPageLongPress();
1052 }
1053 }
1054
1055 protected void cancelCurrentPageLongPress() {
1056 if (mAllowLongPress) {
1057 mAllowLongPress = false;
1058 // Try canceling the long press. It could also have been scheduled
1059 // by a distant descendant, so use the mAllowLongPress flag to block
1060 // everything
1061 final View currentPage = getPageAt(mCurrentPage);
1062 if (currentPage != null) {
1063 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 }
1065 }
1066 }
1067
Adam Cohenb5ba0972011-09-07 18:02:31 -07001068 protected float getScrollProgress(int screenCenter, View v, int page) {
1069 final int halfScreenSize = getMeasuredWidth() / 2;
1070
1071 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1072 int delta = screenCenter - (getChildOffset(page) -
1073 getRelativeChildOffset(page) + halfScreenSize);
1074
1075 float scrollProgress = delta / (totalDistance * 1.0f);
1076 scrollProgress = Math.min(scrollProgress, 1.0f);
1077 scrollProgress = Math.max(scrollProgress, -1.0f);
1078 return scrollProgress;
1079 }
1080
Adam Cohene0f66b52010-11-23 15:06:07 -08001081 // This curve determines how the effect of scrolling over the limits of the page dimishes
1082 // as the user pulls further and further from the bounds
1083 private float overScrollInfluenceCurve(float f) {
1084 f -= 1.0f;
1085 return f * f * f + 1.0f;
1086 }
1087
Adam Cohenb5ba0972011-09-07 18:02:31 -07001088 protected void acceleratedOverScroll(float amount) {
1089 int screenSize = getMeasuredWidth();
1090
1091 // We want to reach the max over scroll effect when the user has
1092 // over scrolled half the size of the screen
1093 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1094
1095 if (f == 0) return;
1096
1097 // Clamp this factor, f, to -1 < f < 1
1098 if (Math.abs(f) >= 1) {
1099 f /= Math.abs(f);
1100 }
1101
1102 int overScrollAmount = (int) Math.round(f * screenSize);
1103 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001104 mOverScrollX = overScrollAmount;
1105 mScrollX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001106 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001107 mOverScrollX = mMaxScrollX + overScrollAmount;
1108 mScrollX = mMaxScrollX;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001109 }
1110 invalidate();
1111 }
1112
1113 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001114 int screenSize = getMeasuredWidth();
1115
1116 float f = (amount / screenSize);
1117
1118 if (f == 0) return;
1119 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1120
Adam Cohen7bfc9792011-01-28 13:52:37 -08001121 // Clamp this factor, f, to -1 < f < 1
1122 if (Math.abs(f) >= 1) {
1123 f /= Math.abs(f);
1124 }
1125
Adam Cohene0f66b52010-11-23 15:06:07 -08001126 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001127 if (amount < 0) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001128 mOverScrollX = overScrollAmount;
1129 mScrollX = 0;
Adam Cohen68d73932010-11-15 10:50:58 -08001130 } else {
Adam Cohenebea84d2011-11-09 17:20:41 -08001131 mOverScrollX = mMaxScrollX + overScrollAmount;
1132 mScrollX = mMaxScrollX;
Adam Cohen68d73932010-11-15 10:50:58 -08001133 }
1134 invalidate();
1135 }
1136
Adam Cohenb5ba0972011-09-07 18:02:31 -07001137 protected void overScroll(float amount) {
1138 dampedOverScroll(amount);
1139 }
1140
Michael Jurkac5b262c2011-01-12 20:24:50 -08001141 protected float maxOverScroll() {
1142 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001143 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001144 float f = 1.0f;
1145 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1146 return OVERSCROLL_DAMP_FACTOR * f;
1147 }
1148
Winson Chung321e9ee2010-08-09 13:37:56 -07001149 @Override
1150 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001151 // Skip touch handling if there are no pages to swipe
1152 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1153
Michael Jurkab8f06722010-10-10 15:58:46 -07001154 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001155
1156 final int action = ev.getAction();
1157
1158 switch (action & MotionEvent.ACTION_MASK) {
1159 case MotionEvent.ACTION_DOWN:
1160 /*
1161 * If being flinged and user touches, stop the fling. isFinished
1162 * will be false if being flinged.
1163 */
1164 if (!mScroller.isFinished()) {
1165 mScroller.abortAnimation();
1166 }
1167
1168 // Remember where the motion event started
1169 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001170 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001171 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001173 if (mTouchState == TOUCH_STATE_SCROLLING) {
1174 pageBeginMoving();
1175 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 break;
1177
1178 case MotionEvent.ACTION_MOVE:
1179 if (mTouchState == TOUCH_STATE_SCROLLING) {
1180 // Scroll to follow the motion event
1181 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1182 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001183 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184
Adam Cohenaefd4e12011-02-14 16:39:38 -08001185 mTotalMotionX += Math.abs(deltaX);
1186
Winson Chungc0844aa2011-02-02 15:25:58 -08001187 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1188 // keep the remainder because we are actually testing if we've moved from the last
1189 // scrolled position (which is discrete).
1190 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001191 mTouchX += deltaX;
1192 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1193 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001194 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001195 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001196 } else {
1197 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001198 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001199 mLastMotionX = x;
1200 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 } else {
1202 awakenScrollBars();
1203 }
Adam Cohen564976a2010-10-13 18:52:07 -07001204 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 determineScrollingStart(ev);
1206 }
1207 break;
1208
1209 case MotionEvent.ACTION_UP:
1210 if (mTouchState == TOUCH_STATE_SCROLLING) {
1211 final int activePointerId = mActivePointerId;
1212 final int pointerIndex = ev.findPointerIndex(activePointerId);
1213 final float x = ev.getX(pointerIndex);
1214 final VelocityTracker velocityTracker = mVelocityTracker;
1215 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1216 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001217 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen00481b32011-11-18 12:03:48 -08001218 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
1219 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1220 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001221
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001222 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1223
Adam Cohen00481b32011-11-18 12:03:48 -08001224 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001225 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001226
Adam Cohenaefd4e12011-02-14 16:39:38 -08001227 // In the case that the page is moved far to one direction and then is flung
1228 // in the opposite direction, we use a threshold to determine whether we should
1229 // just return to the starting page, or if we should skip one further.
1230 boolean returnToOriginalPage = false;
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001231 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohen00481b32011-11-18 12:03:48 -08001232 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001233 returnToOriginalPage = true;
1234 }
1235
Adam Cohenaefd4e12011-02-14 16:39:38 -08001236 int finalPage;
1237 // We give flings precedence over large moves, which is why we short-circuit our
1238 // test for a large move if a fling has been registered. That is, a large
1239 // move to the left and fling to the right will register as a fling to the right.
1240 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1241 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1242 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1243 snapToPageWithVelocity(finalPage, velocityX);
1244 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1245 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001246 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001247 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1248 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 } else {
1250 snapToDestination();
1251 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001252 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 // at this point we have not moved beyond the touch slop
1254 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1255 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001256 int nextPage = Math.max(0, mCurrentPage - 1);
1257 if (nextPage != mCurrentPage) {
1258 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 } else {
1260 snapToDestination();
1261 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001262 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 // at this point we have not moved beyond the touch slop
1264 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1265 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001266 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1267 if (nextPage != mCurrentPage) {
1268 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 } else {
1270 snapToDestination();
1271 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001272 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001273 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 }
1275 mTouchState = TOUCH_STATE_REST;
1276 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001277 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 break;
1279
1280 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001281 if (mTouchState == TOUCH_STATE_SCROLLING) {
1282 snapToDestination();
1283 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001284 mTouchState = TOUCH_STATE_REST;
1285 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001286 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001287 break;
1288
1289 case MotionEvent.ACTION_POINTER_UP:
1290 onSecondaryPointerUp(ev);
1291 break;
1292 }
1293
1294 return true;
1295 }
1296
Winson Chung185d7162011-02-28 13:47:29 -08001297 @Override
1298 public boolean onGenericMotionEvent(MotionEvent event) {
1299 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1300 switch (event.getAction()) {
1301 case MotionEvent.ACTION_SCROLL: {
1302 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1303 final float vscroll;
1304 final float hscroll;
1305 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1306 vscroll = 0;
1307 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1308 } else {
1309 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1310 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1311 }
1312 if (hscroll != 0 || vscroll != 0) {
1313 if (hscroll > 0 || vscroll > 0) {
1314 scrollRight();
1315 } else {
1316 scrollLeft();
1317 }
1318 return true;
1319 }
1320 }
1321 }
1322 }
1323 return super.onGenericMotionEvent(event);
1324 }
1325
Michael Jurkab8f06722010-10-10 15:58:46 -07001326 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1327 if (mVelocityTracker == null) {
1328 mVelocityTracker = VelocityTracker.obtain();
1329 }
1330 mVelocityTracker.addMovement(ev);
1331 }
1332
1333 private void releaseVelocityTracker() {
1334 if (mVelocityTracker != null) {
1335 mVelocityTracker.recycle();
1336 mVelocityTracker = null;
1337 }
1338 }
1339
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 private void onSecondaryPointerUp(MotionEvent ev) {
1341 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1342 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1343 final int pointerId = ev.getPointerId(pointerIndex);
1344 if (pointerId == mActivePointerId) {
1345 // This was our active pointer going up. Choose a new
1346 // active pointer and adjust accordingly.
1347 // TODO: Make this decision more intelligent.
1348 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1349 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1350 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001351 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001352 mActivePointerId = ev.getPointerId(newPointerIndex);
1353 if (mVelocityTracker != null) {
1354 mVelocityTracker.clear();
1355 }
1356 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001357 }
1358
Michael Jurkad771c962011-08-09 15:00:48 -07001359 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001360
1361 @Override
1362 public void requestChildFocus(View child, View focused) {
1363 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001364 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001365 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001366 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 }
1368 }
1369
Winson Chunge3193b92010-09-10 11:44:42 -07001370 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1371 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001372 int left;
1373 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001374 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001375 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001376 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001377 if (left <= relativeOffset && relativeOffset <= right) {
1378 return i;
1379 }
Winson Chunge3193b92010-09-10 11:44:42 -07001380 }
1381 return -1;
1382 }
1383
Winson Chung1908d072011-02-24 18:09:44 -08001384 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001385 // This functions are called enough times that it actually makes a difference in the
1386 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001387 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001388 final int minWidth = mMinimumWidth;
1389 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001390 }
1391
Adam Cohend19d3ca2010-09-15 14:43:42 -07001392 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001393 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 int minDistanceFromScreenCenterIndex = -1;
1395 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1396 final int childCount = getChildCount();
1397 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001398 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001399 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 int halfChildWidth = (childWidth / 2);
1401 int childCenter = getChildOffset(i) + halfChildWidth;
1402 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1403 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1404 minDistanceFromScreenCenter = distanceFromScreenCenter;
1405 minDistanceFromScreenCenterIndex = i;
1406 }
1407 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001408 return minDistanceFromScreenCenterIndex;
1409 }
1410
1411 protected void snapToDestination() {
1412 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 }
1414
Adam Cohene0f66b52010-11-23 15:06:07 -08001415 private static class ScrollInterpolator implements Interpolator {
1416 public ScrollInterpolator() {
1417 }
1418
1419 public float getInterpolation(float t) {
1420 t -= 1.0f;
1421 return t*t*t*t*t + 1;
1422 }
1423 }
1424
1425 // We want the duration of the page snap animation to be influenced by the distance that
1426 // the screen has to travel, however, we don't want this duration to be effected in a
1427 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1428 // of travel has on the overall snap duration.
1429 float distanceInfluenceForSnapDuration(float f) {
1430 f -= 0.5f; // center the values about 0.
1431 f *= 0.3f * Math.PI / 2.0f;
1432 return (float) Math.sin(f);
1433 }
1434
Michael Jurka0142d492010-08-25 17:46:15 -07001435 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001436 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1437 int halfScreenSize = getMeasuredWidth() / 2;
1438
Winson Chung785d2eb2011-04-14 16:08:02 -07001439 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1440 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1441 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001442 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1443 int delta = newX - mUnboundedScrollX;
1444 int duration = 0;
1445
Adam Cohen265b9a62011-12-07 14:37:18 -08001446 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001447 // If the velocity is low enough, then treat this more as an automatic page advance
1448 // as opposed to an apparent physical response to flinging
1449 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1450 return;
1451 }
1452
1453 // Here we compute a "distance" that will be used in the computation of the overall
1454 // snap duration. This is a function of the actual distance that needs to be traveled;
1455 // we keep this value close to half screen size in order to reduce the variance in snap
1456 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001457 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001458 float distance = halfScreenSize + halfScreenSize *
1459 distanceInfluenceForSnapDuration(distanceRatio);
1460
1461 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08001462 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08001463
1464 // we want the page's snap velocity to approximately match the velocity at which the
1465 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001466 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1467 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001468
1469 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001470 }
1471
1472 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001473 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 }
1475
Michael Jurka0142d492010-08-25 17:46:15 -07001476 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001477 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001478
Winson Chung785d2eb2011-04-14 16:08:02 -07001479 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1480 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1481 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001482 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001483 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001485 snapToPage(whichPage, delta, duration);
1486 }
1487
1488 protected void snapToPage(int whichPage, int delta, int duration) {
1489 mNextPage = whichPage;
1490
1491 View focusedChild = getFocusedChild();
1492 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001493 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001494 focusedChild.clearFocus();
1495 }
1496
1497 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001498 awakenScrollBars(duration);
1499 if (duration == 0) {
1500 duration = Math.abs(delta);
1501 }
1502
1503 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001504 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001505
Winson Chungb44b5242011-06-13 11:32:14 -07001506 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1507 // loading associated pages until the scroll settles
1508 if (mDeferScrollUpdate) {
1509 loadAssociatedPages(mNextPage);
1510 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001511 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001512 }
Michael Jurka0142d492010-08-25 17:46:15 -07001513 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001514 invalidate();
1515 }
1516
Winson Chung321e9ee2010-08-09 13:37:56 -07001517 public void scrollLeft() {
1518 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001519 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001520 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001521 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001522 }
1523 }
1524
1525 public void scrollRight() {
1526 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001527 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001528 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001529 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001530 }
1531 }
1532
Winson Chung86f77532010-08-24 11:08:22 -07001533 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 int result = -1;
1535 if (v != null) {
1536 ViewParent vp = v.getParent();
1537 int count = getChildCount();
1538 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001539 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001540 return i;
1541 }
1542 }
1543 }
1544 return result;
1545 }
1546
1547 /**
1548 * @return True is long presses are still allowed for the current touch
1549 */
1550 public boolean allowLongPress() {
1551 return mAllowLongPress;
1552 }
1553
Michael Jurka0142d492010-08-25 17:46:15 -07001554 /**
1555 * Set true to allow long-press events to be triggered, usually checked by
1556 * {@link Launcher} to accept or block dpad-initiated long-presses.
1557 */
1558 public void setAllowLongPress(boolean allowLongPress) {
1559 mAllowLongPress = allowLongPress;
1560 }
1561
Winson Chung321e9ee2010-08-09 13:37:56 -07001562 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001563 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001564
1565 SavedState(Parcelable superState) {
1566 super(superState);
1567 }
1568
1569 private SavedState(Parcel in) {
1570 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001571 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001572 }
1573
1574 @Override
1575 public void writeToParcel(Parcel out, int flags) {
1576 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001577 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001578 }
1579
1580 public static final Parcelable.Creator<SavedState> CREATOR =
1581 new Parcelable.Creator<SavedState>() {
1582 public SavedState createFromParcel(Parcel in) {
1583 return new SavedState(in);
1584 }
1585
1586 public SavedState[] newArray(int size) {
1587 return new SavedState[size];
1588 }
1589 };
1590 }
1591
Winson Chungf314b0e2011-08-16 11:54:27 -07001592 protected void loadAssociatedPages(int page) {
1593 loadAssociatedPages(page, false);
1594 }
1595 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001596 if (mContentIsRefreshable) {
1597 final int count = getChildCount();
1598 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001599 int lowerPageBound = getAssociatedLowerPageBound(page);
1600 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1602 + upperPageBound);
Michael Jurka0cad1112011-11-16 20:43:29 -08001603 // First, clear any pages that should no longer be loaded
1604 for (int i = 0; i < count; ++i) {
1605 Page layout = (Page) getPageAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001606 if ((i < lowerPageBound) || (i > upperPageBound)) {
Michael Jurka0cad1112011-11-16 20:43:29 -08001607 if (layout.getPageChildCount() > 0) {
1608 layout.removeAllViewsOnPage();
1609 }
1610 mDirtyPageContent.set(i, true);
1611 }
1612 }
1613 // Next, load any new pages
Michael Jurka0142d492010-08-25 17:46:15 -07001614 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001615 if ((i != page) && immediateAndOnly) {
1616 continue;
1617 }
Michael Jurka0142d492010-08-25 17:46:15 -07001618 if (lowerPageBound <= i && i <= upperPageBound) {
1619 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001620 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001621 mDirtyPageContent.set(i, false);
1622 }
Winson Chung86f77532010-08-24 11:08:22 -07001623 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001624 }
1625 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001626 }
1627 }
1628
Winson Chunge3193b92010-09-10 11:44:42 -07001629 protected int getAssociatedLowerPageBound(int page) {
1630 return Math.max(0, page - 1);
1631 }
1632 protected int getAssociatedUpperPageBound(int page) {
1633 final int count = getChildCount();
1634 return Math.min(page + 1, count - 1);
1635 }
1636
Winson Chung86f77532010-08-24 11:08:22 -07001637 /**
1638 * This method is called ONLY to synchronize the number of pages that the paged view has.
1639 * To actually fill the pages with information, implement syncPageItems() below. It is
1640 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1641 * and therefore, individual page items do not need to be updated in this method.
1642 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001643 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001644
1645 /**
1646 * This method is called to synchronize the items that are on a particular page. If views on
1647 * the page can be reused, then they should be updated within this method.
1648 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001649 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001650
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001651 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001652 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001653 }
1654 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001655 invalidatePageData(currentPage, false);
1656 }
1657 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001658 if (!mIsDataReady) {
1659 return;
1660 }
1661
Michael Jurka0142d492010-08-25 17:46:15 -07001662 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001663 // Force all scrolling-related behavior to end
1664 mScroller.forceFinished(true);
1665 mNextPage = INVALID_PAGE;
1666
Michael Jurka0142d492010-08-25 17:46:15 -07001667 // Update all the pages
1668 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001669
Winson Chung5a808352011-06-27 19:08:49 -07001670 // We must force a measure after we've loaded the pages to update the content width and
1671 // to determine the full scroll width
1672 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1673 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1674
1675 // Set a new page as the current page if necessary
1676 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001677 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001678 }
1679
Michael Jurka0142d492010-08-25 17:46:15 -07001680 // Mark each of the pages as dirty
1681 final int count = getChildCount();
1682 mDirtyPageContent.clear();
1683 for (int i = 0; i < count; ++i) {
1684 mDirtyPageContent.add(true);
1685 }
1686
1687 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001688 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001689 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001690 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001691 }
Winson Chung007c6982011-06-14 13:27:53 -07001692
Michael Jurkaafaa0502011-12-13 18:22:50 -08001693 protected View getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001694 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1695 // found
1696 if (mHasScrollIndicator && mScrollIndicator == null) {
1697 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaafaa0502011-12-13 18:22:50 -08001698 mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
Winson Chung007c6982011-06-14 13:27:53 -07001699 mHasScrollIndicator = mScrollIndicator != null;
1700 if (mHasScrollIndicator) {
1701 mScrollIndicator.setVisibility(View.VISIBLE);
1702 }
1703 }
1704 return mScrollIndicator;
1705 }
1706
1707 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001708 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001709 }
1710
Winson Chung5a808352011-06-27 19:08:49 -07001711 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1712 @Override
1713 public void run() {
1714 hideScrollingIndicator(false);
1715 }
1716 };
Michael Jurkab737ee62011-11-15 15:57:22 -08001717 protected void flashScrollingIndicator(boolean animated) {
Winson Chung5a808352011-06-27 19:08:49 -07001718 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurkab737ee62011-11-15 15:57:22 -08001719 showScrollingIndicator(!animated);
Winson Chung5a808352011-06-27 19:08:49 -07001720 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001721 }
1722
Michael Jurka430e8a52011-08-08 15:52:14 -07001723 protected void showScrollingIndicator(boolean immediately) {
Michael Jurkabed61d22012-02-14 22:51:29 -08001724 mShouldShowScrollIndicator = true;
1725 mShouldShowScrollIndicatorImmediately = true;
Winson Chung007c6982011-06-14 13:27:53 -07001726 if (getChildCount() <= 1) return;
1727 if (!isScrollingIndicatorEnabled()) return;
1728
Michael Jurkabed61d22012-02-14 22:51:29 -08001729 mShouldShowScrollIndicator = false;
Winson Chung007c6982011-06-14 13:27:53 -07001730 getScrollingIndicator();
1731 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001732 // Fade the indicator in
1733 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001734 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001735 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001736 if (immediately) {
1737 mScrollIndicator.setAlpha(1f);
1738 } else {
1739 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1740 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1741 mScrollIndicatorAnimator.start();
1742 }
Winson Chung007c6982011-06-14 13:27:53 -07001743 }
1744 }
1745
Adam Cohen21b41102011-11-01 17:29:52 -07001746 protected void cancelScrollingIndicatorAnimations() {
1747 if (mScrollIndicatorAnimator != null) {
1748 mScrollIndicatorAnimator.cancel();
1749 }
1750 }
1751
Winson Chung007c6982011-06-14 13:27:53 -07001752 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001753 if (getChildCount() <= 1) return;
1754 if (!isScrollingIndicatorEnabled()) return;
1755
1756 getScrollingIndicator();
1757 if (mScrollIndicator != null) {
1758 // Fade the indicator out
1759 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001760 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001761 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001762 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001763 mScrollIndicator.setAlpha(0f);
1764 } else {
1765 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1766 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1767 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1768 private boolean cancelled = false;
1769 @Override
1770 public void onAnimationCancel(android.animation.Animator animation) {
1771 cancelled = true;
1772 }
1773 @Override
1774 public void onAnimationEnd(Animator animation) {
1775 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001776 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001777 }
1778 }
1779 });
1780 mScrollIndicatorAnimator.start();
1781 }
Winson Chung007c6982011-06-14 13:27:53 -07001782 }
1783 }
1784
Winson Chung32174c82011-07-19 15:47:55 -07001785 /**
1786 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1787 * fill its space on the track or not.
1788 */
1789 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001790 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001791 }
1792
Winson Chung007c6982011-06-14 13:27:53 -07001793 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001794 if (getChildCount() <= 1) return;
1795 if (!isScrollingIndicatorEnabled()) return;
1796
1797 getScrollingIndicator();
1798 if (mScrollIndicator != null) {
1799 updateScrollingIndicatorPosition();
1800 }
Michael Jurkabed61d22012-02-14 22:51:29 -08001801 if (mShouldShowScrollIndicator) {
1802 showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
1803 }
Winson Chung007c6982011-06-14 13:27:53 -07001804 }
1805
Winson Chung007c6982011-06-14 13:27:53 -07001806 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001807 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001808 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001809 int numPages = getChildCount();
1810 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001811 int lastChildIndex = Math.max(0, getChildCount() - 1);
1812 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001813 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001814 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1815 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001816
Winson Chungae890b82011-09-13 18:08:54 -07001817 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001818 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001819 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001820 if (hasElasticScrollIndicator()) {
1821 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1822 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1823 mScrollIndicator.requestLayout();
1824 }
1825 } else {
1826 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1827 indicatorPos += indicatorCenterOffset;
1828 }
Winson Chung007c6982011-06-14 13:27:53 -07001829 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001830 }
1831
Winson Chung3ac74c52011-06-30 17:39:37 -07001832 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001833 }
1834
1835 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001836 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001837
1838 /* Accessibility */
1839 @Override
1840 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1841 super.onInitializeAccessibilityNodeInfo(info);
1842 info.setScrollable(true);
1843 }
1844
1845 @Override
1846 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1847 super.onInitializeAccessibilityEvent(event);
1848 event.setScrollable(true);
1849 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1850 event.setFromIndex(mCurrentPage);
1851 event.setToIndex(mCurrentPage);
1852 event.setItemCount(getChildCount());
1853 }
1854 }
1855
Winson Chung6a0f57d2011-06-29 20:10:49 -07001856 protected String getCurrentPageDescription() {
1857 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1858 return String.format(mContext.getString(R.string.default_scroll_format),
1859 page + 1, getChildCount());
1860 }
Winson Chungd11265e2011-08-30 13:37:23 -07001861
1862 @Override
1863 public boolean onHoverEvent(android.view.MotionEvent event) {
1864 return true;
1865 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001866}