blob: d7e9e06e0ef0ffaf3dabc0464a252fcc8930af4c [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
Adam Cohenb5ba0972011-09-07 18:02:31 -070069 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070070 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080071 private static final int MINIMUM_SNAP_VELOCITY = 2200;
72 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080073 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080074
Michael Jurka0142d492010-08-25 17:46:15 -070075 // the velocity at which a fling gesture will cause us to snap to the next page
76 protected int mSnapVelocity = 500;
77
Adam Cohenb5ba0972011-09-07 18:02:31 -070078 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070079 protected float mSmoothingTime;
80 protected float mTouchX;
81
82 protected boolean mFirstLayout = true;
83
84 protected int mCurrentPage;
85 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080086 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070088 private VelocityTracker mVelocityTracker;
89
90 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080091 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080092 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080093 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080094 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070095 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070096
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected final static int TOUCH_STATE_REST = 0;
98 protected final static int TOUCH_STATE_SCROLLING = 1;
99 protected final static int TOUCH_STATE_PREV_PAGE = 2;
100 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700101 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102
Michael Jurka0142d492010-08-25 17:46:15 -0700103 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104
Michael Jurka0142d492010-08-25 17:46:15 -0700105 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka7426c422010-11-11 15:23:47 -0800107 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Michael Jurka7426c422010-11-11 15:23:47 -0800109 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 private int mPagingTouchSlop;
111 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800112 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700113 protected int mPageSpacing;
114 protected int mPageLayoutPaddingTop;
115 protected int mPageLayoutPaddingBottom;
116 protected int mPageLayoutPaddingLeft;
117 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700118 protected int mPageLayoutWidthGap;
119 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700120 protected int mCellCountX = 0;
121 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700122 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800123 protected boolean mAllowOverScroll = true;
124 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Michael Jurka8c920dd2011-01-20 14:16:56 -0800126 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800127 protected float mLayoutScale = 1.0f;
128
Michael Jurka5f1c5092010-09-03 14:15:02 -0700129 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Michael Jurka5f1c5092010-09-03 14:15:02 -0700131 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Winson Chung86f77532010-08-24 11:08:22 -0700133 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Winson Chung86f77532010-08-24 11:08:22 -0700135 private ArrayList<Boolean> mDirtyPageContent;
Michael Jurka86c119a2011-08-05 20:35:36 -0700136 private boolean mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700138 // choice modes
139 protected static final int CHOICE_MODE_NONE = 0;
140 protected static final int CHOICE_MODE_SINGLE = 1;
141 // Multiple selection mode is not supported by all Launcher actions atm
142 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700143
Michael Jurkae17e19c2010-09-28 11:01:39 -0700144 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700145 private ActionMode mActionMode;
146
Michael Jurka0142d492010-08-25 17:46:15 -0700147 // If true, syncPages and syncPageItems will be called to refresh pages
148 protected boolean mContentIsRefreshable = true;
149
150 // If true, modify alpha of neighboring pages as user scrolls left/right
151 protected boolean mFadeInAdjacentScreens = true;
152
153 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
154 // to switch to a new page
155 protected boolean mUsePagingTouchSlop = true;
156
157 // If true, the subclass should directly update mScrollX itself in its computeScroll method
158 // (SmoothPagedView does this)
159 protected boolean mDeferScrollUpdate = false;
160
Patrick Dubroy1262e362010-10-06 15:49:50 -0700161 protected boolean mIsPageMoving = false;
162
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163 // All syncs and layout passes are deferred until data is ready.
164 protected boolean mIsDataReady = false;
165
Winson Chung007c6982011-06-14 13:27:53 -0700166 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700167 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700168 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700169 private int mScrollIndicatorPaddingLeft;
170 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700171 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700172 protected static final int sScrollIndicatorFadeInDuration = 150;
173 protected static final int sScrollIndicatorFadeOutDuration = 650;
174 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700175
Winson Chungb44b5242011-06-13 11:32:14 -0700176 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700177 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700178
Winson Chung86f77532010-08-24 11:08:22 -0700179 public interface PageSwitchListener {
180 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700181 }
182
Winson Chung321e9ee2010-08-09 13:37:56 -0700183 public PagedView(Context context) {
184 this(context, null);
185 }
186
187 public PagedView(Context context, AttributeSet attrs) {
188 this(context, attrs, 0);
189 }
190
191 public PagedView(Context context, AttributeSet attrs, int defStyle) {
192 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700193 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700194
Adam Cohen9c4949e2010-10-05 12:27:22 -0700195 TypedArray a = context.obtainStyledAttributes(attrs,
196 R.styleable.PagedView, defStyle, 0);
197 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
198 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800199 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800201 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800203 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800205 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700206 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700207 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700208 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700209 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700210 mScrollIndicatorPaddingLeft =
211 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
212 mScrollIndicatorPaddingRight =
213 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700214 a.recycle();
215
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700217 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 }
219
220 /**
221 * Initializes various states for this workspace.
222 */
Michael Jurka0142d492010-08-25 17:46:15 -0700223 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700224 mDirtyPageContent = new ArrayList<Boolean>();
225 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800226 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700227 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700228 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700229
230 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
231 mTouchSlop = configuration.getScaledTouchSlop();
232 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
233 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700234 mDensity = getResources().getDisplayMetrics().density;
Winson Chung321e9ee2010-08-09 13:37:56 -0700235 }
236
Winson Chung86f77532010-08-24 11:08:22 -0700237 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
238 mPageSwitchListener = pageSwitchListener;
239 if (mPageSwitchListener != null) {
240 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 }
242 }
243
244 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700245 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
246 * out pages.
247 */
248 protected void setDataIsReady() {
249 mIsDataReady = true;
250 }
251 protected boolean isDataReady() {
252 return mIsDataReady;
253 }
254
255 /**
Winson Chung86f77532010-08-24 11:08:22 -0700256 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 *
Winson Chung86f77532010-08-24 11:08:22 -0700258 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 */
Winson Chung86f77532010-08-24 11:08:22 -0700260 int getCurrentPage() {
261 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 }
263
Winson Chung86f77532010-08-24 11:08:22 -0700264 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700265 return getChildCount();
266 }
267
Winson Chung86f77532010-08-24 11:08:22 -0700268 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 return getChildAt(index);
270 }
271
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800273 * Updates the scroll of the current page immediately to its final scroll position. We use this
274 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
275 * the previous tab page.
276 */
277 protected void updateCurrentPageScroll() {
278 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
279 scrollTo(newX, 0);
280 mScroller.setFinalX(newX);
281 }
282
283 /**
Winson Chung86f77532010-08-24 11:08:22 -0700284 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 */
Winson Chung86f77532010-08-24 11:08:22 -0700286 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800287 if (!mScroller.isFinished()) {
288 mScroller.abortAnimation();
289 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800290 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
291 // the default
292 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800293 return;
294 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700295
Winson Chung86f77532010-08-24 11:08:22 -0700296 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800297 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700298 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700299 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800300 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700301 }
302
Michael Jurka0142d492010-08-25 17:46:15 -0700303 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700304 if (mPageSwitchListener != null) {
305 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 }
307 }
308
Michael Jurkace7e05f2011-02-01 22:02:35 -0800309 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700310 if (!mIsPageMoving) {
311 mIsPageMoving = true;
312 onPageBeginMoving();
313 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700314 }
315
Michael Jurkace7e05f2011-02-01 22:02:35 -0800316 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700317 if (mIsPageMoving) {
318 mIsPageMoving = false;
319 onPageEndMoving();
320 }
Michael Jurka0142d492010-08-25 17:46:15 -0700321 }
322
Adam Cohen26976d92011-03-22 15:33:33 -0700323 protected boolean isPageMoving() {
324 return mIsPageMoving;
325 }
326
Michael Jurka0142d492010-08-25 17:46:15 -0700327 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700328 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700329 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700330 }
331
332 // a method that subclasses can override to add behavior
333 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700334 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700335 }
336
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 /**
Winson Chung86f77532010-08-24 11:08:22 -0700338 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700339 *
340 * @param l The listener used to respond to long clicks.
341 */
342 @Override
343 public void setOnLongClickListener(OnLongClickListener l) {
344 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700345 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700346 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700347 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 }
349 }
350
351 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800352 public void scrollBy(int x, int y) {
353 scrollTo(mUnboundedScrollX + x, mScrollY + y);
354 }
355
356 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700357 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800358 mUnboundedScrollX = x;
359
360 if (x < 0) {
361 super.scrollTo(0, y);
362 if (mAllowOverScroll) {
363 overScroll(x);
364 }
365 } else if (x > mMaxScrollX) {
366 super.scrollTo(mMaxScrollX, y);
367 if (mAllowOverScroll) {
368 overScroll(x - mMaxScrollX);
369 }
370 } else {
371 super.scrollTo(x, y);
372 }
373
Michael Jurka0142d492010-08-25 17:46:15 -0700374 mTouchX = x;
375 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
376 }
377
378 // we moved this functionality to a helper function so SmoothPagedView can reuse it
379 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700381 // Don't bother scrolling if the page does not need to be moved
382 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
383 mDirtyPageAlpha = true;
384 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
385 }
Michael Jurka0142d492010-08-25 17:46:15 -0700386 invalidate();
387 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700388 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700389 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700390 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700391 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700392 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700393
394 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700395 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700396 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700397 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700398 }
399
Adam Cohen73aa9752010-11-24 16:26:58 -0800400 // We don't want to trigger a page end moving unless the page has settled
401 // and the user has stopped scrolling
402 if (mTouchState == TOUCH_STATE_REST) {
403 pageEndMoving();
404 }
Michael Jurka0142d492010-08-25 17:46:15 -0700405 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 }
Michael Jurka0142d492010-08-25 17:46:15 -0700407 return false;
408 }
409
410 @Override
411 public void computeScroll() {
412 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700413 }
414
415 @Override
416 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700417 if (!mIsDataReady) {
418 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
419 return;
420 }
421
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
423 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
424 if (widthMode != MeasureSpec.EXACTLY) {
425 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
426 }
427
Adam Lesinski6b879f02010-11-04 16:15:23 -0700428 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
429 * of the All apps view on XLarge displays to not take up more space then it needs. Width
430 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
431 * each page to have the same width.
432 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700434 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
435 int maxChildHeight = 0;
436
437 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700438 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700439
Michael Jurka36fcb742011-04-06 17:08:58 -0700440
Winson Chung321e9ee2010-08-09 13:37:56 -0700441 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700442 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700443 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 final int childCount = getChildCount();
445 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700446 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700447 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700448 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
449
450 int childWidthMode;
451 if (lp.width == LayoutParams.WRAP_CONTENT) {
452 childWidthMode = MeasureSpec.AT_MOST;
453 } else {
454 childWidthMode = MeasureSpec.EXACTLY;
455 }
456
457 int childHeightMode;
458 if (lp.height == LayoutParams.WRAP_CONTENT) {
459 childHeightMode = MeasureSpec.AT_MOST;
460 } else {
461 childHeightMode = MeasureSpec.EXACTLY;
462 }
463
464 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700465 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700466 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700467 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700468
469 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700470 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700471 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
472 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700473 }
474
475 if (heightMode == MeasureSpec.AT_MOST) {
476 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 }
Winson Chungae890b82011-09-13 18:08:54 -0700478
479 updateScrollingIndicatorPosition();
480
481 setMeasuredDimension(widthSize, heightSize);
482
483 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions
Adam Cohenfaa28302010-11-19 12:02:18 -0800484 if (childCount > 0) {
485 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
486 } else {
487 mMaxScrollX = 0;
488 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
490
Michael Jurka8c920dd2011-01-20 14:16:56 -0800491 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800492 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
493 int delta = newX - mScrollX;
494
Michael Jurka8c920dd2011-01-20 14:16:56 -0800495 final int pageCount = getChildCount();
496 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700497 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800498 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800499 }
500 setCurrentPage(newCurrentPage);
501 }
502
Michael Jurka8c920dd2011-01-20 14:16:56 -0800503 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
504 // 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 -0800505 // tightens the layout accordingly
506 public void setLayoutScale(float childrenScale) {
507 mLayoutScale = childrenScale;
508
509 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
510 int childCount = getChildCount();
511 float childrenX[] = new float[childCount];
512 float childrenY[] = new float[childCount];
513 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700514 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800515 childrenX[i] = child.getX();
516 childrenY[i] = child.getY();
517 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700518 // Trigger a full re-layout (never just call onLayout directly!)
519 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
520 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
521 requestLayout();
522 measure(widthSpec, heightSpec);
523 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700525 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526 child.setX(childrenX[i]);
527 child.setY(childrenY[i]);
528 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700529
Michael Jurkad3ef3062010-11-23 16:23:58 -0800530 // Also, the page offset has changed (since the pages are now smaller);
531 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800532 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533 }
534
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700536 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700537 if (!mIsDataReady) {
538 return;
539 }
540
Winson Chung785d2eb2011-04-14 16:08:02 -0700541 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700542 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 final int childCount = getChildCount();
544 int childLeft = 0;
545 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700546 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
547 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700548 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700549
550 // Calculate the variable page spacing if necessary
551 if (mPageSpacing < 0) {
552 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
553 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 }
555
556 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700557 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700560 final int childHeight = child.getMeasuredHeight();
561 int childTop = mPaddingTop;
562 if (mCenterPagesVertically) {
563 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
564 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800565
Winson Chung785d2eb2011-04-14 16:08:02 -0700566 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700567 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800568 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700569 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700572
573 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
574 setHorizontalScrollBarEnabled(false);
575 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
576 scrollTo(newX, 0);
577 mScroller.setFinalX(newX);
578 setHorizontalScrollBarEnabled(true);
579 mFirstLayout = false;
580 }
581
Michael Jurkad3ef3062010-11-23 16:23:58 -0800582 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
583 mFirstLayout = false;
584 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700585 }
586
Winson Chung03929772011-02-23 17:07:10 -0800587 protected void forceUpdateAdjacentPagesAlpha() {
588 mDirtyPageAlpha = true;
589 updateAdjacentPagesAlpha();
590 }
591
Winson Chunge3193b92010-09-10 11:44:42 -0700592 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700593 if (mFadeInAdjacentScreens) {
594 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700595 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700596 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700597 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700598 final int childCount = getChildCount();
599 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700600 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800601 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700602 int halfChildWidth = (childWidth / 2);
603 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700604
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700605 // On the first layout, we may not have a width nor a proper offset, so for now
606 // we should just assume full page width (and calculate the offset according to
607 // that).
608 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700609 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700610 childCenter = (i * childWidth) + (childWidth / 2);
611 }
612
Winson Chunge8878e32010-09-15 20:37:09 -0700613 int d = halfChildWidth;
614 int distanceFromScreenCenter = childCenter - screenCenter;
615 if (distanceFromScreenCenter > 0) {
616 if (i > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700617 d += getScaledMeasuredWidth(getPageAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700618 } else {
619 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700620 }
Michael Jurka0142d492010-08-25 17:46:15 -0700621 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700622 if (i < childCount - 1) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700623 d += getScaledMeasuredWidth(getPageAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700624 } else {
625 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700626 }
Michael Jurka0142d492010-08-25 17:46:15 -0700627 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700628 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700629
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700630 // Preventing potential divide-by-zero
631 d = Math.max(1, d);
632
Winson Chunge8878e32010-09-15 20:37:09 -0700633 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
634 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
635 float alpha = 1.0f - dimAlpha;
636
Adam Cohenf34bab52010-09-30 14:11:56 -0700637 if (alpha < ALPHA_QUANTIZE_LEVEL) {
638 alpha = 0.0f;
639 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
640 alpha = 1.0f;
641 }
642
Michael Jurkaa40829a2011-02-16 18:02:45 -0800643 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
644 // this optimization causes alpha to not be properly updated sometimes (repro
645 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
646 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
647 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800648 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700649 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800650 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 }
Michael Jurka0142d492010-08-25 17:46:15 -0700652 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 }
654 }
Winson Chunge3193b92010-09-10 11:44:42 -0700655 }
656
Adam Cohenf34bab52010-09-30 14:11:56 -0700657 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700658 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700659 }
660
Winson Chunge3193b92010-09-10 11:44:42 -0700661 @Override
662 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700663 int halfScreenSize = getMeasuredWidth() / 2;
664 int screenCenter = mScrollX + halfScreenSize;
665
666 if (screenCenter != mLastScreenCenter) {
667 screenScrolled(screenCenter);
668 updateAdjacentPagesAlpha();
669 mLastScreenCenter = screenCenter;
670 }
Michael Jurka0142d492010-08-25 17:46:15 -0700671
672 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700673 // As an optimization, this code assumes that all pages have the same width as the 0th
674 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700675 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700676 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700677 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700678 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700679 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700680 int leftScreen = 0;
681 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700682 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700683 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700684 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700685 }
686 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700687 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700688 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700689 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700690 }
691 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700692
Michael Jurkac4fb9172010-09-02 17:19:20 -0700693 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800694 // Clip to the bounds
695 canvas.save();
696 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
697 mScrollY + mBottom - mTop);
698
Adam Cohen22f823d2011-09-01 17:22:18 -0700699 for (int i = rightScreen; i >= leftScreen; i--) {
700 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700701 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800702 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700703 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700704 }
705
706 @Override
707 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700708 int page = indexOfChild(child);
709 if (page != mCurrentPage || !mScroller.isFinished()) {
710 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 return true;
712 }
713 return false;
714 }
715
716 @Override
717 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700718 int focusablePage;
719 if (mNextPage != INVALID_PAGE) {
720 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700721 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700722 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 }
Winson Chung86f77532010-08-24 11:08:22 -0700724 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700726 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 }
728 return false;
729 }
730
731 @Override
732 public boolean dispatchUnhandledMove(View focused, int direction) {
733 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700734 if (getCurrentPage() > 0) {
735 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 return true;
737 }
738 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700739 if (getCurrentPage() < getPageCount() - 1) {
740 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 return true;
742 }
743 }
744 return super.dispatchUnhandledMove(focused, direction);
745 }
746
747 @Override
748 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700749 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
750 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700753 if (mCurrentPage > 0) {
754 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 }
756 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700757 if (mCurrentPage < getPageCount() - 1) {
758 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 }
760 }
761 }
762
763 /**
764 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700765 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700766 *
Winson Chung86f77532010-08-24 11:08:22 -0700767 * This happens when live folders requery, and if they're off page, they
768 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 */
770 @Override
771 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700772 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 View v = focused;
774 while (true) {
775 if (v == current) {
776 super.focusableViewAvailable(focused);
777 return;
778 }
779 if (v == this) {
780 return;
781 }
782 ViewParent parent = v.getParent();
783 if (parent instanceof View) {
784 v = (View)v.getParent();
785 } else {
786 return;
787 }
788 }
789 }
790
791 /**
792 * {@inheritDoc}
793 */
794 @Override
795 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
796 if (disallowIntercept) {
797 // We need to make sure to cancel our long press if
798 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700799 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700800 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 }
802 super.requestDisallowInterceptTouchEvent(disallowIntercept);
803 }
804
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800805 /**
806 * Return true if a tap at (x, y) should trigger a flip to the previous page.
807 */
808 protected boolean hitsPreviousPage(float x, float y) {
809 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
810 }
811
812 /**
813 * Return true if a tap at (x, y) should trigger a flip to the next page.
814 */
815 protected boolean hitsNextPage(float x, float y) {
816 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
817 }
818
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 @Override
820 public boolean onInterceptTouchEvent(MotionEvent ev) {
821 /*
822 * This method JUST determines whether we want to intercept the motion.
823 * If we return true, onTouchEvent will be called and we do the actual
824 * scrolling there.
825 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800826 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700827
Winson Chung45e1d6e2010-11-09 17:19:49 -0800828 // Skip touch handling if there are no pages to swipe
829 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
830
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 /*
832 * Shortcut the most recurring case: the user is in the dragging
833 * state and he is moving his finger. We want to intercept this
834 * motion.
835 */
836 final int action = ev.getAction();
837 if ((action == MotionEvent.ACTION_MOVE) &&
838 (mTouchState == TOUCH_STATE_SCROLLING)) {
839 return true;
840 }
841
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 switch (action & MotionEvent.ACTION_MASK) {
843 case MotionEvent.ACTION_MOVE: {
844 /*
845 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
846 * whether the user has moved far enough from his original down touch.
847 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700848 if (mActivePointerId != INVALID_POINTER) {
849 determineScrollingStart(ev);
850 break;
851 }
852 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
853 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
854 // i.e. fall through to the next case (don't break)
855 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
856 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 }
858
859 case MotionEvent.ACTION_DOWN: {
860 final float x = ev.getX();
861 final float y = ev.getY();
862 // Remember location of down touch
863 mDownMotionX = x;
864 mLastMotionX = x;
865 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800866 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800867 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 mActivePointerId = ev.getPointerId(0);
869 mAllowLongPress = true;
870
871 /*
872 * If being flinged and user touches the screen, initiate drag;
873 * otherwise don't. mScroller.isFinished should be false when
874 * being flinged.
875 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700876 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700877 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
878 if (finishedScrolling) {
879 mTouchState = TOUCH_STATE_REST;
880 mScroller.abortAnimation();
881 } else {
882 mTouchState = TOUCH_STATE_SCROLLING;
883 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700884
Winson Chung86f77532010-08-24 11:08:22 -0700885 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800887 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800889 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800891 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 mTouchState = TOUCH_STATE_NEXT_PAGE;
893 }
894 }
895 }
896 break;
897 }
898
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800900 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 mTouchState = TOUCH_STATE_REST;
902 mAllowLongPress = false;
903 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800904 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 break;
906
907 case MotionEvent.ACTION_POINTER_UP:
908 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800909 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 break;
911 }
912
913 /*
914 * The only time we want to intercept motion events is if we are in the
915 * drag mode.
916 */
917 return mTouchState != TOUCH_STATE_REST;
918 }
919
Winson Chung80baf5a2010-08-09 16:03:15 -0700920 protected void animateClickFeedback(View v, final Runnable r) {
921 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800922 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
923 loadAnimator(mContext, R.anim.paged_view_click_feedback);
924 anim.setTarget(v);
925 anim.addListener(new AnimatorListenerAdapter() {
926 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700927 r.run();
928 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700929 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800930 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700931 }
932
Adam Cohenf8d28232011-02-01 21:47:00 -0800933 protected void determineScrollingStart(MotionEvent ev) {
934 determineScrollingStart(ev, 1.0f);
935 }
936
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 /*
938 * Determines if we should change the touch state to start scrolling after the
939 * user moves their touch point too far.
940 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800941 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 /*
943 * Locally do absolute value. mLastMotionX is set to the y value
944 * of the down event.
945 */
946 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700947 if (pointerIndex == -1) {
948 return;
949 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 final float x = ev.getX(pointerIndex);
951 final float y = ev.getY(pointerIndex);
952 final int xDiff = (int) Math.abs(x - mLastMotionX);
953 final int yDiff = (int) Math.abs(y - mLastMotionY);
954
Adam Cohenf8d28232011-02-01 21:47:00 -0800955 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 boolean xPaged = xDiff > mPagingTouchSlop;
957 boolean xMoved = xDiff > touchSlop;
958 boolean yMoved = yDiff > touchSlop;
959
Adam Cohenf8d28232011-02-01 21:47:00 -0800960 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700961 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 // Scroll if the user moved far enough along the X axis
963 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800964 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800966 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700967 mTouchX = mScrollX;
968 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
969 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 }
971 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800972 cancelCurrentPageLongPress();
973 }
974 }
975
976 protected void cancelCurrentPageLongPress() {
977 if (mAllowLongPress) {
978 mAllowLongPress = false;
979 // Try canceling the long press. It could also have been scheduled
980 // by a distant descendant, so use the mAllowLongPress flag to block
981 // everything
982 final View currentPage = getPageAt(mCurrentPage);
983 if (currentPage != null) {
984 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700985 }
986 }
987 }
988
Adam Cohenb5ba0972011-09-07 18:02:31 -0700989 protected float getScrollProgress(int screenCenter, View v, int page) {
990 final int halfScreenSize = getMeasuredWidth() / 2;
991
992 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
993 int delta = screenCenter - (getChildOffset(page) -
994 getRelativeChildOffset(page) + halfScreenSize);
995
996 float scrollProgress = delta / (totalDistance * 1.0f);
997 scrollProgress = Math.min(scrollProgress, 1.0f);
998 scrollProgress = Math.max(scrollProgress, -1.0f);
999 return scrollProgress;
1000 }
1001
Adam Cohene0f66b52010-11-23 15:06:07 -08001002 // This curve determines how the effect of scrolling over the limits of the page dimishes
1003 // as the user pulls further and further from the bounds
1004 private float overScrollInfluenceCurve(float f) {
1005 f -= 1.0f;
1006 return f * f * f + 1.0f;
1007 }
1008
Adam Cohenb5ba0972011-09-07 18:02:31 -07001009 protected void acceleratedOverScroll(float amount) {
1010 int screenSize = getMeasuredWidth();
1011
1012 // We want to reach the max over scroll effect when the user has
1013 // over scrolled half the size of the screen
1014 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1015
1016 if (f == 0) return;
1017
1018 // Clamp this factor, f, to -1 < f < 1
1019 if (Math.abs(f) >= 1) {
1020 f /= Math.abs(f);
1021 }
1022
1023 int overScrollAmount = (int) Math.round(f * screenSize);
1024 if (amount < 0) {
1025 mScrollX = overScrollAmount;
1026 } else {
1027 mScrollX = mMaxScrollX + overScrollAmount;
1028 }
1029 invalidate();
1030 }
1031
1032 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001033 int screenSize = getMeasuredWidth();
1034
1035 float f = (amount / screenSize);
1036
1037 if (f == 0) return;
1038 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1039
Adam Cohen7bfc9792011-01-28 13:52:37 -08001040 // Clamp this factor, f, to -1 < f < 1
1041 if (Math.abs(f) >= 1) {
1042 f /= Math.abs(f);
1043 }
1044
Adam Cohene0f66b52010-11-23 15:06:07 -08001045 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001046 if (amount < 0) {
1047 mScrollX = overScrollAmount;
1048 } else {
1049 mScrollX = mMaxScrollX + overScrollAmount;
1050 }
1051 invalidate();
1052 }
1053
Adam Cohenb5ba0972011-09-07 18:02:31 -07001054 protected void overScroll(float amount) {
1055 dampedOverScroll(amount);
1056 }
1057
Michael Jurkac5b262c2011-01-12 20:24:50 -08001058 protected float maxOverScroll() {
1059 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001060 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001061 float f = 1.0f;
1062 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1063 return OVERSCROLL_DAMP_FACTOR * f;
1064 }
1065
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 @Override
1067 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001068 // Skip touch handling if there are no pages to swipe
1069 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1070
Michael Jurkab8f06722010-10-10 15:58:46 -07001071 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001072
1073 final int action = ev.getAction();
1074
1075 switch (action & MotionEvent.ACTION_MASK) {
1076 case MotionEvent.ACTION_DOWN:
1077 /*
1078 * If being flinged and user touches, stop the fling. isFinished
1079 * will be false if being flinged.
1080 */
1081 if (!mScroller.isFinished()) {
1082 mScroller.abortAnimation();
1083 }
1084
1085 // Remember where the motion event started
1086 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001087 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001088 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001089 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001090 if (mTouchState == TOUCH_STATE_SCROLLING) {
1091 pageBeginMoving();
1092 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 break;
1094
1095 case MotionEvent.ACTION_MOVE:
1096 if (mTouchState == TOUCH_STATE_SCROLLING) {
1097 // Scroll to follow the motion event
1098 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1099 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001100 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001101
Adam Cohenaefd4e12011-02-14 16:39:38 -08001102 mTotalMotionX += Math.abs(deltaX);
1103
Winson Chungc0844aa2011-02-02 15:25:58 -08001104 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1105 // keep the remainder because we are actually testing if we've moved from the last
1106 // scrolled position (which is discrete).
1107 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001108 mTouchX += deltaX;
1109 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1110 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001111 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001112 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001113 } else {
1114 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001116 mLastMotionX = x;
1117 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001118 } else {
1119 awakenScrollBars();
1120 }
Adam Cohen564976a2010-10-13 18:52:07 -07001121 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 determineScrollingStart(ev);
1123 }
1124 break;
1125
1126 case MotionEvent.ACTION_UP:
1127 if (mTouchState == TOUCH_STATE_SCROLLING) {
1128 final int activePointerId = mActivePointerId;
1129 final int pointerIndex = ev.findPointerIndex(activePointerId);
1130 final float x = ev.getX(pointerIndex);
1131 final VelocityTracker velocityTracker = mVelocityTracker;
1132 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1133 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001134 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001135 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001136 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001137
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001138 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1139
Adam Cohenaefd4e12011-02-14 16:39:38 -08001140 // In the case that the page is moved far to one direction and then is flung
1141 // in the opposite direction, we use a threshold to determine whether we should
1142 // just return to the starting page, or if we should skip one further.
1143 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001144 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001145 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001146 Math.signum(velocityX) != Math.signum(deltaX)) {
1147 returnToOriginalPage = true;
1148 }
1149
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001150 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001151 Math.abs(velocityX) > snapVelocity;
1152
1153 int finalPage;
1154 // We give flings precedence over large moves, which is why we short-circuit our
1155 // test for a large move if a fling has been registered. That is, a large
1156 // move to the left and fling to the right will register as a fling to the right.
1157 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1158 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1159 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1160 snapToPageWithVelocity(finalPage, velocityX);
1161 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1162 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001163 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001164 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1165 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001166 } else {
1167 snapToDestination();
1168 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001169 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 // at this point we have not moved beyond the touch slop
1171 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1172 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001173 int nextPage = Math.max(0, mCurrentPage - 1);
1174 if (nextPage != mCurrentPage) {
1175 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 } else {
1177 snapToDestination();
1178 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001179 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 // at this point we have not moved beyond the touch slop
1181 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1182 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001183 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1184 if (nextPage != mCurrentPage) {
1185 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 } else {
1187 snapToDestination();
1188 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001189 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001190 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001191 }
1192 mTouchState = TOUCH_STATE_REST;
1193 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001194 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 break;
1196
1197 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001198 if (mTouchState == TOUCH_STATE_SCROLLING) {
1199 snapToDestination();
1200 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 mTouchState = TOUCH_STATE_REST;
1202 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001203 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 break;
1205
1206 case MotionEvent.ACTION_POINTER_UP:
1207 onSecondaryPointerUp(ev);
1208 break;
1209 }
1210
1211 return true;
1212 }
1213
Winson Chung185d7162011-02-28 13:47:29 -08001214 @Override
1215 public boolean onGenericMotionEvent(MotionEvent event) {
1216 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1217 switch (event.getAction()) {
1218 case MotionEvent.ACTION_SCROLL: {
1219 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1220 final float vscroll;
1221 final float hscroll;
1222 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1223 vscroll = 0;
1224 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1225 } else {
1226 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1227 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1228 }
1229 if (hscroll != 0 || vscroll != 0) {
1230 if (hscroll > 0 || vscroll > 0) {
1231 scrollRight();
1232 } else {
1233 scrollLeft();
1234 }
1235 return true;
1236 }
1237 }
1238 }
1239 }
1240 return super.onGenericMotionEvent(event);
1241 }
1242
Michael Jurkab8f06722010-10-10 15:58:46 -07001243 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1244 if (mVelocityTracker == null) {
1245 mVelocityTracker = VelocityTracker.obtain();
1246 }
1247 mVelocityTracker.addMovement(ev);
1248 }
1249
1250 private void releaseVelocityTracker() {
1251 if (mVelocityTracker != null) {
1252 mVelocityTracker.recycle();
1253 mVelocityTracker = null;
1254 }
1255 }
1256
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 private void onSecondaryPointerUp(MotionEvent ev) {
1258 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1259 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1260 final int pointerId = ev.getPointerId(pointerIndex);
1261 if (pointerId == mActivePointerId) {
1262 // This was our active pointer going up. Choose a new
1263 // active pointer and adjust accordingly.
1264 // TODO: Make this decision more intelligent.
1265 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1266 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1267 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001268 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 mActivePointerId = ev.getPointerId(newPointerIndex);
1270 if (mVelocityTracker != null) {
1271 mVelocityTracker.clear();
1272 }
1273 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001274 }
1275
Michael Jurkad771c962011-08-09 15:00:48 -07001276 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001277
1278 @Override
1279 public void requestChildFocus(View child, View focused) {
1280 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001281 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001282 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001283 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001284 }
1285 }
1286
Winson Chunge3193b92010-09-10 11:44:42 -07001287 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1288 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001289 int left;
1290 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001291 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001292 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001293 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001294 if (left <= relativeOffset && relativeOffset <= right) {
1295 return i;
1296 }
Winson Chunge3193b92010-09-10 11:44:42 -07001297 }
1298 return -1;
1299 }
1300
Winson Chung1908d072011-02-24 18:09:44 -08001301 protected void setMinimumWidthOverride(int minimumWidth) {
1302 mMinimumWidth = minimumWidth;
1303 }
Winson Chung34b23d52011-03-18 11:29:34 -07001304 protected void resetMinimumWidthOverride() {
1305 mMinimumWidth = 0;
1306 }
Winson Chung1908d072011-02-24 18:09:44 -08001307
1308 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001309 // This functions are called enough times that it actually makes a difference in the
1310 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001311 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001312 final int minWidth = mMinimumWidth;
1313 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001314 }
1315
Winson Chung321e9ee2010-08-09 13:37:56 -07001316 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001317 int padding = mPaddingLeft + mPaddingRight;
1318 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001319 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001320 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001321 int padding = mPaddingLeft + mPaddingRight;
1322 return mPaddingLeft + (getMeasuredWidth() - padding -
Adam Cohen22f823d2011-09-01 17:22:18 -07001323 getScaledMeasuredWidth(getPageAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001324 }
1325
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 protected int getChildOffset(int index) {
1327 if (getChildCount() == 0)
1328 return 0;
1329
1330 int offset = getRelativeChildOffset(0);
1331 for (int i = 0; i < index; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001332 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001333 }
1334 return offset;
1335 }
1336
Michael Jurkad3ef3062010-11-23 16:23:58 -08001337 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001338 // This functions are called enough times that it actually makes a difference in the
1339 // profiler -- so just inline the max() here
1340 final int measuredWidth = child.getMeasuredWidth();
1341 final int minWidth = mMinimumWidth;
1342 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1343 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001344 }
1345
Adam Cohend19d3ca2010-09-15 14:43:42 -07001346 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001347 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001348 int minDistanceFromScreenCenterIndex = -1;
1349 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1350 final int childCount = getChildCount();
1351 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001352 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001353 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001354 int halfChildWidth = (childWidth / 2);
1355 int childCenter = getChildOffset(i) + halfChildWidth;
1356 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1357 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1358 minDistanceFromScreenCenter = distanceFromScreenCenter;
1359 minDistanceFromScreenCenterIndex = i;
1360 }
1361 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001362 return minDistanceFromScreenCenterIndex;
1363 }
1364
1365 protected void snapToDestination() {
1366 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 }
1368
Adam Cohene0f66b52010-11-23 15:06:07 -08001369 private static class ScrollInterpolator implements Interpolator {
1370 public ScrollInterpolator() {
1371 }
1372
1373 public float getInterpolation(float t) {
1374 t -= 1.0f;
1375 return t*t*t*t*t + 1;
1376 }
1377 }
1378
1379 // We want the duration of the page snap animation to be influenced by the distance that
1380 // the screen has to travel, however, we don't want this duration to be effected in a
1381 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1382 // of travel has on the overall snap duration.
1383 float distanceInfluenceForSnapDuration(float f) {
1384 f -= 0.5f; // center the values about 0.
1385 f *= 0.3f * Math.PI / 2.0f;
1386 return (float) Math.sin(f);
1387 }
1388
Michael Jurka0142d492010-08-25 17:46:15 -07001389 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001390 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1391 int halfScreenSize = getMeasuredWidth() / 2;
1392
Winson Chung785d2eb2011-04-14 16:08:02 -07001393 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1394 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1395 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001396 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1397 int delta = newX - mUnboundedScrollX;
1398 int duration = 0;
1399
1400 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1401 // If the velocity is low enough, then treat this more as an automatic page advance
1402 // as opposed to an apparent physical response to flinging
1403 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1404 return;
1405 }
1406
1407 // Here we compute a "distance" that will be used in the computation of the overall
1408 // snap duration. This is a function of the actual distance that needs to be traveled;
1409 // we keep this value close to half screen size in order to reduce the variance in snap
1410 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001411 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001412 float distance = halfScreenSize + halfScreenSize *
1413 distanceInfluenceForSnapDuration(distanceRatio);
1414
1415 velocity = Math.abs(velocity);
1416 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1417
1418 // we want the page's snap velocity to approximately match the velocity at which the
1419 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001420 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1421 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001422
1423 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001424 }
1425
1426 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001427 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 }
1429
Michael Jurka0142d492010-08-25 17:46:15 -07001430 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001431 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001432
Winson Chung785d2eb2011-04-14 16:08:02 -07001433 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1434 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1435 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001436 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001437 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001439 snapToPage(whichPage, delta, duration);
1440 }
1441
1442 protected void snapToPage(int whichPage, int delta, int duration) {
1443 mNextPage = whichPage;
1444
1445 View focusedChild = getFocusedChild();
1446 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001447 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001448 focusedChild.clearFocus();
1449 }
1450
1451 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001452 awakenScrollBars(duration);
1453 if (duration == 0) {
1454 duration = Math.abs(delta);
1455 }
1456
1457 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001458 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001459
Winson Chungb44b5242011-06-13 11:32:14 -07001460 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1461 // loading associated pages until the scroll settles
1462 if (mDeferScrollUpdate) {
1463 loadAssociatedPages(mNextPage);
1464 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001465 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001466 }
Michael Jurka0142d492010-08-25 17:46:15 -07001467 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 invalidate();
1469 }
1470
Winson Chung321e9ee2010-08-09 13:37:56 -07001471 public void scrollLeft() {
1472 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001473 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001475 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 }
1477 }
1478
1479 public void scrollRight() {
1480 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001481 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001482 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001483 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001484 }
1485 }
1486
Winson Chung86f77532010-08-24 11:08:22 -07001487 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001488 int result = -1;
1489 if (v != null) {
1490 ViewParent vp = v.getParent();
1491 int count = getChildCount();
1492 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001493 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 return i;
1495 }
1496 }
1497 }
1498 return result;
1499 }
1500
1501 /**
1502 * @return True is long presses are still allowed for the current touch
1503 */
1504 public boolean allowLongPress() {
1505 return mAllowLongPress;
1506 }
1507
Michael Jurka0142d492010-08-25 17:46:15 -07001508 /**
1509 * Set true to allow long-press events to be triggered, usually checked by
1510 * {@link Launcher} to accept or block dpad-initiated long-presses.
1511 */
1512 public void setAllowLongPress(boolean allowLongPress) {
1513 mAllowLongPress = allowLongPress;
1514 }
1515
Winson Chung321e9ee2010-08-09 13:37:56 -07001516 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001517 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001518
1519 SavedState(Parcelable superState) {
1520 super(superState);
1521 }
1522
1523 private SavedState(Parcel in) {
1524 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001525 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001526 }
1527
1528 @Override
1529 public void writeToParcel(Parcel out, int flags) {
1530 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001531 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001532 }
1533
1534 public static final Parcelable.Creator<SavedState> CREATOR =
1535 new Parcelable.Creator<SavedState>() {
1536 public SavedState createFromParcel(Parcel in) {
1537 return new SavedState(in);
1538 }
1539
1540 public SavedState[] newArray(int size) {
1541 return new SavedState[size];
1542 }
1543 };
1544 }
1545
Winson Chungf314b0e2011-08-16 11:54:27 -07001546 protected void loadAssociatedPages(int page) {
1547 loadAssociatedPages(page, false);
1548 }
1549 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001550 if (mContentIsRefreshable) {
1551 final int count = getChildCount();
1552 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001553 int lowerPageBound = getAssociatedLowerPageBound(page);
1554 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001555 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1556 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001557 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001558 if ((i != page) && immediateAndOnly) {
1559 continue;
1560 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001561 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001562 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001563 if (lowerPageBound <= i && i <= upperPageBound) {
1564 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001565 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001566 mDirtyPageContent.set(i, false);
1567 }
1568 } else {
1569 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001570 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001571 }
1572 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001573 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001574 }
1575 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001576 }
1577 }
1578
Winson Chunge3193b92010-09-10 11:44:42 -07001579 protected int getAssociatedLowerPageBound(int page) {
1580 return Math.max(0, page - 1);
1581 }
1582 protected int getAssociatedUpperPageBound(int page) {
1583 final int count = getChildCount();
1584 return Math.min(page + 1, count - 1);
1585 }
1586
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001587 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001588 if (isChoiceMode(CHOICE_MODE_NONE)) {
1589 mChoiceMode = mode;
1590 mActionMode = startActionMode(callback);
1591 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001592 }
1593
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001594 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001595 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001596 mChoiceMode = CHOICE_MODE_NONE;
1597 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001598 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001599 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001600 }
1601 }
1602
1603 protected boolean isChoiceMode(int mode) {
1604 return mChoiceMode == mode;
1605 }
1606
1607 protected ArrayList<Checkable> getCheckedGrandchildren() {
1608 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1609 final int childCount = getChildCount();
1610 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001611 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001612 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001613 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001614 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001615 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001616 checked.add((Checkable) v);
1617 }
1618 }
1619 }
1620 return checked;
1621 }
1622
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001623 /**
1624 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1625 * Otherwise, returns null.
1626 */
1627 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001628 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001629 final int childCount = getChildCount();
1630 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001631 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001632 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001633 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001634 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001635 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1636 return (Checkable) v;
1637 }
1638 }
1639 }
1640 }
1641 return null;
1642 }
1643
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001644 protected void resetCheckedGrandchildren() {
1645 // loop through children, and set all of their children to _not_ be checked
1646 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1647 for (int i = 0; i < checked.size(); ++i) {
1648 final Checkable c = checked.get(i);
1649 c.setChecked(false);
1650 }
1651 }
1652
Winson Chung86f77532010-08-24 11:08:22 -07001653 /**
1654 * This method is called ONLY to synchronize the number of pages that the paged view has.
1655 * To actually fill the pages with information, implement syncPageItems() below. It is
1656 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1657 * and therefore, individual page items do not need to be updated in this method.
1658 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001659 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001660
1661 /**
1662 * This method is called to synchronize the items that are on a particular page. If views on
1663 * the page can be reused, then they should be updated within this method.
1664 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001665 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001666
Michael Jurka983e3fd2011-05-26 17:10:29 -07001667 protected void postInvalidatePageData(final boolean clearViews) {
1668 post(new Runnable() {
1669 // post the call to avoid a call to requestLayout from a layout pass
1670 public void run() {
1671 if (clearViews) {
1672 removeAllViews();
1673 }
1674 invalidatePageData();
1675 }
1676 });
1677 }
1678
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001679 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001680 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001681 }
1682 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001683 invalidatePageData(currentPage, false);
1684 }
1685 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001686 if (!mIsDataReady) {
1687 return;
1688 }
1689
Michael Jurka0142d492010-08-25 17:46:15 -07001690 if (mContentIsRefreshable) {
1691 // Update all the pages
1692 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001693
Winson Chung5a808352011-06-27 19:08:49 -07001694 // We must force a measure after we've loaded the pages to update the content width and
1695 // to determine the full scroll width
1696 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1697 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1698
1699 // Set a new page as the current page if necessary
1700 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001701 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001702 }
1703
Michael Jurka0142d492010-08-25 17:46:15 -07001704 // Mark each of the pages as dirty
1705 final int count = getChildCount();
1706 mDirtyPageContent.clear();
1707 for (int i = 0; i < count; ++i) {
1708 mDirtyPageContent.add(true);
1709 }
1710
1711 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001712 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001713 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001714 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001715 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001716 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001717 }
Winson Chung007c6982011-06-14 13:27:53 -07001718
1719 private ImageView getScrollingIndicator() {
1720 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1721 // found
1722 if (mHasScrollIndicator && mScrollIndicator == null) {
1723 ViewGroup parent = (ViewGroup) getParent();
1724 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1725 mHasScrollIndicator = mScrollIndicator != null;
1726 if (mHasScrollIndicator) {
1727 mScrollIndicator.setVisibility(View.VISIBLE);
1728 }
1729 }
1730 return mScrollIndicator;
1731 }
1732
1733 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001734 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001735 }
1736
Winson Chung5a808352011-06-27 19:08:49 -07001737 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1738 @Override
1739 public void run() {
1740 hideScrollingIndicator(false);
1741 }
1742 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001743 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001744 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001745 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001746 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001747 }
1748
Michael Jurka430e8a52011-08-08 15:52:14 -07001749 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001750 if (getChildCount() <= 1) return;
1751 if (!isScrollingIndicatorEnabled()) return;
1752
1753 getScrollingIndicator();
1754 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001755 // Fade the indicator in
1756 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001757 mScrollIndicator.setVisibility(View.VISIBLE);
1758 if (mScrollIndicatorAnimator != null) {
1759 mScrollIndicatorAnimator.cancel();
1760 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001761 if (immediately) {
1762 mScrollIndicator.setAlpha(1f);
1763 } else {
1764 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1765 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1766 mScrollIndicatorAnimator.start();
1767 }
Winson Chung007c6982011-06-14 13:27:53 -07001768 }
1769 }
1770
1771 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001772 if (getChildCount() <= 1) return;
1773 if (!isScrollingIndicatorEnabled()) return;
1774
1775 getScrollingIndicator();
1776 if (mScrollIndicator != null) {
1777 // Fade the indicator out
1778 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001779 if (mScrollIndicatorAnimator != null) {
1780 mScrollIndicatorAnimator.cancel();
1781 }
1782 if (immediately) {
1783 mScrollIndicator.setVisibility(View.GONE);
1784 mScrollIndicator.setAlpha(0f);
1785 } else {
1786 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1787 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1788 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1789 private boolean cancelled = false;
1790 @Override
1791 public void onAnimationCancel(android.animation.Animator animation) {
1792 cancelled = true;
1793 }
1794 @Override
1795 public void onAnimationEnd(Animator animation) {
1796 if (!cancelled) {
1797 mScrollIndicator.setVisibility(View.GONE);
1798 }
1799 }
1800 });
1801 mScrollIndicatorAnimator.start();
1802 }
Winson Chung007c6982011-06-14 13:27:53 -07001803 }
1804 }
1805
Winson Chung32174c82011-07-19 15:47:55 -07001806 /**
1807 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1808 * fill its space on the track or not.
1809 */
1810 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001811 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001812 }
1813
Winson Chung007c6982011-06-14 13:27:53 -07001814 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001815 if (getChildCount() <= 1) return;
1816 if (!isScrollingIndicatorEnabled()) return;
1817
1818 getScrollingIndicator();
1819 if (mScrollIndicator != null) {
1820 updateScrollingIndicatorPosition();
1821 }
1822 }
1823
Winson Chung007c6982011-06-14 13:27:53 -07001824 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001825 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001826 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001827 int numPages = getChildCount();
1828 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001829 int lastChildIndex = Math.max(0, getChildCount() - 1);
1830 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001831 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001832 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1833 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001834
Winson Chungae890b82011-09-13 18:08:54 -07001835 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001836 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001837 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001838 if (hasElasticScrollIndicator()) {
1839 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1840 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1841 mScrollIndicator.requestLayout();
1842 }
1843 } else {
1844 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1845 indicatorPos += indicatorCenterOffset;
1846 }
Winson Chung007c6982011-06-14 13:27:53 -07001847 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001848 mScrollIndicator.invalidate();
1849 }
1850
Winson Chung3ac74c52011-06-30 17:39:37 -07001851 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001852 }
1853
1854 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001855 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001856
1857 /* Accessibility */
1858 @Override
1859 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1860 super.onInitializeAccessibilityNodeInfo(info);
1861 info.setScrollable(true);
1862 }
1863
1864 @Override
1865 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1866 super.onInitializeAccessibilityEvent(event);
1867 event.setScrollable(true);
1868 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1869 event.setFromIndex(mCurrentPage);
1870 event.setToIndex(mCurrentPage);
1871 event.setItemCount(getChildCount());
1872 }
1873 }
1874
1875 @Override
1876 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1877 // Do not append text content to scroll events they are fired frequently
1878 // and the client has already received another event type with the text.
1879 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1880 super.dispatchPopulateAccessibilityEvent(event);
1881 }
1882
1883 onPopulateAccessibilityEvent(event);
1884 return false;
1885 }
1886
1887 @Override
1888 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1889 super.onPopulateAccessibilityEvent(event);
1890
1891 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1892 event.getText().add(getCurrentPageDescription());
1893 }
1894 }
1895
1896 protected String getCurrentPageDescription() {
1897 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1898 return String.format(mContext.getString(R.string.default_scroll_format),
1899 page + 1, getChildCount());
1900 }
Winson Chungd11265e2011-08-30 13:37:23 -07001901
1902 @Override
1903 public boolean onHoverEvent(android.view.MotionEvent event) {
1904 return true;
1905 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001906}