blob: d2d734cf331fe79c10a54bb16d181d8b48b4a4dd [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;
20import android.animation.AnimatorInflater;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ObjectAnimator;
Winson Chungbb6f6a52011-07-25 17:55:44 -070023import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070025import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.graphics.Canvas;
27import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityEvent;
42import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080043import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070044import android.widget.Checkable;
Winson Chung007c6982011-06-14 13:27:53 -070045import android.widget.ImageView;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.widget.Scroller;
47
Winson Chung04998342011-01-05 13:54:43 -080048import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070049
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import java.util.ArrayList;
51
Winson Chung321e9ee2010-08-09 13:37:56 -070052/**
53 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070054 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070055 */
56public abstract class PagedView extends ViewGroup {
57 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070058 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Winson Chung86f77532010-08-24 11:08:22 -070061 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070062 private static final int MIN_LENGTH_FOR_FLING = 25;
63 // The min drag distance to trigger a page shift (regardless of velocity)
64 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Adam Cohene0f66b52010-11-23 15:06:07 -080066 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070067 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Winson Chungb26f3d62011-06-02 10:49:29 -070069 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080070 private static final int MINIMUM_SNAP_VELOCITY = 2200;
71 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080072 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080073
Michael Jurka0142d492010-08-25 17:46:15 -070074 // the velocity at which a fling gesture will cause us to snap to the next page
75 protected int mSnapVelocity = 500;
76
77 protected float mSmoothingTime;
78 protected float mTouchX;
79
80 protected boolean mFirstLayout = true;
81
82 protected int mCurrentPage;
83 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080084 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070086 private VelocityTracker mVelocityTracker;
87
88 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080089 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080090 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080091 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080092 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070093 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070094
Michael Jurka0142d492010-08-25 17:46:15 -070095 protected final static int TOUCH_STATE_REST = 0;
96 protected final static int TOUCH_STATE_SCROLLING = 1;
97 protected final static int TOUCH_STATE_PREV_PAGE = 2;
98 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070099 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700100
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104
Michael Jurka7426c422010-11-11 15:23:47 -0800105 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka7426c422010-11-11 15:23:47 -0800107 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108 private int mPagingTouchSlop;
109 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800110 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700111 protected int mPageSpacing;
112 protected int mPageLayoutPaddingTop;
113 protected int mPageLayoutPaddingBottom;
114 protected int mPageLayoutPaddingLeft;
115 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700116 protected int mPageLayoutWidthGap;
117 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700118 protected int mCellCountX = 0;
119 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700120 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800121 protected boolean mAllowOverScroll = true;
122 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Michael Jurka8c920dd2011-01-20 14:16:56 -0800124 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800125 protected float mLayoutScale = 1.0f;
126
Michael Jurka5f1c5092010-09-03 14:15:02 -0700127 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Michael Jurka5f1c5092010-09-03 14:15:02 -0700129 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Winson Chung86f77532010-08-24 11:08:22 -0700131 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Winson Chung86f77532010-08-24 11:08:22 -0700133 private ArrayList<Boolean> mDirtyPageContent;
Michael Jurka86c119a2011-08-05 20:35:36 -0700134 private boolean mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700136 // choice modes
137 protected static final int CHOICE_MODE_NONE = 0;
138 protected static final int CHOICE_MODE_SINGLE = 1;
139 // Multiple selection mode is not supported by all Launcher actions atm
140 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700141
Michael Jurkae17e19c2010-09-28 11:01:39 -0700142 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700143 private ActionMode mActionMode;
144
Michael Jurka0142d492010-08-25 17:46:15 -0700145 // If true, syncPages and syncPageItems will be called to refresh pages
146 protected boolean mContentIsRefreshable = true;
147
148 // If true, modify alpha of neighboring pages as user scrolls left/right
149 protected boolean mFadeInAdjacentScreens = true;
150
151 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
152 // to switch to a new page
153 protected boolean mUsePagingTouchSlop = true;
154
155 // If true, the subclass should directly update mScrollX itself in its computeScroll method
156 // (SmoothPagedView does this)
157 protected boolean mDeferScrollUpdate = false;
158
Patrick Dubroy1262e362010-10-06 15:49:50 -0700159 protected boolean mIsPageMoving = false;
160
Winson Chungf0ea4d32011-06-06 14:27:16 -0700161 // All syncs and layout passes are deferred until data is ready.
162 protected boolean mIsDataReady = false;
163
Winson Chung007c6982011-06-14 13:27:53 -0700164 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700165 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700166 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700167 private int mScrollIndicatorPaddingLeft;
168 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700169 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700170 protected static final int sScrollIndicatorFadeInDuration = 150;
171 protected static final int sScrollIndicatorFadeOutDuration = 650;
172 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700173
Winson Chungb44b5242011-06-13 11:32:14 -0700174 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700175 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700176
Winson Chung86f77532010-08-24 11:08:22 -0700177 public interface PageSwitchListener {
178 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700179 }
180
Winson Chung321e9ee2010-08-09 13:37:56 -0700181 public PagedView(Context context) {
182 this(context, null);
183 }
184
185 public PagedView(Context context, AttributeSet attrs) {
186 this(context, attrs, 0);
187 }
188
189 public PagedView(Context context, AttributeSet attrs, int defStyle) {
190 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700191 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700192
Adam Cohen9c4949e2010-10-05 12:27:22 -0700193 TypedArray a = context.obtainStyledAttributes(attrs,
194 R.styleable.PagedView, defStyle, 0);
195 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
196 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800197 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700198 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800199 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800201 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800203 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700204 mPageLayoutWidthGap = a.getDimensionPixelSize(
205 R.styleable.PagedView_pageLayoutWidthGap, -1);
206 mPageLayoutHeightGap = a.getDimensionPixelSize(
207 R.styleable.PagedView_pageLayoutHeightGap, -1);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700208 mScrollIndicatorPaddingLeft =
209 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
210 mScrollIndicatorPaddingRight =
211 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700212 a.recycle();
213
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700215 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 }
217
218 /**
219 * Initializes various states for this workspace.
220 */
Michael Jurka0142d492010-08-25 17:46:15 -0700221 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700222 mDirtyPageContent = new ArrayList<Boolean>();
223 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800224 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700225 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700226 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700227
228 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
229 mTouchSlop = configuration.getScaledTouchSlop();
230 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
231 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
232 }
233
Winson Chung86f77532010-08-24 11:08:22 -0700234 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
235 mPageSwitchListener = pageSwitchListener;
236 if (mPageSwitchListener != null) {
237 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 }
239 }
240
241 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
243 * out pages.
244 */
245 protected void setDataIsReady() {
246 mIsDataReady = true;
247 }
248 protected boolean isDataReady() {
249 return mIsDataReady;
250 }
251
252 /**
Winson Chung86f77532010-08-24 11:08:22 -0700253 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 *
Winson Chung86f77532010-08-24 11:08:22 -0700255 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 */
Winson Chung86f77532010-08-24 11:08:22 -0700257 int getCurrentPage() {
258 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 }
260
Winson Chung86f77532010-08-24 11:08:22 -0700261 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 return getChildCount();
263 }
264
Winson Chung86f77532010-08-24 11:08:22 -0700265 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 return getChildAt(index);
267 }
268
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800270 * Updates the scroll of the current page immediately to its final scroll position. We use this
271 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
272 * the previous tab page.
273 */
274 protected void updateCurrentPageScroll() {
275 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
276 scrollTo(newX, 0);
277 mScroller.setFinalX(newX);
278 }
279
280 /**
Winson Chung86f77532010-08-24 11:08:22 -0700281 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700282 */
Winson Chung86f77532010-08-24 11:08:22 -0700283 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800284 if (!mScroller.isFinished()) {
285 mScroller.abortAnimation();
286 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800287 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
288 // the default
289 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800290 return;
291 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700292
Winson Chung86f77532010-08-24 11:08:22 -0700293 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800294 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700295 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700296 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800297 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 }
299
Michael Jurka0142d492010-08-25 17:46:15 -0700300 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700301 if (mPageSwitchListener != null) {
302 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
304 }
305
Michael Jurkace7e05f2011-02-01 22:02:35 -0800306 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700307 if (!mIsPageMoving) {
308 mIsPageMoving = true;
309 onPageBeginMoving();
310 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700311 }
312
Michael Jurkace7e05f2011-02-01 22:02:35 -0800313 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700314 if (mIsPageMoving) {
315 mIsPageMoving = false;
316 onPageEndMoving();
317 }
Michael Jurka0142d492010-08-25 17:46:15 -0700318 }
319
Adam Cohen26976d92011-03-22 15:33:33 -0700320 protected boolean isPageMoving() {
321 return mIsPageMoving;
322 }
323
Michael Jurka0142d492010-08-25 17:46:15 -0700324 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700325 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700326 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700327 }
328
329 // a method that subclasses can override to add behavior
330 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700331 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700332 }
333
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 /**
Winson Chung86f77532010-08-24 11:08:22 -0700335 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 *
337 * @param l The listener used to respond to long clicks.
338 */
339 @Override
340 public void setOnLongClickListener(OnLongClickListener l) {
341 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700342 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700344 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 }
346 }
347
348 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800349 public void scrollBy(int x, int y) {
350 scrollTo(mUnboundedScrollX + x, mScrollY + y);
351 }
352
353 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700354 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800355 mUnboundedScrollX = x;
356
357 if (x < 0) {
358 super.scrollTo(0, y);
359 if (mAllowOverScroll) {
360 overScroll(x);
361 }
362 } else if (x > mMaxScrollX) {
363 super.scrollTo(mMaxScrollX, y);
364 if (mAllowOverScroll) {
365 overScroll(x - mMaxScrollX);
366 }
367 } else {
368 super.scrollTo(x, y);
369 }
370
Michael Jurka0142d492010-08-25 17:46:15 -0700371 mTouchX = x;
372 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
373 }
374
375 // we moved this functionality to a helper function so SmoothPagedView can reuse it
376 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700378 // Don't bother scrolling if the page does not need to be moved
379 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
380 mDirtyPageAlpha = true;
381 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
382 }
Michael Jurka0142d492010-08-25 17:46:15 -0700383 invalidate();
384 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700385 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700386 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700387 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700388 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700389 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700390
391 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700392 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700393 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700394 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700395 }
396
Adam Cohen73aa9752010-11-24 16:26:58 -0800397 // We don't want to trigger a page end moving unless the page has settled
398 // and the user has stopped scrolling
399 if (mTouchState == TOUCH_STATE_REST) {
400 pageEndMoving();
401 }
Michael Jurka0142d492010-08-25 17:46:15 -0700402 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 }
Michael Jurka0142d492010-08-25 17:46:15 -0700404 return false;
405 }
406
407 @Override
408 public void computeScroll() {
409 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700410 }
411
412 @Override
413 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700414 if (!mIsDataReady) {
415 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
416 return;
417 }
418
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
420 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
421 if (widthMode != MeasureSpec.EXACTLY) {
422 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
423 }
424
Adam Lesinski6b879f02010-11-04 16:15:23 -0700425 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
426 * of the All apps view on XLarge displays to not take up more space then it needs. Width
427 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
428 * each page to have the same width.
429 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700431 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
432 int maxChildHeight = 0;
433
434 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700435 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700436
Michael Jurka36fcb742011-04-06 17:08:58 -0700437
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700439 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700440 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700441 final int childCount = getChildCount();
442 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700443 // disallowing padding in paged view (just pass 0)
444 final View child = getChildAt(i);
445 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
446
447 int childWidthMode;
448 if (lp.width == LayoutParams.WRAP_CONTENT) {
449 childWidthMode = MeasureSpec.AT_MOST;
450 } else {
451 childWidthMode = MeasureSpec.EXACTLY;
452 }
453
454 int childHeightMode;
455 if (lp.height == LayoutParams.WRAP_CONTENT) {
456 childHeightMode = MeasureSpec.AT_MOST;
457 } else {
458 childHeightMode = MeasureSpec.EXACTLY;
459 }
460
461 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700462 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700464 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700465
466 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700467 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700468 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
469 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700470 }
471
472 if (heightMode == MeasureSpec.AT_MOST) {
473 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800475 if (childCount > 0) {
476 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
477 } else {
478 mMaxScrollX = 0;
479 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700480
Michael Jurka430e8a52011-08-08 15:52:14 -0700481 updateScrollingIndicatorPosition();
482
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 }
485
Michael Jurka8c920dd2011-01-20 14:16:56 -0800486 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800487 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
488 int delta = newX - mScrollX;
489
Michael Jurka8c920dd2011-01-20 14:16:56 -0800490 final int pageCount = getChildCount();
491 for (int i = 0; i < pageCount; i++) {
492 View page = (View) getChildAt(i);
493 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800494 }
495 setCurrentPage(newCurrentPage);
496 }
497
Michael Jurka8c920dd2011-01-20 14:16:56 -0800498 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
499 // 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 -0800500 // tightens the layout accordingly
501 public void setLayoutScale(float childrenScale) {
502 mLayoutScale = childrenScale;
503
504 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
505 int childCount = getChildCount();
506 float childrenX[] = new float[childCount];
507 float childrenY[] = new float[childCount];
508 for (int i = 0; i < childCount; i++) {
509 final View child = getChildAt(i);
510 childrenX[i] = child.getX();
511 childrenY[i] = child.getY();
512 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700513 // Trigger a full re-layout (never just call onLayout directly!)
514 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
515 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
516 requestLayout();
517 measure(widthSpec, heightSpec);
518 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800519 for (int i = 0; i < childCount; i++) {
520 final View child = getChildAt(i);
521 child.setX(childrenX[i]);
522 child.setY(childrenY[i]);
523 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700524
Michael Jurkad3ef3062010-11-23 16:23:58 -0800525 // Also, the page offset has changed (since the pages are now smaller);
526 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800527 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800528 }
529
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700531 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700532 if (!mIsDataReady) {
533 return;
534 }
535
Winson Chung785d2eb2011-04-14 16:08:02 -0700536 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700537 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
538 setHorizontalScrollBarEnabled(false);
539 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
540 scrollTo(newX, 0);
541 mScroller.setFinalX(newX);
542 setHorizontalScrollBarEnabled(true);
543 mFirstLayout = false;
544 }
545
Adam Lesinski6b879f02010-11-04 16:15:23 -0700546 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 final int childCount = getChildCount();
548 int childLeft = 0;
549 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700550 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
551 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700552 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 }
554
555 for (int i = 0; i < childCount; i++) {
556 final View child = getChildAt(i);
557 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700559 final int childHeight = child.getMeasuredHeight();
560 int childTop = mPaddingTop;
561 if (mCenterPagesVertically) {
562 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
563 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800564
Winson Chung785d2eb2011-04-14 16:08:02 -0700565 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700566 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800567 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700568 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 }
570 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800571 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
572 mFirstLayout = false;
573 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700574 }
575
Winson Chung03929772011-02-23 17:07:10 -0800576 protected void forceUpdateAdjacentPagesAlpha() {
577 mDirtyPageAlpha = true;
578 updateAdjacentPagesAlpha();
579 }
580
Winson Chunge3193b92010-09-10 11:44:42 -0700581 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700582 if (mFadeInAdjacentScreens) {
583 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700584 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700585 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700586 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700587 final int childCount = getChildCount();
588 for (int i = 0; i < childCount; ++i) {
589 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700591 int halfChildWidth = (childWidth / 2);
592 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700593
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700594 // On the first layout, we may not have a width nor a proper offset, so for now
595 // we should just assume full page width (and calculate the offset according to
596 // that).
597 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700598 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700599 childCenter = (i * childWidth) + (childWidth / 2);
600 }
601
Winson Chunge8878e32010-09-15 20:37:09 -0700602 int d = halfChildWidth;
603 int distanceFromScreenCenter = childCenter - screenCenter;
604 if (distanceFromScreenCenter > 0) {
605 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800606 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700607 } else {
608 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700609 }
Michael Jurka0142d492010-08-25 17:46:15 -0700610 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700611 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800612 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700613 } else {
614 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700615 }
Michael Jurka0142d492010-08-25 17:46:15 -0700616 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700617 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700618
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700619 // Preventing potential divide-by-zero
620 d = Math.max(1, d);
621
Winson Chunge8878e32010-09-15 20:37:09 -0700622 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
623 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
624 float alpha = 1.0f - dimAlpha;
625
Adam Cohenf34bab52010-09-30 14:11:56 -0700626 if (alpha < ALPHA_QUANTIZE_LEVEL) {
627 alpha = 0.0f;
628 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
629 alpha = 1.0f;
630 }
631
Michael Jurkaa40829a2011-02-16 18:02:45 -0800632 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
633 // this optimization causes alpha to not be properly updated sometimes (repro
634 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
635 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
636 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800637 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700638 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800639 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700640 }
Michael Jurka0142d492010-08-25 17:46:15 -0700641 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700642 }
643 }
Winson Chunge3193b92010-09-10 11:44:42 -0700644 }
645
Adam Cohenf34bab52010-09-30 14:11:56 -0700646 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700647 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700648 }
649
Winson Chunge3193b92010-09-10 11:44:42 -0700650 @Override
651 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700652 int halfScreenSize = getMeasuredWidth() / 2;
653 int screenCenter = mScrollX + halfScreenSize;
654
655 if (screenCenter != mLastScreenCenter) {
656 screenScrolled(screenCenter);
657 updateAdjacentPagesAlpha();
658 mLastScreenCenter = screenCenter;
659 }
Michael Jurka0142d492010-08-25 17:46:15 -0700660
661 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700662 // As an optimization, this code assumes that all pages have the same width as the 0th
663 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700664 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700665 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800666 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700667 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700668 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700669 int leftScreen = 0;
670 int rightScreen = 0;
671 while (x <= mScrollX) {
672 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800673 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700674 }
675 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800676 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700677 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800678 if (rightScreen < pageCount) {
679 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
680 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700681 }
682 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700683
Michael Jurkac4fb9172010-09-02 17:19:20 -0700684 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800685 // Clip to the bounds
686 canvas.save();
687 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
688 mScrollY + mBottom - mTop);
689
Michael Jurkac4fb9172010-09-02 17:19:20 -0700690 for (int i = leftScreen; i <= rightScreen; i++) {
691 drawChild(canvas, getChildAt(i), drawingTime);
692 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800693 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700694 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 }
696
697 @Override
698 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700699 int page = indexOfChild(child);
700 if (page != mCurrentPage || !mScroller.isFinished()) {
701 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700702 return true;
703 }
704 return false;
705 }
706
707 @Override
708 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700709 int focusablePage;
710 if (mNextPage != INVALID_PAGE) {
711 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700712 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700713 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 }
Winson Chung86f77532010-08-24 11:08:22 -0700715 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700716 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700717 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700718 }
719 return false;
720 }
721
722 @Override
723 public boolean dispatchUnhandledMove(View focused, int direction) {
724 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700725 if (getCurrentPage() > 0) {
726 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 return true;
728 }
729 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700730 if (getCurrentPage() < getPageCount() - 1) {
731 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 return true;
733 }
734 }
735 return super.dispatchUnhandledMove(focused, direction);
736 }
737
738 @Override
739 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700740 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
741 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 }
743 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700744 if (mCurrentPage > 0) {
745 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 }
747 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700748 if (mCurrentPage < getPageCount() - 1) {
749 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 }
751 }
752 }
753
754 /**
755 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700756 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 *
Winson Chung86f77532010-08-24 11:08:22 -0700758 * This happens when live folders requery, and if they're off page, they
759 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 */
761 @Override
762 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700763 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 View v = focused;
765 while (true) {
766 if (v == current) {
767 super.focusableViewAvailable(focused);
768 return;
769 }
770 if (v == this) {
771 return;
772 }
773 ViewParent parent = v.getParent();
774 if (parent instanceof View) {
775 v = (View)v.getParent();
776 } else {
777 return;
778 }
779 }
780 }
781
782 /**
783 * {@inheritDoc}
784 */
785 @Override
786 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
787 if (disallowIntercept) {
788 // We need to make sure to cancel our long press if
789 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700790 final View currentPage = getChildAt(mCurrentPage);
791 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 }
793 super.requestDisallowInterceptTouchEvent(disallowIntercept);
794 }
795
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800796 /**
797 * Return true if a tap at (x, y) should trigger a flip to the previous page.
798 */
799 protected boolean hitsPreviousPage(float x, float y) {
800 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
801 }
802
803 /**
804 * Return true if a tap at (x, y) should trigger a flip to the next page.
805 */
806 protected boolean hitsNextPage(float x, float y) {
807 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
808 }
809
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 @Override
811 public boolean onInterceptTouchEvent(MotionEvent ev) {
812 /*
813 * This method JUST determines whether we want to intercept the motion.
814 * If we return true, onTouchEvent will be called and we do the actual
815 * scrolling there.
816 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800817 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700818
Winson Chung45e1d6e2010-11-09 17:19:49 -0800819 // Skip touch handling if there are no pages to swipe
820 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
821
Winson Chung321e9ee2010-08-09 13:37:56 -0700822 /*
823 * Shortcut the most recurring case: the user is in the dragging
824 * state and he is moving his finger. We want to intercept this
825 * motion.
826 */
827 final int action = ev.getAction();
828 if ((action == MotionEvent.ACTION_MOVE) &&
829 (mTouchState == TOUCH_STATE_SCROLLING)) {
830 return true;
831 }
832
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 switch (action & MotionEvent.ACTION_MASK) {
834 case MotionEvent.ACTION_MOVE: {
835 /*
836 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
837 * whether the user has moved far enough from his original down touch.
838 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700839 if (mActivePointerId != INVALID_POINTER) {
840 determineScrollingStart(ev);
841 break;
842 }
843 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
844 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
845 // i.e. fall through to the next case (don't break)
846 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
847 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 }
849
850 case MotionEvent.ACTION_DOWN: {
851 final float x = ev.getX();
852 final float y = ev.getY();
853 // Remember location of down touch
854 mDownMotionX = x;
855 mLastMotionX = x;
856 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800857 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800858 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 mActivePointerId = ev.getPointerId(0);
860 mAllowLongPress = true;
861
862 /*
863 * If being flinged and user touches the screen, initiate drag;
864 * otherwise don't. mScroller.isFinished should be false when
865 * being flinged.
866 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700867 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700868 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
869 if (finishedScrolling) {
870 mTouchState = TOUCH_STATE_REST;
871 mScroller.abortAnimation();
872 } else {
873 mTouchState = TOUCH_STATE_SCROLLING;
874 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700875
Winson Chung86f77532010-08-24 11:08:22 -0700876 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800878 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800880 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800882 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 mTouchState = TOUCH_STATE_NEXT_PAGE;
884 }
885 }
886 }
887 break;
888 }
889
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800891 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 mTouchState = TOUCH_STATE_REST;
893 mAllowLongPress = false;
894 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800895 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 break;
897
898 case MotionEvent.ACTION_POINTER_UP:
899 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800900 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 break;
902 }
903
904 /*
905 * The only time we want to intercept motion events is if we are in the
906 * drag mode.
907 */
908 return mTouchState != TOUCH_STATE_REST;
909 }
910
Winson Chung80baf5a2010-08-09 16:03:15 -0700911 protected void animateClickFeedback(View v, final Runnable r) {
912 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800913 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
914 loadAnimator(mContext, R.anim.paged_view_click_feedback);
915 anim.setTarget(v);
916 anim.addListener(new AnimatorListenerAdapter() {
917 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700918 r.run();
919 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700920 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800921 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700922 }
923
Adam Cohenf8d28232011-02-01 21:47:00 -0800924 protected void determineScrollingStart(MotionEvent ev) {
925 determineScrollingStart(ev, 1.0f);
926 }
927
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 /*
929 * Determines if we should change the touch state to start scrolling after the
930 * user moves their touch point too far.
931 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800932 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 /*
934 * Locally do absolute value. mLastMotionX is set to the y value
935 * of the down event.
936 */
937 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700938 if (pointerIndex == -1) {
939 return;
940 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 final float x = ev.getX(pointerIndex);
942 final float y = ev.getY(pointerIndex);
943 final int xDiff = (int) Math.abs(x - mLastMotionX);
944 final int yDiff = (int) Math.abs(y - mLastMotionY);
945
Adam Cohenf8d28232011-02-01 21:47:00 -0800946 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 boolean xPaged = xDiff > mPagingTouchSlop;
948 boolean xMoved = xDiff > touchSlop;
949 boolean yMoved = yDiff > touchSlop;
950
Adam Cohenf8d28232011-02-01 21:47:00 -0800951 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700952 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 // Scroll if the user moved far enough along the X axis
954 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800955 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800957 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700958 mTouchX = mScrollX;
959 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
960 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 }
962 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800963 cancelCurrentPageLongPress();
964 }
965 }
966
967 protected void cancelCurrentPageLongPress() {
968 if (mAllowLongPress) {
969 mAllowLongPress = false;
970 // Try canceling the long press. It could also have been scheduled
971 // by a distant descendant, so use the mAllowLongPress flag to block
972 // everything
973 final View currentPage = getPageAt(mCurrentPage);
974 if (currentPage != null) {
975 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 }
977 }
978 }
979
Adam Cohene0f66b52010-11-23 15:06:07 -0800980 // This curve determines how the effect of scrolling over the limits of the page dimishes
981 // as the user pulls further and further from the bounds
982 private float overScrollInfluenceCurve(float f) {
983 f -= 1.0f;
984 return f * f * f + 1.0f;
985 }
986
Adam Cohen68d73932010-11-15 10:50:58 -0800987 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800988 int screenSize = getMeasuredWidth();
989
990 float f = (amount / screenSize);
991
992 if (f == 0) return;
993 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
994
Adam Cohen7bfc9792011-01-28 13:52:37 -0800995 // Clamp this factor, f, to -1 < f < 1
996 if (Math.abs(f) >= 1) {
997 f /= Math.abs(f);
998 }
999
Adam Cohene0f66b52010-11-23 15:06:07 -08001000 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001001 if (amount < 0) {
1002 mScrollX = overScrollAmount;
1003 } else {
1004 mScrollX = mMaxScrollX + overScrollAmount;
1005 }
1006 invalidate();
1007 }
1008
Michael Jurkac5b262c2011-01-12 20:24:50 -08001009 protected float maxOverScroll() {
1010 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
1011 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
1012 float f = 1.0f;
1013 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1014 return OVERSCROLL_DAMP_FACTOR * f;
1015 }
1016
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 @Override
1018 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001019 // Skip touch handling if there are no pages to swipe
1020 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1021
Michael Jurkab8f06722010-10-10 15:58:46 -07001022 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023
1024 final int action = ev.getAction();
1025
1026 switch (action & MotionEvent.ACTION_MASK) {
1027 case MotionEvent.ACTION_DOWN:
1028 /*
1029 * If being flinged and user touches, stop the fling. isFinished
1030 * will be false if being flinged.
1031 */
1032 if (!mScroller.isFinished()) {
1033 mScroller.abortAnimation();
1034 }
1035
1036 // Remember where the motion event started
1037 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001038 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001039 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001041 if (mTouchState == TOUCH_STATE_SCROLLING) {
1042 pageBeginMoving();
1043 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 break;
1045
1046 case MotionEvent.ACTION_MOVE:
1047 if (mTouchState == TOUCH_STATE_SCROLLING) {
1048 // Scroll to follow the motion event
1049 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1050 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001051 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001052
Adam Cohenaefd4e12011-02-14 16:39:38 -08001053 mTotalMotionX += Math.abs(deltaX);
1054
Winson Chungc0844aa2011-02-02 15:25:58 -08001055 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1056 // keep the remainder because we are actually testing if we've moved from the last
1057 // scrolled position (which is discrete).
1058 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001059 mTouchX += deltaX;
1060 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1061 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001062 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001063 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001064 } else {
1065 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001067 mLastMotionX = x;
1068 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 } else {
1070 awakenScrollBars();
1071 }
Adam Cohen564976a2010-10-13 18:52:07 -07001072 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 determineScrollingStart(ev);
1074 }
1075 break;
1076
1077 case MotionEvent.ACTION_UP:
1078 if (mTouchState == TOUCH_STATE_SCROLLING) {
1079 final int activePointerId = mActivePointerId;
1080 final int pointerIndex = ev.findPointerIndex(activePointerId);
1081 final float x = ev.getX(pointerIndex);
1082 final VelocityTracker velocityTracker = mVelocityTracker;
1083 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1084 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001085 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001086 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001087 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001088
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001089 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1090
Adam Cohenaefd4e12011-02-14 16:39:38 -08001091 // In the case that the page is moved far to one direction and then is flung
1092 // in the opposite direction, we use a threshold to determine whether we should
1093 // just return to the starting page, or if we should skip one further.
1094 boolean returnToOriginalPage = false;
1095 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001096 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001097 Math.signum(velocityX) != Math.signum(deltaX)) {
1098 returnToOriginalPage = true;
1099 }
1100
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001101 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001102 Math.abs(velocityX) > snapVelocity;
1103
1104 int finalPage;
1105 // We give flings precedence over large moves, which is why we short-circuit our
1106 // test for a large move if a fling has been registered. That is, a large
1107 // move to the left and fling to the right will register as a fling to the right.
1108 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1109 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1110 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1111 snapToPageWithVelocity(finalPage, velocityX);
1112 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1113 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001114 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001115 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1116 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 } else {
1118 snapToDestination();
1119 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001120 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 // at this point we have not moved beyond the touch slop
1122 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1123 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001124 int nextPage = Math.max(0, mCurrentPage - 1);
1125 if (nextPage != mCurrentPage) {
1126 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 } else {
1128 snapToDestination();
1129 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001130 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 // at this point we have not moved beyond the touch slop
1132 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1133 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001134 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1135 if (nextPage != mCurrentPage) {
1136 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001137 } else {
1138 snapToDestination();
1139 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001140 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001141 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 }
1143 mTouchState = TOUCH_STATE_REST;
1144 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001145 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001146 break;
1147
1148 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001149 if (mTouchState == TOUCH_STATE_SCROLLING) {
1150 snapToDestination();
1151 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 mTouchState = TOUCH_STATE_REST;
1153 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001154 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001155 break;
1156
1157 case MotionEvent.ACTION_POINTER_UP:
1158 onSecondaryPointerUp(ev);
1159 break;
1160 }
1161
1162 return true;
1163 }
1164
Winson Chung185d7162011-02-28 13:47:29 -08001165 @Override
1166 public boolean onGenericMotionEvent(MotionEvent event) {
1167 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1168 switch (event.getAction()) {
1169 case MotionEvent.ACTION_SCROLL: {
1170 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1171 final float vscroll;
1172 final float hscroll;
1173 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1174 vscroll = 0;
1175 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1176 } else {
1177 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1178 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1179 }
1180 if (hscroll != 0 || vscroll != 0) {
1181 if (hscroll > 0 || vscroll > 0) {
1182 scrollRight();
1183 } else {
1184 scrollLeft();
1185 }
1186 return true;
1187 }
1188 }
1189 }
1190 }
1191 return super.onGenericMotionEvent(event);
1192 }
1193
Michael Jurkab8f06722010-10-10 15:58:46 -07001194 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1195 if (mVelocityTracker == null) {
1196 mVelocityTracker = VelocityTracker.obtain();
1197 }
1198 mVelocityTracker.addMovement(ev);
1199 }
1200
1201 private void releaseVelocityTracker() {
1202 if (mVelocityTracker != null) {
1203 mVelocityTracker.recycle();
1204 mVelocityTracker = null;
1205 }
1206 }
1207
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 private void onSecondaryPointerUp(MotionEvent ev) {
1209 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1210 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1211 final int pointerId = ev.getPointerId(pointerIndex);
1212 if (pointerId == mActivePointerId) {
1213 // This was our active pointer going up. Choose a new
1214 // active pointer and adjust accordingly.
1215 // TODO: Make this decision more intelligent.
1216 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1217 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1218 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001219 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 mActivePointerId = ev.getPointerId(newPointerIndex);
1221 if (mVelocityTracker != null) {
1222 mVelocityTracker.clear();
1223 }
1224 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001225 }
1226
Michael Jurkad771c962011-08-09 15:00:48 -07001227 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001228
1229 @Override
1230 public void requestChildFocus(View child, View focused) {
1231 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001232 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001233 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001234 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 }
1236 }
1237
Winson Chunge3193b92010-09-10 11:44:42 -07001238 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1239 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001240 int left;
1241 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001242 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001243 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001244 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001245 if (left <= relativeOffset && relativeOffset <= right) {
1246 return i;
1247 }
Winson Chunge3193b92010-09-10 11:44:42 -07001248 }
1249 return -1;
1250 }
1251
Winson Chung1908d072011-02-24 18:09:44 -08001252 protected void setMinimumWidthOverride(int minimumWidth) {
1253 mMinimumWidth = minimumWidth;
1254 }
Winson Chung34b23d52011-03-18 11:29:34 -07001255 protected void resetMinimumWidthOverride() {
1256 mMinimumWidth = 0;
1257 }
Winson Chung1908d072011-02-24 18:09:44 -08001258
1259 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001260 // This functions are called enough times that it actually makes a difference in the
1261 // profiler -- so just inline the max() here
1262 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1263 final int minWidth = mMinimumWidth;
1264 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001265 }
1266
Winson Chung321e9ee2010-08-09 13:37:56 -07001267 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001268 int padding = mPaddingLeft + mPaddingRight;
1269 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001271 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001272 int padding = mPaddingLeft + mPaddingRight;
1273 return mPaddingLeft + (getMeasuredWidth() - padding -
1274 getScaledMeasuredWidth(getChildAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001275 }
1276
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 protected int getChildOffset(int index) {
1278 if (getChildCount() == 0)
1279 return 0;
1280
1281 int offset = getRelativeChildOffset(0);
1282 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001283 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001284 }
1285 return offset;
1286 }
1287
Michael Jurkad3ef3062010-11-23 16:23:58 -08001288 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001289 // This functions are called enough times that it actually makes a difference in the
1290 // profiler -- so just inline the max() here
1291 final int measuredWidth = child.getMeasuredWidth();
1292 final int minWidth = mMinimumWidth;
1293 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1294 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001295 }
1296
Adam Cohend19d3ca2010-09-15 14:43:42 -07001297 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001298 int minDistanceFromScreenCenter = getMeasuredWidth();
1299 int minDistanceFromScreenCenterIndex = -1;
1300 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1301 final int childCount = getChildCount();
1302 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001303 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001304 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 int halfChildWidth = (childWidth / 2);
1306 int childCenter = getChildOffset(i) + halfChildWidth;
1307 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1308 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1309 minDistanceFromScreenCenter = distanceFromScreenCenter;
1310 minDistanceFromScreenCenterIndex = i;
1311 }
1312 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001313 return minDistanceFromScreenCenterIndex;
1314 }
1315
1316 protected void snapToDestination() {
1317 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 }
1319
Adam Cohene0f66b52010-11-23 15:06:07 -08001320 private static class ScrollInterpolator implements Interpolator {
1321 public ScrollInterpolator() {
1322 }
1323
1324 public float getInterpolation(float t) {
1325 t -= 1.0f;
1326 return t*t*t*t*t + 1;
1327 }
1328 }
1329
1330 // We want the duration of the page snap animation to be influenced by the distance that
1331 // the screen has to travel, however, we don't want this duration to be effected in a
1332 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1333 // of travel has on the overall snap duration.
1334 float distanceInfluenceForSnapDuration(float f) {
1335 f -= 0.5f; // center the values about 0.
1336 f *= 0.3f * Math.PI / 2.0f;
1337 return (float) Math.sin(f);
1338 }
1339
Michael Jurka0142d492010-08-25 17:46:15 -07001340 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001341 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1342 int halfScreenSize = getMeasuredWidth() / 2;
1343
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1345 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1346 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001347 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1348 int delta = newX - mUnboundedScrollX;
1349 int duration = 0;
1350
1351 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1352 // If the velocity is low enough, then treat this more as an automatic page advance
1353 // as opposed to an apparent physical response to flinging
1354 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1355 return;
1356 }
1357
1358 // Here we compute a "distance" that will be used in the computation of the overall
1359 // snap duration. This is a function of the actual distance that needs to be traveled;
1360 // we keep this value close to half screen size in order to reduce the variance in snap
1361 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001362 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001363 float distance = halfScreenSize + halfScreenSize *
1364 distanceInfluenceForSnapDuration(distanceRatio);
1365
1366 velocity = Math.abs(velocity);
1367 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1368
1369 // we want the page's snap velocity to approximately match the velocity at which the
1370 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001371 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1372 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001373
1374 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001375 }
1376
1377 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001378 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001379 }
1380
Michael Jurka0142d492010-08-25 17:46:15 -07001381 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001382 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001383
Winson Chung785d2eb2011-04-14 16:08:02 -07001384 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1385 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1386 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001387 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001388 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001390 snapToPage(whichPage, delta, duration);
1391 }
1392
1393 protected void snapToPage(int whichPage, int delta, int duration) {
1394 mNextPage = whichPage;
1395
1396 View focusedChild = getFocusedChild();
1397 if (focusedChild != null && whichPage != mCurrentPage &&
1398 focusedChild == getChildAt(mCurrentPage)) {
1399 focusedChild.clearFocus();
1400 }
1401
1402 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001403 awakenScrollBars(duration);
1404 if (duration == 0) {
1405 duration = Math.abs(delta);
1406 }
1407
1408 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001409 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001410
Winson Chungb44b5242011-06-13 11:32:14 -07001411 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1412 // loading associated pages until the scroll settles
1413 if (mDeferScrollUpdate) {
1414 loadAssociatedPages(mNextPage);
1415 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001416 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001417 }
Michael Jurka0142d492010-08-25 17:46:15 -07001418 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 invalidate();
1420 }
1421
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 public void scrollLeft() {
1423 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001424 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001426 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 }
1428 }
1429
1430 public void scrollRight() {
1431 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001432 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001434 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001435 }
1436 }
1437
Winson Chung86f77532010-08-24 11:08:22 -07001438 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 int result = -1;
1440 if (v != null) {
1441 ViewParent vp = v.getParent();
1442 int count = getChildCount();
1443 for (int i = 0; i < count; i++) {
1444 if (vp == getChildAt(i)) {
1445 return i;
1446 }
1447 }
1448 }
1449 return result;
1450 }
1451
1452 /**
1453 * @return True is long presses are still allowed for the current touch
1454 */
1455 public boolean allowLongPress() {
1456 return mAllowLongPress;
1457 }
1458
Michael Jurka0142d492010-08-25 17:46:15 -07001459 /**
1460 * Set true to allow long-press events to be triggered, usually checked by
1461 * {@link Launcher} to accept or block dpad-initiated long-presses.
1462 */
1463 public void setAllowLongPress(boolean allowLongPress) {
1464 mAllowLongPress = allowLongPress;
1465 }
1466
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001468 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001469
1470 SavedState(Parcelable superState) {
1471 super(superState);
1472 }
1473
1474 private SavedState(Parcel in) {
1475 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001476 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001477 }
1478
1479 @Override
1480 public void writeToParcel(Parcel out, int flags) {
1481 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001482 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 }
1484
1485 public static final Parcelable.Creator<SavedState> CREATOR =
1486 new Parcelable.Creator<SavedState>() {
1487 public SavedState createFromParcel(Parcel in) {
1488 return new SavedState(in);
1489 }
1490
1491 public SavedState[] newArray(int size) {
1492 return new SavedState[size];
1493 }
1494 };
1495 }
1496
Winson Chung86f77532010-08-24 11:08:22 -07001497 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001498 if (mContentIsRefreshable) {
1499 final int count = getChildCount();
1500 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001501 int lowerPageBound = getAssociatedLowerPageBound(page);
1502 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001503 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1504 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001505 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001506 Page layout = (Page) getChildAt(i);
1507 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001508 if (lowerPageBound <= i && i <= upperPageBound) {
1509 if (mDirtyPageContent.get(i)) {
1510 syncPageItems(i);
1511 mDirtyPageContent.set(i, false);
1512 }
1513 } else {
1514 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001515 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001516 }
1517 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001518 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001519 }
1520 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001521 }
1522 }
1523
Winson Chunge3193b92010-09-10 11:44:42 -07001524 protected int getAssociatedLowerPageBound(int page) {
1525 return Math.max(0, page - 1);
1526 }
1527 protected int getAssociatedUpperPageBound(int page) {
1528 final int count = getChildCount();
1529 return Math.min(page + 1, count - 1);
1530 }
1531
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001532 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001533 if (isChoiceMode(CHOICE_MODE_NONE)) {
1534 mChoiceMode = mode;
1535 mActionMode = startActionMode(callback);
1536 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001537 }
1538
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001539 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001540 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001541 mChoiceMode = CHOICE_MODE_NONE;
1542 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001543 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001544 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001545 }
1546 }
1547
1548 protected boolean isChoiceMode(int mode) {
1549 return mChoiceMode == mode;
1550 }
1551
1552 protected ArrayList<Checkable> getCheckedGrandchildren() {
1553 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1554 final int childCount = getChildCount();
1555 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001556 Page layout = (Page) getChildAt(i);
1557 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001558 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001559 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001560 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001561 checked.add((Checkable) v);
1562 }
1563 }
1564 }
1565 return checked;
1566 }
1567
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001568 /**
1569 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1570 * Otherwise, returns null.
1571 */
1572 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001573 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001574 final int childCount = getChildCount();
1575 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001576 Page layout = (Page) getChildAt(i);
1577 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001578 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001579 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001580 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1581 return (Checkable) v;
1582 }
1583 }
1584 }
1585 }
1586 return null;
1587 }
1588
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001589 protected void resetCheckedGrandchildren() {
1590 // loop through children, and set all of their children to _not_ be checked
1591 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1592 for (int i = 0; i < checked.size(); ++i) {
1593 final Checkable c = checked.get(i);
1594 c.setChecked(false);
1595 }
1596 }
1597
Winson Chung86f77532010-08-24 11:08:22 -07001598 /**
1599 * This method is called ONLY to synchronize the number of pages that the paged view has.
1600 * To actually fill the pages with information, implement syncPageItems() below. It is
1601 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1602 * and therefore, individual page items do not need to be updated in this method.
1603 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001604 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001605
1606 /**
1607 * This method is called to synchronize the items that are on a particular page. If views on
1608 * the page can be reused, then they should be updated within this method.
1609 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001610 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001611
Michael Jurka983e3fd2011-05-26 17:10:29 -07001612 protected void postInvalidatePageData(final boolean clearViews) {
1613 post(new Runnable() {
1614 // post the call to avoid a call to requestLayout from a layout pass
1615 public void run() {
1616 if (clearViews) {
1617 removeAllViews();
1618 }
1619 invalidatePageData();
1620 }
1621 });
1622 }
1623
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001624 protected void invalidatePageData() {
Winson Chung5a808352011-06-27 19:08:49 -07001625 invalidatePageData(-1);
1626 }
1627 protected void invalidatePageData(int currentPage) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001628 if (!mIsDataReady) {
1629 return;
1630 }
1631
Michael Jurka0142d492010-08-25 17:46:15 -07001632 if (mContentIsRefreshable) {
1633 // Update all the pages
1634 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001635
Winson Chung5a808352011-06-27 19:08:49 -07001636 // We must force a measure after we've loaded the pages to update the content width and
1637 // to determine the full scroll width
1638 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1639 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1640
1641 // Set a new page as the current page if necessary
1642 if (currentPage > -1) {
1643 setCurrentPage(currentPage);
1644 }
1645
Michael Jurka0142d492010-08-25 17:46:15 -07001646 // Mark each of the pages as dirty
1647 final int count = getChildCount();
1648 mDirtyPageContent.clear();
1649 for (int i = 0; i < count; ++i) {
1650 mDirtyPageContent.add(true);
1651 }
1652
1653 // Load any pages that are necessary for the current window of views
1654 loadAssociatedPages(mCurrentPage);
1655 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001656 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001657 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001658 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 }
Winson Chung007c6982011-06-14 13:27:53 -07001660
1661 private ImageView getScrollingIndicator() {
1662 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1663 // found
1664 if (mHasScrollIndicator && mScrollIndicator == null) {
1665 ViewGroup parent = (ViewGroup) getParent();
1666 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1667 mHasScrollIndicator = mScrollIndicator != null;
1668 if (mHasScrollIndicator) {
1669 mScrollIndicator.setVisibility(View.VISIBLE);
1670 }
1671 }
1672 return mScrollIndicator;
1673 }
1674
1675 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001676 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001677 }
1678
Winson Chung5a808352011-06-27 19:08:49 -07001679 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1680 @Override
1681 public void run() {
1682 hideScrollingIndicator(false);
1683 }
1684 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001685 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001686 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001687 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001688 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001689 }
1690
Michael Jurka430e8a52011-08-08 15:52:14 -07001691 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001692 if (getChildCount() <= 1) return;
1693 if (!isScrollingIndicatorEnabled()) return;
1694
1695 getScrollingIndicator();
1696 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001697 // Fade the indicator in
1698 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001699 mScrollIndicator.setVisibility(View.VISIBLE);
1700 if (mScrollIndicatorAnimator != null) {
1701 mScrollIndicatorAnimator.cancel();
1702 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001703 if (immediately) {
1704 mScrollIndicator.setAlpha(1f);
1705 } else {
1706 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1707 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1708 mScrollIndicatorAnimator.start();
1709 }
Winson Chung007c6982011-06-14 13:27:53 -07001710 }
1711 }
1712
1713 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001714 if (getChildCount() <= 1) return;
1715 if (!isScrollingIndicatorEnabled()) return;
1716
1717 getScrollingIndicator();
1718 if (mScrollIndicator != null) {
1719 // Fade the indicator out
1720 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001721 if (mScrollIndicatorAnimator != null) {
1722 mScrollIndicatorAnimator.cancel();
1723 }
1724 if (immediately) {
1725 mScrollIndicator.setVisibility(View.GONE);
1726 mScrollIndicator.setAlpha(0f);
1727 } else {
1728 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1729 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1730 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1731 private boolean cancelled = false;
1732 @Override
1733 public void onAnimationCancel(android.animation.Animator animation) {
1734 cancelled = true;
1735 }
1736 @Override
1737 public void onAnimationEnd(Animator animation) {
1738 if (!cancelled) {
1739 mScrollIndicator.setVisibility(View.GONE);
1740 }
1741 }
1742 });
1743 mScrollIndicatorAnimator.start();
1744 }
Winson Chung007c6982011-06-14 13:27:53 -07001745 }
1746 }
1747
Winson Chung32174c82011-07-19 15:47:55 -07001748 /**
1749 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1750 * fill its space on the track or not.
1751 */
1752 protected boolean hasElasticScrollIndicator() {
1753 return false;
1754 }
1755
Winson Chung007c6982011-06-14 13:27:53 -07001756 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001757 if (getChildCount() <= 1) return;
1758 if (!isScrollingIndicatorEnabled()) return;
1759
1760 getScrollingIndicator();
1761 if (mScrollIndicator != null) {
1762 updateScrollingIndicatorPosition();
1763 }
1764 }
1765
Winson Chung007c6982011-06-14 13:27:53 -07001766 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001767 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001768 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001769 int numPages = getChildCount();
1770 int pageWidth = getMeasuredWidth();
Winson Chung4afe9b32011-07-27 17:46:20 -07001771 int maxPageWidth = (numPages * getChildWidth(0)) + ((numPages - 1) * mPageSpacing);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001772 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001773 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1774 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001775
Winson Chung007c6982011-06-14 13:27:53 -07001776 float offset = (float) getScrollX() / maxPageWidth;
Winson Chung32174c82011-07-19 15:47:55 -07001777 int indicatorSpace = trackWidth / numPages;
Winson Chungf5f8cef2011-07-22 11:16:13 -07001778 int indicatorPos = (int) (offset * trackWidth) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001779 if (hasElasticScrollIndicator()) {
1780 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1781 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1782 mScrollIndicator.requestLayout();
1783 }
1784 } else {
1785 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1786 indicatorPos += indicatorCenterOffset;
1787 }
Winson Chung007c6982011-06-14 13:27:53 -07001788 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001789 mScrollIndicator.invalidate();
1790 }
1791
Winson Chung3ac74c52011-06-30 17:39:37 -07001792 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001793 }
1794
1795 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001796 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001797
1798 /* Accessibility */
1799 @Override
1800 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1801 super.onInitializeAccessibilityNodeInfo(info);
1802 info.setScrollable(true);
1803 }
1804
1805 @Override
1806 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1807 super.onInitializeAccessibilityEvent(event);
1808 event.setScrollable(true);
1809 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1810 event.setFromIndex(mCurrentPage);
1811 event.setToIndex(mCurrentPage);
1812 event.setItemCount(getChildCount());
1813 }
1814 }
1815
1816 @Override
1817 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1818 // Do not append text content to scroll events they are fired frequently
1819 // and the client has already received another event type with the text.
1820 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1821 super.dispatchPopulateAccessibilityEvent(event);
1822 }
1823
1824 onPopulateAccessibilityEvent(event);
1825 return false;
1826 }
1827
1828 @Override
1829 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1830 super.onPopulateAccessibilityEvent(event);
1831
1832 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1833 event.getText().add(getCurrentPageDescription());
1834 }
1835 }
1836
1837 protected String getCurrentPageDescription() {
1838 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1839 return String.format(mContext.getString(R.string.default_scroll_format),
1840 page + 1, getChildCount());
1841 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001842}