blob: 877f9d90ccf9055429a257c8e3a1f2fa40534034 [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 Cohen68d73932010-11-15 10:50:58 -080064
Michael Jurka0142d492010-08-25 17:46:15 -070065 // the velocity at which a fling gesture will cause us to snap to the next page
66 protected int mSnapVelocity = 500;
67
68 protected float mSmoothingTime;
69 protected float mTouchX;
70
71 protected boolean mFirstLayout = true;
72
73 protected int mCurrentPage;
74 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080075 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080076 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070077 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070078 private VelocityTracker mVelocityTracker;
79
80 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080081 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080082 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080083 protected float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070084 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070085
Michael Jurka0142d492010-08-25 17:46:15 -070086 protected final static int TOUCH_STATE_REST = 0;
87 protected final static int TOUCH_STATE_SCROLLING = 1;
88 protected final static int TOUCH_STATE_PREV_PAGE = 2;
89 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070090 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka7426c422010-11-11 15:23:47 -080096 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka7426c422010-11-11 15:23:47 -080098 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070099 private int mPagingTouchSlop;
100 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700101 protected int mPageSpacing;
102 protected int mPageLayoutPaddingTop;
103 protected int mPageLayoutPaddingBottom;
104 protected int mPageLayoutPaddingLeft;
105 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700106 protected int mPageLayoutWidthGap;
107 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700108 protected int mCellCountX;
109 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700110 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800111 protected boolean mAllowOverScroll = true;
112 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Michael Jurka8c920dd2011-01-20 14:16:56 -0800114 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800115 protected float mLayoutScale = 1.0f;
116
Michael Jurka5f1c5092010-09-03 14:15:02 -0700117 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Michael Jurka5f1c5092010-09-03 14:15:02 -0700119 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Winson Chung86f77532010-08-24 11:08:22 -0700121 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Winson Chung86f77532010-08-24 11:08:22 -0700123 private ArrayList<Boolean> mDirtyPageContent;
124 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700126 // choice modes
127 protected static final int CHOICE_MODE_NONE = 0;
128 protected static final int CHOICE_MODE_SINGLE = 1;
129 // Multiple selection mode is not supported by all Launcher actions atm
130 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700131
Michael Jurkae17e19c2010-09-28 11:01:39 -0700132 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700133 private ActionMode mActionMode;
134
Winson Chung04998342011-01-05 13:54:43 -0800135 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
136 // AllApps and Customize, and allows them to share holographic icons for the application view
137 // (which is in both).
138 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700139
Michael Jurka0142d492010-08-25 17:46:15 -0700140 // If true, syncPages and syncPageItems will be called to refresh pages
141 protected boolean mContentIsRefreshable = true;
142
143 // If true, modify alpha of neighboring pages as user scrolls left/right
144 protected boolean mFadeInAdjacentScreens = true;
145
146 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
147 // to switch to a new page
148 protected boolean mUsePagingTouchSlop = true;
149
150 // If true, the subclass should directly update mScrollX itself in its computeScroll method
151 // (SmoothPagedView does this)
152 protected boolean mDeferScrollUpdate = false;
153
Patrick Dubroy1262e362010-10-06 15:49:50 -0700154 protected boolean mIsPageMoving = false;
155
Winson Chung86f77532010-08-24 11:08:22 -0700156 public interface PageSwitchListener {
157 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700158 }
159
Winson Chung321e9ee2010-08-09 13:37:56 -0700160 public PagedView(Context context) {
161 this(context, null);
162 }
163
164 public PagedView(Context context, AttributeSet attrs) {
165 this(context, attrs, 0);
166 }
167
168 public PagedView(Context context, AttributeSet attrs, int defStyle) {
169 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700170 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700171
Adam Cohen9c4949e2010-10-05 12:27:22 -0700172 TypedArray a = context.obtainStyledAttributes(attrs,
173 R.styleable.PagedView, defStyle, 0);
174 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
175 mPageLayoutPaddingTop = a.getDimensionPixelSize(
176 R.styleable.PagedView_pageLayoutPaddingTop, 10);
177 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
178 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
179 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
180 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
181 mPageLayoutPaddingRight = a.getDimensionPixelSize(
182 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700183 mPageLayoutWidthGap = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutWidthGap, -1);
185 mPageLayoutHeightGap = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 a.recycle();
188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700190 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
193 /**
194 * Initializes various states for this workspace.
195 */
Michael Jurka0142d492010-08-25 17:46:15 -0700196 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700197 mDirtyPageContent = new ArrayList<Boolean>();
198 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800199 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700200 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700201 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700202
203 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
204 mTouchSlop = configuration.getScaledTouchSlop();
205 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
206 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
207 }
208
Winson Chung86f77532010-08-24 11:08:22 -0700209 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
210 mPageSwitchListener = pageSwitchListener;
211 if (mPageSwitchListener != null) {
212 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 }
214 }
215
216 /**
Winson Chung86f77532010-08-24 11:08:22 -0700217 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 *
Winson Chung86f77532010-08-24 11:08:22 -0700219 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 */
Winson Chung86f77532010-08-24 11:08:22 -0700221 int getCurrentPage() {
222 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224
Winson Chung86f77532010-08-24 11:08:22 -0700225 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 return getChildCount();
227 }
228
Winson Chung86f77532010-08-24 11:08:22 -0700229 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 return getChildAt(index);
231 }
232
233 int getScrollWidth() {
234 return getWidth();
235 }
236
Adam Cohenbe909342011-01-28 17:02:58 -0800237 public int getTouchState() {
238 return mTouchState;
239 }
240
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800242 * Updates the scroll of the current page immediately to its final scroll position. We use this
243 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
244 * the previous tab page.
245 */
246 protected void updateCurrentPageScroll() {
247 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
248 scrollTo(newX, 0);
249 mScroller.setFinalX(newX);
250 }
251
252 /**
Winson Chung86f77532010-08-24 11:08:22 -0700253 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 */
Winson Chung86f77532010-08-24 11:08:22 -0700255 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800256 if (!mScroller.isFinished()) {
257 mScroller.abortAnimation();
258 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800259 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
260 // the default
261 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800262 return;
263 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700264
Winson Chung86f77532010-08-24 11:08:22 -0700265 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800266 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700267 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800268 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 }
270
Michael Jurka0142d492010-08-25 17:46:15 -0700271 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700272 if (mPageSwitchListener != null) {
273 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 }
275 }
276
Patrick Dubroy1262e362010-10-06 15:49:50 -0700277 private void pageBeginMoving() {
278 mIsPageMoving = true;
279 onPageBeginMoving();
280 }
281
282 private void pageEndMoving() {
283 onPageEndMoving();
284 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700285 }
286
287 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700288 protected void onPageBeginMoving() {
289 }
290
291 // a method that subclasses can override to add behavior
292 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700293 }
294
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 /**
Winson Chung86f77532010-08-24 11:08:22 -0700296 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 *
298 * @param l The listener used to respond to long clicks.
299 */
300 @Override
301 public void setOnLongClickListener(OnLongClickListener l) {
302 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700303 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700304 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700305 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 }
307 }
308
309 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800310 public void scrollBy(int x, int y) {
311 scrollTo(mUnboundedScrollX + x, mScrollY + y);
312 }
313
314 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700315 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800316 mUnboundedScrollX = x;
317
318 if (x < 0) {
319 super.scrollTo(0, y);
320 if (mAllowOverScroll) {
321 overScroll(x);
322 }
323 } else if (x > mMaxScrollX) {
324 super.scrollTo(mMaxScrollX, y);
325 if (mAllowOverScroll) {
326 overScroll(x - mMaxScrollX);
327 }
328 } else {
329 super.scrollTo(x, y);
330 }
331
Michael Jurka0142d492010-08-25 17:46:15 -0700332 mTouchX = x;
333 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
334 }
335
336 // we moved this functionality to a helper function so SmoothPagedView can reuse it
337 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700339 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700340 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700341 invalidate();
342 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700343 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700344 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700345 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700346 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700347 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800348 // We don't want to trigger a page end moving unless the page has settled
349 // and the user has stopped scrolling
350 if (mTouchState == TOUCH_STATE_REST) {
351 pageEndMoving();
352 }
Michael Jurka0142d492010-08-25 17:46:15 -0700353 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 }
Michael Jurka0142d492010-08-25 17:46:15 -0700355 return false;
356 }
357
358 @Override
359 public void computeScroll() {
360 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 }
362
363 @Override
364 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
365 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
366 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
367 if (widthMode != MeasureSpec.EXACTLY) {
368 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
369 }
370
Adam Lesinski6b879f02010-11-04 16:15:23 -0700371 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
372 * of the All apps view on XLarge displays to not take up more space then it needs. Width
373 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
374 * each page to have the same width.
375 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700377 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
378 int maxChildHeight = 0;
379
380 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700381
382 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700383 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 final int childCount = getChildCount();
385 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700386 // disallowing padding in paged view (just pass 0)
387 final View child = getChildAt(i);
388 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
389
390 int childWidthMode;
391 if (lp.width == LayoutParams.WRAP_CONTENT) {
392 childWidthMode = MeasureSpec.AT_MOST;
393 } else {
394 childWidthMode = MeasureSpec.EXACTLY;
395 }
396
397 int childHeightMode;
398 if (lp.height == LayoutParams.WRAP_CONTENT) {
399 childHeightMode = MeasureSpec.AT_MOST;
400 } else {
401 childHeightMode = MeasureSpec.EXACTLY;
402 }
403
404 final int childWidthMeasureSpec =
405 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
406 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700407 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700408
409 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700410 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
411 }
412
413 if (heightMode == MeasureSpec.AT_MOST) {
414 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700415 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800416 if (childCount > 0) {
417 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
418 } else {
419 mMaxScrollX = 0;
420 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700421
422 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 }
424
Michael Jurka8c920dd2011-01-20 14:16:56 -0800425 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800426 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
427 int delta = newX - mScrollX;
428
Michael Jurka8c920dd2011-01-20 14:16:56 -0800429 final int pageCount = getChildCount();
430 for (int i = 0; i < pageCount; i++) {
431 View page = (View) getChildAt(i);
432 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800433 }
434 setCurrentPage(newCurrentPage);
435 }
436
Michael Jurka8c920dd2011-01-20 14:16:56 -0800437 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
438 // 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 -0800439 // tightens the layout accordingly
440 public void setLayoutScale(float childrenScale) {
441 mLayoutScale = childrenScale;
442
443 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
444 int childCount = getChildCount();
445 float childrenX[] = new float[childCount];
446 float childrenY[] = new float[childCount];
447 for (int i = 0; i < childCount; i++) {
448 final View child = getChildAt(i);
449 childrenX[i] = child.getX();
450 childrenY[i] = child.getY();
451 }
452 onLayout(false, mLeft, mTop, mRight, mBottom);
453 for (int i = 0; i < childCount; i++) {
454 final View child = getChildAt(i);
455 child.setX(childrenX[i]);
456 child.setY(childrenY[i]);
457 }
458 // Also, the page offset has changed (since the pages are now smaller);
459 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800460 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800461 }
462
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700464 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700465 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
466 setHorizontalScrollBarEnabled(false);
467 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
468 scrollTo(newX, 0);
469 mScroller.setFinalX(newX);
470 setHorizontalScrollBarEnabled(true);
471 mFirstLayout = false;
472 }
473
Adam Lesinski6b879f02010-11-04 16:15:23 -0700474 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700475 final int childCount = getChildCount();
476 int childLeft = 0;
477 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700478 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 }
480
481 for (int i = 0; i < childCount; i++) {
482 final View child = getChildAt(i);
483 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800484 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700485 final int childHeight = child.getMeasuredHeight();
486 int childTop = mPaddingTop;
487 if (mCenterPagesVertically) {
488 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
489 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800490
Adam Lesinski6b879f02010-11-04 16:15:23 -0700491 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800492 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700493 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 }
495 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800496 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
497 mFirstLayout = false;
498 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 }
500
Winson Chunge3193b92010-09-10 11:44:42 -0700501 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700502 if (mFadeInAdjacentScreens) {
503 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700504 int halfScreenSize = getMeasuredWidth() / 2;
505 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700506 final int childCount = getChildCount();
507 for (int i = 0; i < childCount; ++i) {
508 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800509 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700510 int halfChildWidth = (childWidth / 2);
511 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700512
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700513 // On the first layout, we may not have a width nor a proper offset, so for now
514 // we should just assume full page width (and calculate the offset according to
515 // that).
516 if (childWidth <= 0) {
517 childWidth = getMeasuredWidth();
518 childCenter = (i * childWidth) + (childWidth / 2);
519 }
520
Winson Chunge8878e32010-09-15 20:37:09 -0700521 int d = halfChildWidth;
522 int distanceFromScreenCenter = childCenter - screenCenter;
523 if (distanceFromScreenCenter > 0) {
524 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800525 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700526 }
Michael Jurka0142d492010-08-25 17:46:15 -0700527 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700528 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800529 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700530 }
Michael Jurka0142d492010-08-25 17:46:15 -0700531 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700532 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700533
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700534 // Preventing potential divide-by-zero
535 d = Math.max(1, d);
536
Winson Chunge8878e32010-09-15 20:37:09 -0700537 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
538 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
539 float alpha = 1.0f - dimAlpha;
540
Adam Cohenf34bab52010-09-30 14:11:56 -0700541 if (alpha < ALPHA_QUANTIZE_LEVEL) {
542 alpha = 0.0f;
543 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
544 alpha = 1.0f;
545 }
546
Michael Jurka0142d492010-08-25 17:46:15 -0700547 if (Float.compare(alpha, layout.getAlpha()) != 0) {
548 layout.setAlpha(alpha);
549 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700550 }
Michael Jurka0142d492010-08-25 17:46:15 -0700551 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
553 }
Winson Chunge3193b92010-09-10 11:44:42 -0700554 }
555
Adam Cohenf34bab52010-09-30 14:11:56 -0700556 protected void screenScrolled(int screenCenter) {
557 }
558
Winson Chunge3193b92010-09-10 11:44:42 -0700559 @Override
560 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700561 int halfScreenSize = getMeasuredWidth() / 2;
562 int screenCenter = mScrollX + halfScreenSize;
563
564 if (screenCenter != mLastScreenCenter) {
565 screenScrolled(screenCenter);
566 updateAdjacentPagesAlpha();
567 mLastScreenCenter = screenCenter;
568 }
Michael Jurka0142d492010-08-25 17:46:15 -0700569
570 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700571 // As an optimization, this code assumes that all pages have the same width as the 0th
572 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700573 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700574 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800575 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700576 final int screenWidth = getMeasuredWidth();
577 int x = getRelativeChildOffset(0) + pageWidth;
578 int leftScreen = 0;
579 int rightScreen = 0;
580 while (x <= mScrollX) {
581 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800582 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700583 }
584 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800585 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700586 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800587 if (rightScreen < pageCount) {
588 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
589 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700590 }
591 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700592
Michael Jurkac4fb9172010-09-02 17:19:20 -0700593 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800594 // Clip to the bounds
595 canvas.save();
596 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
597 mScrollY + mBottom - mTop);
598
Michael Jurkac4fb9172010-09-02 17:19:20 -0700599 for (int i = leftScreen; i <= rightScreen; i++) {
600 drawChild(canvas, getChildAt(i), drawingTime);
601 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800602 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700603 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700604 }
605
606 @Override
607 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700608 int page = indexOfChild(child);
609 if (page != mCurrentPage || !mScroller.isFinished()) {
610 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700611 return true;
612 }
613 return false;
614 }
615
616 @Override
617 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700618 int focusablePage;
619 if (mNextPage != INVALID_PAGE) {
620 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700621 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700622 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700623 }
Winson Chung86f77532010-08-24 11:08:22 -0700624 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700625 if (v != null) {
626 v.requestFocus(direction, previouslyFocusedRect);
627 }
628 return false;
629 }
630
631 @Override
632 public boolean dispatchUnhandledMove(View focused, int direction) {
633 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700634 if (getCurrentPage() > 0) {
635 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 return true;
637 }
638 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700639 if (getCurrentPage() < getPageCount() - 1) {
640 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700641 return true;
642 }
643 }
644 return super.dispatchUnhandledMove(focused, direction);
645 }
646
647 @Override
648 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700649 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
650 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 }
652 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700653 if (mCurrentPage > 0) {
654 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700655 }
656 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700657 if (mCurrentPage < getPageCount() - 1) {
658 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700659 }
660 }
661 }
662
663 /**
664 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700665 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700666 *
Winson Chung86f77532010-08-24 11:08:22 -0700667 * This happens when live folders requery, and if they're off page, they
668 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700669 */
670 @Override
671 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700672 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 View v = focused;
674 while (true) {
675 if (v == current) {
676 super.focusableViewAvailable(focused);
677 return;
678 }
679 if (v == this) {
680 return;
681 }
682 ViewParent parent = v.getParent();
683 if (parent instanceof View) {
684 v = (View)v.getParent();
685 } else {
686 return;
687 }
688 }
689 }
690
691 /**
692 * {@inheritDoc}
693 */
694 @Override
695 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
696 if (disallowIntercept) {
697 // We need to make sure to cancel our long press if
698 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700699 final View currentPage = getChildAt(mCurrentPage);
700 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 }
702 super.requestDisallowInterceptTouchEvent(disallowIntercept);
703 }
704
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800705 /**
706 * Return true if a tap at (x, y) should trigger a flip to the previous page.
707 */
708 protected boolean hitsPreviousPage(float x, float y) {
709 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
710 }
711
712 /**
713 * Return true if a tap at (x, y) should trigger a flip to the next page.
714 */
715 protected boolean hitsNextPage(float x, float y) {
716 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
717 }
718
Winson Chung321e9ee2010-08-09 13:37:56 -0700719 @Override
720 public boolean onInterceptTouchEvent(MotionEvent ev) {
721 /*
722 * This method JUST determines whether we want to intercept the motion.
723 * If we return true, onTouchEvent will be called and we do the actual
724 * scrolling there.
725 */
726
Winson Chung45e1d6e2010-11-09 17:19:49 -0800727 // Skip touch handling if there are no pages to swipe
728 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
729
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 /*
731 * Shortcut the most recurring case: the user is in the dragging
732 * state and he is moving his finger. We want to intercept this
733 * motion.
734 */
735 final int action = ev.getAction();
736 if ((action == MotionEvent.ACTION_MOVE) &&
737 (mTouchState == TOUCH_STATE_SCROLLING)) {
738 return true;
739 }
740
Winson Chung321e9ee2010-08-09 13:37:56 -0700741 switch (action & MotionEvent.ACTION_MASK) {
742 case MotionEvent.ACTION_MOVE: {
743 /*
744 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
745 * whether the user has moved far enough from his original down touch.
746 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700747 if (mActivePointerId != INVALID_POINTER) {
748 determineScrollingStart(ev);
749 break;
750 }
751 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
752 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
753 // i.e. fall through to the next case (don't break)
754 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
755 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700756 }
757
758 case MotionEvent.ACTION_DOWN: {
759 final float x = ev.getX();
760 final float y = ev.getY();
761 // Remember location of down touch
762 mDownMotionX = x;
763 mLastMotionX = x;
764 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800765 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700766 mActivePointerId = ev.getPointerId(0);
767 mAllowLongPress = true;
768
769 /*
770 * If being flinged and user touches the screen, initiate drag;
771 * otherwise don't. mScroller.isFinished should be false when
772 * being flinged.
773 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700774 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700775 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
776 if (finishedScrolling) {
777 mTouchState = TOUCH_STATE_REST;
778 mScroller.abortAnimation();
779 } else {
780 mTouchState = TOUCH_STATE_SCROLLING;
781 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700782
Winson Chung86f77532010-08-24 11:08:22 -0700783 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800785 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800787 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800789 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 mTouchState = TOUCH_STATE_NEXT_PAGE;
791 }
792 }
793 }
794 break;
795 }
796
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800798 onWallpaperTap(ev);
799 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 mTouchState = TOUCH_STATE_REST;
801 mAllowLongPress = false;
802 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700803 break;
804
805 case MotionEvent.ACTION_POINTER_UP:
806 onSecondaryPointerUp(ev);
807 break;
808 }
809
810 /*
811 * The only time we want to intercept motion events is if we are in the
812 * drag mode.
813 */
814 return mTouchState != TOUCH_STATE_REST;
815 }
816
Winson Chung80baf5a2010-08-09 16:03:15 -0700817 protected void animateClickFeedback(View v, final Runnable r) {
818 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800819 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
820 loadAnimator(mContext, R.anim.paged_view_click_feedback);
821 anim.setTarget(v);
822 anim.addListener(new AnimatorListenerAdapter() {
823 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700824 r.run();
825 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700826 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800827 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700828 }
829
Adam Cohenf8d28232011-02-01 21:47:00 -0800830 protected void determineScrollingStart(MotionEvent ev) {
831 determineScrollingStart(ev, 1.0f);
832 }
833
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 /*
835 * Determines if we should change the touch state to start scrolling after the
836 * user moves their touch point too far.
837 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800838 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700839 /*
840 * Locally do absolute value. mLastMotionX is set to the y value
841 * of the down event.
842 */
843 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
844 final float x = ev.getX(pointerIndex);
845 final float y = ev.getY(pointerIndex);
846 final int xDiff = (int) Math.abs(x - mLastMotionX);
847 final int yDiff = (int) Math.abs(y - mLastMotionY);
848
Adam Cohenf8d28232011-02-01 21:47:00 -0800849 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 boolean xPaged = xDiff > mPagingTouchSlop;
851 boolean xMoved = xDiff > touchSlop;
852 boolean yMoved = yDiff > touchSlop;
853
Adam Cohenf8d28232011-02-01 21:47:00 -0800854 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700855 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 // Scroll if the user moved far enough along the X axis
857 mTouchState = TOUCH_STATE_SCROLLING;
858 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800859 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700860 mTouchX = mScrollX;
861 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
862 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 }
864 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800865 cancelCurrentPageLongPress();
866 }
867 }
868
869 protected void cancelCurrentPageLongPress() {
870 if (mAllowLongPress) {
871 mAllowLongPress = false;
872 // Try canceling the long press. It could also have been scheduled
873 // by a distant descendant, so use the mAllowLongPress flag to block
874 // everything
875 final View currentPage = getPageAt(mCurrentPage);
876 if (currentPage != null) {
877 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700878 }
879 }
880 }
881
Adam Cohene0f66b52010-11-23 15:06:07 -0800882 // This curve determines how the effect of scrolling over the limits of the page dimishes
883 // as the user pulls further and further from the bounds
884 private float overScrollInfluenceCurve(float f) {
885 f -= 1.0f;
886 return f * f * f + 1.0f;
887 }
888
Adam Cohen68d73932010-11-15 10:50:58 -0800889 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800890 int screenSize = getMeasuredWidth();
891
892 float f = (amount / screenSize);
893
894 if (f == 0) return;
895 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
896
Adam Cohen7bfc9792011-01-28 13:52:37 -0800897 // Clamp this factor, f, to -1 < f < 1
898 if (Math.abs(f) >= 1) {
899 f /= Math.abs(f);
900 }
901
Adam Cohene0f66b52010-11-23 15:06:07 -0800902 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800903 if (amount < 0) {
904 mScrollX = overScrollAmount;
905 } else {
906 mScrollX = mMaxScrollX + overScrollAmount;
907 }
908 invalidate();
909 }
910
Michael Jurkac5b262c2011-01-12 20:24:50 -0800911 protected float maxOverScroll() {
912 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
913 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
914 float f = 1.0f;
915 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
916 return OVERSCROLL_DAMP_FACTOR * f;
917 }
918
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 @Override
920 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800921 // Skip touch handling if there are no pages to swipe
922 if (getChildCount() <= 0) return super.onTouchEvent(ev);
923
Michael Jurkab8f06722010-10-10 15:58:46 -0700924 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700925
926 final int action = ev.getAction();
927
928 switch (action & MotionEvent.ACTION_MASK) {
929 case MotionEvent.ACTION_DOWN:
930 /*
931 * If being flinged and user touches, stop the fling. isFinished
932 * will be false if being flinged.
933 */
934 if (!mScroller.isFinished()) {
935 mScroller.abortAnimation();
936 }
937
938 // Remember where the motion event started
939 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800940 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700942 if (mTouchState == TOUCH_STATE_SCROLLING) {
943 pageBeginMoving();
944 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 break;
946
947 case MotionEvent.ACTION_MOVE:
948 if (mTouchState == TOUCH_STATE_SCROLLING) {
949 // Scroll to follow the motion event
950 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
951 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -0800952 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -0700953
Winson Chungc0844aa2011-02-02 15:25:58 -0800954 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
955 // keep the remainder because we are actually testing if we've moved from the last
956 // scrolled position (which is discrete).
957 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -0800958 mTouchX += deltaX;
959 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
960 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -0800961 scrollBy((int) deltaX, 0);
Adam Cohen68d73932010-11-15 10:50:58 -0800962 } else {
963 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 }
Winson Chungc0844aa2011-02-02 15:25:58 -0800965 mLastMotionX = x;
966 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 } else {
968 awakenScrollBars();
969 }
Adam Cohen564976a2010-10-13 18:52:07 -0700970 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 determineScrollingStart(ev);
972 }
973 break;
974
975 case MotionEvent.ACTION_UP:
976 if (mTouchState == TOUCH_STATE_SCROLLING) {
977 final int activePointerId = mActivePointerId;
978 final int pointerIndex = ev.findPointerIndex(activePointerId);
979 final float x = ev.getX(pointerIndex);
980 final VelocityTracker velocityTracker = mVelocityTracker;
981 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
982 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700983 final int deltaX = (int) (x - mDownMotionX);
984 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
985 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700986
Michael Jurka0142d492010-08-25 17:46:15 -0700987 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700988 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800989 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700990 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700991 } else if ((isSignificantMove && deltaX < 0 ||
992 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700993 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700994 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 } else {
996 snapToDestination();
997 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800998 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 // at this point we have not moved beyond the touch slop
1000 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1001 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001002 int nextPage = Math.max(0, mCurrentPage - 1);
1003 if (nextPage != mCurrentPage) {
1004 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 } else {
1006 snapToDestination();
1007 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001008 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 // at this point we have not moved beyond the touch slop
1010 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1011 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001012 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1013 if (nextPage != mCurrentPage) {
1014 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 } else {
1016 snapToDestination();
1017 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001018 } else {
1019 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 }
1021 mTouchState = TOUCH_STATE_REST;
1022 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001023 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 break;
1025
1026 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001027 if (mTouchState == TOUCH_STATE_SCROLLING) {
1028 snapToDestination();
1029 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 mTouchState = TOUCH_STATE_REST;
1031 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001032 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 break;
1034
1035 case MotionEvent.ACTION_POINTER_UP:
1036 onSecondaryPointerUp(ev);
1037 break;
1038 }
1039
1040 return true;
1041 }
1042
Michael Jurkab8f06722010-10-10 15:58:46 -07001043 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1044 if (mVelocityTracker == null) {
1045 mVelocityTracker = VelocityTracker.obtain();
1046 }
1047 mVelocityTracker.addMovement(ev);
1048 }
1049
1050 private void releaseVelocityTracker() {
1051 if (mVelocityTracker != null) {
1052 mVelocityTracker.recycle();
1053 mVelocityTracker = null;
1054 }
1055 }
1056
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 private void onSecondaryPointerUp(MotionEvent ev) {
1058 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1059 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1060 final int pointerId = ev.getPointerId(pointerIndex);
1061 if (pointerId == mActivePointerId) {
1062 // This was our active pointer going up. Choose a new
1063 // active pointer and adjust accordingly.
1064 // TODO: Make this decision more intelligent.
1065 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1066 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1067 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001068 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 mActivePointerId = ev.getPointerId(newPointerIndex);
1070 if (mVelocityTracker != null) {
1071 mVelocityTracker.clear();
1072 }
1073 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001074 if (mTouchState == TOUCH_STATE_REST) {
1075 onWallpaperTap(ev);
1076 }
1077 }
1078
1079 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 }
1081
1082 @Override
1083 public void requestChildFocus(View child, View focused) {
1084 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001085 int page = indexOfChild(child);
1086 if (page >= 0 && !isInTouchMode()) {
1087 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001088 }
1089 }
1090
Winson Chunge3193b92010-09-10 11:44:42 -07001091 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1092 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001093 int left;
1094 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001095 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001096 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001097 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001098 if (left <= relativeOffset && relativeOffset <= right) {
1099 return i;
1100 }
Winson Chunge3193b92010-09-10 11:44:42 -07001101 }
1102 return -1;
1103 }
1104
Winson Chung321e9ee2010-08-09 13:37:56 -07001105 protected int getRelativeChildOffset(int index) {
1106 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1107 }
1108
1109 protected int getChildOffset(int index) {
1110 if (getChildCount() == 0)
1111 return 0;
1112
1113 int offset = getRelativeChildOffset(0);
1114 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001115 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001116 }
1117 return offset;
1118 }
1119
Michael Jurkad3ef3062010-11-23 16:23:58 -08001120 protected int getScaledMeasuredWidth(View child) {
1121 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1122 }
1123
Adam Cohend19d3ca2010-09-15 14:43:42 -07001124 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 int minDistanceFromScreenCenter = getMeasuredWidth();
1126 int minDistanceFromScreenCenterIndex = -1;
1127 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1128 final int childCount = getChildCount();
1129 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001130 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001131 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001132 int halfChildWidth = (childWidth / 2);
1133 int childCenter = getChildOffset(i) + halfChildWidth;
1134 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1135 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1136 minDistanceFromScreenCenter = distanceFromScreenCenter;
1137 minDistanceFromScreenCenterIndex = i;
1138 }
1139 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001140 return minDistanceFromScreenCenterIndex;
1141 }
1142
1143 protected void snapToDestination() {
1144 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001145 }
1146
Adam Cohene0f66b52010-11-23 15:06:07 -08001147 private static class ScrollInterpolator implements Interpolator {
1148 public ScrollInterpolator() {
1149 }
1150
1151 public float getInterpolation(float t) {
1152 t -= 1.0f;
1153 return t*t*t*t*t + 1;
1154 }
1155 }
1156
1157 // We want the duration of the page snap animation to be influenced by the distance that
1158 // the screen has to travel, however, we don't want this duration to be effected in a
1159 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1160 // of travel has on the overall snap duration.
1161 float distanceInfluenceForSnapDuration(float f) {
1162 f -= 0.5f; // center the values about 0.
1163 f *= 0.3f * Math.PI / 2.0f;
1164 return (float) Math.sin(f);
1165 }
1166
Michael Jurka0142d492010-08-25 17:46:15 -07001167 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001168 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1169 int halfScreenSize = getMeasuredWidth() / 2;
1170
1171 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1172 int delta = newX - mUnboundedScrollX;
1173 int duration = 0;
1174
1175 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1176 // If the velocity is low enough, then treat this more as an automatic page advance
1177 // as opposed to an apparent physical response to flinging
1178 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1179 return;
1180 }
1181
1182 // Here we compute a "distance" that will be used in the computation of the overall
1183 // snap duration. This is a function of the actual distance that needs to be traveled;
1184 // we keep this value close to half screen size in order to reduce the variance in snap
1185 // duration as a function of the distance the page needs to travel.
1186 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1187 float distance = halfScreenSize + halfScreenSize *
1188 distanceInfluenceForSnapDuration(distanceRatio);
1189
1190 velocity = Math.abs(velocity);
1191 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1192
1193 // we want the page's snap velocity to approximately match the velocity at which the
1194 // user flings, so we scale the duration by a value near to the derivative of the scroll
1195 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1196 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1197
1198 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001199 }
1200
1201 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001202 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 }
1204
Michael Jurka0142d492010-08-25 17:46:15 -07001205 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001206 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001207
Winson Chung86f77532010-08-24 11:08:22 -07001208 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001209 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001211 snapToPage(whichPage, delta, duration);
1212 }
1213
1214 protected void snapToPage(int whichPage, int delta, int duration) {
1215 mNextPage = whichPage;
1216
1217 View focusedChild = getFocusedChild();
1218 if (focusedChild != null && whichPage != mCurrentPage &&
1219 focusedChild == getChildAt(mCurrentPage)) {
1220 focusedChild.clearFocus();
1221 }
1222
1223 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001224 awakenScrollBars(duration);
1225 if (duration == 0) {
1226 duration = Math.abs(delta);
1227 }
1228
1229 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001230 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001231
1232 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001233 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001234 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 invalidate();
1236 }
1237
Winson Chung321e9ee2010-08-09 13:37:56 -07001238 public void scrollLeft() {
1239 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001240 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001242 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 }
1244 }
1245
1246 public void scrollRight() {
1247 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001248 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001250 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 }
1252 }
1253
Winson Chung86f77532010-08-24 11:08:22 -07001254 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 int result = -1;
1256 if (v != null) {
1257 ViewParent vp = v.getParent();
1258 int count = getChildCount();
1259 for (int i = 0; i < count; i++) {
1260 if (vp == getChildAt(i)) {
1261 return i;
1262 }
1263 }
1264 }
1265 return result;
1266 }
1267
1268 /**
1269 * @return True is long presses are still allowed for the current touch
1270 */
1271 public boolean allowLongPress() {
1272 return mAllowLongPress;
1273 }
1274
Michael Jurka0142d492010-08-25 17:46:15 -07001275 /**
1276 * Set true to allow long-press events to be triggered, usually checked by
1277 * {@link Launcher} to accept or block dpad-initiated long-presses.
1278 */
1279 public void setAllowLongPress(boolean allowLongPress) {
1280 mAllowLongPress = allowLongPress;
1281 }
1282
Winson Chung321e9ee2010-08-09 13:37:56 -07001283 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001284 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001285
1286 SavedState(Parcelable superState) {
1287 super(superState);
1288 }
1289
1290 private SavedState(Parcel in) {
1291 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001292 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 }
1294
1295 @Override
1296 public void writeToParcel(Parcel out, int flags) {
1297 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001298 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 }
1300
1301 public static final Parcelable.Creator<SavedState> CREATOR =
1302 new Parcelable.Creator<SavedState>() {
1303 public SavedState createFromParcel(Parcel in) {
1304 return new SavedState(in);
1305 }
1306
1307 public SavedState[] newArray(int size) {
1308 return new SavedState[size];
1309 }
1310 };
1311 }
1312
Winson Chung86f77532010-08-24 11:08:22 -07001313 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001314 if (mContentIsRefreshable) {
1315 final int count = getChildCount();
1316 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001317 int lowerPageBound = getAssociatedLowerPageBound(page);
1318 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001319 for (int i = 0; i < count; ++i) {
1320 final ViewGroup layout = (ViewGroup) getChildAt(i);
1321 final int childCount = layout.getChildCount();
1322 if (lowerPageBound <= i && i <= upperPageBound) {
1323 if (mDirtyPageContent.get(i)) {
1324 syncPageItems(i);
1325 mDirtyPageContent.set(i, false);
1326 }
1327 } else {
1328 if (childCount > 0) {
1329 layout.removeAllViews();
1330 }
1331 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001332 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001333 }
1334 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001335 }
1336 }
1337
Winson Chunge3193b92010-09-10 11:44:42 -07001338 protected int getAssociatedLowerPageBound(int page) {
1339 return Math.max(0, page - 1);
1340 }
1341 protected int getAssociatedUpperPageBound(int page) {
1342 final int count = getChildCount();
1343 return Math.min(page + 1, count - 1);
1344 }
1345
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001346 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001347 if (isChoiceMode(CHOICE_MODE_NONE)) {
1348 mChoiceMode = mode;
1349 mActionMode = startActionMode(callback);
1350 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001351 }
1352
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001353 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001354 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001355 mChoiceMode = CHOICE_MODE_NONE;
1356 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001357 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001358 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001359 }
1360 }
1361
1362 protected boolean isChoiceMode(int mode) {
1363 return mChoiceMode == mode;
1364 }
1365
1366 protected ArrayList<Checkable> getCheckedGrandchildren() {
1367 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1368 final int childCount = getChildCount();
1369 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001370 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001371 final int grandChildCount = layout.getChildCount();
1372 for (int j = 0; j < grandChildCount; ++j) {
1373 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001374 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001375 checked.add((Checkable) v);
1376 }
1377 }
1378 }
1379 return checked;
1380 }
1381
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001382 /**
1383 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1384 * Otherwise, returns null.
1385 */
1386 protected Checkable getSingleCheckedGrandchild() {
1387 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1388 final int childCount = getChildCount();
1389 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001390 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001391 final int grandChildCount = layout.getChildCount();
1392 for (int j = 0; j < grandChildCount; ++j) {
1393 final View v = layout.getChildAt(j);
1394 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1395 return (Checkable) v;
1396 }
1397 }
1398 }
1399 }
1400 return null;
1401 }
1402
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001403 public Object getChosenItem() {
1404 View checkedView = (View) getSingleCheckedGrandchild();
1405 if (checkedView != null) {
1406 return checkedView.getTag();
1407 }
1408 return null;
1409 }
1410
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001411 protected void resetCheckedGrandchildren() {
1412 // loop through children, and set all of their children to _not_ be checked
1413 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1414 for (int i = 0; i < checked.size(); ++i) {
1415 final Checkable c = checked.get(i);
1416 c.setChecked(false);
1417 }
1418 }
1419
Winson Chunga12a2502010-12-20 14:41:35 -08001420 public void setRestorePage(int restorePage) {
1421 mRestorePage = restorePage;
1422 }
1423
Winson Chung86f77532010-08-24 11:08:22 -07001424 /**
1425 * This method is called ONLY to synchronize the number of pages that the paged view has.
1426 * To actually fill the pages with information, implement syncPageItems() below. It is
1427 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1428 * and therefore, individual page items do not need to be updated in this method.
1429 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001431
1432 /**
1433 * This method is called to synchronize the items that are on a particular page. If views on
1434 * the page can be reused, then they should be updated within this method.
1435 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001437
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001439 if (mContentIsRefreshable) {
1440 // Update all the pages
1441 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001442
Michael Jurka0142d492010-08-25 17:46:15 -07001443 // Mark each of the pages as dirty
1444 final int count = getChildCount();
1445 mDirtyPageContent.clear();
1446 for (int i = 0; i < count; ++i) {
1447 mDirtyPageContent.add(true);
1448 }
1449
Winson Chunga12a2502010-12-20 14:41:35 -08001450 // Use the restore page if necessary
1451 if (mRestorePage > -1) {
1452 mCurrentPage = mRestorePage;
1453 mRestorePage = -1;
1454 }
1455
Michael Jurka0142d492010-08-25 17:46:15 -07001456 // Load any pages that are necessary for the current window of views
1457 loadAssociatedPages(mCurrentPage);
1458 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001459 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001460 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001461 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 }
1463}