blob: 2b5847bde8601cc252bb94b84b3ce8b6a5b50f7e [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(
207 R.styleable.PagedView_pageLayoutWidthGap, -1);
208 mPageLayoutHeightGap = a.getDimensionPixelSize(
209 R.styleable.PagedView_pageLayoutHeightGap, -1);
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 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800478 if (childCount > 0) {
479 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
480 } else {
481 mMaxScrollX = 0;
482 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700483
Michael Jurka430e8a52011-08-08 15:52:14 -0700484 updateScrollingIndicatorPosition();
485
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 }
488
Michael Jurka8c920dd2011-01-20 14:16:56 -0800489 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800490 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
491 int delta = newX - mScrollX;
492
Michael Jurka8c920dd2011-01-20 14:16:56 -0800493 final int pageCount = getChildCount();
494 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700495 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800496 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800497 }
498 setCurrentPage(newCurrentPage);
499 }
500
Michael Jurka8c920dd2011-01-20 14:16:56 -0800501 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
502 // 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 -0800503 // tightens the layout accordingly
504 public void setLayoutScale(float childrenScale) {
505 mLayoutScale = childrenScale;
506
507 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
508 int childCount = getChildCount();
509 float childrenX[] = new float[childCount];
510 float childrenY[] = new float[childCount];
511 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700512 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800513 childrenX[i] = child.getX();
514 childrenY[i] = child.getY();
515 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700516 // Trigger a full re-layout (never just call onLayout directly!)
517 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
518 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
519 requestLayout();
520 measure(widthSpec, heightSpec);
521 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800522 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700523 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 child.setX(childrenX[i]);
525 child.setY(childrenY[i]);
526 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700527
Michael Jurkad3ef3062010-11-23 16:23:58 -0800528 // Also, the page offset has changed (since the pages are now smaller);
529 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800530 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800531 }
532
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700534 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700535 if (!mIsDataReady) {
536 return;
537 }
538
Winson Chung785d2eb2011-04-14 16:08:02 -0700539 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700540 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
541 setHorizontalScrollBarEnabled(false);
542 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
543 scrollTo(newX, 0);
544 mScroller.setFinalX(newX);
545 setHorizontalScrollBarEnabled(true);
546 mFirstLayout = false;
547 }
548
Adam Lesinski6b879f02010-11-04 16:15:23 -0700549 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 final int childCount = getChildCount();
551 int childLeft = 0;
552 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700553 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
554 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700555 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 }
557
558 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700559 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700560 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800561 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700562 final int childHeight = child.getMeasuredHeight();
563 int childTop = mPaddingTop;
564 if (mCenterPagesVertically) {
565 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
566 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800567
Winson Chung785d2eb2011-04-14 16:08:02 -0700568 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700569 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800570 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700571 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 }
573 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800574 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
575 mFirstLayout = false;
576 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 }
578
Winson Chung03929772011-02-23 17:07:10 -0800579 protected void forceUpdateAdjacentPagesAlpha() {
580 mDirtyPageAlpha = true;
581 updateAdjacentPagesAlpha();
582 }
583
Winson Chunge3193b92010-09-10 11:44:42 -0700584 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700585 if (mFadeInAdjacentScreens) {
586 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700587 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700588 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700589 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700590 final int childCount = getChildCount();
591 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700592 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800593 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700594 int halfChildWidth = (childWidth / 2);
595 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700596
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700597 // On the first layout, we may not have a width nor a proper offset, so for now
598 // we should just assume full page width (and calculate the offset according to
599 // that).
600 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700601 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700602 childCenter = (i * childWidth) + (childWidth / 2);
603 }
604
Winson Chunge8878e32010-09-15 20:37:09 -0700605 int d = halfChildWidth;
606 int distanceFromScreenCenter = childCenter - screenCenter;
607 if (distanceFromScreenCenter > 0) {
608 if (i > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700609 d += getScaledMeasuredWidth(getPageAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700610 } else {
611 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700612 }
Michael Jurka0142d492010-08-25 17:46:15 -0700613 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700614 if (i < childCount - 1) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700615 d += getScaledMeasuredWidth(getPageAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700616 } else {
617 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700618 }
Michael Jurka0142d492010-08-25 17:46:15 -0700619 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700620 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700621
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700622 // Preventing potential divide-by-zero
623 d = Math.max(1, d);
624
Winson Chunge8878e32010-09-15 20:37:09 -0700625 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
626 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
627 float alpha = 1.0f - dimAlpha;
628
Adam Cohenf34bab52010-09-30 14:11:56 -0700629 if (alpha < ALPHA_QUANTIZE_LEVEL) {
630 alpha = 0.0f;
631 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
632 alpha = 1.0f;
633 }
634
Michael Jurkaa40829a2011-02-16 18:02:45 -0800635 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
636 // this optimization causes alpha to not be properly updated sometimes (repro
637 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
638 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
639 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800640 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700641 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800642 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 }
Michael Jurka0142d492010-08-25 17:46:15 -0700644 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 }
646 }
Winson Chunge3193b92010-09-10 11:44:42 -0700647 }
648
Adam Cohenf34bab52010-09-30 14:11:56 -0700649 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700650 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700651 }
652
Winson Chunge3193b92010-09-10 11:44:42 -0700653 @Override
654 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700655 int halfScreenSize = getMeasuredWidth() / 2;
656 int screenCenter = mScrollX + halfScreenSize;
657
658 if (screenCenter != mLastScreenCenter) {
659 screenScrolled(screenCenter);
660 updateAdjacentPagesAlpha();
661 mLastScreenCenter = screenCenter;
662 }
Michael Jurka0142d492010-08-25 17:46:15 -0700663
664 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700665 // As an optimization, this code assumes that all pages have the same width as the 0th
666 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700667 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700668 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700669 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700670 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700672 int leftScreen = 0;
673 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700674 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700675 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700676 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700677 }
678 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700679 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700680 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700681 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700682 }
683 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700684
Michael Jurkac4fb9172010-09-02 17:19:20 -0700685 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800686 // Clip to the bounds
687 canvas.save();
688 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
689 mScrollY + mBottom - mTop);
690
Adam Cohen22f823d2011-09-01 17:22:18 -0700691 for (int i = rightScreen; i >= leftScreen; i--) {
692 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700693 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800694 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700695 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700696 }
697
698 @Override
699 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700700 int page = indexOfChild(child);
701 if (page != mCurrentPage || !mScroller.isFinished()) {
702 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 return true;
704 }
705 return false;
706 }
707
708 @Override
709 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700710 int focusablePage;
711 if (mNextPage != INVALID_PAGE) {
712 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700714 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700715 }
Winson Chung86f77532010-08-24 11:08:22 -0700716 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700718 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700719 }
720 return false;
721 }
722
723 @Override
724 public boolean dispatchUnhandledMove(View focused, int direction) {
725 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700726 if (getCurrentPage() > 0) {
727 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700728 return true;
729 }
730 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700731 if (getCurrentPage() < getPageCount() - 1) {
732 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700733 return true;
734 }
735 }
736 return super.dispatchUnhandledMove(focused, direction);
737 }
738
739 @Override
740 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700741 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
742 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 }
744 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700745 if (mCurrentPage > 0) {
746 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 }
748 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700749 if (mCurrentPage < getPageCount() - 1) {
750 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752 }
753 }
754
755 /**
756 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700757 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 *
Winson Chung86f77532010-08-24 11:08:22 -0700759 * This happens when live folders requery, and if they're off page, they
760 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700761 */
762 @Override
763 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700764 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 View v = focused;
766 while (true) {
767 if (v == current) {
768 super.focusableViewAvailable(focused);
769 return;
770 }
771 if (v == this) {
772 return;
773 }
774 ViewParent parent = v.getParent();
775 if (parent instanceof View) {
776 v = (View)v.getParent();
777 } else {
778 return;
779 }
780 }
781 }
782
783 /**
784 * {@inheritDoc}
785 */
786 @Override
787 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
788 if (disallowIntercept) {
789 // We need to make sure to cancel our long press if
790 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700791 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700792 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 }
794 super.requestDisallowInterceptTouchEvent(disallowIntercept);
795 }
796
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800797 /**
798 * Return true if a tap at (x, y) should trigger a flip to the previous page.
799 */
800 protected boolean hitsPreviousPage(float x, float y) {
801 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
802 }
803
804 /**
805 * Return true if a tap at (x, y) should trigger a flip to the next page.
806 */
807 protected boolean hitsNextPage(float x, float y) {
808 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
809 }
810
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 @Override
812 public boolean onInterceptTouchEvent(MotionEvent ev) {
813 /*
814 * This method JUST determines whether we want to intercept the motion.
815 * If we return true, onTouchEvent will be called and we do the actual
816 * scrolling there.
817 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800818 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819
Winson Chung45e1d6e2010-11-09 17:19:49 -0800820 // Skip touch handling if there are no pages to swipe
821 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
822
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 /*
824 * Shortcut the most recurring case: the user is in the dragging
825 * state and he is moving his finger. We want to intercept this
826 * motion.
827 */
828 final int action = ev.getAction();
829 if ((action == MotionEvent.ACTION_MOVE) &&
830 (mTouchState == TOUCH_STATE_SCROLLING)) {
831 return true;
832 }
833
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 switch (action & MotionEvent.ACTION_MASK) {
835 case MotionEvent.ACTION_MOVE: {
836 /*
837 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
838 * whether the user has moved far enough from his original down touch.
839 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700840 if (mActivePointerId != INVALID_POINTER) {
841 determineScrollingStart(ev);
842 break;
843 }
844 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
845 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
846 // i.e. fall through to the next case (don't break)
847 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
848 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 }
850
851 case MotionEvent.ACTION_DOWN: {
852 final float x = ev.getX();
853 final float y = ev.getY();
854 // Remember location of down touch
855 mDownMotionX = x;
856 mLastMotionX = x;
857 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800858 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800859 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 mActivePointerId = ev.getPointerId(0);
861 mAllowLongPress = true;
862
863 /*
864 * If being flinged and user touches the screen, initiate drag;
865 * otherwise don't. mScroller.isFinished should be false when
866 * being flinged.
867 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700868 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700869 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
870 if (finishedScrolling) {
871 mTouchState = TOUCH_STATE_REST;
872 mScroller.abortAnimation();
873 } else {
874 mTouchState = TOUCH_STATE_SCROLLING;
875 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700876
Winson Chung86f77532010-08-24 11:08:22 -0700877 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800879 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800881 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800883 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 mTouchState = TOUCH_STATE_NEXT_PAGE;
885 }
886 }
887 }
888 break;
889 }
890
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800892 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700893 mTouchState = TOUCH_STATE_REST;
894 mAllowLongPress = false;
895 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800896 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 break;
898
899 case MotionEvent.ACTION_POINTER_UP:
900 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800901 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 break;
903 }
904
905 /*
906 * The only time we want to intercept motion events is if we are in the
907 * drag mode.
908 */
909 return mTouchState != TOUCH_STATE_REST;
910 }
911
Winson Chung80baf5a2010-08-09 16:03:15 -0700912 protected void animateClickFeedback(View v, final Runnable r) {
913 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800914 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
915 loadAnimator(mContext, R.anim.paged_view_click_feedback);
916 anim.setTarget(v);
917 anim.addListener(new AnimatorListenerAdapter() {
918 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700919 r.run();
920 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700921 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800922 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700923 }
924
Adam Cohenf8d28232011-02-01 21:47:00 -0800925 protected void determineScrollingStart(MotionEvent ev) {
926 determineScrollingStart(ev, 1.0f);
927 }
928
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 /*
930 * Determines if we should change the touch state to start scrolling after the
931 * user moves their touch point too far.
932 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800933 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 /*
935 * Locally do absolute value. mLastMotionX is set to the y value
936 * of the down event.
937 */
938 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700939 if (pointerIndex == -1) {
940 return;
941 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 final float x = ev.getX(pointerIndex);
943 final float y = ev.getY(pointerIndex);
944 final int xDiff = (int) Math.abs(x - mLastMotionX);
945 final int yDiff = (int) Math.abs(y - mLastMotionY);
946
Adam Cohenf8d28232011-02-01 21:47:00 -0800947 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 boolean xPaged = xDiff > mPagingTouchSlop;
949 boolean xMoved = xDiff > touchSlop;
950 boolean yMoved = yDiff > touchSlop;
951
Adam Cohenf8d28232011-02-01 21:47:00 -0800952 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700953 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 // Scroll if the user moved far enough along the X axis
955 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800956 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800958 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700959 mTouchX = mScrollX;
960 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
961 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 }
963 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800964 cancelCurrentPageLongPress();
965 }
966 }
967
968 protected void cancelCurrentPageLongPress() {
969 if (mAllowLongPress) {
970 mAllowLongPress = false;
971 // Try canceling the long press. It could also have been scheduled
972 // by a distant descendant, so use the mAllowLongPress flag to block
973 // everything
974 final View currentPage = getPageAt(mCurrentPage);
975 if (currentPage != null) {
976 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 }
978 }
979 }
980
Adam Cohenb5ba0972011-09-07 18:02:31 -0700981 protected float getScrollProgress(int screenCenter, View v, int page) {
982 final int halfScreenSize = getMeasuredWidth() / 2;
983
984 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
985 int delta = screenCenter - (getChildOffset(page) -
986 getRelativeChildOffset(page) + halfScreenSize);
987
988 float scrollProgress = delta / (totalDistance * 1.0f);
989 scrollProgress = Math.min(scrollProgress, 1.0f);
990 scrollProgress = Math.max(scrollProgress, -1.0f);
991 return scrollProgress;
992 }
993
Adam Cohene0f66b52010-11-23 15:06:07 -0800994 // This curve determines how the effect of scrolling over the limits of the page dimishes
995 // as the user pulls further and further from the bounds
996 private float overScrollInfluenceCurve(float f) {
997 f -= 1.0f;
998 return f * f * f + 1.0f;
999 }
1000
Adam Cohenb5ba0972011-09-07 18:02:31 -07001001 protected void acceleratedOverScroll(float amount) {
1002 int screenSize = getMeasuredWidth();
1003
1004 // We want to reach the max over scroll effect when the user has
1005 // over scrolled half the size of the screen
1006 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1007
1008 if (f == 0) return;
1009
1010 // Clamp this factor, f, to -1 < f < 1
1011 if (Math.abs(f) >= 1) {
1012 f /= Math.abs(f);
1013 }
1014
1015 int overScrollAmount = (int) Math.round(f * screenSize);
1016 if (amount < 0) {
1017 mScrollX = overScrollAmount;
1018 } else {
1019 mScrollX = mMaxScrollX + overScrollAmount;
1020 }
1021 invalidate();
1022 }
1023
1024 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001025 int screenSize = getMeasuredWidth();
1026
1027 float f = (amount / screenSize);
1028
1029 if (f == 0) return;
1030 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1031
Adam Cohen7bfc9792011-01-28 13:52:37 -08001032 // Clamp this factor, f, to -1 < f < 1
1033 if (Math.abs(f) >= 1) {
1034 f /= Math.abs(f);
1035 }
1036
Adam Cohene0f66b52010-11-23 15:06:07 -08001037 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001038 if (amount < 0) {
1039 mScrollX = overScrollAmount;
1040 } else {
1041 mScrollX = mMaxScrollX + overScrollAmount;
1042 }
1043 invalidate();
1044 }
1045
Adam Cohenb5ba0972011-09-07 18:02:31 -07001046 protected void overScroll(float amount) {
1047 dampedOverScroll(amount);
1048 }
1049
Michael Jurkac5b262c2011-01-12 20:24:50 -08001050 protected float maxOverScroll() {
1051 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001052 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001053 float f = 1.0f;
1054 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1055 return OVERSCROLL_DAMP_FACTOR * f;
1056 }
1057
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 @Override
1059 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001060 // Skip touch handling if there are no pages to swipe
1061 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1062
Michael Jurkab8f06722010-10-10 15:58:46 -07001063 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001064
1065 final int action = ev.getAction();
1066
1067 switch (action & MotionEvent.ACTION_MASK) {
1068 case MotionEvent.ACTION_DOWN:
1069 /*
1070 * If being flinged and user touches, stop the fling. isFinished
1071 * will be false if being flinged.
1072 */
1073 if (!mScroller.isFinished()) {
1074 mScroller.abortAnimation();
1075 }
1076
1077 // Remember where the motion event started
1078 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001079 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001080 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001082 if (mTouchState == TOUCH_STATE_SCROLLING) {
1083 pageBeginMoving();
1084 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001085 break;
1086
1087 case MotionEvent.ACTION_MOVE:
1088 if (mTouchState == TOUCH_STATE_SCROLLING) {
1089 // Scroll to follow the motion event
1090 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1091 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001092 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001093
Adam Cohenaefd4e12011-02-14 16:39:38 -08001094 mTotalMotionX += Math.abs(deltaX);
1095
Winson Chungc0844aa2011-02-02 15:25:58 -08001096 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1097 // keep the remainder because we are actually testing if we've moved from the last
1098 // scrolled position (which is discrete).
1099 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001100 mTouchX += deltaX;
1101 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1102 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001103 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001104 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001105 } else {
1106 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001108 mLastMotionX = x;
1109 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001110 } else {
1111 awakenScrollBars();
1112 }
Adam Cohen564976a2010-10-13 18:52:07 -07001113 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001114 determineScrollingStart(ev);
1115 }
1116 break;
1117
1118 case MotionEvent.ACTION_UP:
1119 if (mTouchState == TOUCH_STATE_SCROLLING) {
1120 final int activePointerId = mActivePointerId;
1121 final int pointerIndex = ev.findPointerIndex(activePointerId);
1122 final float x = ev.getX(pointerIndex);
1123 final VelocityTracker velocityTracker = mVelocityTracker;
1124 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1125 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001126 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001127 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001128 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001129
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001130 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1131
Adam Cohenaefd4e12011-02-14 16:39:38 -08001132 // In the case that the page is moved far to one direction and then is flung
1133 // in the opposite direction, we use a threshold to determine whether we should
1134 // just return to the starting page, or if we should skip one further.
1135 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001136 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001137 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001138 Math.signum(velocityX) != Math.signum(deltaX)) {
1139 returnToOriginalPage = true;
1140 }
1141
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001142 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001143 Math.abs(velocityX) > snapVelocity;
1144
1145 int finalPage;
1146 // We give flings precedence over large moves, which is why we short-circuit our
1147 // test for a large move if a fling has been registered. That is, a large
1148 // move to the left and fling to the right will register as a fling to the right.
1149 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1150 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1151 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1152 snapToPageWithVelocity(finalPage, velocityX);
1153 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1154 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001155 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001156 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1157 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001158 } else {
1159 snapToDestination();
1160 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001161 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001162 // at this point we have not moved beyond the touch slop
1163 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1164 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001165 int nextPage = Math.max(0, mCurrentPage - 1);
1166 if (nextPage != mCurrentPage) {
1167 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 } else {
1169 snapToDestination();
1170 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001171 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 // at this point we have not moved beyond the touch slop
1173 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1174 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001175 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1176 if (nextPage != mCurrentPage) {
1177 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 } else {
1179 snapToDestination();
1180 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001181 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001182 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 }
1184 mTouchState = TOUCH_STATE_REST;
1185 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001186 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 break;
1188
1189 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001190 if (mTouchState == TOUCH_STATE_SCROLLING) {
1191 snapToDestination();
1192 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 mTouchState = TOUCH_STATE_REST;
1194 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001195 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001196 break;
1197
1198 case MotionEvent.ACTION_POINTER_UP:
1199 onSecondaryPointerUp(ev);
1200 break;
1201 }
1202
1203 return true;
1204 }
1205
Winson Chung185d7162011-02-28 13:47:29 -08001206 @Override
1207 public boolean onGenericMotionEvent(MotionEvent event) {
1208 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1209 switch (event.getAction()) {
1210 case MotionEvent.ACTION_SCROLL: {
1211 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1212 final float vscroll;
1213 final float hscroll;
1214 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1215 vscroll = 0;
1216 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1217 } else {
1218 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1219 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1220 }
1221 if (hscroll != 0 || vscroll != 0) {
1222 if (hscroll > 0 || vscroll > 0) {
1223 scrollRight();
1224 } else {
1225 scrollLeft();
1226 }
1227 return true;
1228 }
1229 }
1230 }
1231 }
1232 return super.onGenericMotionEvent(event);
1233 }
1234
Michael Jurkab8f06722010-10-10 15:58:46 -07001235 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1236 if (mVelocityTracker == null) {
1237 mVelocityTracker = VelocityTracker.obtain();
1238 }
1239 mVelocityTracker.addMovement(ev);
1240 }
1241
1242 private void releaseVelocityTracker() {
1243 if (mVelocityTracker != null) {
1244 mVelocityTracker.recycle();
1245 mVelocityTracker = null;
1246 }
1247 }
1248
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 private void onSecondaryPointerUp(MotionEvent ev) {
1250 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1251 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1252 final int pointerId = ev.getPointerId(pointerIndex);
1253 if (pointerId == mActivePointerId) {
1254 // This was our active pointer going up. Choose a new
1255 // active pointer and adjust accordingly.
1256 // TODO: Make this decision more intelligent.
1257 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1258 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1259 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001260 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 mActivePointerId = ev.getPointerId(newPointerIndex);
1262 if (mVelocityTracker != null) {
1263 mVelocityTracker.clear();
1264 }
1265 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001266 }
1267
Michael Jurkad771c962011-08-09 15:00:48 -07001268 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001269
1270 @Override
1271 public void requestChildFocus(View child, View focused) {
1272 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001273 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001274 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001275 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 }
1277 }
1278
Winson Chunge3193b92010-09-10 11:44:42 -07001279 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1280 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001281 int left;
1282 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001283 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001284 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001285 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001286 if (left <= relativeOffset && relativeOffset <= right) {
1287 return i;
1288 }
Winson Chunge3193b92010-09-10 11:44:42 -07001289 }
1290 return -1;
1291 }
1292
Winson Chung1908d072011-02-24 18:09:44 -08001293 protected void setMinimumWidthOverride(int minimumWidth) {
1294 mMinimumWidth = minimumWidth;
1295 }
Winson Chung34b23d52011-03-18 11:29:34 -07001296 protected void resetMinimumWidthOverride() {
1297 mMinimumWidth = 0;
1298 }
Winson Chung1908d072011-02-24 18:09:44 -08001299
1300 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001301 // This functions are called enough times that it actually makes a difference in the
1302 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001303 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001304 final int minWidth = mMinimumWidth;
1305 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001306 }
1307
Winson Chung321e9ee2010-08-09 13:37:56 -07001308 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001309 int padding = mPaddingLeft + mPaddingRight;
1310 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001312 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001313 int padding = mPaddingLeft + mPaddingRight;
1314 return mPaddingLeft + (getMeasuredWidth() - padding -
Adam Cohen22f823d2011-09-01 17:22:18 -07001315 getScaledMeasuredWidth(getPageAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001316 }
1317
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 protected int getChildOffset(int index) {
1319 if (getChildCount() == 0)
1320 return 0;
1321
1322 int offset = getRelativeChildOffset(0);
1323 for (int i = 0; i < index; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001324 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 }
1326 return offset;
1327 }
1328
Michael Jurkad3ef3062010-11-23 16:23:58 -08001329 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001330 // This functions are called enough times that it actually makes a difference in the
1331 // profiler -- so just inline the max() here
1332 final int measuredWidth = child.getMeasuredWidth();
1333 final int minWidth = mMinimumWidth;
1334 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1335 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001336 }
1337
Adam Cohend19d3ca2010-09-15 14:43:42 -07001338 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001339 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 int minDistanceFromScreenCenterIndex = -1;
1341 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1342 final int childCount = getChildCount();
1343 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001344 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001345 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001346 int halfChildWidth = (childWidth / 2);
1347 int childCenter = getChildOffset(i) + halfChildWidth;
1348 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1349 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1350 minDistanceFromScreenCenter = distanceFromScreenCenter;
1351 minDistanceFromScreenCenterIndex = i;
1352 }
1353 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001354 return minDistanceFromScreenCenterIndex;
1355 }
1356
1357 protected void snapToDestination() {
1358 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 }
1360
Adam Cohene0f66b52010-11-23 15:06:07 -08001361 private static class ScrollInterpolator implements Interpolator {
1362 public ScrollInterpolator() {
1363 }
1364
1365 public float getInterpolation(float t) {
1366 t -= 1.0f;
1367 return t*t*t*t*t + 1;
1368 }
1369 }
1370
1371 // We want the duration of the page snap animation to be influenced by the distance that
1372 // the screen has to travel, however, we don't want this duration to be effected in a
1373 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1374 // of travel has on the overall snap duration.
1375 float distanceInfluenceForSnapDuration(float f) {
1376 f -= 0.5f; // center the values about 0.
1377 f *= 0.3f * Math.PI / 2.0f;
1378 return (float) Math.sin(f);
1379 }
1380
Michael Jurka0142d492010-08-25 17:46:15 -07001381 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001382 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1383 int halfScreenSize = getMeasuredWidth() / 2;
1384
Winson Chung785d2eb2011-04-14 16:08:02 -07001385 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1386 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1387 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001388 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1389 int delta = newX - mUnboundedScrollX;
1390 int duration = 0;
1391
1392 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1393 // If the velocity is low enough, then treat this more as an automatic page advance
1394 // as opposed to an apparent physical response to flinging
1395 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1396 return;
1397 }
1398
1399 // Here we compute a "distance" that will be used in the computation of the overall
1400 // snap duration. This is a function of the actual distance that needs to be traveled;
1401 // we keep this value close to half screen size in order to reduce the variance in snap
1402 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001403 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001404 float distance = halfScreenSize + halfScreenSize *
1405 distanceInfluenceForSnapDuration(distanceRatio);
1406
1407 velocity = Math.abs(velocity);
1408 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1409
1410 // we want the page's snap velocity to approximately match the velocity at which the
1411 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001412 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1413 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001414
1415 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001416 }
1417
1418 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001419 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 }
1421
Michael Jurka0142d492010-08-25 17:46:15 -07001422 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001423 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001424
Winson Chung785d2eb2011-04-14 16:08:02 -07001425 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1426 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1427 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001428 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001429 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001431 snapToPage(whichPage, delta, duration);
1432 }
1433
1434 protected void snapToPage(int whichPage, int delta, int duration) {
1435 mNextPage = whichPage;
1436
1437 View focusedChild = getFocusedChild();
1438 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001439 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001440 focusedChild.clearFocus();
1441 }
1442
1443 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 awakenScrollBars(duration);
1445 if (duration == 0) {
1446 duration = Math.abs(delta);
1447 }
1448
1449 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001450 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001451
Winson Chungb44b5242011-06-13 11:32:14 -07001452 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1453 // loading associated pages until the scroll settles
1454 if (mDeferScrollUpdate) {
1455 loadAssociatedPages(mNextPage);
1456 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001457 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001458 }
Michael Jurka0142d492010-08-25 17:46:15 -07001459 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 invalidate();
1461 }
1462
Winson Chung321e9ee2010-08-09 13:37:56 -07001463 public void scrollLeft() {
1464 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001465 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001467 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001468 }
1469 }
1470
1471 public void scrollRight() {
1472 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001473 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001475 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 }
1477 }
1478
Winson Chung86f77532010-08-24 11:08:22 -07001479 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001480 int result = -1;
1481 if (v != null) {
1482 ViewParent vp = v.getParent();
1483 int count = getChildCount();
1484 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001485 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 return i;
1487 }
1488 }
1489 }
1490 return result;
1491 }
1492
1493 /**
1494 * @return True is long presses are still allowed for the current touch
1495 */
1496 public boolean allowLongPress() {
1497 return mAllowLongPress;
1498 }
1499
Michael Jurka0142d492010-08-25 17:46:15 -07001500 /**
1501 * Set true to allow long-press events to be triggered, usually checked by
1502 * {@link Launcher} to accept or block dpad-initiated long-presses.
1503 */
1504 public void setAllowLongPress(boolean allowLongPress) {
1505 mAllowLongPress = allowLongPress;
1506 }
1507
Winson Chung321e9ee2010-08-09 13:37:56 -07001508 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001509 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001510
1511 SavedState(Parcelable superState) {
1512 super(superState);
1513 }
1514
1515 private SavedState(Parcel in) {
1516 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001517 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001518 }
1519
1520 @Override
1521 public void writeToParcel(Parcel out, int flags) {
1522 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001523 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001524 }
1525
1526 public static final Parcelable.Creator<SavedState> CREATOR =
1527 new Parcelable.Creator<SavedState>() {
1528 public SavedState createFromParcel(Parcel in) {
1529 return new SavedState(in);
1530 }
1531
1532 public SavedState[] newArray(int size) {
1533 return new SavedState[size];
1534 }
1535 };
1536 }
1537
Winson Chungf314b0e2011-08-16 11:54:27 -07001538 protected void loadAssociatedPages(int page) {
1539 loadAssociatedPages(page, false);
1540 }
1541 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001542 if (mContentIsRefreshable) {
1543 final int count = getChildCount();
1544 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001545 int lowerPageBound = getAssociatedLowerPageBound(page);
1546 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001547 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1548 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001549 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001550 if ((i != page) && immediateAndOnly) {
1551 continue;
1552 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001553 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001554 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001555 if (lowerPageBound <= i && i <= upperPageBound) {
1556 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001557 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001558 mDirtyPageContent.set(i, false);
1559 }
1560 } else {
1561 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001562 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001563 }
1564 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001565 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001566 }
1567 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001568 }
1569 }
1570
Winson Chunge3193b92010-09-10 11:44:42 -07001571 protected int getAssociatedLowerPageBound(int page) {
1572 return Math.max(0, page - 1);
1573 }
1574 protected int getAssociatedUpperPageBound(int page) {
1575 final int count = getChildCount();
1576 return Math.min(page + 1, count - 1);
1577 }
1578
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001579 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001580 if (isChoiceMode(CHOICE_MODE_NONE)) {
1581 mChoiceMode = mode;
1582 mActionMode = startActionMode(callback);
1583 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001584 }
1585
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001586 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001587 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001588 mChoiceMode = CHOICE_MODE_NONE;
1589 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001590 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001591 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001592 }
1593 }
1594
1595 protected boolean isChoiceMode(int mode) {
1596 return mChoiceMode == mode;
1597 }
1598
1599 protected ArrayList<Checkable> getCheckedGrandchildren() {
1600 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1601 final int childCount = getChildCount();
1602 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001603 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001604 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001605 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001606 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001607 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001608 checked.add((Checkable) v);
1609 }
1610 }
1611 }
1612 return checked;
1613 }
1614
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001615 /**
1616 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1617 * Otherwise, returns null.
1618 */
1619 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001620 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001621 final int childCount = getChildCount();
1622 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001623 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001624 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001625 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001626 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001627 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1628 return (Checkable) v;
1629 }
1630 }
1631 }
1632 }
1633 return null;
1634 }
1635
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001636 protected void resetCheckedGrandchildren() {
1637 // loop through children, and set all of their children to _not_ be checked
1638 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1639 for (int i = 0; i < checked.size(); ++i) {
1640 final Checkable c = checked.get(i);
1641 c.setChecked(false);
1642 }
1643 }
1644
Winson Chung86f77532010-08-24 11:08:22 -07001645 /**
1646 * This method is called ONLY to synchronize the number of pages that the paged view has.
1647 * To actually fill the pages with information, implement syncPageItems() below. It is
1648 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1649 * and therefore, individual page items do not need to be updated in this method.
1650 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001651 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001652
1653 /**
1654 * This method is called to synchronize the items that are on a particular page. If views on
1655 * the page can be reused, then they should be updated within this method.
1656 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001657 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001658
Michael Jurka983e3fd2011-05-26 17:10:29 -07001659 protected void postInvalidatePageData(final boolean clearViews) {
1660 post(new Runnable() {
1661 // post the call to avoid a call to requestLayout from a layout pass
1662 public void run() {
1663 if (clearViews) {
1664 removeAllViews();
1665 }
1666 invalidatePageData();
1667 }
1668 });
1669 }
1670
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001671 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001672 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001673 }
1674 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001675 invalidatePageData(currentPage, false);
1676 }
1677 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001678 if (!mIsDataReady) {
1679 return;
1680 }
1681
Michael Jurka0142d492010-08-25 17:46:15 -07001682 if (mContentIsRefreshable) {
1683 // Update all the pages
1684 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001685
Winson Chung5a808352011-06-27 19:08:49 -07001686 // We must force a measure after we've loaded the pages to update the content width and
1687 // to determine the full scroll width
1688 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1689 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1690
1691 // Set a new page as the current page if necessary
1692 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001693 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001694 }
1695
Michael Jurka0142d492010-08-25 17:46:15 -07001696 // Mark each of the pages as dirty
1697 final int count = getChildCount();
1698 mDirtyPageContent.clear();
1699 for (int i = 0; i < count; ++i) {
1700 mDirtyPageContent.add(true);
1701 }
1702
1703 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001704 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001705 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001706 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001707 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001708 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001709 }
Winson Chung007c6982011-06-14 13:27:53 -07001710
1711 private ImageView getScrollingIndicator() {
1712 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1713 // found
1714 if (mHasScrollIndicator && mScrollIndicator == null) {
1715 ViewGroup parent = (ViewGroup) getParent();
1716 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1717 mHasScrollIndicator = mScrollIndicator != null;
1718 if (mHasScrollIndicator) {
1719 mScrollIndicator.setVisibility(View.VISIBLE);
1720 }
1721 }
1722 return mScrollIndicator;
1723 }
1724
1725 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001726 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001727 }
1728
Winson Chung5a808352011-06-27 19:08:49 -07001729 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1730 @Override
1731 public void run() {
1732 hideScrollingIndicator(false);
1733 }
1734 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001735 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001736 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001737 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001738 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001739 }
1740
Michael Jurka430e8a52011-08-08 15:52:14 -07001741 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001742 if (getChildCount() <= 1) return;
1743 if (!isScrollingIndicatorEnabled()) return;
1744
1745 getScrollingIndicator();
1746 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001747 // Fade the indicator in
1748 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001749 mScrollIndicator.setVisibility(View.VISIBLE);
1750 if (mScrollIndicatorAnimator != null) {
1751 mScrollIndicatorAnimator.cancel();
1752 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001753 if (immediately) {
1754 mScrollIndicator.setAlpha(1f);
1755 } else {
1756 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1757 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1758 mScrollIndicatorAnimator.start();
1759 }
Winson Chung007c6982011-06-14 13:27:53 -07001760 }
1761 }
1762
1763 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001764 if (getChildCount() <= 1) return;
1765 if (!isScrollingIndicatorEnabled()) return;
1766
1767 getScrollingIndicator();
1768 if (mScrollIndicator != null) {
1769 // Fade the indicator out
1770 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001771 if (mScrollIndicatorAnimator != null) {
1772 mScrollIndicatorAnimator.cancel();
1773 }
1774 if (immediately) {
1775 mScrollIndicator.setVisibility(View.GONE);
1776 mScrollIndicator.setAlpha(0f);
1777 } else {
1778 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1779 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1780 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1781 private boolean cancelled = false;
1782 @Override
1783 public void onAnimationCancel(android.animation.Animator animation) {
1784 cancelled = true;
1785 }
1786 @Override
1787 public void onAnimationEnd(Animator animation) {
1788 if (!cancelled) {
1789 mScrollIndicator.setVisibility(View.GONE);
1790 }
1791 }
1792 });
1793 mScrollIndicatorAnimator.start();
1794 }
Winson Chung007c6982011-06-14 13:27:53 -07001795 }
1796 }
1797
Winson Chung32174c82011-07-19 15:47:55 -07001798 /**
1799 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1800 * fill its space on the track or not.
1801 */
1802 protected boolean hasElasticScrollIndicator() {
1803 return false;
1804 }
1805
Winson Chung007c6982011-06-14 13:27:53 -07001806 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001807 if (getChildCount() <= 1) return;
1808 if (!isScrollingIndicatorEnabled()) return;
1809
1810 getScrollingIndicator();
1811 if (mScrollIndicator != null) {
1812 updateScrollingIndicatorPosition();
1813 }
1814 }
1815
Winson Chung007c6982011-06-14 13:27:53 -07001816 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001817 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001818 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001819 int numPages = getChildCount();
1820 int pageWidth = getMeasuredWidth();
Winson Chung4afe9b32011-07-27 17:46:20 -07001821 int maxPageWidth = (numPages * getChildWidth(0)) + ((numPages - 1) * mPageSpacing);
Winson Chung09c15522011-09-09 11:44:31 -07001822 int maxScrollPosition = maxPageWidth - pageWidth; // n-1 * pageWidth
Winson Chungf5f8cef2011-07-22 11:16:13 -07001823 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001824 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1825 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001826
Winson Chung09c15522011-09-09 11:44:31 -07001827 float offset = (float) Math.max(0f, Math.min(maxScrollPosition, getScrollX()))
1828 / maxPageWidth;
Winson Chung32174c82011-07-19 15:47:55 -07001829 int indicatorSpace = trackWidth / numPages;
Winson Chungf5f8cef2011-07-22 11:16:13 -07001830 int indicatorPos = (int) (offset * trackWidth) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001831 if (hasElasticScrollIndicator()) {
1832 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1833 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1834 mScrollIndicator.requestLayout();
1835 }
1836 } else {
1837 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1838 indicatorPos += indicatorCenterOffset;
1839 }
Winson Chung007c6982011-06-14 13:27:53 -07001840 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001841 mScrollIndicator.invalidate();
1842 }
1843
Winson Chung3ac74c52011-06-30 17:39:37 -07001844 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001845 }
1846
1847 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001848 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001849
1850 /* Accessibility */
1851 @Override
1852 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1853 super.onInitializeAccessibilityNodeInfo(info);
1854 info.setScrollable(true);
1855 }
1856
1857 @Override
1858 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1859 super.onInitializeAccessibilityEvent(event);
1860 event.setScrollable(true);
1861 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1862 event.setFromIndex(mCurrentPage);
1863 event.setToIndex(mCurrentPage);
1864 event.setItemCount(getChildCount());
1865 }
1866 }
1867
1868 @Override
1869 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1870 // Do not append text content to scroll events they are fired frequently
1871 // and the client has already received another event type with the text.
1872 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1873 super.dispatchPopulateAccessibilityEvent(event);
1874 }
1875
1876 onPopulateAccessibilityEvent(event);
1877 return false;
1878 }
1879
1880 @Override
1881 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1882 super.onPopulateAccessibilityEvent(event);
1883
1884 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1885 event.getText().add(getCurrentPageDescription());
1886 }
1887 }
1888
1889 protected String getCurrentPageDescription() {
1890 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1891 return String.format(mContext.getString(R.string.default_scroll_format),
1892 page + 1, getChildCount());
1893 }
Winson Chungd11265e2011-08-30 13:37:23 -07001894
1895 @Override
1896 public boolean onHoverEvent(android.view.MotionEvent event) {
1897 return true;
1898 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001899}