blob: 605f4b69fe87f9fdb3cbd55e2fa110f8c87a334f [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;
Winson Chunga12a2502010-12-20 14:41:35 -080080 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080081 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070082 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070083 private VelocityTracker mVelocityTracker;
84
85 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080086 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080087 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080088 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080089 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070090 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected final static int TOUCH_STATE_REST = 0;
93 protected final static int TOUCH_STATE_SCROLLING = 1;
94 protected final static int TOUCH_STATE_PREV_PAGE = 2;
95 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070096 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070097
Michael Jurka0142d492010-08-25 17:46:15 -070098 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka7426c422010-11-11 15:23:47 -0800102 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka7426c422010-11-11 15:23:47 -0800104 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105 private int mPagingTouchSlop;
106 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800107 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700108 protected int mPageSpacing;
109 protected int mPageLayoutPaddingTop;
110 protected int mPageLayoutPaddingBottom;
111 protected int mPageLayoutPaddingLeft;
112 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700113 protected int mPageLayoutWidthGap;
114 protected int mPageLayoutHeightGap;
Michael Jurka36fcb742011-04-06 17:08:58 -0700115 protected int mPageLayoutMaxHeight;
Michael Jurka87b14902011-05-25 22:13:09 -0700116 protected int mCellCountX = 0;
117 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700118 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800119 protected boolean mAllowOverScroll = true;
120 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Michael Jurka8c920dd2011-01-20 14:16:56 -0800122 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800123 protected float mLayoutScale = 1.0f;
124
Michael Jurka5f1c5092010-09-03 14:15:02 -0700125 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700126
Michael Jurka5f1c5092010-09-03 14:15:02 -0700127 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128
Winson Chung86f77532010-08-24 11:08:22 -0700129 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Winson Chung86f77532010-08-24 11:08:22 -0700131 private ArrayList<Boolean> mDirtyPageContent;
132 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700133
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700134 // choice modes
135 protected static final int CHOICE_MODE_NONE = 0;
136 protected static final int CHOICE_MODE_SINGLE = 1;
137 // Multiple selection mode is not supported by all Launcher actions atm
138 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700139
Michael Jurkae17e19c2010-09-28 11:01:39 -0700140 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700141 private ActionMode mActionMode;
142
Winson Chung04998342011-01-05 13:54:43 -0800143 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
144 // AllApps and Customize, and allows them to share holographic icons for the application view
145 // (which is in both).
146 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700147
Michael Jurka0142d492010-08-25 17:46:15 -0700148 // If true, syncPages and syncPageItems will be called to refresh pages
149 protected boolean mContentIsRefreshable = true;
150
151 // If true, modify alpha of neighboring pages as user scrolls left/right
152 protected boolean mFadeInAdjacentScreens = true;
153
154 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
155 // to switch to a new page
156 protected boolean mUsePagingTouchSlop = true;
157
158 // If true, the subclass should directly update mScrollX itself in its computeScroll method
159 // (SmoothPagedView does this)
160 protected boolean mDeferScrollUpdate = false;
161
Patrick Dubroy1262e362010-10-06 15:49:50 -0700162 protected boolean mIsPageMoving = false;
163
Winson Chung86f77532010-08-24 11:08:22 -0700164 public interface PageSwitchListener {
165 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 }
167
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 public PagedView(Context context) {
169 this(context, null);
170 }
171
172 public PagedView(Context context, AttributeSet attrs) {
173 this(context, attrs, 0);
174 }
175
176 public PagedView(Context context, AttributeSet attrs, int defStyle) {
177 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700178 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700179
Adam Cohen9c4949e2010-10-05 12:27:22 -0700180 TypedArray a = context.obtainStyledAttributes(attrs,
181 R.styleable.PagedView, defStyle, 0);
182 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
183 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800184 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700185 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800186 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800188 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700189 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800190 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700191 mPageLayoutWidthGap = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutWidthGap, -1);
193 mPageLayoutHeightGap = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutHeightGap, -1);
Michael Jurka36fcb742011-04-06 17:08:58 -0700195 mPageLayoutMaxHeight = a.getDimensionPixelSize(
196 R.styleable.PagedView_pageLayoutMaxHeight, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700197 a.recycle();
198
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700200 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202
203 /**
204 * Initializes various states for this workspace.
205 */
Michael Jurka0142d492010-08-25 17:46:15 -0700206 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700207 mDirtyPageContent = new ArrayList<Boolean>();
208 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800209 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700210 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700211 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
217 }
218
Winson Chung86f77532010-08-24 11:08:22 -0700219 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
220 mPageSwitchListener = pageSwitchListener;
221 if (mPageSwitchListener != null) {
222 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224 }
225
226 /**
Winson Chung86f77532010-08-24 11:08:22 -0700227 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 *
Winson Chung86f77532010-08-24 11:08:22 -0700229 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 */
Winson Chung86f77532010-08-24 11:08:22 -0700231 int getCurrentPage() {
232 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 }
234
Winson Chung86f77532010-08-24 11:08:22 -0700235 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Winson Chung86f77532010-08-24 11:08:22 -0700239 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
243 int getScrollWidth() {
244 return getWidth();
245 }
246
Adam Cohenbe909342011-01-28 17:02:58 -0800247 public int getTouchState() {
248 return mTouchState;
249 }
250
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800252 * Updates the scroll of the current page immediately to its final scroll position. We use this
253 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
254 * the previous tab page.
255 */
256 protected void updateCurrentPageScroll() {
257 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
258 scrollTo(newX, 0);
259 mScroller.setFinalX(newX);
260 }
261
262 /**
Winson Chung86f77532010-08-24 11:08:22 -0700263 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 */
Winson Chung86f77532010-08-24 11:08:22 -0700265 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800266 if (!mScroller.isFinished()) {
267 mScroller.abortAnimation();
268 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800269 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
270 // the default
271 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800272 return;
273 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700274
Winson Chung86f77532010-08-24 11:08:22 -0700275 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800276 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700277 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800278 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 }
280
Michael Jurka0142d492010-08-25 17:46:15 -0700281 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700282 if (mPageSwitchListener != null) {
283 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700284 }
285 }
286
Michael Jurkace7e05f2011-02-01 22:02:35 -0800287 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700288 mIsPageMoving = true;
289 onPageBeginMoving();
290 }
291
Michael Jurkace7e05f2011-02-01 22:02:35 -0800292 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 onPageEndMoving();
294 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700295 }
296
Adam Cohen26976d92011-03-22 15:33:33 -0700297 protected boolean isPageMoving() {
298 return mIsPageMoving;
299 }
300
Michael Jurka0142d492010-08-25 17:46:15 -0700301 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700302 protected void onPageBeginMoving() {
303 }
304
305 // a method that subclasses can override to add behavior
306 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700307 }
308
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 /**
Winson Chung86f77532010-08-24 11:08:22 -0700310 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 *
312 * @param l The listener used to respond to long clicks.
313 */
314 @Override
315 public void setOnLongClickListener(OnLongClickListener l) {
316 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700317 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700319 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
321 }
322
323 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800324 public void scrollBy(int x, int y) {
325 scrollTo(mUnboundedScrollX + x, mScrollY + y);
326 }
327
328 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700329 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800330 mUnboundedScrollX = x;
331
332 if (x < 0) {
333 super.scrollTo(0, y);
334 if (mAllowOverScroll) {
335 overScroll(x);
336 }
337 } else if (x > mMaxScrollX) {
338 super.scrollTo(mMaxScrollX, y);
339 if (mAllowOverScroll) {
340 overScroll(x - mMaxScrollX);
341 }
342 } else {
343 super.scrollTo(x, y);
344 }
345
Michael Jurka0142d492010-08-25 17:46:15 -0700346 mTouchX = x;
347 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
348 }
349
350 // we moved this functionality to a helper function so SmoothPagedView can reuse it
351 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700353 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700355 invalidate();
356 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700357 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700358 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700359 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700360 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700361 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800362 // We don't want to trigger a page end moving unless the page has settled
363 // and the user has stopped scrolling
364 if (mTouchState == TOUCH_STATE_REST) {
365 pageEndMoving();
366 }
Michael Jurka0142d492010-08-25 17:46:15 -0700367 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700368 }
Michael Jurka0142d492010-08-25 17:46:15 -0700369 return false;
370 }
371
372 @Override
373 public void computeScroll() {
374 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 }
376
377 @Override
378 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
379 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
380 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
381 if (widthMode != MeasureSpec.EXACTLY) {
382 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
383 }
384
Adam Lesinski6b879f02010-11-04 16:15:23 -0700385 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
386 * of the All apps view on XLarge displays to not take up more space then it needs. Width
387 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
388 * each page to have the same width.
389 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700391 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
392 int maxChildHeight = 0;
393
394 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700395
Michael Jurka36fcb742011-04-06 17:08:58 -0700396 if (mPageLayoutMaxHeight != -1) {
397 heightSize = Math.min(mPageLayoutMaxHeight, heightSize);
398 }
399
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700401 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700402 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 final int childCount = getChildCount();
404 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700405 // disallowing padding in paged view (just pass 0)
406 final View child = getChildAt(i);
407 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
408
409 int childWidthMode;
410 if (lp.width == LayoutParams.WRAP_CONTENT) {
411 childWidthMode = MeasureSpec.AT_MOST;
412 } else {
413 childWidthMode = MeasureSpec.EXACTLY;
414 }
415
416 int childHeightMode;
417 if (lp.height == LayoutParams.WRAP_CONTENT) {
418 childHeightMode = MeasureSpec.AT_MOST;
419 } else {
420 childHeightMode = MeasureSpec.EXACTLY;
421 }
422
423 final int childWidthMeasureSpec =
424 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
425 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700426 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700427
428 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700429 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700430 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
431 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700432 }
433
434 if (heightMode == MeasureSpec.AT_MOST) {
435 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800437 if (childCount > 0) {
438 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
439 } else {
440 mMaxScrollX = 0;
441 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700442
443 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 }
445
Michael Jurka8c920dd2011-01-20 14:16:56 -0800446 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800447 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
448 int delta = newX - mScrollX;
449
Michael Jurka8c920dd2011-01-20 14:16:56 -0800450 final int pageCount = getChildCount();
451 for (int i = 0; i < pageCount; i++) {
452 View page = (View) getChildAt(i);
453 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800454 }
455 setCurrentPage(newCurrentPage);
456 }
457
Michael Jurka8c920dd2011-01-20 14:16:56 -0800458 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
459 // 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 -0800460 // tightens the layout accordingly
461 public void setLayoutScale(float childrenScale) {
462 mLayoutScale = childrenScale;
463
464 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
465 int childCount = getChildCount();
466 float childrenX[] = new float[childCount];
467 float childrenY[] = new float[childCount];
468 for (int i = 0; i < childCount; i++) {
469 final View child = getChildAt(i);
470 childrenX[i] = child.getX();
471 childrenY[i] = child.getY();
472 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700473 // Trigger a full re-layout (never just call onLayout directly!)
474 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
475 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
476 requestLayout();
477 measure(widthSpec, heightSpec);
478 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800479 for (int i = 0; i < childCount; i++) {
480 final View child = getChildAt(i);
481 child.setX(childrenX[i]);
482 child.setY(childrenY[i]);
483 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700484
Michael Jurkad3ef3062010-11-23 16:23:58 -0800485 // Also, the page offset has changed (since the pages are now smaller);
486 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800487 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800488 }
489
Winson Chung321e9ee2010-08-09 13:37:56 -0700490 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700491 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700492 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700493 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
494 setHorizontalScrollBarEnabled(false);
495 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
496 scrollTo(newX, 0);
497 mScroller.setFinalX(newX);
498 setHorizontalScrollBarEnabled(true);
499 mFirstLayout = false;
500 }
501
Adam Lesinski6b879f02010-11-04 16:15:23 -0700502 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700503 final int childCount = getChildCount();
504 int childLeft = 0;
505 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700506 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
507 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700508 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700509 }
510
511 for (int i = 0; i < childCount; i++) {
512 final View child = getChildAt(i);
513 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800514 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700515 final int childHeight = child.getMeasuredHeight();
516 int childTop = mPaddingTop;
517 if (mCenterPagesVertically) {
518 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
519 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800520
Winson Chung785d2eb2011-04-14 16:08:02 -0700521 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700522 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800523 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700524 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 }
526 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800527 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
528 mFirstLayout = false;
529 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 }
531
Winson Chung03929772011-02-23 17:07:10 -0800532 protected void forceUpdateAdjacentPagesAlpha() {
533 mDirtyPageAlpha = true;
534 updateAdjacentPagesAlpha();
535 }
536
Winson Chunge3193b92010-09-10 11:44:42 -0700537 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700538 if (mFadeInAdjacentScreens) {
539 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700540 int screenWidth = getMeasuredWidth();
541 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700542 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700543 final int childCount = getChildCount();
544 for (int i = 0; i < childCount; ++i) {
545 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800546 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700547 int halfChildWidth = (childWidth / 2);
548 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700549
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700550 // On the first layout, we may not have a width nor a proper offset, so for now
551 // we should just assume full page width (and calculate the offset according to
552 // that).
553 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700554 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700555 childCenter = (i * childWidth) + (childWidth / 2);
556 }
557
Winson Chunge8878e32010-09-15 20:37:09 -0700558 int d = halfChildWidth;
559 int distanceFromScreenCenter = childCenter - screenCenter;
560 if (distanceFromScreenCenter > 0) {
561 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800562 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700563 }
Michael Jurka0142d492010-08-25 17:46:15 -0700564 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700565 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800566 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700567 }
Michael Jurka0142d492010-08-25 17:46:15 -0700568 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700569 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700570
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700571 // Preventing potential divide-by-zero
572 d = Math.max(1, d);
573
Winson Chunge8878e32010-09-15 20:37:09 -0700574 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
575 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
576 float alpha = 1.0f - dimAlpha;
577
Adam Cohenf34bab52010-09-30 14:11:56 -0700578 if (alpha < ALPHA_QUANTIZE_LEVEL) {
579 alpha = 0.0f;
580 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
581 alpha = 1.0f;
582 }
583
Michael Jurkaa40829a2011-02-16 18:02:45 -0800584 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
585 // this optimization causes alpha to not be properly updated sometimes (repro
586 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
587 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
588 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800589 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700590 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800591 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700592 }
Michael Jurka0142d492010-08-25 17:46:15 -0700593 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
595 }
Winson Chunge3193b92010-09-10 11:44:42 -0700596 }
597
Adam Cohenf34bab52010-09-30 14:11:56 -0700598 protected void screenScrolled(int screenCenter) {
599 }
600
Winson Chunge3193b92010-09-10 11:44:42 -0700601 @Override
602 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700603 int halfScreenSize = getMeasuredWidth() / 2;
604 int screenCenter = mScrollX + halfScreenSize;
605
606 if (screenCenter != mLastScreenCenter) {
607 screenScrolled(screenCenter);
608 updateAdjacentPagesAlpha();
609 mLastScreenCenter = screenCenter;
610 }
Michael Jurka0142d492010-08-25 17:46:15 -0700611
612 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700613 // As an optimization, this code assumes that all pages have the same width as the 0th
614 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700615 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700616 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800617 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700618 final int screenWidth = getMeasuredWidth();
619 int x = getRelativeChildOffset(0) + pageWidth;
620 int leftScreen = 0;
621 int rightScreen = 0;
622 while (x <= mScrollX) {
623 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800624 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700625 }
626 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800627 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700628 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800629 if (rightScreen < pageCount) {
630 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
631 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700632 }
633 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700634
Michael Jurkac4fb9172010-09-02 17:19:20 -0700635 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800636 // Clip to the bounds
637 canvas.save();
638 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
639 mScrollY + mBottom - mTop);
640
Michael Jurkac4fb9172010-09-02 17:19:20 -0700641 for (int i = leftScreen; i <= rightScreen; i++) {
642 drawChild(canvas, getChildAt(i), drawingTime);
643 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800644 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700645 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
647
648 @Override
649 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700650 int page = indexOfChild(child);
651 if (page != mCurrentPage || !mScroller.isFinished()) {
652 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 return true;
654 }
655 return false;
656 }
657
658 @Override
659 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700660 int focusablePage;
661 if (mNextPage != INVALID_PAGE) {
662 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700663 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700664 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 }
Winson Chung86f77532010-08-24 11:08:22 -0700666 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 if (v != null) {
668 v.requestFocus(direction, previouslyFocusedRect);
669 }
670 return false;
671 }
672
673 @Override
674 public boolean dispatchUnhandledMove(View focused, int direction) {
675 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700676 if (getCurrentPage() > 0) {
677 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700678 return true;
679 }
680 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700681 if (getCurrentPage() < getPageCount() - 1) {
682 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700683 return true;
684 }
685 }
686 return super.dispatchUnhandledMove(focused, direction);
687 }
688
689 @Override
690 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700691 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
692 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700693 }
694 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700695 if (mCurrentPage > 0) {
696 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 }
698 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700699 if (mCurrentPage < getPageCount() - 1) {
700 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 }
702 }
703 }
704
705 /**
706 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700707 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 *
Winson Chung86f77532010-08-24 11:08:22 -0700709 * This happens when live folders requery, and if they're off page, they
710 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700711 */
712 @Override
713 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700714 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700715 View v = focused;
716 while (true) {
717 if (v == current) {
718 super.focusableViewAvailable(focused);
719 return;
720 }
721 if (v == this) {
722 return;
723 }
724 ViewParent parent = v.getParent();
725 if (parent instanceof View) {
726 v = (View)v.getParent();
727 } else {
728 return;
729 }
730 }
731 }
732
733 /**
734 * {@inheritDoc}
735 */
736 @Override
737 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
738 if (disallowIntercept) {
739 // We need to make sure to cancel our long press if
740 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700741 final View currentPage = getChildAt(mCurrentPage);
742 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 }
744 super.requestDisallowInterceptTouchEvent(disallowIntercept);
745 }
746
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800747 /**
748 * Return true if a tap at (x, y) should trigger a flip to the previous page.
749 */
750 protected boolean hitsPreviousPage(float x, float y) {
751 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
752 }
753
754 /**
755 * Return true if a tap at (x, y) should trigger a flip to the next page.
756 */
757 protected boolean hitsNextPage(float x, float y) {
758 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
759 }
760
Winson Chung321e9ee2010-08-09 13:37:56 -0700761 @Override
762 public boolean onInterceptTouchEvent(MotionEvent ev) {
763 /*
764 * This method JUST determines whether we want to intercept the motion.
765 * If we return true, onTouchEvent will be called and we do the actual
766 * scrolling there.
767 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800768 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700769
Winson Chung45e1d6e2010-11-09 17:19:49 -0800770 // Skip touch handling if there are no pages to swipe
771 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
772
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 /*
774 * Shortcut the most recurring case: the user is in the dragging
775 * state and he is moving his finger. We want to intercept this
776 * motion.
777 */
778 final int action = ev.getAction();
779 if ((action == MotionEvent.ACTION_MOVE) &&
780 (mTouchState == TOUCH_STATE_SCROLLING)) {
781 return true;
782 }
783
Winson Chung321e9ee2010-08-09 13:37:56 -0700784 switch (action & MotionEvent.ACTION_MASK) {
785 case MotionEvent.ACTION_MOVE: {
786 /*
787 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
788 * whether the user has moved far enough from his original down touch.
789 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700790 if (mActivePointerId != INVALID_POINTER) {
791 determineScrollingStart(ev);
792 break;
793 }
794 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
795 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
796 // i.e. fall through to the next case (don't break)
797 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
798 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700799 }
800
801 case MotionEvent.ACTION_DOWN: {
802 final float x = ev.getX();
803 final float y = ev.getY();
804 // Remember location of down touch
805 mDownMotionX = x;
806 mLastMotionX = x;
807 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800808 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800809 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 mActivePointerId = ev.getPointerId(0);
811 mAllowLongPress = true;
812
813 /*
814 * If being flinged and user touches the screen, initiate drag;
815 * otherwise don't. mScroller.isFinished should be false when
816 * being flinged.
817 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700818 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700819 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
820 if (finishedScrolling) {
821 mTouchState = TOUCH_STATE_REST;
822 mScroller.abortAnimation();
823 } else {
824 mTouchState = TOUCH_STATE_SCROLLING;
825 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700826
Winson Chung86f77532010-08-24 11:08:22 -0700827 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800829 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800831 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800833 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 mTouchState = TOUCH_STATE_NEXT_PAGE;
835 }
836 }
837 }
838 break;
839 }
840
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800842 onWallpaperTap(ev);
843 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 mTouchState = TOUCH_STATE_REST;
845 mAllowLongPress = false;
846 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800847 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 break;
849
850 case MotionEvent.ACTION_POINTER_UP:
851 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800852 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 break;
854 }
855
856 /*
857 * The only time we want to intercept motion events is if we are in the
858 * drag mode.
859 */
860 return mTouchState != TOUCH_STATE_REST;
861 }
862
Winson Chung80baf5a2010-08-09 16:03:15 -0700863 protected void animateClickFeedback(View v, final Runnable r) {
864 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800865 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
866 loadAnimator(mContext, R.anim.paged_view_click_feedback);
867 anim.setTarget(v);
868 anim.addListener(new AnimatorListenerAdapter() {
869 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700870 r.run();
871 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700872 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800873 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700874 }
875
Adam Cohenf8d28232011-02-01 21:47:00 -0800876 protected void determineScrollingStart(MotionEvent ev) {
877 determineScrollingStart(ev, 1.0f);
878 }
879
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 /*
881 * Determines if we should change the touch state to start scrolling after the
882 * user moves their touch point too far.
883 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800884 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700885 /*
886 * Locally do absolute value. mLastMotionX is set to the y value
887 * of the down event.
888 */
889 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
890 final float x = ev.getX(pointerIndex);
891 final float y = ev.getY(pointerIndex);
892 final int xDiff = (int) Math.abs(x - mLastMotionX);
893 final int yDiff = (int) Math.abs(y - mLastMotionY);
894
Adam Cohenf8d28232011-02-01 21:47:00 -0800895 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 boolean xPaged = xDiff > mPagingTouchSlop;
897 boolean xMoved = xDiff > touchSlop;
898 boolean yMoved = yDiff > touchSlop;
899
Adam Cohenf8d28232011-02-01 21:47:00 -0800900 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700901 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 // Scroll if the user moved far enough along the X axis
903 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800904 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800906 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700907 mTouchX = mScrollX;
908 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
909 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 }
911 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800912 cancelCurrentPageLongPress();
913 }
914 }
915
916 protected void cancelCurrentPageLongPress() {
917 if (mAllowLongPress) {
918 mAllowLongPress = false;
919 // Try canceling the long press. It could also have been scheduled
920 // by a distant descendant, so use the mAllowLongPress flag to block
921 // everything
922 final View currentPage = getPageAt(mCurrentPage);
923 if (currentPage != null) {
924 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 }
926 }
927 }
928
Adam Cohene0f66b52010-11-23 15:06:07 -0800929 // This curve determines how the effect of scrolling over the limits of the page dimishes
930 // as the user pulls further and further from the bounds
931 private float overScrollInfluenceCurve(float f) {
932 f -= 1.0f;
933 return f * f * f + 1.0f;
934 }
935
Adam Cohen68d73932010-11-15 10:50:58 -0800936 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800937 int screenSize = getMeasuredWidth();
938
939 float f = (amount / screenSize);
940
941 if (f == 0) return;
942 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
943
Adam Cohen7bfc9792011-01-28 13:52:37 -0800944 // Clamp this factor, f, to -1 < f < 1
945 if (Math.abs(f) >= 1) {
946 f /= Math.abs(f);
947 }
948
Adam Cohene0f66b52010-11-23 15:06:07 -0800949 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800950 if (amount < 0) {
951 mScrollX = overScrollAmount;
952 } else {
953 mScrollX = mMaxScrollX + overScrollAmount;
954 }
955 invalidate();
956 }
957
Michael Jurkac5b262c2011-01-12 20:24:50 -0800958 protected float maxOverScroll() {
959 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
960 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
961 float f = 1.0f;
962 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
963 return OVERSCROLL_DAMP_FACTOR * f;
964 }
965
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 @Override
967 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800968 // Skip touch handling if there are no pages to swipe
969 if (getChildCount() <= 0) return super.onTouchEvent(ev);
970
Michael Jurkab8f06722010-10-10 15:58:46 -0700971 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700972
973 final int action = ev.getAction();
974
975 switch (action & MotionEvent.ACTION_MASK) {
976 case MotionEvent.ACTION_DOWN:
977 /*
978 * If being flinged and user touches, stop the fling. isFinished
979 * will be false if being flinged.
980 */
981 if (!mScroller.isFinished()) {
982 mScroller.abortAnimation();
983 }
984
985 // Remember where the motion event started
986 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -0800987 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800988 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700990 if (mTouchState == TOUCH_STATE_SCROLLING) {
991 pageBeginMoving();
992 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 break;
994
995 case MotionEvent.ACTION_MOVE:
996 if (mTouchState == TOUCH_STATE_SCROLLING) {
997 // Scroll to follow the motion event
998 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
999 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001000 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001001
Adam Cohenaefd4e12011-02-14 16:39:38 -08001002 mTotalMotionX += Math.abs(deltaX);
1003
Winson Chungc0844aa2011-02-02 15:25:58 -08001004 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1005 // keep the remainder because we are actually testing if we've moved from the last
1006 // scrolled position (which is discrete).
1007 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001008 mTouchX += deltaX;
1009 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1010 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001011 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001012 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001013 } else {
1014 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001016 mLastMotionX = x;
1017 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001018 } else {
1019 awakenScrollBars();
1020 }
Adam Cohen564976a2010-10-13 18:52:07 -07001021 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 determineScrollingStart(ev);
1023 }
1024 break;
1025
1026 case MotionEvent.ACTION_UP:
1027 if (mTouchState == TOUCH_STATE_SCROLLING) {
1028 final int activePointerId = mActivePointerId;
1029 final int pointerIndex = ev.findPointerIndex(activePointerId);
1030 final float x = ev.getX(pointerIndex);
1031 final VelocityTracker velocityTracker = mVelocityTracker;
1032 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1033 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001034 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001035 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001036 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001037
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001038 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1039
Adam Cohenaefd4e12011-02-14 16:39:38 -08001040 // In the case that the page is moved far to one direction and then is flung
1041 // in the opposite direction, we use a threshold to determine whether we should
1042 // just return to the starting page, or if we should skip one further.
1043 boolean returnToOriginalPage = false;
1044 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001045 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001046 Math.signum(velocityX) != Math.signum(deltaX)) {
1047 returnToOriginalPage = true;
1048 }
1049
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001050 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001051 Math.abs(velocityX) > snapVelocity;
1052
1053 int finalPage;
1054 // We give flings precedence over large moves, which is why we short-circuit our
1055 // test for a large move if a fling has been registered. That is, a large
1056 // move to the left and fling to the right will register as a fling to the right.
1057 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1058 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1059 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1060 snapToPageWithVelocity(finalPage, velocityX);
1061 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1062 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001063 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001064 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1065 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 } else {
1067 snapToDestination();
1068 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001069 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 // at this point we have not moved beyond the touch slop
1071 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1072 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001073 int nextPage = Math.max(0, mCurrentPage - 1);
1074 if (nextPage != mCurrentPage) {
1075 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001076 } else {
1077 snapToDestination();
1078 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001079 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001080 // at this point we have not moved beyond the touch slop
1081 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1082 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001083 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1084 if (nextPage != mCurrentPage) {
1085 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001086 } else {
1087 snapToDestination();
1088 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001089 } else {
1090 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001091 }
1092 mTouchState = TOUCH_STATE_REST;
1093 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001094 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 break;
1096
1097 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001098 if (mTouchState == TOUCH_STATE_SCROLLING) {
1099 snapToDestination();
1100 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001101 mTouchState = TOUCH_STATE_REST;
1102 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001103 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001104 break;
1105
1106 case MotionEvent.ACTION_POINTER_UP:
1107 onSecondaryPointerUp(ev);
1108 break;
1109 }
1110
1111 return true;
1112 }
1113
Winson Chung185d7162011-02-28 13:47:29 -08001114 @Override
1115 public boolean onGenericMotionEvent(MotionEvent event) {
1116 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1117 switch (event.getAction()) {
1118 case MotionEvent.ACTION_SCROLL: {
1119 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1120 final float vscroll;
1121 final float hscroll;
1122 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1123 vscroll = 0;
1124 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1125 } else {
1126 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1127 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1128 }
1129 if (hscroll != 0 || vscroll != 0) {
1130 if (hscroll > 0 || vscroll > 0) {
1131 scrollRight();
1132 } else {
1133 scrollLeft();
1134 }
1135 return true;
1136 }
1137 }
1138 }
1139 }
1140 return super.onGenericMotionEvent(event);
1141 }
1142
Michael Jurkab8f06722010-10-10 15:58:46 -07001143 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1144 if (mVelocityTracker == null) {
1145 mVelocityTracker = VelocityTracker.obtain();
1146 }
1147 mVelocityTracker.addMovement(ev);
1148 }
1149
1150 private void releaseVelocityTracker() {
1151 if (mVelocityTracker != null) {
1152 mVelocityTracker.recycle();
1153 mVelocityTracker = null;
1154 }
1155 }
1156
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 private void onSecondaryPointerUp(MotionEvent ev) {
1158 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1159 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1160 final int pointerId = ev.getPointerId(pointerIndex);
1161 if (pointerId == mActivePointerId) {
1162 // This was our active pointer going up. Choose a new
1163 // active pointer and adjust accordingly.
1164 // TODO: Make this decision more intelligent.
1165 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1166 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1167 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001168 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001169 mActivePointerId = ev.getPointerId(newPointerIndex);
1170 if (mVelocityTracker != null) {
1171 mVelocityTracker.clear();
1172 }
1173 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001174 if (mTouchState == TOUCH_STATE_REST) {
1175 onWallpaperTap(ev);
1176 }
1177 }
1178
1179 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 }
1181
1182 @Override
1183 public void requestChildFocus(View child, View focused) {
1184 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001185 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001186 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001187 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 }
1189 }
1190
Winson Chunge3193b92010-09-10 11:44:42 -07001191 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1192 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001193 int left;
1194 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001195 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001196 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001197 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001198 if (left <= relativeOffset && relativeOffset <= right) {
1199 return i;
1200 }
Winson Chunge3193b92010-09-10 11:44:42 -07001201 }
1202 return -1;
1203 }
1204
Winson Chung1908d072011-02-24 18:09:44 -08001205 protected void setMinimumWidthOverride(int minimumWidth) {
1206 mMinimumWidth = minimumWidth;
1207 }
Winson Chung34b23d52011-03-18 11:29:34 -07001208 protected void resetMinimumWidthOverride() {
1209 mMinimumWidth = 0;
1210 }
Winson Chung1908d072011-02-24 18:09:44 -08001211
1212 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001213 // This functions are called enough times that it actually makes a difference in the
1214 // profiler -- so just inline the max() here
1215 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1216 final int minWidth = mMinimumWidth;
1217 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001218 }
1219
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001221 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 }
1223
1224 protected int getChildOffset(int index) {
1225 if (getChildCount() == 0)
1226 return 0;
1227
1228 int offset = getRelativeChildOffset(0);
1229 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001230 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 }
1232 return offset;
1233 }
1234
Michael Jurkad3ef3062010-11-23 16:23:58 -08001235 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001236 // This functions are called enough times that it actually makes a difference in the
1237 // profiler -- so just inline the max() here
1238 final int measuredWidth = child.getMeasuredWidth();
1239 final int minWidth = mMinimumWidth;
1240 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1241 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001242 }
1243
Adam Cohend19d3ca2010-09-15 14:43:42 -07001244 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 int minDistanceFromScreenCenter = getMeasuredWidth();
1246 int minDistanceFromScreenCenterIndex = -1;
1247 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1248 final int childCount = getChildCount();
1249 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001250 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001251 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 int halfChildWidth = (childWidth / 2);
1253 int childCenter = getChildOffset(i) + halfChildWidth;
1254 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1255 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1256 minDistanceFromScreenCenter = distanceFromScreenCenter;
1257 minDistanceFromScreenCenterIndex = i;
1258 }
1259 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001260 return minDistanceFromScreenCenterIndex;
1261 }
1262
1263 protected void snapToDestination() {
1264 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 }
1266
Adam Cohene0f66b52010-11-23 15:06:07 -08001267 private static class ScrollInterpolator implements Interpolator {
1268 public ScrollInterpolator() {
1269 }
1270
1271 public float getInterpolation(float t) {
1272 t -= 1.0f;
1273 return t*t*t*t*t + 1;
1274 }
1275 }
1276
1277 // We want the duration of the page snap animation to be influenced by the distance that
1278 // the screen has to travel, however, we don't want this duration to be effected in a
1279 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1280 // of travel has on the overall snap duration.
1281 float distanceInfluenceForSnapDuration(float f) {
1282 f -= 0.5f; // center the values about 0.
1283 f *= 0.3f * Math.PI / 2.0f;
1284 return (float) Math.sin(f);
1285 }
1286
Michael Jurka0142d492010-08-25 17:46:15 -07001287 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001288 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1289 int halfScreenSize = getMeasuredWidth() / 2;
1290
Winson Chung785d2eb2011-04-14 16:08:02 -07001291 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1292 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1293 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001294 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1295 int delta = newX - mUnboundedScrollX;
1296 int duration = 0;
1297
1298 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1299 // If the velocity is low enough, then treat this more as an automatic page advance
1300 // as opposed to an apparent physical response to flinging
1301 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1302 return;
1303 }
1304
1305 // Here we compute a "distance" that will be used in the computation of the overall
1306 // snap duration. This is a function of the actual distance that needs to be traveled;
1307 // we keep this value close to half screen size in order to reduce the variance in snap
1308 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001309 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001310 float distance = halfScreenSize + halfScreenSize *
1311 distanceInfluenceForSnapDuration(distanceRatio);
1312
1313 velocity = Math.abs(velocity);
1314 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1315
1316 // we want the page's snap velocity to approximately match the velocity at which the
1317 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001318 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1319 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001320
1321 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001322 }
1323
1324 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001325 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 }
1327
Michael Jurka0142d492010-08-25 17:46:15 -07001328 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001329 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001330
Winson Chung785d2eb2011-04-14 16:08:02 -07001331 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1332 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1333 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001334 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001335 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001337 snapToPage(whichPage, delta, duration);
1338 }
1339
1340 protected void snapToPage(int whichPage, int delta, int duration) {
1341 mNextPage = whichPage;
1342
1343 View focusedChild = getFocusedChild();
1344 if (focusedChild != null && whichPage != mCurrentPage &&
1345 focusedChild == getChildAt(mCurrentPage)) {
1346 focusedChild.clearFocus();
1347 }
1348
1349 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001350 awakenScrollBars(duration);
1351 if (duration == 0) {
1352 duration = Math.abs(delta);
1353 }
1354
1355 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001356 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001357
1358 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001359 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001360 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 invalidate();
1362 }
1363
Winson Chung321e9ee2010-08-09 13:37:56 -07001364 public void scrollLeft() {
1365 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001366 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001368 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001369 }
1370 }
1371
1372 public void scrollRight() {
1373 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001374 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001375 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001376 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 }
1378 }
1379
Winson Chung86f77532010-08-24 11:08:22 -07001380 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001381 int result = -1;
1382 if (v != null) {
1383 ViewParent vp = v.getParent();
1384 int count = getChildCount();
1385 for (int i = 0; i < count; i++) {
1386 if (vp == getChildAt(i)) {
1387 return i;
1388 }
1389 }
1390 }
1391 return result;
1392 }
1393
1394 /**
1395 * @return True is long presses are still allowed for the current touch
1396 */
1397 public boolean allowLongPress() {
1398 return mAllowLongPress;
1399 }
1400
Michael Jurka0142d492010-08-25 17:46:15 -07001401 /**
1402 * Set true to allow long-press events to be triggered, usually checked by
1403 * {@link Launcher} to accept or block dpad-initiated long-presses.
1404 */
1405 public void setAllowLongPress(boolean allowLongPress) {
1406 mAllowLongPress = allowLongPress;
1407 }
1408
Winson Chung321e9ee2010-08-09 13:37:56 -07001409 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001410 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001411
1412 SavedState(Parcelable superState) {
1413 super(superState);
1414 }
1415
1416 private SavedState(Parcel in) {
1417 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001418 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 }
1420
1421 @Override
1422 public void writeToParcel(Parcel out, int flags) {
1423 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001424 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001425 }
1426
1427 public static final Parcelable.Creator<SavedState> CREATOR =
1428 new Parcelable.Creator<SavedState>() {
1429 public SavedState createFromParcel(Parcel in) {
1430 return new SavedState(in);
1431 }
1432
1433 public SavedState[] newArray(int size) {
1434 return new SavedState[size];
1435 }
1436 };
1437 }
1438
Winson Chung86f77532010-08-24 11:08:22 -07001439 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001440 if (mContentIsRefreshable) {
1441 final int count = getChildCount();
1442 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001443 int lowerPageBound = getAssociatedLowerPageBound(page);
1444 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001445 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1446 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001447 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001448 Page layout = (Page) getChildAt(i);
1449 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001450 if (lowerPageBound <= i && i <= upperPageBound) {
1451 if (mDirtyPageContent.get(i)) {
1452 syncPageItems(i);
1453 mDirtyPageContent.set(i, false);
1454 }
1455 } else {
1456 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001457 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001458 }
1459 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001460 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001461 }
1462 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001463 }
1464 }
1465
Winson Chunge3193b92010-09-10 11:44:42 -07001466 protected int getAssociatedLowerPageBound(int page) {
1467 return Math.max(0, page - 1);
1468 }
1469 protected int getAssociatedUpperPageBound(int page) {
1470 final int count = getChildCount();
1471 return Math.min(page + 1, count - 1);
1472 }
1473
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001474 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001475 if (isChoiceMode(CHOICE_MODE_NONE)) {
1476 mChoiceMode = mode;
1477 mActionMode = startActionMode(callback);
1478 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001479 }
1480
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001481 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001482 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001483 mChoiceMode = CHOICE_MODE_NONE;
1484 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001485 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001486 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001487 }
1488 }
1489
1490 protected boolean isChoiceMode(int mode) {
1491 return mChoiceMode == mode;
1492 }
1493
1494 protected ArrayList<Checkable> getCheckedGrandchildren() {
1495 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1496 final int childCount = getChildCount();
1497 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001498 Page layout = (Page) getChildAt(i);
1499 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001500 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001501 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001502 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001503 checked.add((Checkable) v);
1504 }
1505 }
1506 }
1507 return checked;
1508 }
1509
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001510 /**
1511 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1512 * Otherwise, returns null.
1513 */
1514 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001515 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001516 final int childCount = getChildCount();
1517 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001518 Page layout = (Page) getChildAt(i);
1519 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001520 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001521 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001522 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1523 return (Checkable) v;
1524 }
1525 }
1526 }
1527 }
1528 return null;
1529 }
1530
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001531 protected void resetCheckedGrandchildren() {
1532 // loop through children, and set all of their children to _not_ be checked
1533 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1534 for (int i = 0; i < checked.size(); ++i) {
1535 final Checkable c = checked.get(i);
1536 c.setChecked(false);
1537 }
1538 }
1539
Winson Chunga12a2502010-12-20 14:41:35 -08001540 public void setRestorePage(int restorePage) {
1541 mRestorePage = restorePage;
1542 }
1543
Winson Chung86f77532010-08-24 11:08:22 -07001544 /**
1545 * This method is called ONLY to synchronize the number of pages that the paged view has.
1546 * To actually fill the pages with information, implement syncPageItems() below. It is
1547 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1548 * and therefore, individual page items do not need to be updated in this method.
1549 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001551
1552 /**
1553 * This method is called to synchronize the items that are on a particular page. If views on
1554 * the page can be reused, then they should be updated within this method.
1555 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001556 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001557
Michael Jurka983e3fd2011-05-26 17:10:29 -07001558 protected void postInvalidatePageData(final boolean clearViews) {
1559 post(new Runnable() {
1560 // post the call to avoid a call to requestLayout from a layout pass
1561 public void run() {
1562 if (clearViews) {
1563 removeAllViews();
1564 }
1565 invalidatePageData();
1566 }
1567 });
1568 }
1569
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001570 protected void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001571 if (mContentIsRefreshable) {
1572 // Update all the pages
1573 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001574
Michael Jurka0142d492010-08-25 17:46:15 -07001575 // Mark each of the pages as dirty
1576 final int count = getChildCount();
1577 mDirtyPageContent.clear();
1578 for (int i = 0; i < count; ++i) {
1579 mDirtyPageContent.add(true);
1580 }
1581
Winson Chunga12a2502010-12-20 14:41:35 -08001582 // Use the restore page if necessary
1583 if (mRestorePage > -1) {
1584 mCurrentPage = mRestorePage;
1585 mRestorePage = -1;
1586 }
1587
Michael Jurka0142d492010-08-25 17:46:15 -07001588 // Load any pages that are necessary for the current window of views
1589 loadAssociatedPages(mCurrentPage);
1590 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001591 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001592 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001593 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001594 }
1595}