blob: 66383cb6b3c8e6692e11d2807db0f374ce212d0a [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 Chung03929772011-02-23 17:07:10 -0800503 protected void forceUpdateAdjacentPagesAlpha() {
504 mDirtyPageAlpha = true;
505 updateAdjacentPagesAlpha();
506 }
507
Winson Chunge3193b92010-09-10 11:44:42 -0700508 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700509 if (mFadeInAdjacentScreens) {
510 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700511 int halfScreenSize = getMeasuredWidth() / 2;
512 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700513 final int childCount = getChildCount();
514 for (int i = 0; i < childCount; ++i) {
515 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800516 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700517 int halfChildWidth = (childWidth / 2);
518 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700519
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700520 // On the first layout, we may not have a width nor a proper offset, so for now
521 // we should just assume full page width (and calculate the offset according to
522 // that).
523 if (childWidth <= 0) {
524 childWidth = getMeasuredWidth();
525 childCenter = (i * childWidth) + (childWidth / 2);
526 }
527
Winson Chunge8878e32010-09-15 20:37:09 -0700528 int d = halfChildWidth;
529 int distanceFromScreenCenter = childCenter - screenCenter;
530 if (distanceFromScreenCenter > 0) {
531 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800532 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700533 }
Michael Jurka0142d492010-08-25 17:46:15 -0700534 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700535 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800536 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700537 }
Michael Jurka0142d492010-08-25 17:46:15 -0700538 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700539 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700540
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700541 // Preventing potential divide-by-zero
542 d = Math.max(1, d);
543
Winson Chunge8878e32010-09-15 20:37:09 -0700544 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
545 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
546 float alpha = 1.0f - dimAlpha;
547
Adam Cohenf34bab52010-09-30 14:11:56 -0700548 if (alpha < ALPHA_QUANTIZE_LEVEL) {
549 alpha = 0.0f;
550 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
551 alpha = 1.0f;
552 }
553
Michael Jurkaa40829a2011-02-16 18:02:45 -0800554 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
555 // this optimization causes alpha to not be properly updated sometimes (repro
556 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
557 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
558 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800559 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700560 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800561 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
Michael Jurka0142d492010-08-25 17:46:15 -0700563 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 }
565 }
Winson Chunge3193b92010-09-10 11:44:42 -0700566 }
567
Adam Cohenf34bab52010-09-30 14:11:56 -0700568 protected void screenScrolled(int screenCenter) {
569 }
570
Winson Chunge3193b92010-09-10 11:44:42 -0700571 @Override
572 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700573 int halfScreenSize = getMeasuredWidth() / 2;
574 int screenCenter = mScrollX + halfScreenSize;
575
576 if (screenCenter != mLastScreenCenter) {
577 screenScrolled(screenCenter);
578 updateAdjacentPagesAlpha();
579 mLastScreenCenter = screenCenter;
580 }
Michael Jurka0142d492010-08-25 17:46:15 -0700581
582 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700583 // As an optimization, this code assumes that all pages have the same width as the 0th
584 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700585 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700586 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800587 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700588 final int screenWidth = getMeasuredWidth();
589 int x = getRelativeChildOffset(0) + pageWidth;
590 int leftScreen = 0;
591 int rightScreen = 0;
592 while (x <= mScrollX) {
593 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800594 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700595 }
596 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800597 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700598 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800599 if (rightScreen < pageCount) {
600 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
601 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700602 }
603 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700604
Michael Jurkac4fb9172010-09-02 17:19:20 -0700605 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800606 // Clip to the bounds
607 canvas.save();
608 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
609 mScrollY + mBottom - mTop);
610
Michael Jurkac4fb9172010-09-02 17:19:20 -0700611 for (int i = leftScreen; i <= rightScreen; i++) {
612 drawChild(canvas, getChildAt(i), drawingTime);
613 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800614 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700615 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700616 }
617
618 @Override
619 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700620 int page = indexOfChild(child);
621 if (page != mCurrentPage || !mScroller.isFinished()) {
622 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700623 return true;
624 }
625 return false;
626 }
627
628 @Override
629 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700630 int focusablePage;
631 if (mNextPage != INVALID_PAGE) {
632 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700633 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700634 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 }
Winson Chung86f77532010-08-24 11:08:22 -0700636 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700637 if (v != null) {
638 v.requestFocus(direction, previouslyFocusedRect);
639 }
640 return false;
641 }
642
643 @Override
644 public boolean dispatchUnhandledMove(View focused, int direction) {
645 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700646 if (getCurrentPage() > 0) {
647 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700648 return true;
649 }
650 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700651 if (getCurrentPage() < getPageCount() - 1) {
652 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 return true;
654 }
655 }
656 return super.dispatchUnhandledMove(focused, direction);
657 }
658
659 @Override
660 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700661 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
662 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700663 }
664 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700665 if (mCurrentPage > 0) {
666 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 }
668 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700669 if (mCurrentPage < getPageCount() - 1) {
670 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700671 }
672 }
673 }
674
675 /**
676 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700677 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700678 *
Winson Chung86f77532010-08-24 11:08:22 -0700679 * This happens when live folders requery, and if they're off page, they
680 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 */
682 @Override
683 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700684 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 View v = focused;
686 while (true) {
687 if (v == current) {
688 super.focusableViewAvailable(focused);
689 return;
690 }
691 if (v == this) {
692 return;
693 }
694 ViewParent parent = v.getParent();
695 if (parent instanceof View) {
696 v = (View)v.getParent();
697 } else {
698 return;
699 }
700 }
701 }
702
703 /**
704 * {@inheritDoc}
705 */
706 @Override
707 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
708 if (disallowIntercept) {
709 // We need to make sure to cancel our long press if
710 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700711 final View currentPage = getChildAt(mCurrentPage);
712 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 }
714 super.requestDisallowInterceptTouchEvent(disallowIntercept);
715 }
716
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800717 /**
718 * Return true if a tap at (x, y) should trigger a flip to the previous page.
719 */
720 protected boolean hitsPreviousPage(float x, float y) {
721 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
722 }
723
724 /**
725 * Return true if a tap at (x, y) should trigger a flip to the next page.
726 */
727 protected boolean hitsNextPage(float x, float y) {
728 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
729 }
730
Winson Chung321e9ee2010-08-09 13:37:56 -0700731 @Override
732 public boolean onInterceptTouchEvent(MotionEvent ev) {
733 /*
734 * This method JUST determines whether we want to intercept the motion.
735 * If we return true, onTouchEvent will be called and we do the actual
736 * scrolling there.
737 */
738
Winson Chung45e1d6e2010-11-09 17:19:49 -0800739 // Skip touch handling if there are no pages to swipe
740 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
741
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 /*
743 * Shortcut the most recurring case: the user is in the dragging
744 * state and he is moving his finger. We want to intercept this
745 * motion.
746 */
747 final int action = ev.getAction();
748 if ((action == MotionEvent.ACTION_MOVE) &&
749 (mTouchState == TOUCH_STATE_SCROLLING)) {
750 return true;
751 }
752
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 switch (action & MotionEvent.ACTION_MASK) {
754 case MotionEvent.ACTION_MOVE: {
755 /*
756 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
757 * whether the user has moved far enough from his original down touch.
758 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700759 if (mActivePointerId != INVALID_POINTER) {
760 determineScrollingStart(ev);
761 break;
762 }
763 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
764 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
765 // i.e. fall through to the next case (don't break)
766 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
767 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
769
770 case MotionEvent.ACTION_DOWN: {
771 final float x = ev.getX();
772 final float y = ev.getY();
773 // Remember location of down touch
774 mDownMotionX = x;
775 mLastMotionX = x;
776 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800777 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800778 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 mActivePointerId = ev.getPointerId(0);
780 mAllowLongPress = true;
781
782 /*
783 * If being flinged and user touches the screen, initiate drag;
784 * otherwise don't. mScroller.isFinished should be false when
785 * being flinged.
786 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700787 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700788 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
789 if (finishedScrolling) {
790 mTouchState = TOUCH_STATE_REST;
791 mScroller.abortAnimation();
792 } else {
793 mTouchState = TOUCH_STATE_SCROLLING;
794 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700795
Winson Chung86f77532010-08-24 11:08:22 -0700796 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800798 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800800 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800802 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 mTouchState = TOUCH_STATE_NEXT_PAGE;
804 }
805 }
806 }
807 break;
808 }
809
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800811 onWallpaperTap(ev);
812 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 mTouchState = TOUCH_STATE_REST;
814 mAllowLongPress = false;
815 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 break;
817
818 case MotionEvent.ACTION_POINTER_UP:
819 onSecondaryPointerUp(ev);
820 break;
821 }
822
823 /*
824 * The only time we want to intercept motion events is if we are in the
825 * drag mode.
826 */
827 return mTouchState != TOUCH_STATE_REST;
828 }
829
Winson Chung80baf5a2010-08-09 16:03:15 -0700830 protected void animateClickFeedback(View v, final Runnable r) {
831 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800832 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
833 loadAnimator(mContext, R.anim.paged_view_click_feedback);
834 anim.setTarget(v);
835 anim.addListener(new AnimatorListenerAdapter() {
836 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700837 r.run();
838 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700839 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800840 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700841 }
842
Adam Cohenf8d28232011-02-01 21:47:00 -0800843 protected void determineScrollingStart(MotionEvent ev) {
844 determineScrollingStart(ev, 1.0f);
845 }
846
Winson Chung321e9ee2010-08-09 13:37:56 -0700847 /*
848 * Determines if we should change the touch state to start scrolling after the
849 * user moves their touch point too far.
850 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800851 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700852 /*
853 * Locally do absolute value. mLastMotionX is set to the y value
854 * of the down event.
855 */
856 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
857 final float x = ev.getX(pointerIndex);
858 final float y = ev.getY(pointerIndex);
859 final int xDiff = (int) Math.abs(x - mLastMotionX);
860 final int yDiff = (int) Math.abs(y - mLastMotionY);
861
Adam Cohenf8d28232011-02-01 21:47:00 -0800862 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 boolean xPaged = xDiff > mPagingTouchSlop;
864 boolean xMoved = xDiff > touchSlop;
865 boolean yMoved = yDiff > touchSlop;
866
Adam Cohenf8d28232011-02-01 21:47:00 -0800867 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700868 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 // Scroll if the user moved far enough along the X axis
870 mTouchState = TOUCH_STATE_SCROLLING;
871 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800872 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700873 mTouchX = mScrollX;
874 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
875 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700876 }
877 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800878 cancelCurrentPageLongPress();
879 }
880 }
881
882 protected void cancelCurrentPageLongPress() {
883 if (mAllowLongPress) {
884 mAllowLongPress = false;
885 // Try canceling the long press. It could also have been scheduled
886 // by a distant descendant, so use the mAllowLongPress flag to block
887 // everything
888 final View currentPage = getPageAt(mCurrentPage);
889 if (currentPage != null) {
890 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 }
892 }
893 }
894
Adam Cohene0f66b52010-11-23 15:06:07 -0800895 // This curve determines how the effect of scrolling over the limits of the page dimishes
896 // as the user pulls further and further from the bounds
897 private float overScrollInfluenceCurve(float f) {
898 f -= 1.0f;
899 return f * f * f + 1.0f;
900 }
901
Adam Cohen68d73932010-11-15 10:50:58 -0800902 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800903 int screenSize = getMeasuredWidth();
904
905 float f = (amount / screenSize);
906
907 if (f == 0) return;
908 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
909
Adam Cohen7bfc9792011-01-28 13:52:37 -0800910 // Clamp this factor, f, to -1 < f < 1
911 if (Math.abs(f) >= 1) {
912 f /= Math.abs(f);
913 }
914
Adam Cohene0f66b52010-11-23 15:06:07 -0800915 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800916 if (amount < 0) {
917 mScrollX = overScrollAmount;
918 } else {
919 mScrollX = mMaxScrollX + overScrollAmount;
920 }
921 invalidate();
922 }
923
Michael Jurkac5b262c2011-01-12 20:24:50 -0800924 protected float maxOverScroll() {
925 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
926 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
927 float f = 1.0f;
928 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
929 return OVERSCROLL_DAMP_FACTOR * f;
930 }
931
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 @Override
933 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800934 // Skip touch handling if there are no pages to swipe
935 if (getChildCount() <= 0) return super.onTouchEvent(ev);
936
Michael Jurkab8f06722010-10-10 15:58:46 -0700937 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700938
939 final int action = ev.getAction();
940
941 switch (action & MotionEvent.ACTION_MASK) {
942 case MotionEvent.ACTION_DOWN:
943 /*
944 * If being flinged and user touches, stop the fling. isFinished
945 * will be false if being flinged.
946 */
947 if (!mScroller.isFinished()) {
948 mScroller.abortAnimation();
949 }
950
951 // Remember where the motion event started
952 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800953 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800954 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700956 if (mTouchState == TOUCH_STATE_SCROLLING) {
957 pageBeginMoving();
958 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 break;
960
961 case MotionEvent.ACTION_MOVE:
962 if (mTouchState == TOUCH_STATE_SCROLLING) {
963 // Scroll to follow the motion event
964 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
965 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800966 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700967
Adam Cohenaefd4e12011-02-14 16:39:38 -0800968 mTotalMotionX += Math.abs(deltaX);
969
Winson Chungc0844aa2011-02-02 15:25:58 -0800970 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
971 // keep the remainder because we are actually testing if we've moved from the last
972 // scrolled position (which is discrete).
973 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800974 mTouchX += deltaX;
975 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
976 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800977 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800978 } else {
979 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800981 mLastMotionX = x;
982 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 } else {
984 awakenScrollBars();
985 }
Adam Cohen564976a2010-10-13 18:52:07 -0700986 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 determineScrollingStart(ev);
988 }
989 break;
990
991 case MotionEvent.ACTION_UP:
992 if (mTouchState == TOUCH_STATE_SCROLLING) {
993 final int activePointerId = mActivePointerId;
994 final int pointerIndex = ev.findPointerIndex(activePointerId);
995 final float x = ev.getX(pointerIndex);
996 final VelocityTracker velocityTracker = mVelocityTracker;
997 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
998 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700999 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001000 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001001 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001002
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001003 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1004
Adam Cohenaefd4e12011-02-14 16:39:38 -08001005 // In the case that the page is moved far to one direction and then is flung
1006 // in the opposite direction, we use a threshold to determine whether we should
1007 // just return to the starting page, or if we should skip one further.
1008 boolean returnToOriginalPage = false;
1009 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001010 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001011 Math.signum(velocityX) != Math.signum(deltaX)) {
1012 returnToOriginalPage = true;
1013 }
1014
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001015 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001016 Math.abs(velocityX) > snapVelocity;
1017
1018 int finalPage;
1019 // We give flings precedence over large moves, which is why we short-circuit our
1020 // test for a large move if a fling has been registered. That is, a large
1021 // move to the left and fling to the right will register as a fling to the right.
1022 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1023 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1024 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1025 snapToPageWithVelocity(finalPage, velocityX);
1026 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1027 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001028 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001029 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1030 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 } else {
1032 snapToDestination();
1033 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001034 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 // at this point we have not moved beyond the touch slop
1036 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1037 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001038 int nextPage = Math.max(0, mCurrentPage - 1);
1039 if (nextPage != mCurrentPage) {
1040 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001041 } else {
1042 snapToDestination();
1043 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001044 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 // at this point we have not moved beyond the touch slop
1046 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1047 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001048 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1049 if (nextPage != mCurrentPage) {
1050 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001051 } else {
1052 snapToDestination();
1053 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001054 } else {
1055 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 }
1057 mTouchState = TOUCH_STATE_REST;
1058 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001059 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 break;
1061
1062 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001063 if (mTouchState == TOUCH_STATE_SCROLLING) {
1064 snapToDestination();
1065 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 mTouchState = TOUCH_STATE_REST;
1067 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001068 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 break;
1070
1071 case MotionEvent.ACTION_POINTER_UP:
1072 onSecondaryPointerUp(ev);
1073 break;
1074 }
1075
1076 return true;
1077 }
1078
Michael Jurkab8f06722010-10-10 15:58:46 -07001079 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1080 if (mVelocityTracker == null) {
1081 mVelocityTracker = VelocityTracker.obtain();
1082 }
1083 mVelocityTracker.addMovement(ev);
1084 }
1085
1086 private void releaseVelocityTracker() {
1087 if (mVelocityTracker != null) {
1088 mVelocityTracker.recycle();
1089 mVelocityTracker = null;
1090 }
1091 }
1092
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 private void onSecondaryPointerUp(MotionEvent ev) {
1094 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1095 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1096 final int pointerId = ev.getPointerId(pointerIndex);
1097 if (pointerId == mActivePointerId) {
1098 // This was our active pointer going up. Choose a new
1099 // active pointer and adjust accordingly.
1100 // TODO: Make this decision more intelligent.
1101 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1102 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1103 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001104 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001105 mActivePointerId = ev.getPointerId(newPointerIndex);
1106 if (mVelocityTracker != null) {
1107 mVelocityTracker.clear();
1108 }
1109 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001110 if (mTouchState == TOUCH_STATE_REST) {
1111 onWallpaperTap(ev);
1112 }
1113 }
1114
1115 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001116 }
1117
1118 @Override
1119 public void requestChildFocus(View child, View focused) {
1120 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001121 int page = indexOfChild(child);
1122 if (page >= 0 && !isInTouchMode()) {
1123 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 }
1125 }
1126
Winson Chunge3193b92010-09-10 11:44:42 -07001127 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1128 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001129 int left;
1130 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001131 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001132 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001133 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001134 if (left <= relativeOffset && relativeOffset <= right) {
1135 return i;
1136 }
Winson Chunge3193b92010-09-10 11:44:42 -07001137 }
1138 return -1;
1139 }
1140
Winson Chung321e9ee2010-08-09 13:37:56 -07001141 protected int getRelativeChildOffset(int index) {
1142 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1143 }
1144
1145 protected int getChildOffset(int index) {
1146 if (getChildCount() == 0)
1147 return 0;
1148
1149 int offset = getRelativeChildOffset(0);
1150 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001151 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001152 }
1153 return offset;
1154 }
1155
Michael Jurkad3ef3062010-11-23 16:23:58 -08001156 protected int getScaledMeasuredWidth(View child) {
1157 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1158 }
1159
Adam Cohend19d3ca2010-09-15 14:43:42 -07001160 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 int minDistanceFromScreenCenter = getMeasuredWidth();
1162 int minDistanceFromScreenCenterIndex = -1;
1163 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1164 final int childCount = getChildCount();
1165 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001166 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001167 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001168 int halfChildWidth = (childWidth / 2);
1169 int childCenter = getChildOffset(i) + halfChildWidth;
1170 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1171 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1172 minDistanceFromScreenCenter = distanceFromScreenCenter;
1173 minDistanceFromScreenCenterIndex = i;
1174 }
1175 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001176 return minDistanceFromScreenCenterIndex;
1177 }
1178
1179 protected void snapToDestination() {
1180 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001181 }
1182
Adam Cohene0f66b52010-11-23 15:06:07 -08001183 private static class ScrollInterpolator implements Interpolator {
1184 public ScrollInterpolator() {
1185 }
1186
1187 public float getInterpolation(float t) {
1188 t -= 1.0f;
1189 return t*t*t*t*t + 1;
1190 }
1191 }
1192
1193 // We want the duration of the page snap animation to be influenced by the distance that
1194 // the screen has to travel, however, we don't want this duration to be effected in a
1195 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1196 // of travel has on the overall snap duration.
1197 float distanceInfluenceForSnapDuration(float f) {
1198 f -= 0.5f; // center the values about 0.
1199 f *= 0.3f * Math.PI / 2.0f;
1200 return (float) Math.sin(f);
1201 }
1202
Michael Jurka0142d492010-08-25 17:46:15 -07001203 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001204 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1205 int halfScreenSize = getMeasuredWidth() / 2;
1206
1207 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1208 int delta = newX - mUnboundedScrollX;
1209 int duration = 0;
1210
1211 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1212 // If the velocity is low enough, then treat this more as an automatic page advance
1213 // as opposed to an apparent physical response to flinging
1214 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1215 return;
1216 }
1217
1218 // Here we compute a "distance" that will be used in the computation of the overall
1219 // snap duration. This is a function of the actual distance that needs to be traveled;
1220 // we keep this value close to half screen size in order to reduce the variance in snap
1221 // duration as a function of the distance the page needs to travel.
1222 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1223 float distance = halfScreenSize + halfScreenSize *
1224 distanceInfluenceForSnapDuration(distanceRatio);
1225
1226 velocity = Math.abs(velocity);
1227 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1228
1229 // we want the page's snap velocity to approximately match the velocity at which the
1230 // user flings, so we scale the duration by a value near to the derivative of the scroll
1231 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1232 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1233
1234 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001235 }
1236
1237 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001238 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001239 }
1240
Michael Jurka0142d492010-08-25 17:46:15 -07001241 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001242 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001243
Winson Chung86f77532010-08-24 11:08:22 -07001244 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001245 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001247 snapToPage(whichPage, delta, duration);
1248 }
1249
1250 protected void snapToPage(int whichPage, int delta, int duration) {
1251 mNextPage = whichPage;
1252
1253 View focusedChild = getFocusedChild();
1254 if (focusedChild != null && whichPage != mCurrentPage &&
1255 focusedChild == getChildAt(mCurrentPage)) {
1256 focusedChild.clearFocus();
1257 }
1258
1259 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 awakenScrollBars(duration);
1261 if (duration == 0) {
1262 duration = Math.abs(delta);
1263 }
1264
1265 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001266 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001267
1268 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001269 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001270 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 invalidate();
1272 }
1273
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 public void scrollLeft() {
1275 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001276 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001278 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 }
1280 }
1281
1282 public void scrollRight() {
1283 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001284 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001285 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001286 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001287 }
1288 }
1289
Winson Chung86f77532010-08-24 11:08:22 -07001290 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 int result = -1;
1292 if (v != null) {
1293 ViewParent vp = v.getParent();
1294 int count = getChildCount();
1295 for (int i = 0; i < count; i++) {
1296 if (vp == getChildAt(i)) {
1297 return i;
1298 }
1299 }
1300 }
1301 return result;
1302 }
1303
1304 /**
1305 * @return True is long presses are still allowed for the current touch
1306 */
1307 public boolean allowLongPress() {
1308 return mAllowLongPress;
1309 }
1310
Michael Jurka0142d492010-08-25 17:46:15 -07001311 /**
1312 * Set true to allow long-press events to be triggered, usually checked by
1313 * {@link Launcher} to accept or block dpad-initiated long-presses.
1314 */
1315 public void setAllowLongPress(boolean allowLongPress) {
1316 mAllowLongPress = allowLongPress;
1317 }
1318
Winson Chung321e9ee2010-08-09 13:37:56 -07001319 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001320 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001321
1322 SavedState(Parcelable superState) {
1323 super(superState);
1324 }
1325
1326 private SavedState(Parcel in) {
1327 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001328 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 }
1330
1331 @Override
1332 public void writeToParcel(Parcel out, int flags) {
1333 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001334 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001335 }
1336
1337 public static final Parcelable.Creator<SavedState> CREATOR =
1338 new Parcelable.Creator<SavedState>() {
1339 public SavedState createFromParcel(Parcel in) {
1340 return new SavedState(in);
1341 }
1342
1343 public SavedState[] newArray(int size) {
1344 return new SavedState[size];
1345 }
1346 };
1347 }
1348
Winson Chung86f77532010-08-24 11:08:22 -07001349 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001350 if (mContentIsRefreshable) {
1351 final int count = getChildCount();
1352 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001353 int lowerPageBound = getAssociatedLowerPageBound(page);
1354 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001355 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001356 Page layout = (Page) getChildAt(i);
1357 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001358 if (lowerPageBound <= i && i <= upperPageBound) {
1359 if (mDirtyPageContent.get(i)) {
1360 syncPageItems(i);
1361 mDirtyPageContent.set(i, false);
1362 }
1363 } else {
1364 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001365 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001366 }
1367 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001368 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001369 }
1370 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001371 }
1372 }
1373
Winson Chunge3193b92010-09-10 11:44:42 -07001374 protected int getAssociatedLowerPageBound(int page) {
1375 return Math.max(0, page - 1);
1376 }
1377 protected int getAssociatedUpperPageBound(int page) {
1378 final int count = getChildCount();
1379 return Math.min(page + 1, count - 1);
1380 }
1381
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001382 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001383 if (isChoiceMode(CHOICE_MODE_NONE)) {
1384 mChoiceMode = mode;
1385 mActionMode = startActionMode(callback);
1386 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001387 }
1388
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001389 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001390 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001391 mChoiceMode = CHOICE_MODE_NONE;
1392 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001393 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001394 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001395 }
1396 }
1397
1398 protected boolean isChoiceMode(int mode) {
1399 return mChoiceMode == mode;
1400 }
1401
1402 protected ArrayList<Checkable> getCheckedGrandchildren() {
1403 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1404 final int childCount = getChildCount();
1405 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001406 Page layout = (Page) getChildAt(i);
1407 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001408 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001409 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001410 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001411 checked.add((Checkable) v);
1412 }
1413 }
1414 }
1415 return checked;
1416 }
1417
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001418 /**
1419 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1420 * Otherwise, returns null.
1421 */
1422 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001423 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001424 final int childCount = getChildCount();
1425 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001426 Page layout = (Page) getChildAt(i);
1427 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001428 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001429 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001430 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1431 return (Checkable) v;
1432 }
1433 }
1434 }
1435 }
1436 return null;
1437 }
1438
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001439 protected void resetCheckedGrandchildren() {
1440 // loop through children, and set all of their children to _not_ be checked
1441 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1442 for (int i = 0; i < checked.size(); ++i) {
1443 final Checkable c = checked.get(i);
1444 c.setChecked(false);
1445 }
1446 }
1447
Winson Chunga12a2502010-12-20 14:41:35 -08001448 public void setRestorePage(int restorePage) {
1449 mRestorePage = restorePage;
1450 }
1451
Winson Chung86f77532010-08-24 11:08:22 -07001452 /**
1453 * This method is called ONLY to synchronize the number of pages that the paged view has.
1454 * To actually fill the pages with information, implement syncPageItems() below. It is
1455 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1456 * and therefore, individual page items do not need to be updated in this method.
1457 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001459
1460 /**
1461 * This method is called to synchronize the items that are on a particular page. If views on
1462 * the page can be reused, then they should be updated within this method.
1463 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001465
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001467 if (mContentIsRefreshable) {
1468 // Update all the pages
1469 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001470
Michael Jurka0142d492010-08-25 17:46:15 -07001471 // Mark each of the pages as dirty
1472 final int count = getChildCount();
1473 mDirtyPageContent.clear();
1474 for (int i = 0; i < count; ++i) {
1475 mDirtyPageContent.add(true);
1476 }
1477
Winson Chunga12a2502010-12-20 14:41:35 -08001478 // Use the restore page if necessary
1479 if (mRestorePage > -1) {
1480 mCurrentPage = mRestorePage;
1481 mRestorePage = -1;
1482 }
1483
Michael Jurka0142d492010-08-25 17:46:15 -07001484 // Load any pages that are necessary for the current window of views
1485 loadAssociatedPages(mCurrentPage);
1486 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001487 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001488 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001489 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001490 }
1491}