blob: dca4d14487ad2118b49a575b5d61213c09821e21 [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 }
Winson Chungae890b82011-09-13 18:08:54 -0700478
479 updateScrollingIndicatorPosition();
480
481 setMeasuredDimension(widthSize, heightSize);
482
483 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions
Adam Cohenfaa28302010-11-19 12:02:18 -0800484 if (childCount > 0) {
485 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
486 } else {
487 mMaxScrollX = 0;
488 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
490
Michael Jurka8c920dd2011-01-20 14:16:56 -0800491 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800492 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
493 int delta = newX - mScrollX;
494
Michael Jurka8c920dd2011-01-20 14:16:56 -0800495 final int pageCount = getChildCount();
496 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700497 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800498 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800499 }
500 setCurrentPage(newCurrentPage);
501 }
502
Michael Jurka8c920dd2011-01-20 14:16:56 -0800503 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
504 // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and
Michael Jurkad3ef3062010-11-23 16:23:58 -0800505 // tightens the layout accordingly
506 public void setLayoutScale(float childrenScale) {
507 mLayoutScale = childrenScale;
508
509 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
510 int childCount = getChildCount();
511 float childrenX[] = new float[childCount];
512 float childrenY[] = new float[childCount];
513 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700514 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800515 childrenX[i] = child.getX();
516 childrenY[i] = child.getY();
517 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700518 // Trigger a full re-layout (never just call onLayout directly!)
519 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
520 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
521 requestLayout();
522 measure(widthSpec, heightSpec);
523 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700525 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526 child.setX(childrenX[i]);
527 child.setY(childrenY[i]);
528 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700529
Michael Jurkad3ef3062010-11-23 16:23:58 -0800530 // Also, the page offset has changed (since the pages are now smaller);
531 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800532 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533 }
534
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700536 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700537 if (!mIsDataReady) {
538 return;
539 }
540
Winson Chung785d2eb2011-04-14 16:08:02 -0700541 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700542 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
543 setHorizontalScrollBarEnabled(false);
544 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
545 scrollTo(newX, 0);
546 mScroller.setFinalX(newX);
547 setHorizontalScrollBarEnabled(true);
548 mFirstLayout = false;
549 }
550
Adam Lesinski6b879f02010-11-04 16:15:23 -0700551 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 final int childCount = getChildCount();
553 int childLeft = 0;
554 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700555 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
556 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700557 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700558
559 // Calculate the variable page spacing if necessary
560 if (mPageSpacing < 0) {
561 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
562 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564
565 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700566 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800568 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700569 final int childHeight = child.getMeasuredHeight();
570 int childTop = mPaddingTop;
571 if (mCenterPagesVertically) {
572 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
573 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800574
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700576 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800577 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700578 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700579 }
580 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800581 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
582 mFirstLayout = false;
583 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 }
585
Winson Chung03929772011-02-23 17:07:10 -0800586 protected void forceUpdateAdjacentPagesAlpha() {
587 mDirtyPageAlpha = true;
588 updateAdjacentPagesAlpha();
589 }
590
Winson Chunge3193b92010-09-10 11:44:42 -0700591 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700592 if (mFadeInAdjacentScreens) {
593 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700594 int screenWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight;
Winson Chung63257c12011-05-05 17:06:13 -0700595 int halfScreenSize = screenWidth / 2;
Winson Chung4afe9b32011-07-27 17:46:20 -0700596 int screenCenter = mScrollX + halfScreenSize + mPaddingLeft;
Michael Jurka0142d492010-08-25 17:46:15 -0700597 final int childCount = getChildCount();
598 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700599 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800600 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700601 int halfChildWidth = (childWidth / 2);
602 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700603
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700604 // On the first layout, we may not have a width nor a proper offset, so for now
605 // we should just assume full page width (and calculate the offset according to
606 // that).
607 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700608 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700609 childCenter = (i * childWidth) + (childWidth / 2);
610 }
611
Winson Chunge8878e32010-09-15 20:37:09 -0700612 int d = halfChildWidth;
613 int distanceFromScreenCenter = childCenter - screenCenter;
614 if (distanceFromScreenCenter > 0) {
615 if (i > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700616 d += getScaledMeasuredWidth(getPageAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700617 } else {
618 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700619 }
Michael Jurka0142d492010-08-25 17:46:15 -0700620 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700621 if (i < childCount - 1) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700622 d += getScaledMeasuredWidth(getPageAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700623 } else {
624 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700625 }
Michael Jurka0142d492010-08-25 17:46:15 -0700626 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700627 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700628
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700629 // Preventing potential divide-by-zero
630 d = Math.max(1, d);
631
Winson Chunge8878e32010-09-15 20:37:09 -0700632 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
633 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
634 float alpha = 1.0f - dimAlpha;
635
Adam Cohenf34bab52010-09-30 14:11:56 -0700636 if (alpha < ALPHA_QUANTIZE_LEVEL) {
637 alpha = 0.0f;
638 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
639 alpha = 1.0f;
640 }
641
Michael Jurkaa40829a2011-02-16 18:02:45 -0800642 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
643 // this optimization causes alpha to not be properly updated sometimes (repro
644 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
645 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
646 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800647 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700648 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800649 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 }
Michael Jurka0142d492010-08-25 17:46:15 -0700651 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700652 }
653 }
Winson Chunge3193b92010-09-10 11:44:42 -0700654 }
655
Adam Cohenf34bab52010-09-30 14:11:56 -0700656 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700657 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700658 }
659
Winson Chunge3193b92010-09-10 11:44:42 -0700660 @Override
661 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700662 int halfScreenSize = getMeasuredWidth() / 2;
663 int screenCenter = mScrollX + halfScreenSize;
664
665 if (screenCenter != mLastScreenCenter) {
666 screenScrolled(screenCenter);
667 updateAdjacentPagesAlpha();
668 mLastScreenCenter = screenCenter;
669 }
Michael Jurka0142d492010-08-25 17:46:15 -0700670
671 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700672 // As an optimization, this code assumes that all pages have the same width as the 0th
673 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700674 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700675 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700676 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700677 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700678 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700679 int leftScreen = 0;
680 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700681 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700682 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700683 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700684 }
685 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700686 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700687 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700688 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700689 }
690 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700691
Michael Jurkac4fb9172010-09-02 17:19:20 -0700692 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800693 // Clip to the bounds
694 canvas.save();
695 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
696 mScrollY + mBottom - mTop);
697
Adam Cohen22f823d2011-09-01 17:22:18 -0700698 for (int i = rightScreen; i >= leftScreen; i--) {
699 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700700 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800701 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700702 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 }
704
705 @Override
706 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700707 int page = indexOfChild(child);
708 if (page != mCurrentPage || !mScroller.isFinished()) {
709 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 return true;
711 }
712 return false;
713 }
714
715 @Override
716 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700717 int focusablePage;
718 if (mNextPage != INVALID_PAGE) {
719 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700721 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 }
Winson Chung86f77532010-08-24 11:08:22 -0700723 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700725 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 }
727 return false;
728 }
729
730 @Override
731 public boolean dispatchUnhandledMove(View focused, int direction) {
732 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700733 if (getCurrentPage() > 0) {
734 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 return true;
736 }
737 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700738 if (getCurrentPage() < getPageCount() - 1) {
739 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 return true;
741 }
742 }
743 return super.dispatchUnhandledMove(focused, direction);
744 }
745
746 @Override
747 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700748 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
749 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 }
751 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700752 if (mCurrentPage > 0) {
753 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 }
755 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700756 if (mCurrentPage < getPageCount() - 1) {
757 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 }
759 }
760 }
761
762 /**
763 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700764 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 *
Winson Chung86f77532010-08-24 11:08:22 -0700766 * This happens when live folders requery, and if they're off page, they
767 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 */
769 @Override
770 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700771 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 View v = focused;
773 while (true) {
774 if (v == current) {
775 super.focusableViewAvailable(focused);
776 return;
777 }
778 if (v == this) {
779 return;
780 }
781 ViewParent parent = v.getParent();
782 if (parent instanceof View) {
783 v = (View)v.getParent();
784 } else {
785 return;
786 }
787 }
788 }
789
790 /**
791 * {@inheritDoc}
792 */
793 @Override
794 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
795 if (disallowIntercept) {
796 // We need to make sure to cancel our long press if
797 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700798 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700799 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 }
801 super.requestDisallowInterceptTouchEvent(disallowIntercept);
802 }
803
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800804 /**
805 * Return true if a tap at (x, y) should trigger a flip to the previous page.
806 */
807 protected boolean hitsPreviousPage(float x, float y) {
808 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
809 }
810
811 /**
812 * Return true if a tap at (x, y) should trigger a flip to the next page.
813 */
814 protected boolean hitsNextPage(float x, float y) {
815 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
816 }
817
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 @Override
819 public boolean onInterceptTouchEvent(MotionEvent ev) {
820 /*
821 * This method JUST determines whether we want to intercept the motion.
822 * If we return true, onTouchEvent will be called and we do the actual
823 * scrolling there.
824 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800825 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700826
Winson Chung45e1d6e2010-11-09 17:19:49 -0800827 // Skip touch handling if there are no pages to swipe
828 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
829
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 /*
831 * Shortcut the most recurring case: the user is in the dragging
832 * state and he is moving his finger. We want to intercept this
833 * motion.
834 */
835 final int action = ev.getAction();
836 if ((action == MotionEvent.ACTION_MOVE) &&
837 (mTouchState == TOUCH_STATE_SCROLLING)) {
838 return true;
839 }
840
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 switch (action & MotionEvent.ACTION_MASK) {
842 case MotionEvent.ACTION_MOVE: {
843 /*
844 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
845 * whether the user has moved far enough from his original down touch.
846 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700847 if (mActivePointerId != INVALID_POINTER) {
848 determineScrollingStart(ev);
849 break;
850 }
851 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
852 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
853 // i.e. fall through to the next case (don't break)
854 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
855 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 }
857
858 case MotionEvent.ACTION_DOWN: {
859 final float x = ev.getX();
860 final float y = ev.getY();
861 // Remember location of down touch
862 mDownMotionX = x;
863 mLastMotionX = x;
864 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800865 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800866 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 mActivePointerId = ev.getPointerId(0);
868 mAllowLongPress = true;
869
870 /*
871 * If being flinged and user touches the screen, initiate drag;
872 * otherwise don't. mScroller.isFinished should be false when
873 * being flinged.
874 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700875 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700876 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
877 if (finishedScrolling) {
878 mTouchState = TOUCH_STATE_REST;
879 mScroller.abortAnimation();
880 } else {
881 mTouchState = TOUCH_STATE_SCROLLING;
882 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700883
Winson Chung86f77532010-08-24 11:08:22 -0700884 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800886 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800888 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700889 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800890 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 mTouchState = TOUCH_STATE_NEXT_PAGE;
892 }
893 }
894 }
895 break;
896 }
897
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800899 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 mTouchState = TOUCH_STATE_REST;
901 mAllowLongPress = false;
902 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800903 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 break;
905
906 case MotionEvent.ACTION_POINTER_UP:
907 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800908 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700909 break;
910 }
911
912 /*
913 * The only time we want to intercept motion events is if we are in the
914 * drag mode.
915 */
916 return mTouchState != TOUCH_STATE_REST;
917 }
918
Winson Chung80baf5a2010-08-09 16:03:15 -0700919 protected void animateClickFeedback(View v, final Runnable r) {
920 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800921 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
922 loadAnimator(mContext, R.anim.paged_view_click_feedback);
923 anim.setTarget(v);
924 anim.addListener(new AnimatorListenerAdapter() {
925 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700926 r.run();
927 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700928 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800929 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700930 }
931
Adam Cohenf8d28232011-02-01 21:47:00 -0800932 protected void determineScrollingStart(MotionEvent ev) {
933 determineScrollingStart(ev, 1.0f);
934 }
935
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 /*
937 * Determines if we should change the touch state to start scrolling after the
938 * user moves their touch point too far.
939 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800940 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 /*
942 * Locally do absolute value. mLastMotionX is set to the y value
943 * of the down event.
944 */
945 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700946 if (pointerIndex == -1) {
947 return;
948 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 final float x = ev.getX(pointerIndex);
950 final float y = ev.getY(pointerIndex);
951 final int xDiff = (int) Math.abs(x - mLastMotionX);
952 final int yDiff = (int) Math.abs(y - mLastMotionY);
953
Adam Cohenf8d28232011-02-01 21:47:00 -0800954 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 boolean xPaged = xDiff > mPagingTouchSlop;
956 boolean xMoved = xDiff > touchSlop;
957 boolean yMoved = yDiff > touchSlop;
958
Adam Cohenf8d28232011-02-01 21:47:00 -0800959 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700960 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 // Scroll if the user moved far enough along the X axis
962 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800963 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800965 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700966 mTouchX = mScrollX;
967 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
968 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 }
970 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800971 cancelCurrentPageLongPress();
972 }
973 }
974
975 protected void cancelCurrentPageLongPress() {
976 if (mAllowLongPress) {
977 mAllowLongPress = false;
978 // Try canceling the long press. It could also have been scheduled
979 // by a distant descendant, so use the mAllowLongPress flag to block
980 // everything
981 final View currentPage = getPageAt(mCurrentPage);
982 if (currentPage != null) {
983 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 }
985 }
986 }
987
Adam Cohenb5ba0972011-09-07 18:02:31 -0700988 protected float getScrollProgress(int screenCenter, View v, int page) {
989 final int halfScreenSize = getMeasuredWidth() / 2;
990
991 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
992 int delta = screenCenter - (getChildOffset(page) -
993 getRelativeChildOffset(page) + halfScreenSize);
994
995 float scrollProgress = delta / (totalDistance * 1.0f);
996 scrollProgress = Math.min(scrollProgress, 1.0f);
997 scrollProgress = Math.max(scrollProgress, -1.0f);
998 return scrollProgress;
999 }
1000
Adam Cohene0f66b52010-11-23 15:06:07 -08001001 // This curve determines how the effect of scrolling over the limits of the page dimishes
1002 // as the user pulls further and further from the bounds
1003 private float overScrollInfluenceCurve(float f) {
1004 f -= 1.0f;
1005 return f * f * f + 1.0f;
1006 }
1007
Adam Cohenb5ba0972011-09-07 18:02:31 -07001008 protected void acceleratedOverScroll(float amount) {
1009 int screenSize = getMeasuredWidth();
1010
1011 // We want to reach the max over scroll effect when the user has
1012 // over scrolled half the size of the screen
1013 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1014
1015 if (f == 0) return;
1016
1017 // Clamp this factor, f, to -1 < f < 1
1018 if (Math.abs(f) >= 1) {
1019 f /= Math.abs(f);
1020 }
1021
1022 int overScrollAmount = (int) Math.round(f * screenSize);
1023 if (amount < 0) {
1024 mScrollX = overScrollAmount;
1025 } else {
1026 mScrollX = mMaxScrollX + overScrollAmount;
1027 }
1028 invalidate();
1029 }
1030
1031 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001032 int screenSize = getMeasuredWidth();
1033
1034 float f = (amount / screenSize);
1035
1036 if (f == 0) return;
1037 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1038
Adam Cohen7bfc9792011-01-28 13:52:37 -08001039 // Clamp this factor, f, to -1 < f < 1
1040 if (Math.abs(f) >= 1) {
1041 f /= Math.abs(f);
1042 }
1043
Adam Cohene0f66b52010-11-23 15:06:07 -08001044 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001045 if (amount < 0) {
1046 mScrollX = overScrollAmount;
1047 } else {
1048 mScrollX = mMaxScrollX + overScrollAmount;
1049 }
1050 invalidate();
1051 }
1052
Adam Cohenb5ba0972011-09-07 18:02:31 -07001053 protected void overScroll(float amount) {
1054 dampedOverScroll(amount);
1055 }
1056
Michael Jurkac5b262c2011-01-12 20:24:50 -08001057 protected float maxOverScroll() {
1058 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001059 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001060 float f = 1.0f;
1061 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1062 return OVERSCROLL_DAMP_FACTOR * f;
1063 }
1064
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 @Override
1066 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001067 // Skip touch handling if there are no pages to swipe
1068 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1069
Michael Jurkab8f06722010-10-10 15:58:46 -07001070 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001071
1072 final int action = ev.getAction();
1073
1074 switch (action & MotionEvent.ACTION_MASK) {
1075 case MotionEvent.ACTION_DOWN:
1076 /*
1077 * If being flinged and user touches, stop the fling. isFinished
1078 * will be false if being flinged.
1079 */
1080 if (!mScroller.isFinished()) {
1081 mScroller.abortAnimation();
1082 }
1083
1084 // Remember where the motion event started
1085 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001086 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001087 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001088 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001089 if (mTouchState == TOUCH_STATE_SCROLLING) {
1090 pageBeginMoving();
1091 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 break;
1093
1094 case MotionEvent.ACTION_MOVE:
1095 if (mTouchState == TOUCH_STATE_SCROLLING) {
1096 // Scroll to follow the motion event
1097 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1098 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001099 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001100
Adam Cohenaefd4e12011-02-14 16:39:38 -08001101 mTotalMotionX += Math.abs(deltaX);
1102
Winson Chungc0844aa2011-02-02 15:25:58 -08001103 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1104 // keep the remainder because we are actually testing if we've moved from the last
1105 // scrolled position (which is discrete).
1106 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001107 mTouchX += deltaX;
1108 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1109 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001110 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001111 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001112 } else {
1113 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001114 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001115 mLastMotionX = x;
1116 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 } else {
1118 awakenScrollBars();
1119 }
Adam Cohen564976a2010-10-13 18:52:07 -07001120 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 determineScrollingStart(ev);
1122 }
1123 break;
1124
1125 case MotionEvent.ACTION_UP:
1126 if (mTouchState == TOUCH_STATE_SCROLLING) {
1127 final int activePointerId = mActivePointerId;
1128 final int pointerIndex = ev.findPointerIndex(activePointerId);
1129 final float x = ev.getX(pointerIndex);
1130 final VelocityTracker velocityTracker = mVelocityTracker;
1131 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1132 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001133 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001134 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001135 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001136
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001137 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1138
Adam Cohenaefd4e12011-02-14 16:39:38 -08001139 // In the case that the page is moved far to one direction and then is flung
1140 // in the opposite direction, we use a threshold to determine whether we should
1141 // just return to the starting page, or if we should skip one further.
1142 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001143 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001144 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001145 Math.signum(velocityX) != Math.signum(deltaX)) {
1146 returnToOriginalPage = true;
1147 }
1148
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001149 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001150 Math.abs(velocityX) > snapVelocity;
1151
1152 int finalPage;
1153 // We give flings precedence over large moves, which is why we short-circuit our
1154 // test for a large move if a fling has been registered. That is, a large
1155 // move to the left and fling to the right will register as a fling to the right.
1156 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1157 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1158 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1159 snapToPageWithVelocity(finalPage, velocityX);
1160 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1161 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001162 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001163 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1164 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001165 } else {
1166 snapToDestination();
1167 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001168 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 // at this point we have not moved beyond the touch slop
1170 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1171 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001172 int nextPage = Math.max(0, mCurrentPage - 1);
1173 if (nextPage != mCurrentPage) {
1174 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 } else {
1176 snapToDestination();
1177 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001178 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001179 // at this point we have not moved beyond the touch slop
1180 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1181 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001182 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1183 if (nextPage != mCurrentPage) {
1184 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 } else {
1186 snapToDestination();
1187 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001188 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001189 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001190 }
1191 mTouchState = TOUCH_STATE_REST;
1192 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001193 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001194 break;
1195
1196 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001197 if (mTouchState == TOUCH_STATE_SCROLLING) {
1198 snapToDestination();
1199 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 mTouchState = TOUCH_STATE_REST;
1201 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001202 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 break;
1204
1205 case MotionEvent.ACTION_POINTER_UP:
1206 onSecondaryPointerUp(ev);
1207 break;
1208 }
1209
1210 return true;
1211 }
1212
Winson Chung185d7162011-02-28 13:47:29 -08001213 @Override
1214 public boolean onGenericMotionEvent(MotionEvent event) {
1215 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1216 switch (event.getAction()) {
1217 case MotionEvent.ACTION_SCROLL: {
1218 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1219 final float vscroll;
1220 final float hscroll;
1221 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1222 vscroll = 0;
1223 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1224 } else {
1225 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1226 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1227 }
1228 if (hscroll != 0 || vscroll != 0) {
1229 if (hscroll > 0 || vscroll > 0) {
1230 scrollRight();
1231 } else {
1232 scrollLeft();
1233 }
1234 return true;
1235 }
1236 }
1237 }
1238 }
1239 return super.onGenericMotionEvent(event);
1240 }
1241
Michael Jurkab8f06722010-10-10 15:58:46 -07001242 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1243 if (mVelocityTracker == null) {
1244 mVelocityTracker = VelocityTracker.obtain();
1245 }
1246 mVelocityTracker.addMovement(ev);
1247 }
1248
1249 private void releaseVelocityTracker() {
1250 if (mVelocityTracker != null) {
1251 mVelocityTracker.recycle();
1252 mVelocityTracker = null;
1253 }
1254 }
1255
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 private void onSecondaryPointerUp(MotionEvent ev) {
1257 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1258 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1259 final int pointerId = ev.getPointerId(pointerIndex);
1260 if (pointerId == mActivePointerId) {
1261 // This was our active pointer going up. Choose a new
1262 // active pointer and adjust accordingly.
1263 // TODO: Make this decision more intelligent.
1264 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1265 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1266 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001267 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 mActivePointerId = ev.getPointerId(newPointerIndex);
1269 if (mVelocityTracker != null) {
1270 mVelocityTracker.clear();
1271 }
1272 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001273 }
1274
Michael Jurkad771c962011-08-09 15:00:48 -07001275 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001276
1277 @Override
1278 public void requestChildFocus(View child, View focused) {
1279 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001280 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001281 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001282 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 }
1284 }
1285
Winson Chunge3193b92010-09-10 11:44:42 -07001286 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1287 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001288 int left;
1289 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001290 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001291 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001292 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001293 if (left <= relativeOffset && relativeOffset <= right) {
1294 return i;
1295 }
Winson Chunge3193b92010-09-10 11:44:42 -07001296 }
1297 return -1;
1298 }
1299
Winson Chung1908d072011-02-24 18:09:44 -08001300 protected void setMinimumWidthOverride(int minimumWidth) {
1301 mMinimumWidth = minimumWidth;
1302 }
Winson Chung34b23d52011-03-18 11:29:34 -07001303 protected void resetMinimumWidthOverride() {
1304 mMinimumWidth = 0;
1305 }
Winson Chung1908d072011-02-24 18:09:44 -08001306
1307 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001308 // This functions are called enough times that it actually makes a difference in the
1309 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001310 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001311 final int minWidth = mMinimumWidth;
1312 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001313 }
1314
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 protected int getRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001316 int padding = mPaddingLeft + mPaddingRight;
1317 return mPaddingLeft + (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001318 }
Winson Chung557d6ed2011-07-08 15:34:52 -07001319 protected int getScaledRelativeChildOffset(int index) {
Winson Chung4afe9b32011-07-27 17:46:20 -07001320 int padding = mPaddingLeft + mPaddingRight;
1321 return mPaddingLeft + (getMeasuredWidth() - padding -
Adam Cohen22f823d2011-09-01 17:22:18 -07001322 getScaledMeasuredWidth(getPageAt(index))) / 2;
Winson Chung557d6ed2011-07-08 15:34:52 -07001323 }
1324
Winson Chung321e9ee2010-08-09 13:37:56 -07001325 protected int getChildOffset(int index) {
1326 if (getChildCount() == 0)
1327 return 0;
1328
1329 int offset = getRelativeChildOffset(0);
1330 for (int i = 0; i < index; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001331 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001332 }
1333 return offset;
1334 }
1335
Michael Jurkad3ef3062010-11-23 16:23:58 -08001336 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001337 // This functions are called enough times that it actually makes a difference in the
1338 // profiler -- so just inline the max() here
1339 final int measuredWidth = child.getMeasuredWidth();
1340 final int minWidth = mMinimumWidth;
1341 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1342 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001343 }
1344
Adam Cohend19d3ca2010-09-15 14:43:42 -07001345 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001346 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 int minDistanceFromScreenCenterIndex = -1;
1348 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1349 final int childCount = getChildCount();
1350 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001351 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001352 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 int halfChildWidth = (childWidth / 2);
1354 int childCenter = getChildOffset(i) + halfChildWidth;
1355 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1356 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1357 minDistanceFromScreenCenter = distanceFromScreenCenter;
1358 minDistanceFromScreenCenterIndex = i;
1359 }
1360 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001361 return minDistanceFromScreenCenterIndex;
1362 }
1363
1364 protected void snapToDestination() {
1365 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 }
1367
Adam Cohene0f66b52010-11-23 15:06:07 -08001368 private static class ScrollInterpolator implements Interpolator {
1369 public ScrollInterpolator() {
1370 }
1371
1372 public float getInterpolation(float t) {
1373 t -= 1.0f;
1374 return t*t*t*t*t + 1;
1375 }
1376 }
1377
1378 // We want the duration of the page snap animation to be influenced by the distance that
1379 // the screen has to travel, however, we don't want this duration to be effected in a
1380 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1381 // of travel has on the overall snap duration.
1382 float distanceInfluenceForSnapDuration(float f) {
1383 f -= 0.5f; // center the values about 0.
1384 f *= 0.3f * Math.PI / 2.0f;
1385 return (float) Math.sin(f);
1386 }
1387
Michael Jurka0142d492010-08-25 17:46:15 -07001388 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001389 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1390 int halfScreenSize = getMeasuredWidth() / 2;
1391
Winson Chung785d2eb2011-04-14 16:08:02 -07001392 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1393 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1394 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001395 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1396 int delta = newX - mUnboundedScrollX;
1397 int duration = 0;
1398
1399 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1400 // If the velocity is low enough, then treat this more as an automatic page advance
1401 // as opposed to an apparent physical response to flinging
1402 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1403 return;
1404 }
1405
1406 // Here we compute a "distance" that will be used in the computation of the overall
1407 // snap duration. This is a function of the actual distance that needs to be traveled;
1408 // we keep this value close to half screen size in order to reduce the variance in snap
1409 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001410 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001411 float distance = halfScreenSize + halfScreenSize *
1412 distanceInfluenceForSnapDuration(distanceRatio);
1413
1414 velocity = Math.abs(velocity);
1415 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1416
1417 // we want the page's snap velocity to approximately match the velocity at which the
1418 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001419 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1420 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001421
1422 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001423 }
1424
1425 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001426 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 }
1428
Michael Jurka0142d492010-08-25 17:46:15 -07001429 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001430 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001431
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1433 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1434 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001435 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001436 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001438 snapToPage(whichPage, delta, duration);
1439 }
1440
1441 protected void snapToPage(int whichPage, int delta, int duration) {
1442 mNextPage = whichPage;
1443
1444 View focusedChild = getFocusedChild();
1445 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001446 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001447 focusedChild.clearFocus();
1448 }
1449
1450 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 awakenScrollBars(duration);
1452 if (duration == 0) {
1453 duration = Math.abs(delta);
1454 }
1455
1456 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001457 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001458
Winson Chungb44b5242011-06-13 11:32:14 -07001459 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1460 // loading associated pages until the scroll settles
1461 if (mDeferScrollUpdate) {
1462 loadAssociatedPages(mNextPage);
1463 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001464 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001465 }
Michael Jurka0142d492010-08-25 17:46:15 -07001466 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 invalidate();
1468 }
1469
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 public void scrollLeft() {
1471 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001472 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001474 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 }
1476 }
1477
1478 public void scrollRight() {
1479 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001480 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001482 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001483 }
1484 }
1485
Winson Chung86f77532010-08-24 11:08:22 -07001486 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 int result = -1;
1488 if (v != null) {
1489 ViewParent vp = v.getParent();
1490 int count = getChildCount();
1491 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001492 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 return i;
1494 }
1495 }
1496 }
1497 return result;
1498 }
1499
1500 /**
1501 * @return True is long presses are still allowed for the current touch
1502 */
1503 public boolean allowLongPress() {
1504 return mAllowLongPress;
1505 }
1506
Michael Jurka0142d492010-08-25 17:46:15 -07001507 /**
1508 * Set true to allow long-press events to be triggered, usually checked by
1509 * {@link Launcher} to accept or block dpad-initiated long-presses.
1510 */
1511 public void setAllowLongPress(boolean allowLongPress) {
1512 mAllowLongPress = allowLongPress;
1513 }
1514
Winson Chung321e9ee2010-08-09 13:37:56 -07001515 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001516 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001517
1518 SavedState(Parcelable superState) {
1519 super(superState);
1520 }
1521
1522 private SavedState(Parcel in) {
1523 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001524 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001525 }
1526
1527 @Override
1528 public void writeToParcel(Parcel out, int flags) {
1529 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001530 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001531 }
1532
1533 public static final Parcelable.Creator<SavedState> CREATOR =
1534 new Parcelable.Creator<SavedState>() {
1535 public SavedState createFromParcel(Parcel in) {
1536 return new SavedState(in);
1537 }
1538
1539 public SavedState[] newArray(int size) {
1540 return new SavedState[size];
1541 }
1542 };
1543 }
1544
Winson Chungf314b0e2011-08-16 11:54:27 -07001545 protected void loadAssociatedPages(int page) {
1546 loadAssociatedPages(page, false);
1547 }
1548 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001549 if (mContentIsRefreshable) {
1550 final int count = getChildCount();
1551 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001552 int lowerPageBound = getAssociatedLowerPageBound(page);
1553 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001554 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1555 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001556 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001557 if ((i != page) && immediateAndOnly) {
1558 continue;
1559 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001560 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001561 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001562 if (lowerPageBound <= i && i <= upperPageBound) {
1563 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001564 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001565 mDirtyPageContent.set(i, false);
1566 }
1567 } else {
1568 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001569 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001570 }
1571 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001572 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001573 }
1574 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001575 }
1576 }
1577
Winson Chunge3193b92010-09-10 11:44:42 -07001578 protected int getAssociatedLowerPageBound(int page) {
1579 return Math.max(0, page - 1);
1580 }
1581 protected int getAssociatedUpperPageBound(int page) {
1582 final int count = getChildCount();
1583 return Math.min(page + 1, count - 1);
1584 }
1585
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001586 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001587 if (isChoiceMode(CHOICE_MODE_NONE)) {
1588 mChoiceMode = mode;
1589 mActionMode = startActionMode(callback);
1590 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001591 }
1592
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001593 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001594 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001595 mChoiceMode = CHOICE_MODE_NONE;
1596 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001597 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001598 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001599 }
1600 }
1601
1602 protected boolean isChoiceMode(int mode) {
1603 return mChoiceMode == mode;
1604 }
1605
1606 protected ArrayList<Checkable> getCheckedGrandchildren() {
1607 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1608 final int childCount = getChildCount();
1609 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001610 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001611 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001612 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001613 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001614 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001615 checked.add((Checkable) v);
1616 }
1617 }
1618 }
1619 return checked;
1620 }
1621
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001622 /**
1623 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1624 * Otherwise, returns null.
1625 */
1626 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001627 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001628 final int childCount = getChildCount();
1629 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001630 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001631 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001632 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001633 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001634 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1635 return (Checkable) v;
1636 }
1637 }
1638 }
1639 }
1640 return null;
1641 }
1642
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001643 protected void resetCheckedGrandchildren() {
1644 // loop through children, and set all of their children to _not_ be checked
1645 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1646 for (int i = 0; i < checked.size(); ++i) {
1647 final Checkable c = checked.get(i);
1648 c.setChecked(false);
1649 }
1650 }
1651
Winson Chung86f77532010-08-24 11:08:22 -07001652 /**
1653 * This method is called ONLY to synchronize the number of pages that the paged view has.
1654 * To actually fill the pages with information, implement syncPageItems() below. It is
1655 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1656 * and therefore, individual page items do not need to be updated in this method.
1657 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001658 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001659
1660 /**
1661 * This method is called to synchronize the items that are on a particular page. If views on
1662 * the page can be reused, then they should be updated within this method.
1663 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001664 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001665
Michael Jurka983e3fd2011-05-26 17:10:29 -07001666 protected void postInvalidatePageData(final boolean clearViews) {
1667 post(new Runnable() {
1668 // post the call to avoid a call to requestLayout from a layout pass
1669 public void run() {
1670 if (clearViews) {
1671 removeAllViews();
1672 }
1673 invalidatePageData();
1674 }
1675 });
1676 }
1677
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001678 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001679 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001680 }
1681 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001682 invalidatePageData(currentPage, false);
1683 }
1684 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001685 if (!mIsDataReady) {
1686 return;
1687 }
1688
Michael Jurka0142d492010-08-25 17:46:15 -07001689 if (mContentIsRefreshable) {
1690 // Update all the pages
1691 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001692
Winson Chung5a808352011-06-27 19:08:49 -07001693 // We must force a measure after we've loaded the pages to update the content width and
1694 // to determine the full scroll width
1695 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1696 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1697
1698 // Set a new page as the current page if necessary
1699 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001700 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001701 }
1702
Michael Jurka0142d492010-08-25 17:46:15 -07001703 // Mark each of the pages as dirty
1704 final int count = getChildCount();
1705 mDirtyPageContent.clear();
1706 for (int i = 0; i < count; ++i) {
1707 mDirtyPageContent.add(true);
1708 }
1709
1710 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001711 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001712 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001713 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001714 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001715 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001716 }
Winson Chung007c6982011-06-14 13:27:53 -07001717
1718 private ImageView getScrollingIndicator() {
1719 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1720 // found
1721 if (mHasScrollIndicator && mScrollIndicator == null) {
1722 ViewGroup parent = (ViewGroup) getParent();
1723 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1724 mHasScrollIndicator = mScrollIndicator != null;
1725 if (mHasScrollIndicator) {
1726 mScrollIndicator.setVisibility(View.VISIBLE);
1727 }
1728 }
1729 return mScrollIndicator;
1730 }
1731
1732 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001733 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001734 }
1735
Winson Chung5a808352011-06-27 19:08:49 -07001736 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1737 @Override
1738 public void run() {
1739 hideScrollingIndicator(false);
1740 }
1741 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001742 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001743 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001744 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001745 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001746 }
1747
Michael Jurka430e8a52011-08-08 15:52:14 -07001748 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001749 if (getChildCount() <= 1) return;
1750 if (!isScrollingIndicatorEnabled()) return;
1751
1752 getScrollingIndicator();
1753 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001754 // Fade the indicator in
1755 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001756 mScrollIndicator.setVisibility(View.VISIBLE);
1757 if (mScrollIndicatorAnimator != null) {
1758 mScrollIndicatorAnimator.cancel();
1759 }
Michael Jurka430e8a52011-08-08 15:52:14 -07001760 if (immediately) {
1761 mScrollIndicator.setAlpha(1f);
1762 } else {
1763 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1764 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1765 mScrollIndicatorAnimator.start();
1766 }
Winson Chung007c6982011-06-14 13:27:53 -07001767 }
1768 }
1769
1770 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001771 if (getChildCount() <= 1) return;
1772 if (!isScrollingIndicatorEnabled()) return;
1773
1774 getScrollingIndicator();
1775 if (mScrollIndicator != null) {
1776 // Fade the indicator out
1777 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001778 if (mScrollIndicatorAnimator != null) {
1779 mScrollIndicatorAnimator.cancel();
1780 }
1781 if (immediately) {
1782 mScrollIndicator.setVisibility(View.GONE);
1783 mScrollIndicator.setAlpha(0f);
1784 } else {
1785 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1786 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1787 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1788 private boolean cancelled = false;
1789 @Override
1790 public void onAnimationCancel(android.animation.Animator animation) {
1791 cancelled = true;
1792 }
1793 @Override
1794 public void onAnimationEnd(Animator animation) {
1795 if (!cancelled) {
1796 mScrollIndicator.setVisibility(View.GONE);
1797 }
1798 }
1799 });
1800 mScrollIndicatorAnimator.start();
1801 }
Winson Chung007c6982011-06-14 13:27:53 -07001802 }
1803 }
1804
Winson Chung32174c82011-07-19 15:47:55 -07001805 /**
1806 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1807 * fill its space on the track or not.
1808 */
1809 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001810 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001811 }
1812
Winson Chung007c6982011-06-14 13:27:53 -07001813 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001814 if (getChildCount() <= 1) return;
1815 if (!isScrollingIndicatorEnabled()) return;
1816
1817 getScrollingIndicator();
1818 if (mScrollIndicator != null) {
1819 updateScrollingIndicatorPosition();
1820 }
1821 }
1822
Winson Chung007c6982011-06-14 13:27:53 -07001823 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001824 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001825 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001826 int numPages = getChildCount();
1827 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001828 int lastChildIndex = Math.max(0, getChildCount() - 1);
1829 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001830 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001831 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1832 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001833
Winson Chungae890b82011-09-13 18:08:54 -07001834 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001835 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001836 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001837 if (hasElasticScrollIndicator()) {
1838 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1839 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1840 mScrollIndicator.requestLayout();
1841 }
1842 } else {
1843 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1844 indicatorPos += indicatorCenterOffset;
1845 }
Winson Chung007c6982011-06-14 13:27:53 -07001846 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001847 mScrollIndicator.invalidate();
1848 }
1849
Winson Chung3ac74c52011-06-30 17:39:37 -07001850 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001851 }
1852
1853 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001854 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001855
1856 /* Accessibility */
1857 @Override
1858 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1859 super.onInitializeAccessibilityNodeInfo(info);
1860 info.setScrollable(true);
1861 }
1862
1863 @Override
1864 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1865 super.onInitializeAccessibilityEvent(event);
1866 event.setScrollable(true);
1867 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1868 event.setFromIndex(mCurrentPage);
1869 event.setToIndex(mCurrentPage);
1870 event.setItemCount(getChildCount());
1871 }
1872 }
1873
1874 @Override
1875 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1876 // Do not append text content to scroll events they are fired frequently
1877 // and the client has already received another event type with the text.
1878 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1879 super.dispatchPopulateAccessibilityEvent(event);
1880 }
1881
1882 onPopulateAccessibilityEvent(event);
1883 return false;
1884 }
1885
1886 @Override
1887 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1888 super.onPopulateAccessibilityEvent(event);
1889
1890 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1891 event.getText().add(getCurrentPageDescription());
1892 }
1893 }
1894
1895 protected String getCurrentPageDescription() {
1896 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1897 return String.format(mContext.getString(R.string.default_scroll_format),
1898 page + 1, getChildCount());
1899 }
Winson Chungd11265e2011-08-30 13:37:23 -07001900
1901 @Override
1902 public boolean onHoverEvent(android.view.MotionEvent event) {
1903 return true;
1904 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001905}