blob: 159e821d51b9cf1b8713e8030527563a9ee236b5 [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 Chung04998342011-01-05 13:54:43 -080019import java.util.ArrayList;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070020
Winson Chung228a0fa2011-01-26 22:14:13 -080021import android.animation.Animator;
22import android.animation.AnimatorInflater;
23import android.animation.AnimatorListenerAdapter;
24import android.animation.ObjectAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070026import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.graphics.Canvas;
28import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.os.Parcel;
30import android.os.Parcelable;
31import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.view.MotionEvent;
34import android.view.VelocityTracker;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080039import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Winson Chung04998342011-01-05 13:54:43 -080043import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070044
Winson Chung321e9ee2010-08-09 13:37:56 -070045/**
46 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070047 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070048 */
49public abstract class PagedView extends ViewGroup {
50 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070051 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070052
Winson Chung86f77532010-08-24 11:08:22 -070053 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070054 private static final int MIN_LENGTH_FOR_FLING = 25;
55 // The min drag distance to trigger a page shift (regardless of velocity)
56 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Adam Cohene0f66b52010-11-23 15:06:07 -080058 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Adam Cohene0f66b52010-11-23 15:06:07 -080061 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
62 private static final int MINIMUM_SNAP_VELOCITY = 2200;
63 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080064 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080065
Michael Jurka0142d492010-08-25 17:46:15 -070066 // the velocity at which a fling gesture will cause us to snap to the next page
67 protected int mSnapVelocity = 500;
68
69 protected float mSmoothingTime;
70 protected float mTouchX;
71
72 protected boolean mFirstLayout = true;
73
74 protected int mCurrentPage;
75 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080076 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080077 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070078 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070079 private VelocityTracker mVelocityTracker;
80
81 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080082 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080083 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080084 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080085 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070086 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070087
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected final static int TOUCH_STATE_REST = 0;
89 protected final static int TOUCH_STATE_SCROLLING = 1;
90 protected final static int TOUCH_STATE_PREV_PAGE = 2;
91 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070092 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka0142d492010-08-25 17:46:15 -070096 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka7426c422010-11-11 15:23:47 -0800100 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101 private int mPagingTouchSlop;
102 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700103 protected int mPageSpacing;
104 protected int mPageLayoutPaddingTop;
105 protected int mPageLayoutPaddingBottom;
106 protected int mPageLayoutPaddingLeft;
107 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700108 protected int mPageLayoutWidthGap;
109 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700110 protected int mCellCountX;
111 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700112 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800113 protected boolean mAllowOverScroll = true;
114 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Michael Jurka8c920dd2011-01-20 14:16:56 -0800116 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800117 protected float mLayoutScale = 1.0f;
118
Michael Jurka5f1c5092010-09-03 14:15:02 -0700119 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka5f1c5092010-09-03 14:15:02 -0700121 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Winson Chung86f77532010-08-24 11:08:22 -0700123 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Winson Chung86f77532010-08-24 11:08:22 -0700125 private ArrayList<Boolean> mDirtyPageContent;
126 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700128 // choice modes
129 protected static final int CHOICE_MODE_NONE = 0;
130 protected static final int CHOICE_MODE_SINGLE = 1;
131 // Multiple selection mode is not supported by all Launcher actions atm
132 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700133
Michael Jurkae17e19c2010-09-28 11:01:39 -0700134 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700135 private ActionMode mActionMode;
136
Winson Chung04998342011-01-05 13:54:43 -0800137 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
138 // AllApps and Customize, and allows them to share holographic icons for the application view
139 // (which is in both).
140 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700141
Michael Jurka0142d492010-08-25 17:46:15 -0700142 // If true, syncPages and syncPageItems will be called to refresh pages
143 protected boolean mContentIsRefreshable = true;
144
145 // If true, modify alpha of neighboring pages as user scrolls left/right
146 protected boolean mFadeInAdjacentScreens = true;
147
148 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
149 // to switch to a new page
150 protected boolean mUsePagingTouchSlop = true;
151
152 // If true, the subclass should directly update mScrollX itself in its computeScroll method
153 // (SmoothPagedView does this)
154 protected boolean mDeferScrollUpdate = false;
155
Patrick Dubroy1262e362010-10-06 15:49:50 -0700156 protected boolean mIsPageMoving = false;
157
Winson Chung86f77532010-08-24 11:08:22 -0700158 public interface PageSwitchListener {
159 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 }
161
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 public PagedView(Context context) {
163 this(context, null);
164 }
165
166 public PagedView(Context context, AttributeSet attrs) {
167 this(context, attrs, 0);
168 }
169
170 public PagedView(Context context, AttributeSet attrs, int defStyle) {
171 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700172 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700173
Adam Cohen9c4949e2010-10-05 12:27:22 -0700174 TypedArray a = context.obtainStyledAttributes(attrs,
175 R.styleable.PagedView, defStyle, 0);
176 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
177 mPageLayoutPaddingTop = a.getDimensionPixelSize(
178 R.styleable.PagedView_pageLayoutPaddingTop, 10);
179 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
180 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
181 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
182 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
183 mPageLayoutPaddingRight = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700185 mPageLayoutWidthGap = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutWidthGap, -1);
187 mPageLayoutHeightGap = a.getDimensionPixelSize(
188 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700189 a.recycle();
190
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700192 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700193 }
194
195 /**
196 * Initializes various states for this workspace.
197 */
Michael Jurka0142d492010-08-25 17:46:15 -0700198 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700199 mDirtyPageContent = new ArrayList<Boolean>();
200 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800201 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700202 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700203 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700204
205 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
206 mTouchSlop = configuration.getScaledTouchSlop();
207 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
208 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
209 }
210
Winson Chung86f77532010-08-24 11:08:22 -0700211 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
212 mPageSwitchListener = pageSwitchListener;
213 if (mPageSwitchListener != null) {
214 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 }
216 }
217
218 /**
Winson Chung86f77532010-08-24 11:08:22 -0700219 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 *
Winson Chung86f77532010-08-24 11:08:22 -0700221 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 */
Winson Chung86f77532010-08-24 11:08:22 -0700223 int getCurrentPage() {
224 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 }
226
Winson Chung86f77532010-08-24 11:08:22 -0700227 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 return getChildCount();
229 }
230
Winson Chung86f77532010-08-24 11:08:22 -0700231 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 return getChildAt(index);
233 }
234
235 int getScrollWidth() {
236 return getWidth();
237 }
238
Adam Cohenbe909342011-01-28 17:02:58 -0800239 public int getTouchState() {
240 return mTouchState;
241 }
242
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800244 * Updates the scroll of the current page immediately to its final scroll position. We use this
245 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
246 * the previous tab page.
247 */
248 protected void updateCurrentPageScroll() {
249 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
250 scrollTo(newX, 0);
251 mScroller.setFinalX(newX);
252 }
253
254 /**
Winson Chung86f77532010-08-24 11:08:22 -0700255 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 */
Winson Chung86f77532010-08-24 11:08:22 -0700257 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800258 if (!mScroller.isFinished()) {
259 mScroller.abortAnimation();
260 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800261 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
262 // the default
263 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800264 return;
265 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700266
Winson Chung86f77532010-08-24 11:08:22 -0700267 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800268 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700269 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800270 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700271 }
272
Michael Jurka0142d492010-08-25 17:46:15 -0700273 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700274 if (mPageSwitchListener != null) {
275 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700276 }
277 }
278
Michael Jurkace7e05f2011-02-01 22:02:35 -0800279 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700280 mIsPageMoving = true;
281 onPageBeginMoving();
282 }
283
Michael Jurkace7e05f2011-02-01 22:02:35 -0800284 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700285 onPageEndMoving();
286 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700287 }
288
289 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700290 protected void onPageBeginMoving() {
291 }
292
293 // a method that subclasses can override to add behavior
294 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700295 }
296
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 /**
Winson Chung86f77532010-08-24 11:08:22 -0700298 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 *
300 * @param l The listener used to respond to long clicks.
301 */
302 @Override
303 public void setOnLongClickListener(OnLongClickListener l) {
304 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700305 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700307 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 }
309 }
310
311 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800312 public void scrollBy(int x, int y) {
313 scrollTo(mUnboundedScrollX + x, mScrollY + y);
314 }
315
316 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700317 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800318 mUnboundedScrollX = x;
319
320 if (x < 0) {
321 super.scrollTo(0, y);
322 if (mAllowOverScroll) {
323 overScroll(x);
324 }
325 } else if (x > mMaxScrollX) {
326 super.scrollTo(mMaxScrollX, y);
327 if (mAllowOverScroll) {
328 overScroll(x - mMaxScrollX);
329 }
330 } else {
331 super.scrollTo(x, y);
332 }
333
Michael Jurka0142d492010-08-25 17:46:15 -0700334 mTouchX = x;
335 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
336 }
337
338 // we moved this functionality to a helper function so SmoothPagedView can reuse it
339 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700340 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700341 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700343 invalidate();
344 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700345 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700346 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700347 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700348 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700349 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800350 // We don't want to trigger a page end moving unless the page has settled
351 // and the user has stopped scrolling
352 if (mTouchState == TOUCH_STATE_REST) {
353 pageEndMoving();
354 }
Michael Jurka0142d492010-08-25 17:46:15 -0700355 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
Michael Jurka0142d492010-08-25 17:46:15 -0700357 return false;
358 }
359
360 @Override
361 public void computeScroll() {
362 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
364
365 @Override
366 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
367 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
368 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
369 if (widthMode != MeasureSpec.EXACTLY) {
370 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
371 }
372
Adam Lesinski6b879f02010-11-04 16:15:23 -0700373 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
374 * of the All apps view on XLarge displays to not take up more space then it needs. Width
375 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
376 * each page to have the same width.
377 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700378 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700379 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
380 int maxChildHeight = 0;
381
382 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700383
384 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700385 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 final int childCount = getChildCount();
387 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700388 // disallowing padding in paged view (just pass 0)
389 final View child = getChildAt(i);
390 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
391
392 int childWidthMode;
393 if (lp.width == LayoutParams.WRAP_CONTENT) {
394 childWidthMode = MeasureSpec.AT_MOST;
395 } else {
396 childWidthMode = MeasureSpec.EXACTLY;
397 }
398
399 int childHeightMode;
400 if (lp.height == LayoutParams.WRAP_CONTENT) {
401 childHeightMode = MeasureSpec.AT_MOST;
402 } else {
403 childHeightMode = MeasureSpec.EXACTLY;
404 }
405
406 final int childWidthMeasureSpec =
407 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
408 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700409 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700410
411 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700412 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
413 }
414
415 if (heightMode == MeasureSpec.AT_MOST) {
416 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800418 if (childCount > 0) {
419 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
420 } else {
421 mMaxScrollX = 0;
422 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700423
424 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700425 }
426
Michael Jurka8c920dd2011-01-20 14:16:56 -0800427 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800428 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
429 int delta = newX - mScrollX;
430
Michael Jurka8c920dd2011-01-20 14:16:56 -0800431 final int pageCount = getChildCount();
432 for (int i = 0; i < pageCount; i++) {
433 View page = (View) getChildAt(i);
434 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800435 }
436 setCurrentPage(newCurrentPage);
437 }
438
Michael Jurka8c920dd2011-01-20 14:16:56 -0800439 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
440 // 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 -0800441 // tightens the layout accordingly
442 public void setLayoutScale(float childrenScale) {
443 mLayoutScale = childrenScale;
444
445 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
446 int childCount = getChildCount();
447 float childrenX[] = new float[childCount];
448 float childrenY[] = new float[childCount];
449 for (int i = 0; i < childCount; i++) {
450 final View child = getChildAt(i);
451 childrenX[i] = child.getX();
452 childrenY[i] = child.getY();
453 }
454 onLayout(false, mLeft, mTop, mRight, mBottom);
455 for (int i = 0; i < childCount; i++) {
456 final View child = getChildAt(i);
457 child.setX(childrenX[i]);
458 child.setY(childrenY[i]);
459 }
460 // Also, the page offset has changed (since the pages are now smaller);
461 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800462 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800463 }
464
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700466 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700467 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
468 setHorizontalScrollBarEnabled(false);
469 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
470 scrollTo(newX, 0);
471 mScroller.setFinalX(newX);
472 setHorizontalScrollBarEnabled(true);
473 mFirstLayout = false;
474 }
475
Adam Lesinski6b879f02010-11-04 16:15:23 -0700476 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700477 final int childCount = getChildCount();
478 int childLeft = 0;
479 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700480 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 }
482
483 for (int i = 0; i < childCount; i++) {
484 final View child = getChildAt(i);
485 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800486 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700487 final int childHeight = child.getMeasuredHeight();
488 int childTop = mPaddingTop;
489 if (mCenterPagesVertically) {
490 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
491 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800492
Adam Lesinski6b879f02010-11-04 16:15:23 -0700493 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800494 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700495 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 }
497 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800498 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
499 mFirstLayout = false;
500 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 }
502
Winson Chunge3193b92010-09-10 11:44:42 -0700503 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700504 if (mFadeInAdjacentScreens) {
505 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700506 int halfScreenSize = getMeasuredWidth() / 2;
507 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700508 final int childCount = getChildCount();
509 for (int i = 0; i < childCount; ++i) {
510 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800511 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700512 int halfChildWidth = (childWidth / 2);
513 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700514
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700515 // On the first layout, we may not have a width nor a proper offset, so for now
516 // we should just assume full page width (and calculate the offset according to
517 // that).
518 if (childWidth <= 0) {
519 childWidth = getMeasuredWidth();
520 childCenter = (i * childWidth) + (childWidth / 2);
521 }
522
Winson Chunge8878e32010-09-15 20:37:09 -0700523 int d = halfChildWidth;
524 int distanceFromScreenCenter = childCenter - screenCenter;
525 if (distanceFromScreenCenter > 0) {
526 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800527 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700528 }
Michael Jurka0142d492010-08-25 17:46:15 -0700529 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700530 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800531 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700532 }
Michael Jurka0142d492010-08-25 17:46:15 -0700533 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700534 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700535
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700536 // Preventing potential divide-by-zero
537 d = Math.max(1, d);
538
Winson Chunge8878e32010-09-15 20:37:09 -0700539 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
540 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
541 float alpha = 1.0f - dimAlpha;
542
Adam Cohenf34bab52010-09-30 14:11:56 -0700543 if (alpha < ALPHA_QUANTIZE_LEVEL) {
544 alpha = 0.0f;
545 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
546 alpha = 1.0f;
547 }
548
Michael Jurkaa40829a2011-02-16 18:02:45 -0800549 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
550 // this optimization causes alpha to not be properly updated sometimes (repro
551 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
552 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
553 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800554 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700555 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800556 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
Michael Jurka0142d492010-08-25 17:46:15 -0700558 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 }
560 }
Winson Chunge3193b92010-09-10 11:44:42 -0700561 }
562
Adam Cohenf34bab52010-09-30 14:11:56 -0700563 protected void screenScrolled(int screenCenter) {
564 }
565
Winson Chunge3193b92010-09-10 11:44:42 -0700566 @Override
567 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700568 int halfScreenSize = getMeasuredWidth() / 2;
569 int screenCenter = mScrollX + halfScreenSize;
570
571 if (screenCenter != mLastScreenCenter) {
572 screenScrolled(screenCenter);
573 updateAdjacentPagesAlpha();
574 mLastScreenCenter = screenCenter;
575 }
Michael Jurka0142d492010-08-25 17:46:15 -0700576
577 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700578 // As an optimization, this code assumes that all pages have the same width as the 0th
579 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700580 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700581 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800582 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700583 final int screenWidth = getMeasuredWidth();
584 int x = getRelativeChildOffset(0) + pageWidth;
585 int leftScreen = 0;
586 int rightScreen = 0;
587 while (x <= mScrollX) {
588 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800589 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700590 }
591 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800592 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700593 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800594 if (rightScreen < pageCount) {
595 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
596 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700597 }
598 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700599
Michael Jurkac4fb9172010-09-02 17:19:20 -0700600 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800601 // Clip to the bounds
602 canvas.save();
603 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
604 mScrollY + mBottom - mTop);
605
Michael Jurkac4fb9172010-09-02 17:19:20 -0700606 for (int i = leftScreen; i <= rightScreen; i++) {
607 drawChild(canvas, getChildAt(i), drawingTime);
608 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800609 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700610 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 }
612
613 @Override
614 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700615 int page = indexOfChild(child);
616 if (page != mCurrentPage || !mScroller.isFinished()) {
617 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 return true;
619 }
620 return false;
621 }
622
623 @Override
624 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700625 int focusablePage;
626 if (mNextPage != INVALID_PAGE) {
627 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700629 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700630 }
Winson Chung86f77532010-08-24 11:08:22 -0700631 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700632 if (v != null) {
633 v.requestFocus(direction, previouslyFocusedRect);
634 }
635 return false;
636 }
637
638 @Override
639 public boolean dispatchUnhandledMove(View focused, int direction) {
640 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700641 if (getCurrentPage() > 0) {
642 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 return true;
644 }
645 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700646 if (getCurrentPage() < getPageCount() - 1) {
647 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 return true;
649 }
650 }
651 return super.dispatchUnhandledMove(focused, direction);
652 }
653
654 @Override
655 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700656 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
657 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 }
659 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700660 if (mCurrentPage > 0) {
661 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 }
663 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700664 if (mCurrentPage < getPageCount() - 1) {
665 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700666 }
667 }
668 }
669
670 /**
671 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700672 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 *
Winson Chung86f77532010-08-24 11:08:22 -0700674 * This happens when live folders requery, and if they're off page, they
675 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 */
677 @Override
678 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700679 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 View v = focused;
681 while (true) {
682 if (v == current) {
683 super.focusableViewAvailable(focused);
684 return;
685 }
686 if (v == this) {
687 return;
688 }
689 ViewParent parent = v.getParent();
690 if (parent instanceof View) {
691 v = (View)v.getParent();
692 } else {
693 return;
694 }
695 }
696 }
697
698 /**
699 * {@inheritDoc}
700 */
701 @Override
702 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
703 if (disallowIntercept) {
704 // We need to make sure to cancel our long press if
705 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700706 final View currentPage = getChildAt(mCurrentPage);
707 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 }
709 super.requestDisallowInterceptTouchEvent(disallowIntercept);
710 }
711
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800712 /**
713 * Return true if a tap at (x, y) should trigger a flip to the previous page.
714 */
715 protected boolean hitsPreviousPage(float x, float y) {
716 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
717 }
718
719 /**
720 * Return true if a tap at (x, y) should trigger a flip to the next page.
721 */
722 protected boolean hitsNextPage(float x, float y) {
723 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
724 }
725
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 @Override
727 public boolean onInterceptTouchEvent(MotionEvent ev) {
728 /*
729 * This method JUST determines whether we want to intercept the motion.
730 * If we return true, onTouchEvent will be called and we do the actual
731 * scrolling there.
732 */
733
Winson Chung45e1d6e2010-11-09 17:19:49 -0800734 // Skip touch handling if there are no pages to swipe
735 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
736
Winson Chung321e9ee2010-08-09 13:37:56 -0700737 /*
738 * Shortcut the most recurring case: the user is in the dragging
739 * state and he is moving his finger. We want to intercept this
740 * motion.
741 */
742 final int action = ev.getAction();
743 if ((action == MotionEvent.ACTION_MOVE) &&
744 (mTouchState == TOUCH_STATE_SCROLLING)) {
745 return true;
746 }
747
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 switch (action & MotionEvent.ACTION_MASK) {
749 case MotionEvent.ACTION_MOVE: {
750 /*
751 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
752 * whether the user has moved far enough from his original down touch.
753 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700754 if (mActivePointerId != INVALID_POINTER) {
755 determineScrollingStart(ev);
756 break;
757 }
758 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
759 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
760 // i.e. fall through to the next case (don't break)
761 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
762 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 }
764
765 case MotionEvent.ACTION_DOWN: {
766 final float x = ev.getX();
767 final float y = ev.getY();
768 // Remember location of down touch
769 mDownMotionX = x;
770 mLastMotionX = x;
771 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800772 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800773 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 mActivePointerId = ev.getPointerId(0);
775 mAllowLongPress = true;
776
777 /*
778 * If being flinged and user touches the screen, initiate drag;
779 * otherwise don't. mScroller.isFinished should be false when
780 * being flinged.
781 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700782 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700783 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
784 if (finishedScrolling) {
785 mTouchState = TOUCH_STATE_REST;
786 mScroller.abortAnimation();
787 } else {
788 mTouchState = TOUCH_STATE_SCROLLING;
789 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700790
Winson Chung86f77532010-08-24 11:08:22 -0700791 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700792 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800793 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800795 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800797 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 mTouchState = TOUCH_STATE_NEXT_PAGE;
799 }
800 }
801 }
802 break;
803 }
804
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800806 onWallpaperTap(ev);
807 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 mTouchState = TOUCH_STATE_REST;
809 mAllowLongPress = false;
810 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 break;
812
813 case MotionEvent.ACTION_POINTER_UP:
814 onSecondaryPointerUp(ev);
815 break;
816 }
817
818 /*
819 * The only time we want to intercept motion events is if we are in the
820 * drag mode.
821 */
822 return mTouchState != TOUCH_STATE_REST;
823 }
824
Winson Chung80baf5a2010-08-09 16:03:15 -0700825 protected void animateClickFeedback(View v, final Runnable r) {
826 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800827 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
828 loadAnimator(mContext, R.anim.paged_view_click_feedback);
829 anim.setTarget(v);
830 anim.addListener(new AnimatorListenerAdapter() {
831 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700832 r.run();
833 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700834 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800835 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700836 }
837
Adam Cohenf8d28232011-02-01 21:47:00 -0800838 protected void determineScrollingStart(MotionEvent ev) {
839 determineScrollingStart(ev, 1.0f);
840 }
841
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 /*
843 * Determines if we should change the touch state to start scrolling after the
844 * user moves their touch point too far.
845 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800846 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 /*
848 * Locally do absolute value. mLastMotionX is set to the y value
849 * of the down event.
850 */
851 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
852 final float x = ev.getX(pointerIndex);
853 final float y = ev.getY(pointerIndex);
854 final int xDiff = (int) Math.abs(x - mLastMotionX);
855 final int yDiff = (int) Math.abs(y - mLastMotionY);
856
Adam Cohenf8d28232011-02-01 21:47:00 -0800857 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 boolean xPaged = xDiff > mPagingTouchSlop;
859 boolean xMoved = xDiff > touchSlop;
860 boolean yMoved = yDiff > touchSlop;
861
Adam Cohenf8d28232011-02-01 21:47:00 -0800862 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700863 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 // Scroll if the user moved far enough along the X axis
865 mTouchState = TOUCH_STATE_SCROLLING;
866 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800867 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700868 mTouchX = mScrollX;
869 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
870 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 }
872 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800873 cancelCurrentPageLongPress();
874 }
875 }
876
877 protected void cancelCurrentPageLongPress() {
878 if (mAllowLongPress) {
879 mAllowLongPress = false;
880 // Try canceling the long press. It could also have been scheduled
881 // by a distant descendant, so use the mAllowLongPress flag to block
882 // everything
883 final View currentPage = getPageAt(mCurrentPage);
884 if (currentPage != null) {
885 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 }
887 }
888 }
889
Adam Cohene0f66b52010-11-23 15:06:07 -0800890 // This curve determines how the effect of scrolling over the limits of the page dimishes
891 // as the user pulls further and further from the bounds
892 private float overScrollInfluenceCurve(float f) {
893 f -= 1.0f;
894 return f * f * f + 1.0f;
895 }
896
Adam Cohen68d73932010-11-15 10:50:58 -0800897 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800898 int screenSize = getMeasuredWidth();
899
900 float f = (amount / screenSize);
901
902 if (f == 0) return;
903 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
904
Adam Cohen7bfc9792011-01-28 13:52:37 -0800905 // Clamp this factor, f, to -1 < f < 1
906 if (Math.abs(f) >= 1) {
907 f /= Math.abs(f);
908 }
909
Adam Cohene0f66b52010-11-23 15:06:07 -0800910 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800911 if (amount < 0) {
912 mScrollX = overScrollAmount;
913 } else {
914 mScrollX = mMaxScrollX + overScrollAmount;
915 }
916 invalidate();
917 }
918
Michael Jurkac5b262c2011-01-12 20:24:50 -0800919 protected float maxOverScroll() {
920 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
921 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
922 float f = 1.0f;
923 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
924 return OVERSCROLL_DAMP_FACTOR * f;
925 }
926
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 @Override
928 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800929 // Skip touch handling if there are no pages to swipe
930 if (getChildCount() <= 0) return super.onTouchEvent(ev);
931
Michael Jurkab8f06722010-10-10 15:58:46 -0700932 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700933
934 final int action = ev.getAction();
935
936 switch (action & MotionEvent.ACTION_MASK) {
937 case MotionEvent.ACTION_DOWN:
938 /*
939 * If being flinged and user touches, stop the fling. isFinished
940 * will be false if being flinged.
941 */
942 if (!mScroller.isFinished()) {
943 mScroller.abortAnimation();
944 }
945
946 // Remember where the motion event started
947 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800948 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800949 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700951 if (mTouchState == TOUCH_STATE_SCROLLING) {
952 pageBeginMoving();
953 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 break;
955
956 case MotionEvent.ACTION_MOVE:
957 if (mTouchState == TOUCH_STATE_SCROLLING) {
958 // Scroll to follow the motion event
959 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
960 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800961 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700962
Adam Cohenaefd4e12011-02-14 16:39:38 -0800963 mTotalMotionX += Math.abs(deltaX);
964
Winson Chungc0844aa2011-02-02 15:25:58 -0800965 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
966 // keep the remainder because we are actually testing if we've moved from the last
967 // scrolled position (which is discrete).
968 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800969 mTouchX += deltaX;
970 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
971 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800972 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800973 } else {
974 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800976 mLastMotionX = x;
977 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 } else {
979 awakenScrollBars();
980 }
Adam Cohen564976a2010-10-13 18:52:07 -0700981 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 determineScrollingStart(ev);
983 }
984 break;
985
986 case MotionEvent.ACTION_UP:
987 if (mTouchState == TOUCH_STATE_SCROLLING) {
988 final int activePointerId = mActivePointerId;
989 final int pointerIndex = ev.findPointerIndex(activePointerId);
990 final float x = ev.getX(pointerIndex);
991 final VelocityTracker velocityTracker = mVelocityTracker;
992 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
993 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700994 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -0800995 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -0700996 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800997
Adam Cohenb64cb5a2011-02-15 13:53:42 -0800998 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
999
Adam Cohenaefd4e12011-02-14 16:39:38 -08001000 // In the case that the page is moved far to one direction and then is flung
1001 // in the opposite direction, we use a threshold to determine whether we should
1002 // just return to the starting page, or if we should skip one further.
1003 boolean returnToOriginalPage = false;
1004 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001005 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001006 Math.signum(velocityX) != Math.signum(deltaX)) {
1007 returnToOriginalPage = true;
1008 }
1009
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001010 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001011 Math.abs(velocityX) > snapVelocity;
1012
1013 int finalPage;
1014 // We give flings precedence over large moves, which is why we short-circuit our
1015 // test for a large move if a fling has been registered. That is, a large
1016 // move to the left and fling to the right will register as a fling to the right.
1017 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1018 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1019 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1020 snapToPageWithVelocity(finalPage, velocityX);
1021 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1022 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001023 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001024 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1025 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001026 } else {
1027 snapToDestination();
1028 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001029 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 // at this point we have not moved beyond the touch slop
1031 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1032 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001033 int nextPage = Math.max(0, mCurrentPage - 1);
1034 if (nextPage != mCurrentPage) {
1035 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 } else {
1037 snapToDestination();
1038 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001039 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 // at this point we have not moved beyond the touch slop
1041 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1042 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001043 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1044 if (nextPage != mCurrentPage) {
1045 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 } else {
1047 snapToDestination();
1048 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001049 } else {
1050 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 }
1052 mTouchState = TOUCH_STATE_REST;
1053 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001054 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 break;
1056
1057 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001058 if (mTouchState == TOUCH_STATE_SCROLLING) {
1059 snapToDestination();
1060 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001061 mTouchState = TOUCH_STATE_REST;
1062 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001063 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 break;
1065
1066 case MotionEvent.ACTION_POINTER_UP:
1067 onSecondaryPointerUp(ev);
1068 break;
1069 }
1070
1071 return true;
1072 }
1073
Michael Jurkab8f06722010-10-10 15:58:46 -07001074 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1075 if (mVelocityTracker == null) {
1076 mVelocityTracker = VelocityTracker.obtain();
1077 }
1078 mVelocityTracker.addMovement(ev);
1079 }
1080
1081 private void releaseVelocityTracker() {
1082 if (mVelocityTracker != null) {
1083 mVelocityTracker.recycle();
1084 mVelocityTracker = null;
1085 }
1086 }
1087
Winson Chung321e9ee2010-08-09 13:37:56 -07001088 private void onSecondaryPointerUp(MotionEvent ev) {
1089 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1090 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1091 final int pointerId = ev.getPointerId(pointerIndex);
1092 if (pointerId == mActivePointerId) {
1093 // This was our active pointer going up. Choose a new
1094 // active pointer and adjust accordingly.
1095 // TODO: Make this decision more intelligent.
1096 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1097 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1098 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001099 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001100 mActivePointerId = ev.getPointerId(newPointerIndex);
1101 if (mVelocityTracker != null) {
1102 mVelocityTracker.clear();
1103 }
1104 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001105 if (mTouchState == TOUCH_STATE_REST) {
1106 onWallpaperTap(ev);
1107 }
1108 }
1109
1110 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 }
1112
1113 @Override
1114 public void requestChildFocus(View child, View focused) {
1115 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001116 int page = indexOfChild(child);
1117 if (page >= 0 && !isInTouchMode()) {
1118 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 }
1120 }
1121
Winson Chunge3193b92010-09-10 11:44:42 -07001122 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1123 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001124 int left;
1125 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001126 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001127 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001128 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001129 if (left <= relativeOffset && relativeOffset <= right) {
1130 return i;
1131 }
Winson Chunge3193b92010-09-10 11:44:42 -07001132 }
1133 return -1;
1134 }
1135
Winson Chung321e9ee2010-08-09 13:37:56 -07001136 protected int getRelativeChildOffset(int index) {
1137 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1138 }
1139
1140 protected int getChildOffset(int index) {
1141 if (getChildCount() == 0)
1142 return 0;
1143
1144 int offset = getRelativeChildOffset(0);
1145 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001146 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001147 }
1148 return offset;
1149 }
1150
Michael Jurkad3ef3062010-11-23 16:23:58 -08001151 protected int getScaledMeasuredWidth(View child) {
1152 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1153 }
1154
Adam Cohend19d3ca2010-09-15 14:43:42 -07001155 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001156 int minDistanceFromScreenCenter = getMeasuredWidth();
1157 int minDistanceFromScreenCenterIndex = -1;
1158 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1159 final int childCount = getChildCount();
1160 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001161 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001163 int halfChildWidth = (childWidth / 2);
1164 int childCenter = getChildOffset(i) + halfChildWidth;
1165 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1166 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1167 minDistanceFromScreenCenter = distanceFromScreenCenter;
1168 minDistanceFromScreenCenterIndex = i;
1169 }
1170 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001171 return minDistanceFromScreenCenterIndex;
1172 }
1173
1174 protected void snapToDestination() {
1175 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001176 }
1177
Adam Cohene0f66b52010-11-23 15:06:07 -08001178 private static class ScrollInterpolator implements Interpolator {
1179 public ScrollInterpolator() {
1180 }
1181
1182 public float getInterpolation(float t) {
1183 t -= 1.0f;
1184 return t*t*t*t*t + 1;
1185 }
1186 }
1187
1188 // We want the duration of the page snap animation to be influenced by the distance that
1189 // the screen has to travel, however, we don't want this duration to be effected in a
1190 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1191 // of travel has on the overall snap duration.
1192 float distanceInfluenceForSnapDuration(float f) {
1193 f -= 0.5f; // center the values about 0.
1194 f *= 0.3f * Math.PI / 2.0f;
1195 return (float) Math.sin(f);
1196 }
1197
Michael Jurka0142d492010-08-25 17:46:15 -07001198 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001199 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1200 int halfScreenSize = getMeasuredWidth() / 2;
1201
1202 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1203 int delta = newX - mUnboundedScrollX;
1204 int duration = 0;
1205
1206 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1207 // If the velocity is low enough, then treat this more as an automatic page advance
1208 // as opposed to an apparent physical response to flinging
1209 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1210 return;
1211 }
1212
1213 // Here we compute a "distance" that will be used in the computation of the overall
1214 // snap duration. This is a function of the actual distance that needs to be traveled;
1215 // we keep this value close to half screen size in order to reduce the variance in snap
1216 // duration as a function of the distance the page needs to travel.
1217 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1218 float distance = halfScreenSize + halfScreenSize *
1219 distanceInfluenceForSnapDuration(distanceRatio);
1220
1221 velocity = Math.abs(velocity);
1222 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1223
1224 // we want the page's snap velocity to approximately match the velocity at which the
1225 // user flings, so we scale the duration by a value near to the derivative of the scroll
1226 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1227 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1228
1229 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001230 }
1231
1232 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001233 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235
Michael Jurka0142d492010-08-25 17:46:15 -07001236 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001237 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001238
Winson Chung86f77532010-08-24 11:08:22 -07001239 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001240 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001242 snapToPage(whichPage, delta, duration);
1243 }
1244
1245 protected void snapToPage(int whichPage, int delta, int duration) {
1246 mNextPage = whichPage;
1247
1248 View focusedChild = getFocusedChild();
1249 if (focusedChild != null && whichPage != mCurrentPage &&
1250 focusedChild == getChildAt(mCurrentPage)) {
1251 focusedChild.clearFocus();
1252 }
1253
1254 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 awakenScrollBars(duration);
1256 if (duration == 0) {
1257 duration = Math.abs(delta);
1258 }
1259
1260 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001261 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001262
1263 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001264 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001265 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 invalidate();
1267 }
1268
Winson Chung321e9ee2010-08-09 13:37:56 -07001269 public void scrollLeft() {
1270 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001271 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001273 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 }
1275 }
1276
1277 public void scrollRight() {
1278 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001279 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001280 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001281 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 }
1283 }
1284
Winson Chung86f77532010-08-24 11:08:22 -07001285 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 int result = -1;
1287 if (v != null) {
1288 ViewParent vp = v.getParent();
1289 int count = getChildCount();
1290 for (int i = 0; i < count; i++) {
1291 if (vp == getChildAt(i)) {
1292 return i;
1293 }
1294 }
1295 }
1296 return result;
1297 }
1298
1299 /**
1300 * @return True is long presses are still allowed for the current touch
1301 */
1302 public boolean allowLongPress() {
1303 return mAllowLongPress;
1304 }
1305
Michael Jurka0142d492010-08-25 17:46:15 -07001306 /**
1307 * Set true to allow long-press events to be triggered, usually checked by
1308 * {@link Launcher} to accept or block dpad-initiated long-presses.
1309 */
1310 public void setAllowLongPress(boolean allowLongPress) {
1311 mAllowLongPress = allowLongPress;
1312 }
1313
Winson Chung321e9ee2010-08-09 13:37:56 -07001314 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001315 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001316
1317 SavedState(Parcelable superState) {
1318 super(superState);
1319 }
1320
1321 private SavedState(Parcel in) {
1322 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001323 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 }
1325
1326 @Override
1327 public void writeToParcel(Parcel out, int flags) {
1328 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001329 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001330 }
1331
1332 public static final Parcelable.Creator<SavedState> CREATOR =
1333 new Parcelable.Creator<SavedState>() {
1334 public SavedState createFromParcel(Parcel in) {
1335 return new SavedState(in);
1336 }
1337
1338 public SavedState[] newArray(int size) {
1339 return new SavedState[size];
1340 }
1341 };
1342 }
1343
Winson Chung86f77532010-08-24 11:08:22 -07001344 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001345 if (mContentIsRefreshable) {
1346 final int count = getChildCount();
1347 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001348 int lowerPageBound = getAssociatedLowerPageBound(page);
1349 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001350 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001351 Page layout = (Page) getChildAt(i);
1352 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001353 if (lowerPageBound <= i && i <= upperPageBound) {
1354 if (mDirtyPageContent.get(i)) {
1355 syncPageItems(i);
1356 mDirtyPageContent.set(i, false);
1357 }
1358 } else {
1359 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001360 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001361 }
1362 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001363 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001364 }
1365 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001366 }
1367 }
1368
Winson Chunge3193b92010-09-10 11:44:42 -07001369 protected int getAssociatedLowerPageBound(int page) {
1370 return Math.max(0, page - 1);
1371 }
1372 protected int getAssociatedUpperPageBound(int page) {
1373 final int count = getChildCount();
1374 return Math.min(page + 1, count - 1);
1375 }
1376
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001377 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001378 if (isChoiceMode(CHOICE_MODE_NONE)) {
1379 mChoiceMode = mode;
1380 mActionMode = startActionMode(callback);
1381 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001382 }
1383
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001384 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001385 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001386 mChoiceMode = CHOICE_MODE_NONE;
1387 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001388 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001389 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001390 }
1391 }
1392
1393 protected boolean isChoiceMode(int mode) {
1394 return mChoiceMode == mode;
1395 }
1396
1397 protected ArrayList<Checkable> getCheckedGrandchildren() {
1398 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1399 final int childCount = getChildCount();
1400 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001401 Page layout = (Page) getChildAt(i);
1402 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001403 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001404 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001405 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001406 checked.add((Checkable) v);
1407 }
1408 }
1409 }
1410 return checked;
1411 }
1412
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001413 /**
1414 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1415 * Otherwise, returns null.
1416 */
1417 protected Checkable getSingleCheckedGrandchild() {
1418 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1419 final int childCount = getChildCount();
1420 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001421 Page layout = (Page) getChildAt(i);
1422 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001423 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001424 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001425 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1426 return (Checkable) v;
1427 }
1428 }
1429 }
1430 }
1431 return null;
1432 }
1433
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001434 public Object getChosenItem() {
1435 View checkedView = (View) getSingleCheckedGrandchild();
1436 if (checkedView != null) {
1437 return checkedView.getTag();
1438 }
1439 return null;
1440 }
1441
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001442 protected void resetCheckedGrandchildren() {
1443 // loop through children, and set all of their children to _not_ be checked
1444 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1445 for (int i = 0; i < checked.size(); ++i) {
1446 final Checkable c = checked.get(i);
1447 c.setChecked(false);
1448 }
1449 }
1450
Winson Chunga12a2502010-12-20 14:41:35 -08001451 public void setRestorePage(int restorePage) {
1452 mRestorePage = restorePage;
1453 }
1454
Winson Chung86f77532010-08-24 11:08:22 -07001455 /**
1456 * This method is called ONLY to synchronize the number of pages that the paged view has.
1457 * To actually fill the pages with information, implement syncPageItems() below. It is
1458 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1459 * and therefore, individual page items do not need to be updated in this method.
1460 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001462
1463 /**
1464 * This method is called to synchronize the items that are on a particular page. If views on
1465 * the page can be reused, then they should be updated within this method.
1466 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001467 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001468
Winson Chung321e9ee2010-08-09 13:37:56 -07001469 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001470 if (mContentIsRefreshable) {
1471 // Update all the pages
1472 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001473
Michael Jurka0142d492010-08-25 17:46:15 -07001474 // Mark each of the pages as dirty
1475 final int count = getChildCount();
1476 mDirtyPageContent.clear();
1477 for (int i = 0; i < count; ++i) {
1478 mDirtyPageContent.add(true);
1479 }
1480
Winson Chunga12a2502010-12-20 14:41:35 -08001481 // Use the restore page if necessary
1482 if (mRestorePage > -1) {
1483 mCurrentPage = mRestorePage;
1484 mRestorePage = -1;
1485 }
1486
Michael Jurka0142d492010-08-25 17:46:15 -07001487 // Load any pages that are necessary for the current window of views
1488 loadAssociatedPages(mCurrentPage);
1489 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001490 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001491 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001492 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 }
1494}