blob: d83db743552065124f87997e79ed968b5446a8c0 [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 Chung785d2eb2011-04-14 16:08:02 -070032import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070033import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080034import android.view.InputDevice;
35import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070036import android.view.MotionEvent;
37import android.view.VelocityTracker;
38import android.view.View;
39import android.view.ViewConfiguration;
40import android.view.ViewGroup;
41import android.view.ViewParent;
Adam Cohene0f66b52010-11-23 15:06:07 -080042import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070043import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070044import android.widget.Scroller;
45
Winson Chung04998342011-01-05 13:54:43 -080046import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070047
Winson Chung321e9ee2010-08-09 13:37:56 -070048/**
49 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070050 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070051 */
52public abstract class PagedView extends ViewGroup {
53 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070054 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070055 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070056
Winson Chung86f77532010-08-24 11:08:22 -070057 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070058 private static final int MIN_LENGTH_FOR_FLING = 25;
59 // The min drag distance to trigger a page shift (regardless of velocity)
60 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Adam Cohene0f66b52010-11-23 15:06:07 -080062 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chungb26f3d62011-06-02 10:49:29 -070065 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080066 private static final int MINIMUM_SNAP_VELOCITY = 2200;
67 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080068 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080069
Michael Jurka0142d492010-08-25 17:46:15 -070070 // the velocity at which a fling gesture will cause us to snap to the next page
71 protected int mSnapVelocity = 500;
72
73 protected float mSmoothingTime;
74 protected float mTouchX;
75
76 protected boolean mFirstLayout = true;
77
78 protected int mCurrentPage;
79 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080080 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070081 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070082 private VelocityTracker mVelocityTracker;
83
84 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080085 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080086 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080087 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080088 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070089 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070090
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected final static int TOUCH_STATE_REST = 0;
92 protected final static int TOUCH_STATE_SCROLLING = 1;
93 protected final static int TOUCH_STATE_PREV_PAGE = 2;
94 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070095 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070096
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070098
Michael Jurka0142d492010-08-25 17:46:15 -070099 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700100
Michael Jurka7426c422010-11-11 15:23:47 -0800101 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700102
Michael Jurka7426c422010-11-11 15:23:47 -0800103 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700104 private int mPagingTouchSlop;
105 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800106 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700107 protected int mPageSpacing;
108 protected int mPageLayoutPaddingTop;
109 protected int mPageLayoutPaddingBottom;
110 protected int mPageLayoutPaddingLeft;
111 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700112 protected int mPageLayoutWidthGap;
113 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700114 protected int mCellCountX = 0;
115 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700116 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800117 protected boolean mAllowOverScroll = true;
118 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Michael Jurka8c920dd2011-01-20 14:16:56 -0800120 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800121 protected float mLayoutScale = 1.0f;
122
Michael Jurka5f1c5092010-09-03 14:15:02 -0700123 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka5f1c5092010-09-03 14:15:02 -0700125 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Winson Chung86f77532010-08-24 11:08:22 -0700127 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Winson Chung86f77532010-08-24 11:08:22 -0700129 private ArrayList<Boolean> mDirtyPageContent;
130 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700132 // choice modes
133 protected static final int CHOICE_MODE_NONE = 0;
134 protected static final int CHOICE_MODE_SINGLE = 1;
135 // Multiple selection mode is not supported by all Launcher actions atm
136 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700137
Michael Jurkae17e19c2010-09-28 11:01:39 -0700138 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700139 private ActionMode mActionMode;
140
Winson Chung04998342011-01-05 13:54:43 -0800141 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
142 // AllApps and Customize, and allows them to share holographic icons for the application view
143 // (which is in both).
144 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700145
Michael Jurka0142d492010-08-25 17:46:15 -0700146 // If true, syncPages and syncPageItems will be called to refresh pages
147 protected boolean mContentIsRefreshable = true;
148
149 // If true, modify alpha of neighboring pages as user scrolls left/right
150 protected boolean mFadeInAdjacentScreens = true;
151
152 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
153 // to switch to a new page
154 protected boolean mUsePagingTouchSlop = true;
155
156 // If true, the subclass should directly update mScrollX itself in its computeScroll method
157 // (SmoothPagedView does this)
158 protected boolean mDeferScrollUpdate = false;
159
Patrick Dubroy1262e362010-10-06 15:49:50 -0700160 protected boolean mIsPageMoving = false;
161
Winson Chungf0ea4d32011-06-06 14:27:16 -0700162 // All syncs and layout passes are deferred until data is ready.
163 protected boolean mIsDataReady = false;
164
Winson Chung86f77532010-08-24 11:08:22 -0700165 public interface PageSwitchListener {
166 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 }
168
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 public PagedView(Context context) {
170 this(context, null);
171 }
172
173 public PagedView(Context context, AttributeSet attrs) {
174 this(context, attrs, 0);
175 }
176
177 public PagedView(Context context, AttributeSet attrs, int defStyle) {
178 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Adam Cohen9c4949e2010-10-05 12:27:22 -0700181 TypedArray a = context.obtainStyledAttributes(attrs,
182 R.styleable.PagedView, defStyle, 0);
183 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
184 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800185 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700186 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800187 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700188 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800189 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700190 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800191 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700192 mPageLayoutWidthGap = a.getDimensionPixelSize(
193 R.styleable.PagedView_pageLayoutWidthGap, -1);
194 mPageLayoutHeightGap = a.getDimensionPixelSize(
195 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 a.recycle();
197
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700199 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
202 /**
203 * Initializes various states for this workspace.
204 */
Michael Jurka0142d492010-08-25 17:46:15 -0700205 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700206 mDirtyPageContent = new ArrayList<Boolean>();
207 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800208 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700209 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700210 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700211
212 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
213 mTouchSlop = configuration.getScaledTouchSlop();
214 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
215 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
216 }
217
Winson Chung86f77532010-08-24 11:08:22 -0700218 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
219 mPageSwitchListener = pageSwitchListener;
220 if (mPageSwitchListener != null) {
221 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 }
223 }
224
225 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700226 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
227 * out pages.
228 */
229 protected void setDataIsReady() {
230 mIsDataReady = true;
231 }
232 protected boolean isDataReady() {
233 return mIsDataReady;
234 }
235
236 /**
Winson Chung86f77532010-08-24 11:08:22 -0700237 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 *
Winson Chung86f77532010-08-24 11:08:22 -0700239 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 */
Winson Chung86f77532010-08-24 11:08:22 -0700241 int getCurrentPage() {
242 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 }
244
Winson Chung86f77532010-08-24 11:08:22 -0700245 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 return getChildCount();
247 }
248
Winson Chung86f77532010-08-24 11:08:22 -0700249 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 return getChildAt(index);
251 }
252
253 int getScrollWidth() {
254 return getWidth();
255 }
256
257 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800258 * Updates the scroll of the current page immediately to its final scroll position. We use this
259 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
260 * the previous tab page.
261 */
262 protected void updateCurrentPageScroll() {
263 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
264 scrollTo(newX, 0);
265 mScroller.setFinalX(newX);
266 }
267
268 /**
Winson Chung86f77532010-08-24 11:08:22 -0700269 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 */
Winson Chung86f77532010-08-24 11:08:22 -0700271 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800272 if (!mScroller.isFinished()) {
273 mScroller.abortAnimation();
274 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800275 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
276 // the default
277 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800278 return;
279 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700280
Winson Chung86f77532010-08-24 11:08:22 -0700281 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800282 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700283 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800284 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 }
286
Michael Jurka0142d492010-08-25 17:46:15 -0700287 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700288 if (mPageSwitchListener != null) {
289 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 }
291 }
292
Michael Jurkace7e05f2011-02-01 22:02:35 -0800293 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700294 mIsPageMoving = true;
295 onPageBeginMoving();
296 }
297
Michael Jurkace7e05f2011-02-01 22:02:35 -0800298 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700299 onPageEndMoving();
300 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700301 }
302
Adam Cohen26976d92011-03-22 15:33:33 -0700303 protected boolean isPageMoving() {
304 return mIsPageMoving;
305 }
306
Michael Jurka0142d492010-08-25 17:46:15 -0700307 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700308 protected void onPageBeginMoving() {
309 }
310
311 // a method that subclasses can override to add behavior
312 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700313 }
314
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 /**
Winson Chung86f77532010-08-24 11:08:22 -0700316 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 *
318 * @param l The listener used to respond to long clicks.
319 */
320 @Override
321 public void setOnLongClickListener(OnLongClickListener l) {
322 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700323 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700324 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700325 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 }
327 }
328
329 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800330 public void scrollBy(int x, int y) {
331 scrollTo(mUnboundedScrollX + x, mScrollY + y);
332 }
333
334 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700335 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800336 mUnboundedScrollX = x;
337
338 if (x < 0) {
339 super.scrollTo(0, y);
340 if (mAllowOverScroll) {
341 overScroll(x);
342 }
343 } else if (x > mMaxScrollX) {
344 super.scrollTo(mMaxScrollX, y);
345 if (mAllowOverScroll) {
346 overScroll(x - mMaxScrollX);
347 }
348 } else {
349 super.scrollTo(x, y);
350 }
351
Michael Jurka0142d492010-08-25 17:46:15 -0700352 mTouchX = x;
353 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
354 }
355
356 // we moved this functionality to a helper function so SmoothPagedView can reuse it
357 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700359 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700360 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700361 invalidate();
362 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700363 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700364 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700365 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700366 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700367 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800368 // We don't want to trigger a page end moving unless the page has settled
369 // and the user has stopped scrolling
370 if (mTouchState == TOUCH_STATE_REST) {
371 pageEndMoving();
372 }
Michael Jurka0142d492010-08-25 17:46:15 -0700373 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 }
Michael Jurka0142d492010-08-25 17:46:15 -0700375 return false;
376 }
377
378 @Override
379 public void computeScroll() {
380 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 }
382
383 @Override
384 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700385 if (!mIsDataReady) {
386 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
387 return;
388 }
389
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
391 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
392 if (widthMode != MeasureSpec.EXACTLY) {
393 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
394 }
395
Adam Lesinski6b879f02010-11-04 16:15:23 -0700396 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
397 * of the All apps view on XLarge displays to not take up more space then it needs. Width
398 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
399 * each page to have the same width.
400 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700402 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
403 int maxChildHeight = 0;
404
405 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700406
Michael Jurka36fcb742011-04-06 17:08:58 -0700407
Winson Chung321e9ee2010-08-09 13:37:56 -0700408 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700409 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700410 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700411 final int childCount = getChildCount();
412 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700413 // disallowing padding in paged view (just pass 0)
414 final View child = getChildAt(i);
415 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
416
417 int childWidthMode;
418 if (lp.width == LayoutParams.WRAP_CONTENT) {
419 childWidthMode = MeasureSpec.AT_MOST;
420 } else {
421 childWidthMode = MeasureSpec.EXACTLY;
422 }
423
424 int childHeightMode;
425 if (lp.height == LayoutParams.WRAP_CONTENT) {
426 childHeightMode = MeasureSpec.AT_MOST;
427 } else {
428 childHeightMode = MeasureSpec.EXACTLY;
429 }
430
431 final int childWidthMeasureSpec =
432 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
433 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700434 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700435
436 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700437 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700438 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
439 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700440 }
441
442 if (heightMode == MeasureSpec.AT_MOST) {
443 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800445 if (childCount > 0) {
446 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
447 } else {
448 mMaxScrollX = 0;
449 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700450
451 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700452 }
453
Michael Jurka8c920dd2011-01-20 14:16:56 -0800454 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800455 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
456 int delta = newX - mScrollX;
457
Michael Jurka8c920dd2011-01-20 14:16:56 -0800458 final int pageCount = getChildCount();
459 for (int i = 0; i < pageCount; i++) {
460 View page = (View) getChildAt(i);
461 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800462 }
463 setCurrentPage(newCurrentPage);
464 }
465
Michael Jurka8c920dd2011-01-20 14:16:56 -0800466 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
467 // 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 -0800468 // tightens the layout accordingly
469 public void setLayoutScale(float childrenScale) {
470 mLayoutScale = childrenScale;
471
472 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
473 int childCount = getChildCount();
474 float childrenX[] = new float[childCount];
475 float childrenY[] = new float[childCount];
476 for (int i = 0; i < childCount; i++) {
477 final View child = getChildAt(i);
478 childrenX[i] = child.getX();
479 childrenY[i] = child.getY();
480 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700481 // Trigger a full re-layout (never just call onLayout directly!)
482 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
483 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
484 requestLayout();
485 measure(widthSpec, heightSpec);
486 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800487 for (int i = 0; i < childCount; i++) {
488 final View child = getChildAt(i);
489 child.setX(childrenX[i]);
490 child.setY(childrenY[i]);
491 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700492
Michael Jurkad3ef3062010-11-23 16:23:58 -0800493 // Also, the page offset has changed (since the pages are now smaller);
494 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800495 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800496 }
497
Winson Chung321e9ee2010-08-09 13:37:56 -0700498 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700499 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700500 if (!mIsDataReady) {
501 return;
502 }
503
Winson Chung785d2eb2011-04-14 16:08:02 -0700504 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700505 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
506 setHorizontalScrollBarEnabled(false);
507 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
508 scrollTo(newX, 0);
509 mScroller.setFinalX(newX);
510 setHorizontalScrollBarEnabled(true);
511 mFirstLayout = false;
512 }
513
Adam Lesinski6b879f02010-11-04 16:15:23 -0700514 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 final int childCount = getChildCount();
516 int childLeft = 0;
517 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
519 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700520 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 }
522
523 for (int i = 0; i < childCount; i++) {
524 final View child = getChildAt(i);
525 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700527 final int childHeight = child.getMeasuredHeight();
528 int childTop = mPaddingTop;
529 if (mCenterPagesVertically) {
530 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
531 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800532
Winson Chung785d2eb2011-04-14 16:08:02 -0700533 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700534 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800535 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700536 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 }
538 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800539 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
540 mFirstLayout = false;
541 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700542 }
543
Winson Chung03929772011-02-23 17:07:10 -0800544 protected void forceUpdateAdjacentPagesAlpha() {
545 mDirtyPageAlpha = true;
546 updateAdjacentPagesAlpha();
547 }
548
Winson Chunge3193b92010-09-10 11:44:42 -0700549 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700550 if (mFadeInAdjacentScreens) {
551 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700552 int screenWidth = getMeasuredWidth();
553 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700554 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700555 final int childCount = getChildCount();
556 for (int i = 0; i < childCount; ++i) {
557 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700559 int halfChildWidth = (childWidth / 2);
560 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700561
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700562 // On the first layout, we may not have a width nor a proper offset, so for now
563 // we should just assume full page width (and calculate the offset according to
564 // that).
565 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700566 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700567 childCenter = (i * childWidth) + (childWidth / 2);
568 }
569
Winson Chunge8878e32010-09-15 20:37:09 -0700570 int d = halfChildWidth;
571 int distanceFromScreenCenter = childCenter - screenCenter;
572 if (distanceFromScreenCenter > 0) {
573 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800574 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700575 } else {
576 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700577 }
Michael Jurka0142d492010-08-25 17:46:15 -0700578 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700579 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800580 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700581 } else {
582 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700583 }
Michael Jurka0142d492010-08-25 17:46:15 -0700584 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700585 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700586
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700587 // Preventing potential divide-by-zero
588 d = Math.max(1, d);
589
Winson Chunge8878e32010-09-15 20:37:09 -0700590 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
591 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
592 float alpha = 1.0f - dimAlpha;
593
Adam Cohenf34bab52010-09-30 14:11:56 -0700594 if (alpha < ALPHA_QUANTIZE_LEVEL) {
595 alpha = 0.0f;
596 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
597 alpha = 1.0f;
598 }
599
Michael Jurkaa40829a2011-02-16 18:02:45 -0800600 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
601 // this optimization causes alpha to not be properly updated sometimes (repro
602 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
603 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
604 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800605 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700606 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800607 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 }
Michael Jurka0142d492010-08-25 17:46:15 -0700609 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
611 }
Winson Chunge3193b92010-09-10 11:44:42 -0700612 }
613
Adam Cohenf34bab52010-09-30 14:11:56 -0700614 protected void screenScrolled(int screenCenter) {
615 }
616
Winson Chunge3193b92010-09-10 11:44:42 -0700617 @Override
618 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700619 int halfScreenSize = getMeasuredWidth() / 2;
620 int screenCenter = mScrollX + halfScreenSize;
621
622 if (screenCenter != mLastScreenCenter) {
623 screenScrolled(screenCenter);
624 updateAdjacentPagesAlpha();
625 mLastScreenCenter = screenCenter;
626 }
Michael Jurka0142d492010-08-25 17:46:15 -0700627
628 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700629 // As an optimization, this code assumes that all pages have the same width as the 0th
630 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700631 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700632 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800633 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700634 final int screenWidth = getMeasuredWidth();
635 int x = getRelativeChildOffset(0) + pageWidth;
636 int leftScreen = 0;
637 int rightScreen = 0;
638 while (x <= mScrollX) {
639 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800640 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700641 }
642 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800643 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700644 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800645 if (rightScreen < pageCount) {
646 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
647 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700648 }
649 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700650
Michael Jurkac4fb9172010-09-02 17:19:20 -0700651 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800652 // Clip to the bounds
653 canvas.save();
654 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
655 mScrollY + mBottom - mTop);
656
Michael Jurkac4fb9172010-09-02 17:19:20 -0700657 for (int i = leftScreen; i <= rightScreen; i++) {
658 drawChild(canvas, getChildAt(i), drawingTime);
659 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800660 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700661 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 }
663
664 @Override
665 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700666 int page = indexOfChild(child);
667 if (page != mCurrentPage || !mScroller.isFinished()) {
668 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700669 return true;
670 }
671 return false;
672 }
673
674 @Override
675 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700676 int focusablePage;
677 if (mNextPage != INVALID_PAGE) {
678 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700680 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 }
Winson Chung86f77532010-08-24 11:08:22 -0700682 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700683 if (v != null) {
684 v.requestFocus(direction, previouslyFocusedRect);
685 }
686 return false;
687 }
688
689 @Override
690 public boolean dispatchUnhandledMove(View focused, int direction) {
691 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700692 if (getCurrentPage() > 0) {
693 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 return true;
695 }
696 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700697 if (getCurrentPage() < getPageCount() - 1) {
698 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 return true;
700 }
701 }
702 return super.dispatchUnhandledMove(focused, direction);
703 }
704
705 @Override
706 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700707 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
708 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700709 }
710 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700711 if (mCurrentPage > 0) {
712 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 }
714 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700715 if (mCurrentPage < getPageCount() - 1) {
716 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 }
718 }
719 }
720
721 /**
722 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700723 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 *
Winson Chung86f77532010-08-24 11:08:22 -0700725 * This happens when live folders requery, and if they're off page, they
726 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 */
728 @Override
729 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700730 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700731 View v = focused;
732 while (true) {
733 if (v == current) {
734 super.focusableViewAvailable(focused);
735 return;
736 }
737 if (v == this) {
738 return;
739 }
740 ViewParent parent = v.getParent();
741 if (parent instanceof View) {
742 v = (View)v.getParent();
743 } else {
744 return;
745 }
746 }
747 }
748
749 /**
750 * {@inheritDoc}
751 */
752 @Override
753 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
754 if (disallowIntercept) {
755 // We need to make sure to cancel our long press if
756 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700757 final View currentPage = getChildAt(mCurrentPage);
758 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 }
760 super.requestDisallowInterceptTouchEvent(disallowIntercept);
761 }
762
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800763 /**
764 * Return true if a tap at (x, y) should trigger a flip to the previous page.
765 */
766 protected boolean hitsPreviousPage(float x, float y) {
767 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
768 }
769
770 /**
771 * Return true if a tap at (x, y) should trigger a flip to the next page.
772 */
773 protected boolean hitsNextPage(float x, float y) {
774 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
775 }
776
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 @Override
778 public boolean onInterceptTouchEvent(MotionEvent ev) {
779 /*
780 * This method JUST determines whether we want to intercept the motion.
781 * If we return true, onTouchEvent will be called and we do the actual
782 * scrolling there.
783 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800784 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700785
Winson Chung45e1d6e2010-11-09 17:19:49 -0800786 // Skip touch handling if there are no pages to swipe
787 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
788
Winson Chung321e9ee2010-08-09 13:37:56 -0700789 /*
790 * Shortcut the most recurring case: the user is in the dragging
791 * state and he is moving his finger. We want to intercept this
792 * motion.
793 */
794 final int action = ev.getAction();
795 if ((action == MotionEvent.ACTION_MOVE) &&
796 (mTouchState == TOUCH_STATE_SCROLLING)) {
797 return true;
798 }
799
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 switch (action & MotionEvent.ACTION_MASK) {
801 case MotionEvent.ACTION_MOVE: {
802 /*
803 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
804 * whether the user has moved far enough from his original down touch.
805 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700806 if (mActivePointerId != INVALID_POINTER) {
807 determineScrollingStart(ev);
808 break;
809 }
810 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
811 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
812 // i.e. fall through to the next case (don't break)
813 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
814 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 }
816
817 case MotionEvent.ACTION_DOWN: {
818 final float x = ev.getX();
819 final float y = ev.getY();
820 // Remember location of down touch
821 mDownMotionX = x;
822 mLastMotionX = x;
823 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800824 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800825 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 mActivePointerId = ev.getPointerId(0);
827 mAllowLongPress = true;
828
829 /*
830 * If being flinged and user touches the screen, initiate drag;
831 * otherwise don't. mScroller.isFinished should be false when
832 * being flinged.
833 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700834 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700835 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
836 if (finishedScrolling) {
837 mTouchState = TOUCH_STATE_REST;
838 mScroller.abortAnimation();
839 } else {
840 mTouchState = TOUCH_STATE_SCROLLING;
841 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700842
Winson Chung86f77532010-08-24 11:08:22 -0700843 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800845 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700846 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800847 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800849 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700850 mTouchState = TOUCH_STATE_NEXT_PAGE;
851 }
852 }
853 }
854 break;
855 }
856
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800858 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 mTouchState = TOUCH_STATE_REST;
860 mAllowLongPress = false;
861 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800862 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 break;
864
865 case MotionEvent.ACTION_POINTER_UP:
866 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800867 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 break;
869 }
870
871 /*
872 * The only time we want to intercept motion events is if we are in the
873 * drag mode.
874 */
875 return mTouchState != TOUCH_STATE_REST;
876 }
877
Winson Chung80baf5a2010-08-09 16:03:15 -0700878 protected void animateClickFeedback(View v, final Runnable r) {
879 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800880 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
881 loadAnimator(mContext, R.anim.paged_view_click_feedback);
882 anim.setTarget(v);
883 anim.addListener(new AnimatorListenerAdapter() {
884 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700885 r.run();
886 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700887 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800888 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700889 }
890
Adam Cohenf8d28232011-02-01 21:47:00 -0800891 protected void determineScrollingStart(MotionEvent ev) {
892 determineScrollingStart(ev, 1.0f);
893 }
894
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 /*
896 * Determines if we should change the touch state to start scrolling after the
897 * user moves their touch point too far.
898 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800899 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 /*
901 * Locally do absolute value. mLastMotionX is set to the y value
902 * of the down event.
903 */
904 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
905 final float x = ev.getX(pointerIndex);
906 final float y = ev.getY(pointerIndex);
907 final int xDiff = (int) Math.abs(x - mLastMotionX);
908 final int yDiff = (int) Math.abs(y - mLastMotionY);
909
Adam Cohenf8d28232011-02-01 21:47:00 -0800910 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 boolean xPaged = xDiff > mPagingTouchSlop;
912 boolean xMoved = xDiff > touchSlop;
913 boolean yMoved = yDiff > touchSlop;
914
Adam Cohenf8d28232011-02-01 21:47:00 -0800915 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700916 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 // Scroll if the user moved far enough along the X axis
918 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800919 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700920 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800921 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700922 mTouchX = mScrollX;
923 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
924 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 }
926 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800927 cancelCurrentPageLongPress();
928 }
929 }
930
931 protected void cancelCurrentPageLongPress() {
932 if (mAllowLongPress) {
933 mAllowLongPress = false;
934 // Try canceling the long press. It could also have been scheduled
935 // by a distant descendant, so use the mAllowLongPress flag to block
936 // everything
937 final View currentPage = getPageAt(mCurrentPage);
938 if (currentPage != null) {
939 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 }
941 }
942 }
943
Adam Cohene0f66b52010-11-23 15:06:07 -0800944 // This curve determines how the effect of scrolling over the limits of the page dimishes
945 // as the user pulls further and further from the bounds
946 private float overScrollInfluenceCurve(float f) {
947 f -= 1.0f;
948 return f * f * f + 1.0f;
949 }
950
Adam Cohen68d73932010-11-15 10:50:58 -0800951 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800952 int screenSize = getMeasuredWidth();
953
954 float f = (amount / screenSize);
955
956 if (f == 0) return;
957 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
958
Adam Cohen7bfc9792011-01-28 13:52:37 -0800959 // Clamp this factor, f, to -1 < f < 1
960 if (Math.abs(f) >= 1) {
961 f /= Math.abs(f);
962 }
963
Adam Cohene0f66b52010-11-23 15:06:07 -0800964 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800965 if (amount < 0) {
966 mScrollX = overScrollAmount;
967 } else {
968 mScrollX = mMaxScrollX + overScrollAmount;
969 }
970 invalidate();
971 }
972
Michael Jurkac5b262c2011-01-12 20:24:50 -0800973 protected float maxOverScroll() {
974 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
975 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
976 float f = 1.0f;
977 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
978 return OVERSCROLL_DAMP_FACTOR * f;
979 }
980
Winson Chung321e9ee2010-08-09 13:37:56 -0700981 @Override
982 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800983 // Skip touch handling if there are no pages to swipe
984 if (getChildCount() <= 0) return super.onTouchEvent(ev);
985
Michael Jurkab8f06722010-10-10 15:58:46 -0700986 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700987
988 final int action = ev.getAction();
989
990 switch (action & MotionEvent.ACTION_MASK) {
991 case MotionEvent.ACTION_DOWN:
992 /*
993 * If being flinged and user touches, stop the fling. isFinished
994 * will be false if being flinged.
995 */
996 if (!mScroller.isFinished()) {
997 mScroller.abortAnimation();
998 }
999
1000 // Remember where the motion event started
1001 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001002 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001003 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001005 if (mTouchState == TOUCH_STATE_SCROLLING) {
1006 pageBeginMoving();
1007 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 break;
1009
1010 case MotionEvent.ACTION_MOVE:
1011 if (mTouchState == TOUCH_STATE_SCROLLING) {
1012 // Scroll to follow the motion event
1013 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1014 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001015 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001016
Adam Cohenaefd4e12011-02-14 16:39:38 -08001017 mTotalMotionX += Math.abs(deltaX);
1018
Winson Chungc0844aa2011-02-02 15:25:58 -08001019 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1020 // keep the remainder because we are actually testing if we've moved from the last
1021 // scrolled position (which is discrete).
1022 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001023 mTouchX += deltaX;
1024 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1025 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001026 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001028 } else {
1029 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001031 mLastMotionX = x;
1032 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 } else {
1034 awakenScrollBars();
1035 }
Adam Cohen564976a2010-10-13 18:52:07 -07001036 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001037 determineScrollingStart(ev);
1038 }
1039 break;
1040
1041 case MotionEvent.ACTION_UP:
1042 if (mTouchState == TOUCH_STATE_SCROLLING) {
1043 final int activePointerId = mActivePointerId;
1044 final int pointerIndex = ev.findPointerIndex(activePointerId);
1045 final float x = ev.getX(pointerIndex);
1046 final VelocityTracker velocityTracker = mVelocityTracker;
1047 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1048 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001049 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001050 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001051 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001052
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001053 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1054
Adam Cohenaefd4e12011-02-14 16:39:38 -08001055 // In the case that the page is moved far to one direction and then is flung
1056 // in the opposite direction, we use a threshold to determine whether we should
1057 // just return to the starting page, or if we should skip one further.
1058 boolean returnToOriginalPage = false;
1059 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001060 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001061 Math.signum(velocityX) != Math.signum(deltaX)) {
1062 returnToOriginalPage = true;
1063 }
1064
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001065 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001066 Math.abs(velocityX) > snapVelocity;
1067
1068 int finalPage;
1069 // We give flings precedence over large moves, which is why we short-circuit our
1070 // test for a large move if a fling has been registered. That is, a large
1071 // move to the left and fling to the right will register as a fling to the right.
1072 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1073 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1074 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1075 snapToPageWithVelocity(finalPage, velocityX);
1076 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1077 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001078 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001079 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1080 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001081 } else {
1082 snapToDestination();
1083 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001084 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001085 // at this point we have not moved beyond the touch slop
1086 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1087 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001088 int nextPage = Math.max(0, mCurrentPage - 1);
1089 if (nextPage != mCurrentPage) {
1090 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001091 } else {
1092 snapToDestination();
1093 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001094 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 // at this point we have not moved beyond the touch slop
1096 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1097 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001098 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1099 if (nextPage != mCurrentPage) {
1100 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001101 } else {
1102 snapToDestination();
1103 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001104 } else {
1105 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 }
1107 mTouchState = TOUCH_STATE_REST;
1108 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001109 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001110 break;
1111
1112 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001113 if (mTouchState == TOUCH_STATE_SCROLLING) {
1114 snapToDestination();
1115 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001116 mTouchState = TOUCH_STATE_REST;
1117 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001118 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001119 break;
1120
1121 case MotionEvent.ACTION_POINTER_UP:
1122 onSecondaryPointerUp(ev);
1123 break;
1124 }
1125
1126 return true;
1127 }
1128
Winson Chung185d7162011-02-28 13:47:29 -08001129 @Override
1130 public boolean onGenericMotionEvent(MotionEvent event) {
1131 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1132 switch (event.getAction()) {
1133 case MotionEvent.ACTION_SCROLL: {
1134 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1135 final float vscroll;
1136 final float hscroll;
1137 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1138 vscroll = 0;
1139 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1140 } else {
1141 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1142 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1143 }
1144 if (hscroll != 0 || vscroll != 0) {
1145 if (hscroll > 0 || vscroll > 0) {
1146 scrollRight();
1147 } else {
1148 scrollLeft();
1149 }
1150 return true;
1151 }
1152 }
1153 }
1154 }
1155 return super.onGenericMotionEvent(event);
1156 }
1157
Michael Jurkab8f06722010-10-10 15:58:46 -07001158 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1159 if (mVelocityTracker == null) {
1160 mVelocityTracker = VelocityTracker.obtain();
1161 }
1162 mVelocityTracker.addMovement(ev);
1163 }
1164
1165 private void releaseVelocityTracker() {
1166 if (mVelocityTracker != null) {
1167 mVelocityTracker.recycle();
1168 mVelocityTracker = null;
1169 }
1170 }
1171
Winson Chung321e9ee2010-08-09 13:37:56 -07001172 private void onSecondaryPointerUp(MotionEvent ev) {
1173 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1174 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1175 final int pointerId = ev.getPointerId(pointerIndex);
1176 if (pointerId == mActivePointerId) {
1177 // This was our active pointer going up. Choose a new
1178 // active pointer and adjust accordingly.
1179 // TODO: Make this decision more intelligent.
1180 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1181 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1182 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001183 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001184 mActivePointerId = ev.getPointerId(newPointerIndex);
1185 if (mVelocityTracker != null) {
1186 mVelocityTracker.clear();
1187 }
1188 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001189 if (mTouchState == TOUCH_STATE_REST) {
1190 onWallpaperTap(ev);
1191 }
1192 }
1193
Winson Chungf0ea4d32011-06-06 14:27:16 -07001194 protected void onWallpaperTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001195
1196 @Override
1197 public void requestChildFocus(View child, View focused) {
1198 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001199 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001200 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001201 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 }
1203 }
1204
Winson Chunge3193b92010-09-10 11:44:42 -07001205 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1206 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001207 int left;
1208 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001209 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001210 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001211 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001212 if (left <= relativeOffset && relativeOffset <= right) {
1213 return i;
1214 }
Winson Chunge3193b92010-09-10 11:44:42 -07001215 }
1216 return -1;
1217 }
1218
Winson Chung1908d072011-02-24 18:09:44 -08001219 protected void setMinimumWidthOverride(int minimumWidth) {
1220 mMinimumWidth = minimumWidth;
1221 }
Winson Chung34b23d52011-03-18 11:29:34 -07001222 protected void resetMinimumWidthOverride() {
1223 mMinimumWidth = 0;
1224 }
Winson Chung1908d072011-02-24 18:09:44 -08001225
1226 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001227 // This functions are called enough times that it actually makes a difference in the
1228 // profiler -- so just inline the max() here
1229 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1230 final int minWidth = mMinimumWidth;
1231 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001232 }
1233
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001235 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 }
1237
1238 protected int getChildOffset(int index) {
1239 if (getChildCount() == 0)
1240 return 0;
1241
1242 int offset = getRelativeChildOffset(0);
1243 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001244 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 }
1246 return offset;
1247 }
1248
Michael Jurkad3ef3062010-11-23 16:23:58 -08001249 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001250 // This functions are called enough times that it actually makes a difference in the
1251 // profiler -- so just inline the max() here
1252 final int measuredWidth = child.getMeasuredWidth();
1253 final int minWidth = mMinimumWidth;
1254 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1255 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001256 }
1257
Adam Cohend19d3ca2010-09-15 14:43:42 -07001258 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 int minDistanceFromScreenCenter = getMeasuredWidth();
1260 int minDistanceFromScreenCenterIndex = -1;
1261 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1262 final int childCount = getChildCount();
1263 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001264 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001265 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 int halfChildWidth = (childWidth / 2);
1267 int childCenter = getChildOffset(i) + halfChildWidth;
1268 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1269 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1270 minDistanceFromScreenCenter = distanceFromScreenCenter;
1271 minDistanceFromScreenCenterIndex = i;
1272 }
1273 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001274 return minDistanceFromScreenCenterIndex;
1275 }
1276
1277 protected void snapToDestination() {
1278 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 }
1280
Adam Cohene0f66b52010-11-23 15:06:07 -08001281 private static class ScrollInterpolator implements Interpolator {
1282 public ScrollInterpolator() {
1283 }
1284
1285 public float getInterpolation(float t) {
1286 t -= 1.0f;
1287 return t*t*t*t*t + 1;
1288 }
1289 }
1290
1291 // We want the duration of the page snap animation to be influenced by the distance that
1292 // the screen has to travel, however, we don't want this duration to be effected in a
1293 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1294 // of travel has on the overall snap duration.
1295 float distanceInfluenceForSnapDuration(float f) {
1296 f -= 0.5f; // center the values about 0.
1297 f *= 0.3f * Math.PI / 2.0f;
1298 return (float) Math.sin(f);
1299 }
1300
Michael Jurka0142d492010-08-25 17:46:15 -07001301 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001302 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1303 int halfScreenSize = getMeasuredWidth() / 2;
1304
Winson Chung785d2eb2011-04-14 16:08:02 -07001305 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1306 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1307 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001308 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1309 int delta = newX - mUnboundedScrollX;
1310 int duration = 0;
1311
1312 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1313 // If the velocity is low enough, then treat this more as an automatic page advance
1314 // as opposed to an apparent physical response to flinging
1315 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1316 return;
1317 }
1318
1319 // Here we compute a "distance" that will be used in the computation of the overall
1320 // snap duration. This is a function of the actual distance that needs to be traveled;
1321 // we keep this value close to half screen size in order to reduce the variance in snap
1322 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001323 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001324 float distance = halfScreenSize + halfScreenSize *
1325 distanceInfluenceForSnapDuration(distanceRatio);
1326
1327 velocity = Math.abs(velocity);
1328 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1329
1330 // we want the page's snap velocity to approximately match the velocity at which the
1331 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001332 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1333 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001334
1335 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001336 }
1337
1338 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001339 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001340 }
1341
Michael Jurka0142d492010-08-25 17:46:15 -07001342 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001343 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001344
Winson Chung785d2eb2011-04-14 16:08:02 -07001345 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1346 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1347 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001348 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001349 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001351 snapToPage(whichPage, delta, duration);
1352 }
1353
1354 protected void snapToPage(int whichPage, int delta, int duration) {
1355 mNextPage = whichPage;
1356
1357 View focusedChild = getFocusedChild();
1358 if (focusedChild != null && whichPage != mCurrentPage &&
1359 focusedChild == getChildAt(mCurrentPage)) {
1360 focusedChild.clearFocus();
1361 }
1362
1363 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 awakenScrollBars(duration);
1365 if (duration == 0) {
1366 duration = Math.abs(delta);
1367 }
1368
1369 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001370 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001371
1372 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001373 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001374 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001375 invalidate();
1376 }
1377
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 public void scrollLeft() {
1379 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001380 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001381 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001382 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001383 }
1384 }
1385
1386 public void scrollRight() {
1387 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001388 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001390 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 }
1392 }
1393
Winson Chung86f77532010-08-24 11:08:22 -07001394 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001395 int result = -1;
1396 if (v != null) {
1397 ViewParent vp = v.getParent();
1398 int count = getChildCount();
1399 for (int i = 0; i < count; i++) {
1400 if (vp == getChildAt(i)) {
1401 return i;
1402 }
1403 }
1404 }
1405 return result;
1406 }
1407
1408 /**
1409 * @return True is long presses are still allowed for the current touch
1410 */
1411 public boolean allowLongPress() {
1412 return mAllowLongPress;
1413 }
1414
Michael Jurka0142d492010-08-25 17:46:15 -07001415 /**
1416 * Set true to allow long-press events to be triggered, usually checked by
1417 * {@link Launcher} to accept or block dpad-initiated long-presses.
1418 */
1419 public void setAllowLongPress(boolean allowLongPress) {
1420 mAllowLongPress = allowLongPress;
1421 }
1422
Winson Chung321e9ee2010-08-09 13:37:56 -07001423 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001424 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001425
1426 SavedState(Parcelable superState) {
1427 super(superState);
1428 }
1429
1430 private SavedState(Parcel in) {
1431 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001432 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 }
1434
1435 @Override
1436 public void writeToParcel(Parcel out, int flags) {
1437 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001438 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 }
1440
1441 public static final Parcelable.Creator<SavedState> CREATOR =
1442 new Parcelable.Creator<SavedState>() {
1443 public SavedState createFromParcel(Parcel in) {
1444 return new SavedState(in);
1445 }
1446
1447 public SavedState[] newArray(int size) {
1448 return new SavedState[size];
1449 }
1450 };
1451 }
1452
Winson Chung86f77532010-08-24 11:08:22 -07001453 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001454 if (mContentIsRefreshable) {
1455 final int count = getChildCount();
1456 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001457 int lowerPageBound = getAssociatedLowerPageBound(page);
1458 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1460 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001461 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001462 Page layout = (Page) getChildAt(i);
1463 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001464 if (lowerPageBound <= i && i <= upperPageBound) {
1465 if (mDirtyPageContent.get(i)) {
1466 syncPageItems(i);
1467 mDirtyPageContent.set(i, false);
1468 }
1469 } else {
1470 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001471 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001472 }
1473 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001474 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001475 }
1476 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001477 }
1478 }
1479
Winson Chunge3193b92010-09-10 11:44:42 -07001480 protected int getAssociatedLowerPageBound(int page) {
1481 return Math.max(0, page - 1);
1482 }
1483 protected int getAssociatedUpperPageBound(int page) {
1484 final int count = getChildCount();
1485 return Math.min(page + 1, count - 1);
1486 }
1487
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001488 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001489 if (isChoiceMode(CHOICE_MODE_NONE)) {
1490 mChoiceMode = mode;
1491 mActionMode = startActionMode(callback);
1492 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001493 }
1494
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001495 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001496 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001497 mChoiceMode = CHOICE_MODE_NONE;
1498 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001499 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001500 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001501 }
1502 }
1503
1504 protected boolean isChoiceMode(int mode) {
1505 return mChoiceMode == mode;
1506 }
1507
1508 protected ArrayList<Checkable> getCheckedGrandchildren() {
1509 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1510 final int childCount = getChildCount();
1511 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001512 Page layout = (Page) getChildAt(i);
1513 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001514 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001515 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001516 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001517 checked.add((Checkable) v);
1518 }
1519 }
1520 }
1521 return checked;
1522 }
1523
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001524 /**
1525 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1526 * Otherwise, returns null.
1527 */
1528 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001529 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001530 final int childCount = getChildCount();
1531 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001532 Page layout = (Page) getChildAt(i);
1533 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001534 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001535 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001536 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1537 return (Checkable) v;
1538 }
1539 }
1540 }
1541 }
1542 return null;
1543 }
1544
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001545 protected void resetCheckedGrandchildren() {
1546 // loop through children, and set all of their children to _not_ be checked
1547 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1548 for (int i = 0; i < checked.size(); ++i) {
1549 final Checkable c = checked.get(i);
1550 c.setChecked(false);
1551 }
1552 }
1553
Winson Chung86f77532010-08-24 11:08:22 -07001554 /**
1555 * This method is called ONLY to synchronize the number of pages that the paged view has.
1556 * To actually fill the pages with information, implement syncPageItems() below. It is
1557 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1558 * and therefore, individual page items do not need to be updated in this method.
1559 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001560 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001561
1562 /**
1563 * This method is called to synchronize the items that are on a particular page. If views on
1564 * the page can be reused, then they should be updated within this method.
1565 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001566 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001567
Michael Jurka983e3fd2011-05-26 17:10:29 -07001568 protected void postInvalidatePageData(final boolean clearViews) {
1569 post(new Runnable() {
1570 // post the call to avoid a call to requestLayout from a layout pass
1571 public void run() {
1572 if (clearViews) {
1573 removeAllViews();
1574 }
1575 invalidatePageData();
1576 }
1577 });
1578 }
1579
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001580 protected void invalidatePageData() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001581 if (!mIsDataReady) {
1582 return;
1583 }
1584
Michael Jurka0142d492010-08-25 17:46:15 -07001585 if (mContentIsRefreshable) {
1586 // Update all the pages
1587 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001588
Michael Jurka0142d492010-08-25 17:46:15 -07001589 // Mark each of the pages as dirty
1590 final int count = getChildCount();
1591 mDirtyPageContent.clear();
1592 for (int i = 0; i < count; ++i) {
1593 mDirtyPageContent.add(true);
1594 }
1595
1596 // Load any pages that are necessary for the current window of views
1597 loadAssociatedPages(mCurrentPage);
1598 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001599 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001600 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001601 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001602 }
1603}