blob: 1fa23cf4c655a740c308e0e9ee1bbd59a14e0f87 [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 Chung007c6982011-06-14 13:27:53 -070044import android.widget.ImageView;
Winson Chung321e9ee2010-08-09 13:37:56 -070045import android.widget.Scroller;
46
Winson Chung04998342011-01-05 13:54:43 -080047import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070048
Winson Chung321e9ee2010-08-09 13:37:56 -070049/**
50 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070051 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070052 */
53public abstract class PagedView extends ViewGroup {
54 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070055 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070056 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070057
Winson Chung86f77532010-08-24 11:08:22 -070058 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070059 private static final int MIN_LENGTH_FOR_FLING = 25;
60 // The min drag distance to trigger a page shift (regardless of velocity)
61 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Adam Cohene0f66b52010-11-23 15:06:07 -080063 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070064 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070065
Winson Chungb26f3d62011-06-02 10:49:29 -070066 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080067 private static final int MINIMUM_SNAP_VELOCITY = 2200;
68 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080069 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080070
Michael Jurka0142d492010-08-25 17:46:15 -070071 // the velocity at which a fling gesture will cause us to snap to the next page
72 protected int mSnapVelocity = 500;
73
74 protected float mSmoothingTime;
75 protected float mTouchX;
76
77 protected boolean mFirstLayout = true;
78
79 protected int mCurrentPage;
80 protected int mNextPage = INVALID_PAGE;
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 Jurka87b14902011-05-25 22:13:09 -0700115 protected int mCellCountX = 0;
116 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700117 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800118 protected boolean mAllowOverScroll = true;
119 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Michael Jurka8c920dd2011-01-20 14:16:56 -0800121 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800122 protected float mLayoutScale = 1.0f;
123
Michael Jurka5f1c5092010-09-03 14:15:02 -0700124 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700125
Michael Jurka5f1c5092010-09-03 14:15:02 -0700126 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Winson Chung86f77532010-08-24 11:08:22 -0700128 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Winson Chung86f77532010-08-24 11:08:22 -0700130 private ArrayList<Boolean> mDirtyPageContent;
131 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700132
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700133 // choice modes
134 protected static final int CHOICE_MODE_NONE = 0;
135 protected static final int CHOICE_MODE_SINGLE = 1;
136 // Multiple selection mode is not supported by all Launcher actions atm
137 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700138
Michael Jurkae17e19c2010-09-28 11:01:39 -0700139 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700140 private ActionMode mActionMode;
141
Winson Chung04998342011-01-05 13:54:43 -0800142 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
143 // AllApps and Customize, and allows them to share holographic icons for the application view
144 // (which is in both).
145 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700146
Michael Jurka0142d492010-08-25 17:46:15 -0700147 // If true, syncPages and syncPageItems will be called to refresh pages
148 protected boolean mContentIsRefreshable = true;
149
150 // If true, modify alpha of neighboring pages as user scrolls left/right
151 protected boolean mFadeInAdjacentScreens = true;
152
153 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
154 // to switch to a new page
155 protected boolean mUsePagingTouchSlop = true;
156
157 // If true, the subclass should directly update mScrollX itself in its computeScroll method
158 // (SmoothPagedView does this)
159 protected boolean mDeferScrollUpdate = false;
160
Patrick Dubroy1262e362010-10-06 15:49:50 -0700161 protected boolean mIsPageMoving = false;
162
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163 // All syncs and layout passes are deferred until data is ready.
164 protected boolean mIsDataReady = false;
165
Winson Chung007c6982011-06-14 13:27:53 -0700166 // Scrolling indicator
167 private ImageView mScrollIndicator;
168 private boolean mHasScrollIndicator = true;
169 private static final int sScrollIndicatorFadeInDuration = 150;
170 private static final int sScrollIndicatorFastFadeOutDuration = 50;
171 private static final int sScrollIndicatorFadeOutDuration = 650;
172
Winson Chung86f77532010-08-24 11:08:22 -0700173 public interface PageSwitchListener {
174 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 }
176
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 public PagedView(Context context) {
178 this(context, null);
179 }
180
181 public PagedView(Context context, AttributeSet attrs) {
182 this(context, attrs, 0);
183 }
184
185 public PagedView(Context context, AttributeSet attrs, int defStyle) {
186 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700187 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700188
Adam Cohen9c4949e2010-10-05 12:27:22 -0700189 TypedArray a = context.obtainStyledAttributes(attrs,
190 R.styleable.PagedView, defStyle, 0);
191 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
192 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800193 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700194 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800195 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800197 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700198 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800199 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700200 mPageLayoutWidthGap = a.getDimensionPixelSize(
201 R.styleable.PagedView_pageLayoutWidthGap, -1);
202 mPageLayoutHeightGap = a.getDimensionPixelSize(
203 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 a.recycle();
205
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700207 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 }
209
210 /**
211 * Initializes various states for this workspace.
212 */
Michael Jurka0142d492010-08-25 17:46:15 -0700213 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700214 mDirtyPageContent = new ArrayList<Boolean>();
215 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800216 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700217 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700218 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700219
220 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
221 mTouchSlop = configuration.getScaledTouchSlop();
222 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
223 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
224 }
225
Winson Chung86f77532010-08-24 11:08:22 -0700226 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
227 mPageSwitchListener = pageSwitchListener;
228 if (mPageSwitchListener != null) {
229 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 }
231 }
232
233 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700234 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
235 * out pages.
236 */
237 protected void setDataIsReady() {
238 mIsDataReady = true;
239 }
240 protected boolean isDataReady() {
241 return mIsDataReady;
242 }
243
244 /**
Winson Chung86f77532010-08-24 11:08:22 -0700245 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 *
Winson Chung86f77532010-08-24 11:08:22 -0700247 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 */
Winson Chung86f77532010-08-24 11:08:22 -0700249 int getCurrentPage() {
250 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 }
252
Winson Chung86f77532010-08-24 11:08:22 -0700253 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 return getChildCount();
255 }
256
Winson Chung86f77532010-08-24 11:08:22 -0700257 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 return getChildAt(index);
259 }
260
261 int getScrollWidth() {
262 return getWidth();
263 }
264
265 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800266 * Updates the scroll of the current page immediately to its final scroll position. We use this
267 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
268 * the previous tab page.
269 */
270 protected void updateCurrentPageScroll() {
271 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
272 scrollTo(newX, 0);
273 mScroller.setFinalX(newX);
274 }
275
276 /**
Winson Chung86f77532010-08-24 11:08:22 -0700277 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 */
Winson Chung86f77532010-08-24 11:08:22 -0700279 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800280 if (!mScroller.isFinished()) {
281 mScroller.abortAnimation();
282 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800283 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
284 // the default
285 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800286 return;
287 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700288
Winson Chung86f77532010-08-24 11:08:22 -0700289 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800290 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700291 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800292 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 }
294
Michael Jurka0142d492010-08-25 17:46:15 -0700295 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700296 if (mPageSwitchListener != null) {
297 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 }
299 }
300
Michael Jurkace7e05f2011-02-01 22:02:35 -0800301 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700302 mIsPageMoving = true;
303 onPageBeginMoving();
304 }
305
Michael Jurkace7e05f2011-02-01 22:02:35 -0800306 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700307 onPageEndMoving();
308 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700309 }
310
Adam Cohen26976d92011-03-22 15:33:33 -0700311 protected boolean isPageMoving() {
312 return mIsPageMoving;
313 }
314
Michael Jurka0142d492010-08-25 17:46:15 -0700315 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700316 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700317 showScrollingIndicator();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700318 }
319
320 // a method that subclasses can override to add behavior
321 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700322 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700323 }
324
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 /**
Winson Chung86f77532010-08-24 11:08:22 -0700326 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 *
328 * @param l The listener used to respond to long clicks.
329 */
330 @Override
331 public void setOnLongClickListener(OnLongClickListener l) {
332 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700333 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700335 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 }
337 }
338
339 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800340 public void scrollBy(int x, int y) {
341 scrollTo(mUnboundedScrollX + x, mScrollY + y);
342 }
343
344 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700345 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800346 mUnboundedScrollX = x;
347
348 if (x < 0) {
349 super.scrollTo(0, y);
350 if (mAllowOverScroll) {
351 overScroll(x);
352 }
353 } else if (x > mMaxScrollX) {
354 super.scrollTo(mMaxScrollX, y);
355 if (mAllowOverScroll) {
356 overScroll(x - mMaxScrollX);
357 }
358 } else {
359 super.scrollTo(x, y);
360 }
361
Michael Jurka0142d492010-08-25 17:46:15 -0700362 mTouchX = x;
363 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
364 }
365
366 // we moved this functionality to a helper function so SmoothPagedView can reuse it
367 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700368 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700369 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700371 invalidate();
372 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700373 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700374 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700375 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700376 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700377 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800378 // We don't want to trigger a page end moving unless the page has settled
379 // and the user has stopped scrolling
380 if (mTouchState == TOUCH_STATE_REST) {
381 pageEndMoving();
382 }
Michael Jurka0142d492010-08-25 17:46:15 -0700383 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 }
Michael Jurka0142d492010-08-25 17:46:15 -0700385 return false;
386 }
387
388 @Override
389 public void computeScroll() {
390 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700391 }
392
393 @Override
394 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700395 if (!mIsDataReady) {
396 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
397 return;
398 }
399
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
401 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
402 if (widthMode != MeasureSpec.EXACTLY) {
403 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
404 }
405
Adam Lesinski6b879f02010-11-04 16:15:23 -0700406 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
407 * of the All apps view on XLarge displays to not take up more space then it needs. Width
408 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
409 * each page to have the same width.
410 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700411 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700412 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
413 int maxChildHeight = 0;
414
415 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700416
Michael Jurka36fcb742011-04-06 17:08:58 -0700417
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700419 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700420 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700421 final int childCount = getChildCount();
422 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700423 // disallowing padding in paged view (just pass 0)
424 final View child = getChildAt(i);
425 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
426
427 int childWidthMode;
428 if (lp.width == LayoutParams.WRAP_CONTENT) {
429 childWidthMode = MeasureSpec.AT_MOST;
430 } else {
431 childWidthMode = MeasureSpec.EXACTLY;
432 }
433
434 int childHeightMode;
435 if (lp.height == LayoutParams.WRAP_CONTENT) {
436 childHeightMode = MeasureSpec.AT_MOST;
437 } else {
438 childHeightMode = MeasureSpec.EXACTLY;
439 }
440
441 final int childWidthMeasureSpec =
442 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
443 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700444 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700445
446 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700447 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700448 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
449 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700450 }
451
452 if (heightMode == MeasureSpec.AT_MOST) {
453 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800455 if (childCount > 0) {
456 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
457 } else {
458 mMaxScrollX = 0;
459 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700460
461 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700462 }
463
Michael Jurka8c920dd2011-01-20 14:16:56 -0800464 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800465 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
466 int delta = newX - mScrollX;
467
Michael Jurka8c920dd2011-01-20 14:16:56 -0800468 final int pageCount = getChildCount();
469 for (int i = 0; i < pageCount; i++) {
470 View page = (View) getChildAt(i);
471 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800472 }
473 setCurrentPage(newCurrentPage);
474 }
475
Michael Jurka8c920dd2011-01-20 14:16:56 -0800476 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
477 // 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 -0800478 // tightens the layout accordingly
479 public void setLayoutScale(float childrenScale) {
480 mLayoutScale = childrenScale;
481
482 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
483 int childCount = getChildCount();
484 float childrenX[] = new float[childCount];
485 float childrenY[] = new float[childCount];
486 for (int i = 0; i < childCount; i++) {
487 final View child = getChildAt(i);
488 childrenX[i] = child.getX();
489 childrenY[i] = child.getY();
490 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700491 // Trigger a full re-layout (never just call onLayout directly!)
492 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
493 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
494 requestLayout();
495 measure(widthSpec, heightSpec);
496 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800497 for (int i = 0; i < childCount; i++) {
498 final View child = getChildAt(i);
499 child.setX(childrenX[i]);
500 child.setY(childrenY[i]);
501 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700502
Michael Jurkad3ef3062010-11-23 16:23:58 -0800503 // Also, the page offset has changed (since the pages are now smaller);
504 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800505 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800506 }
507
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700509 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700510 if (!mIsDataReady) {
511 return;
512 }
513
Winson Chung785d2eb2011-04-14 16:08:02 -0700514 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700515 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
516 setHorizontalScrollBarEnabled(false);
517 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
518 scrollTo(newX, 0);
519 mScroller.setFinalX(newX);
520 setHorizontalScrollBarEnabled(true);
521 mFirstLayout = false;
522 }
523
Adam Lesinski6b879f02010-11-04 16:15:23 -0700524 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 final int childCount = getChildCount();
526 int childLeft = 0;
527 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700528 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
529 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700530 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700531 }
532
533 for (int i = 0; i < childCount; i++) {
534 final View child = getChildAt(i);
535 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800536 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700537 final int childHeight = child.getMeasuredHeight();
538 int childTop = mPaddingTop;
539 if (mCenterPagesVertically) {
540 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
541 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542
Winson Chung785d2eb2011-04-14 16:08:02 -0700543 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700544 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800545 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700546 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 }
548 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800549 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
550 mFirstLayout = false;
551 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
553
Winson Chung03929772011-02-23 17:07:10 -0800554 protected void forceUpdateAdjacentPagesAlpha() {
555 mDirtyPageAlpha = true;
556 updateAdjacentPagesAlpha();
557 }
558
Winson Chunge3193b92010-09-10 11:44:42 -0700559 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700560 if (mFadeInAdjacentScreens) {
561 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700562 int screenWidth = getMeasuredWidth();
563 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700564 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700565 final int childCount = getChildCount();
566 for (int i = 0; i < childCount; ++i) {
567 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800568 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700569 int halfChildWidth = (childWidth / 2);
570 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700571
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700572 // On the first layout, we may not have a width nor a proper offset, so for now
573 // we should just assume full page width (and calculate the offset according to
574 // that).
575 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700576 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700577 childCenter = (i * childWidth) + (childWidth / 2);
578 }
579
Winson Chunge8878e32010-09-15 20:37:09 -0700580 int d = halfChildWidth;
581 int distanceFromScreenCenter = childCenter - screenCenter;
582 if (distanceFromScreenCenter > 0) {
583 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800584 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700585 } else {
586 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700587 }
Michael Jurka0142d492010-08-25 17:46:15 -0700588 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700589 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800590 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700591 } else {
592 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700593 }
Michael Jurka0142d492010-08-25 17:46:15 -0700594 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700595 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700596
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700597 // Preventing potential divide-by-zero
598 d = Math.max(1, d);
599
Winson Chunge8878e32010-09-15 20:37:09 -0700600 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
601 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
602 float alpha = 1.0f - dimAlpha;
603
Adam Cohenf34bab52010-09-30 14:11:56 -0700604 if (alpha < ALPHA_QUANTIZE_LEVEL) {
605 alpha = 0.0f;
606 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
607 alpha = 1.0f;
608 }
609
Michael Jurkaa40829a2011-02-16 18:02:45 -0800610 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
611 // this optimization causes alpha to not be properly updated sometimes (repro
612 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
613 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
614 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800615 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700616 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800617 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 }
Michael Jurka0142d492010-08-25 17:46:15 -0700619 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 }
621 }
Winson Chunge3193b92010-09-10 11:44:42 -0700622 }
623
Adam Cohenf34bab52010-09-30 14:11:56 -0700624 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700625 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700626 }
627
Winson Chunge3193b92010-09-10 11:44:42 -0700628 @Override
629 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700630 int halfScreenSize = getMeasuredWidth() / 2;
631 int screenCenter = mScrollX + halfScreenSize;
632
633 if (screenCenter != mLastScreenCenter) {
634 screenScrolled(screenCenter);
635 updateAdjacentPagesAlpha();
636 mLastScreenCenter = screenCenter;
637 }
Michael Jurka0142d492010-08-25 17:46:15 -0700638
639 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700640 // As an optimization, this code assumes that all pages have the same width as the 0th
641 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700642 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700643 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800644 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700645 final int screenWidth = getMeasuredWidth();
646 int x = getRelativeChildOffset(0) + pageWidth;
647 int leftScreen = 0;
648 int rightScreen = 0;
649 while (x <= mScrollX) {
650 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800651 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700652 }
653 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800654 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700655 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800656 if (rightScreen < pageCount) {
657 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
658 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700659 }
660 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700661
Michael Jurkac4fb9172010-09-02 17:19:20 -0700662 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800663 // Clip to the bounds
664 canvas.save();
665 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
666 mScrollY + mBottom - mTop);
667
Michael Jurkac4fb9172010-09-02 17:19:20 -0700668 for (int i = leftScreen; i <= rightScreen; i++) {
669 drawChild(canvas, getChildAt(i), drawingTime);
670 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800671 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700672 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 }
674
675 @Override
676 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700677 int page = indexOfChild(child);
678 if (page != mCurrentPage || !mScroller.isFinished()) {
679 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 return true;
681 }
682 return false;
683 }
684
685 @Override
686 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700687 int focusablePage;
688 if (mNextPage != INVALID_PAGE) {
689 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700690 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700691 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700692 }
Winson Chung86f77532010-08-24 11:08:22 -0700693 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 if (v != null) {
695 v.requestFocus(direction, previouslyFocusedRect);
696 }
697 return false;
698 }
699
700 @Override
701 public boolean dispatchUnhandledMove(View focused, int direction) {
702 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700703 if (getCurrentPage() > 0) {
704 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 return true;
706 }
707 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700708 if (getCurrentPage() < getPageCount() - 1) {
709 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 return true;
711 }
712 }
713 return super.dispatchUnhandledMove(focused, direction);
714 }
715
716 @Override
717 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700718 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
719 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700720 }
721 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700722 if (mCurrentPage > 0) {
723 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 }
725 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700726 if (mCurrentPage < getPageCount() - 1) {
727 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700728 }
729 }
730 }
731
732 /**
733 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700734 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 *
Winson Chung86f77532010-08-24 11:08:22 -0700736 * This happens when live folders requery, and if they're off page, they
737 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 */
739 @Override
740 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700741 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 View v = focused;
743 while (true) {
744 if (v == current) {
745 super.focusableViewAvailable(focused);
746 return;
747 }
748 if (v == this) {
749 return;
750 }
751 ViewParent parent = v.getParent();
752 if (parent instanceof View) {
753 v = (View)v.getParent();
754 } else {
755 return;
756 }
757 }
758 }
759
760 /**
761 * {@inheritDoc}
762 */
763 @Override
764 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
765 if (disallowIntercept) {
766 // We need to make sure to cancel our long press if
767 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700768 final View currentPage = getChildAt(mCurrentPage);
769 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 }
771 super.requestDisallowInterceptTouchEvent(disallowIntercept);
772 }
773
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800774 /**
775 * Return true if a tap at (x, y) should trigger a flip to the previous page.
776 */
777 protected boolean hitsPreviousPage(float x, float y) {
778 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
779 }
780
781 /**
782 * Return true if a tap at (x, y) should trigger a flip to the next page.
783 */
784 protected boolean hitsNextPage(float x, float y) {
785 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
786 }
787
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 @Override
789 public boolean onInterceptTouchEvent(MotionEvent ev) {
790 /*
791 * This method JUST determines whether we want to intercept the motion.
792 * If we return true, onTouchEvent will be called and we do the actual
793 * scrolling there.
794 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800795 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796
Winson Chung45e1d6e2010-11-09 17:19:49 -0800797 // Skip touch handling if there are no pages to swipe
798 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
799
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 /*
801 * Shortcut the most recurring case: the user is in the dragging
802 * state and he is moving his finger. We want to intercept this
803 * motion.
804 */
805 final int action = ev.getAction();
806 if ((action == MotionEvent.ACTION_MOVE) &&
807 (mTouchState == TOUCH_STATE_SCROLLING)) {
808 return true;
809 }
810
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 switch (action & MotionEvent.ACTION_MASK) {
812 case MotionEvent.ACTION_MOVE: {
813 /*
814 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
815 * whether the user has moved far enough from his original down touch.
816 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700817 if (mActivePointerId != INVALID_POINTER) {
818 determineScrollingStart(ev);
819 break;
820 }
821 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
822 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
823 // i.e. fall through to the next case (don't break)
824 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
825 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 }
827
828 case MotionEvent.ACTION_DOWN: {
829 final float x = ev.getX();
830 final float y = ev.getY();
831 // Remember location of down touch
832 mDownMotionX = x;
833 mLastMotionX = x;
834 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800835 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800836 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 mActivePointerId = ev.getPointerId(0);
838 mAllowLongPress = true;
839
840 /*
841 * If being flinged and user touches the screen, initiate drag;
842 * otherwise don't. mScroller.isFinished should be false when
843 * being flinged.
844 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700845 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700846 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
847 if (finishedScrolling) {
848 mTouchState = TOUCH_STATE_REST;
849 mScroller.abortAnimation();
850 } else {
851 mTouchState = TOUCH_STATE_SCROLLING;
852 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700853
Winson Chung86f77532010-08-24 11:08:22 -0700854 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800856 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700857 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800858 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800860 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 mTouchState = TOUCH_STATE_NEXT_PAGE;
862 }
863 }
864 }
865 break;
866 }
867
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800869 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700870 mTouchState = TOUCH_STATE_REST;
871 mAllowLongPress = false;
872 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800873 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 break;
875
876 case MotionEvent.ACTION_POINTER_UP:
877 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800878 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 break;
880 }
881
882 /*
883 * The only time we want to intercept motion events is if we are in the
884 * drag mode.
885 */
886 return mTouchState != TOUCH_STATE_REST;
887 }
888
Winson Chung80baf5a2010-08-09 16:03:15 -0700889 protected void animateClickFeedback(View v, final Runnable r) {
890 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800891 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
892 loadAnimator(mContext, R.anim.paged_view_click_feedback);
893 anim.setTarget(v);
894 anim.addListener(new AnimatorListenerAdapter() {
895 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700896 r.run();
897 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700898 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800899 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700900 }
901
Adam Cohenf8d28232011-02-01 21:47:00 -0800902 protected void determineScrollingStart(MotionEvent ev) {
903 determineScrollingStart(ev, 1.0f);
904 }
905
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 /*
907 * Determines if we should change the touch state to start scrolling after the
908 * user moves their touch point too far.
909 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800910 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 /*
912 * Locally do absolute value. mLastMotionX is set to the y value
913 * of the down event.
914 */
915 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
916 final float x = ev.getX(pointerIndex);
917 final float y = ev.getY(pointerIndex);
918 final int xDiff = (int) Math.abs(x - mLastMotionX);
919 final int yDiff = (int) Math.abs(y - mLastMotionY);
920
Adam Cohenf8d28232011-02-01 21:47:00 -0800921 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 boolean xPaged = xDiff > mPagingTouchSlop;
923 boolean xMoved = xDiff > touchSlop;
924 boolean yMoved = yDiff > touchSlop;
925
Adam Cohenf8d28232011-02-01 21:47:00 -0800926 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700927 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 // Scroll if the user moved far enough along the X axis
929 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800930 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800932 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700933 mTouchX = mScrollX;
934 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
935 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 }
937 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800938 cancelCurrentPageLongPress();
939 }
940 }
941
942 protected void cancelCurrentPageLongPress() {
943 if (mAllowLongPress) {
944 mAllowLongPress = false;
945 // Try canceling the long press. It could also have been scheduled
946 // by a distant descendant, so use the mAllowLongPress flag to block
947 // everything
948 final View currentPage = getPageAt(mCurrentPage);
949 if (currentPage != null) {
950 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 }
952 }
953 }
954
Adam Cohene0f66b52010-11-23 15:06:07 -0800955 // This curve determines how the effect of scrolling over the limits of the page dimishes
956 // as the user pulls further and further from the bounds
957 private float overScrollInfluenceCurve(float f) {
958 f -= 1.0f;
959 return f * f * f + 1.0f;
960 }
961
Adam Cohen68d73932010-11-15 10:50:58 -0800962 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800963 int screenSize = getMeasuredWidth();
964
965 float f = (amount / screenSize);
966
967 if (f == 0) return;
968 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
969
Adam Cohen7bfc9792011-01-28 13:52:37 -0800970 // Clamp this factor, f, to -1 < f < 1
971 if (Math.abs(f) >= 1) {
972 f /= Math.abs(f);
973 }
974
Adam Cohene0f66b52010-11-23 15:06:07 -0800975 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800976 if (amount < 0) {
977 mScrollX = overScrollAmount;
978 } else {
979 mScrollX = mMaxScrollX + overScrollAmount;
980 }
981 invalidate();
982 }
983
Michael Jurkac5b262c2011-01-12 20:24:50 -0800984 protected float maxOverScroll() {
985 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
986 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
987 float f = 1.0f;
988 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
989 return OVERSCROLL_DAMP_FACTOR * f;
990 }
991
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 @Override
993 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800994 // Skip touch handling if there are no pages to swipe
995 if (getChildCount() <= 0) return super.onTouchEvent(ev);
996
Michael Jurkab8f06722010-10-10 15:58:46 -0700997 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700998
999 final int action = ev.getAction();
1000
1001 switch (action & MotionEvent.ACTION_MASK) {
1002 case MotionEvent.ACTION_DOWN:
1003 /*
1004 * If being flinged and user touches, stop the fling. isFinished
1005 * will be false if being flinged.
1006 */
1007 if (!mScroller.isFinished()) {
1008 mScroller.abortAnimation();
1009 }
1010
1011 // Remember where the motion event started
1012 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001013 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001014 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001016 if (mTouchState == TOUCH_STATE_SCROLLING) {
1017 pageBeginMoving();
1018 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 break;
1020
1021 case MotionEvent.ACTION_MOVE:
1022 if (mTouchState == TOUCH_STATE_SCROLLING) {
1023 // Scroll to follow the motion event
1024 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1025 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001026 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001027
Adam Cohenaefd4e12011-02-14 16:39:38 -08001028 mTotalMotionX += Math.abs(deltaX);
1029
Winson Chungc0844aa2011-02-02 15:25:58 -08001030 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1031 // keep the remainder because we are actually testing if we've moved from the last
1032 // scrolled position (which is discrete).
1033 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001034 mTouchX += deltaX;
1035 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1036 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001037 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001038 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001039 } else {
1040 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001041 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001042 mLastMotionX = x;
1043 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 } else {
1045 awakenScrollBars();
1046 }
Adam Cohen564976a2010-10-13 18:52:07 -07001047 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 determineScrollingStart(ev);
1049 }
1050 break;
1051
1052 case MotionEvent.ACTION_UP:
1053 if (mTouchState == TOUCH_STATE_SCROLLING) {
1054 final int activePointerId = mActivePointerId;
1055 final int pointerIndex = ev.findPointerIndex(activePointerId);
1056 final float x = ev.getX(pointerIndex);
1057 final VelocityTracker velocityTracker = mVelocityTracker;
1058 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1059 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001060 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001061 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001062 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001063
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001064 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1065
Adam Cohenaefd4e12011-02-14 16:39:38 -08001066 // In the case that the page is moved far to one direction and then is flung
1067 // in the opposite direction, we use a threshold to determine whether we should
1068 // just return to the starting page, or if we should skip one further.
1069 boolean returnToOriginalPage = false;
1070 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001071 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001072 Math.signum(velocityX) != Math.signum(deltaX)) {
1073 returnToOriginalPage = true;
1074 }
1075
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001076 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001077 Math.abs(velocityX) > snapVelocity;
1078
1079 int finalPage;
1080 // We give flings precedence over large moves, which is why we short-circuit our
1081 // test for a large move if a fling has been registered. That is, a large
1082 // move to the left and fling to the right will register as a fling to the right.
1083 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1084 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1085 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1086 snapToPageWithVelocity(finalPage, velocityX);
1087 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1088 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001089 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001090 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1091 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001092 } else {
1093 snapToDestination();
1094 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001095 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 // at this point we have not moved beyond the touch slop
1097 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1098 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001099 int nextPage = Math.max(0, mCurrentPage - 1);
1100 if (nextPage != mCurrentPage) {
1101 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 } else {
1103 snapToDestination();
1104 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001105 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 // at this point we have not moved beyond the touch slop
1107 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1108 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001109 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1110 if (nextPage != mCurrentPage) {
1111 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001112 } else {
1113 snapToDestination();
1114 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001115 } else {
1116 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 }
1118 mTouchState = TOUCH_STATE_REST;
1119 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001120 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 break;
1122
1123 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001124 if (mTouchState == TOUCH_STATE_SCROLLING) {
1125 snapToDestination();
1126 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 mTouchState = TOUCH_STATE_REST;
1128 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001129 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001130 break;
1131
1132 case MotionEvent.ACTION_POINTER_UP:
1133 onSecondaryPointerUp(ev);
1134 break;
1135 }
1136
1137 return true;
1138 }
1139
Winson Chung185d7162011-02-28 13:47:29 -08001140 @Override
1141 public boolean onGenericMotionEvent(MotionEvent event) {
1142 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1143 switch (event.getAction()) {
1144 case MotionEvent.ACTION_SCROLL: {
1145 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1146 final float vscroll;
1147 final float hscroll;
1148 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1149 vscroll = 0;
1150 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1151 } else {
1152 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1153 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1154 }
1155 if (hscroll != 0 || vscroll != 0) {
1156 if (hscroll > 0 || vscroll > 0) {
1157 scrollRight();
1158 } else {
1159 scrollLeft();
1160 }
1161 return true;
1162 }
1163 }
1164 }
1165 }
1166 return super.onGenericMotionEvent(event);
1167 }
1168
Michael Jurkab8f06722010-10-10 15:58:46 -07001169 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1170 if (mVelocityTracker == null) {
1171 mVelocityTracker = VelocityTracker.obtain();
1172 }
1173 mVelocityTracker.addMovement(ev);
1174 }
1175
1176 private void releaseVelocityTracker() {
1177 if (mVelocityTracker != null) {
1178 mVelocityTracker.recycle();
1179 mVelocityTracker = null;
1180 }
1181 }
1182
Winson Chung321e9ee2010-08-09 13:37:56 -07001183 private void onSecondaryPointerUp(MotionEvent ev) {
1184 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1185 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1186 final int pointerId = ev.getPointerId(pointerIndex);
1187 if (pointerId == mActivePointerId) {
1188 // This was our active pointer going up. Choose a new
1189 // active pointer and adjust accordingly.
1190 // TODO: Make this decision more intelligent.
1191 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1192 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1193 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001194 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 mActivePointerId = ev.getPointerId(newPointerIndex);
1196 if (mVelocityTracker != null) {
1197 mVelocityTracker.clear();
1198 }
1199 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001200 if (mTouchState == TOUCH_STATE_REST) {
1201 onWallpaperTap(ev);
1202 }
1203 }
1204
Winson Chungf0ea4d32011-06-06 14:27:16 -07001205 protected void onWallpaperTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001206
1207 @Override
1208 public void requestChildFocus(View child, View focused) {
1209 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001210 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001211 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001212 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001213 }
1214 }
1215
Winson Chunge3193b92010-09-10 11:44:42 -07001216 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1217 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001218 int left;
1219 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001220 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001221 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001222 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001223 if (left <= relativeOffset && relativeOffset <= right) {
1224 return i;
1225 }
Winson Chunge3193b92010-09-10 11:44:42 -07001226 }
1227 return -1;
1228 }
1229
Winson Chung1908d072011-02-24 18:09:44 -08001230 protected void setMinimumWidthOverride(int minimumWidth) {
1231 mMinimumWidth = minimumWidth;
1232 }
Winson Chung34b23d52011-03-18 11:29:34 -07001233 protected void resetMinimumWidthOverride() {
1234 mMinimumWidth = 0;
1235 }
Winson Chung1908d072011-02-24 18:09:44 -08001236
1237 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001238 // This functions are called enough times that it actually makes a difference in the
1239 // profiler -- so just inline the max() here
1240 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1241 final int minWidth = mMinimumWidth;
1242 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001243 }
1244
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001246 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 }
1248
1249 protected int getChildOffset(int index) {
1250 if (getChildCount() == 0)
1251 return 0;
1252
1253 int offset = getRelativeChildOffset(0);
1254 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001255 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 }
1257 return offset;
1258 }
1259
Michael Jurkad3ef3062010-11-23 16:23:58 -08001260 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001261 // This functions are called enough times that it actually makes a difference in the
1262 // profiler -- so just inline the max() here
1263 final int measuredWidth = child.getMeasuredWidth();
1264 final int minWidth = mMinimumWidth;
1265 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1266 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001267 }
1268
Adam Cohend19d3ca2010-09-15 14:43:42 -07001269 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 int minDistanceFromScreenCenter = getMeasuredWidth();
1271 int minDistanceFromScreenCenterIndex = -1;
1272 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1273 final int childCount = getChildCount();
1274 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001275 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001276 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001277 int halfChildWidth = (childWidth / 2);
1278 int childCenter = getChildOffset(i) + halfChildWidth;
1279 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1280 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1281 minDistanceFromScreenCenter = distanceFromScreenCenter;
1282 minDistanceFromScreenCenterIndex = i;
1283 }
1284 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001285 return minDistanceFromScreenCenterIndex;
1286 }
1287
1288 protected void snapToDestination() {
1289 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001290 }
1291
Adam Cohene0f66b52010-11-23 15:06:07 -08001292 private static class ScrollInterpolator implements Interpolator {
1293 public ScrollInterpolator() {
1294 }
1295
1296 public float getInterpolation(float t) {
1297 t -= 1.0f;
1298 return t*t*t*t*t + 1;
1299 }
1300 }
1301
1302 // We want the duration of the page snap animation to be influenced by the distance that
1303 // the screen has to travel, however, we don't want this duration to be effected in a
1304 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1305 // of travel has on the overall snap duration.
1306 float distanceInfluenceForSnapDuration(float f) {
1307 f -= 0.5f; // center the values about 0.
1308 f *= 0.3f * Math.PI / 2.0f;
1309 return (float) Math.sin(f);
1310 }
1311
Michael Jurka0142d492010-08-25 17:46:15 -07001312 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001313 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1314 int halfScreenSize = getMeasuredWidth() / 2;
1315
Winson Chung785d2eb2011-04-14 16:08:02 -07001316 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1317 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1318 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001319 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1320 int delta = newX - mUnboundedScrollX;
1321 int duration = 0;
1322
1323 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1324 // If the velocity is low enough, then treat this more as an automatic page advance
1325 // as opposed to an apparent physical response to flinging
1326 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1327 return;
1328 }
1329
1330 // Here we compute a "distance" that will be used in the computation of the overall
1331 // snap duration. This is a function of the actual distance that needs to be traveled;
1332 // we keep this value close to half screen size in order to reduce the variance in snap
1333 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001334 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001335 float distance = halfScreenSize + halfScreenSize *
1336 distanceInfluenceForSnapDuration(distanceRatio);
1337
1338 velocity = Math.abs(velocity);
1339 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1340
1341 // we want the page's snap velocity to approximately match the velocity at which the
1342 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001343 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1344 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001345
1346 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001347 }
1348
1349 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001350 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001351 }
1352
Michael Jurka0142d492010-08-25 17:46:15 -07001353 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001354 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001355
Winson Chung785d2eb2011-04-14 16:08:02 -07001356 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1357 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1358 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001359 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001360 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001361 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001362 snapToPage(whichPage, delta, duration);
1363 }
1364
1365 protected void snapToPage(int whichPage, int delta, int duration) {
1366 mNextPage = whichPage;
1367
1368 View focusedChild = getFocusedChild();
1369 if (focusedChild != null && whichPage != mCurrentPage &&
1370 focusedChild == getChildAt(mCurrentPage)) {
1371 focusedChild.clearFocus();
1372 }
1373
1374 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001375 awakenScrollBars(duration);
1376 if (duration == 0) {
1377 duration = Math.abs(delta);
1378 }
1379
1380 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001381 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001382
1383 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001384 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001385 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 invalidate();
1387 }
1388
Winson Chung321e9ee2010-08-09 13:37:56 -07001389 public void scrollLeft() {
1390 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001391 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001392 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001393 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001394 }
1395 }
1396
1397 public void scrollRight() {
1398 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001399 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001401 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 }
1403 }
1404
Winson Chung86f77532010-08-24 11:08:22 -07001405 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001406 int result = -1;
1407 if (v != null) {
1408 ViewParent vp = v.getParent();
1409 int count = getChildCount();
1410 for (int i = 0; i < count; i++) {
1411 if (vp == getChildAt(i)) {
1412 return i;
1413 }
1414 }
1415 }
1416 return result;
1417 }
1418
1419 /**
1420 * @return True is long presses are still allowed for the current touch
1421 */
1422 public boolean allowLongPress() {
1423 return mAllowLongPress;
1424 }
1425
Michael Jurka0142d492010-08-25 17:46:15 -07001426 /**
1427 * Set true to allow long-press events to be triggered, usually checked by
1428 * {@link Launcher} to accept or block dpad-initiated long-presses.
1429 */
1430 public void setAllowLongPress(boolean allowLongPress) {
1431 mAllowLongPress = allowLongPress;
1432 }
1433
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001435 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001436
1437 SavedState(Parcelable superState) {
1438 super(superState);
1439 }
1440
1441 private SavedState(Parcel in) {
1442 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001443 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 }
1445
1446 @Override
1447 public void writeToParcel(Parcel out, int flags) {
1448 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001449 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001450 }
1451
1452 public static final Parcelable.Creator<SavedState> CREATOR =
1453 new Parcelable.Creator<SavedState>() {
1454 public SavedState createFromParcel(Parcel in) {
1455 return new SavedState(in);
1456 }
1457
1458 public SavedState[] newArray(int size) {
1459 return new SavedState[size];
1460 }
1461 };
1462 }
1463
Winson Chung86f77532010-08-24 11:08:22 -07001464 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001465 if (mContentIsRefreshable) {
1466 final int count = getChildCount();
1467 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001468 int lowerPageBound = getAssociatedLowerPageBound(page);
1469 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001470 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1471 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001472 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001473 Page layout = (Page) getChildAt(i);
1474 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001475 if (lowerPageBound <= i && i <= upperPageBound) {
1476 if (mDirtyPageContent.get(i)) {
1477 syncPageItems(i);
1478 mDirtyPageContent.set(i, false);
1479 }
1480 } else {
1481 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001482 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001483 }
1484 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001485 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001486 }
1487 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001488 }
1489 }
1490
Winson Chunge3193b92010-09-10 11:44:42 -07001491 protected int getAssociatedLowerPageBound(int page) {
1492 return Math.max(0, page - 1);
1493 }
1494 protected int getAssociatedUpperPageBound(int page) {
1495 final int count = getChildCount();
1496 return Math.min(page + 1, count - 1);
1497 }
1498
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001499 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001500 if (isChoiceMode(CHOICE_MODE_NONE)) {
1501 mChoiceMode = mode;
1502 mActionMode = startActionMode(callback);
1503 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001504 }
1505
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001506 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001507 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001508 mChoiceMode = CHOICE_MODE_NONE;
1509 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001510 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001511 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001512 }
1513 }
1514
1515 protected boolean isChoiceMode(int mode) {
1516 return mChoiceMode == mode;
1517 }
1518
1519 protected ArrayList<Checkable> getCheckedGrandchildren() {
1520 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1521 final int childCount = getChildCount();
1522 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001523 Page layout = (Page) getChildAt(i);
1524 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001525 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001526 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001527 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001528 checked.add((Checkable) v);
1529 }
1530 }
1531 }
1532 return checked;
1533 }
1534
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001535 /**
1536 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1537 * Otherwise, returns null.
1538 */
1539 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001540 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001541 final int childCount = getChildCount();
1542 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001543 Page layout = (Page) getChildAt(i);
1544 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001545 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001546 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001547 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1548 return (Checkable) v;
1549 }
1550 }
1551 }
1552 }
1553 return null;
1554 }
1555
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001556 protected void resetCheckedGrandchildren() {
1557 // loop through children, and set all of their children to _not_ be checked
1558 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1559 for (int i = 0; i < checked.size(); ++i) {
1560 final Checkable c = checked.get(i);
1561 c.setChecked(false);
1562 }
1563 }
1564
Winson Chung86f77532010-08-24 11:08:22 -07001565 /**
1566 * This method is called ONLY to synchronize the number of pages that the paged view has.
1567 * To actually fill the pages with information, implement syncPageItems() below. It is
1568 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1569 * and therefore, individual page items do not need to be updated in this method.
1570 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001571 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001572
1573 /**
1574 * This method is called to synchronize the items that are on a particular page. If views on
1575 * the page can be reused, then they should be updated within this method.
1576 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001577 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001578
Michael Jurka983e3fd2011-05-26 17:10:29 -07001579 protected void postInvalidatePageData(final boolean clearViews) {
1580 post(new Runnable() {
1581 // post the call to avoid a call to requestLayout from a layout pass
1582 public void run() {
1583 if (clearViews) {
1584 removeAllViews();
1585 }
1586 invalidatePageData();
1587 }
1588 });
1589 }
1590
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001591 protected void invalidatePageData() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001592 if (!mIsDataReady) {
1593 return;
1594 }
1595
Michael Jurka0142d492010-08-25 17:46:15 -07001596 if (mContentIsRefreshable) {
Winson Chung007c6982011-06-14 13:27:53 -07001597 hideScrollingIndicator(true);
1598
Michael Jurka0142d492010-08-25 17:46:15 -07001599 // Update all the pages
1600 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001601
Michael Jurka0142d492010-08-25 17:46:15 -07001602 // Mark each of the pages as dirty
1603 final int count = getChildCount();
1604 mDirtyPageContent.clear();
1605 for (int i = 0; i < count; ++i) {
1606 mDirtyPageContent.add(true);
1607 }
1608
1609 // Load any pages that are necessary for the current window of views
1610 loadAssociatedPages(mCurrentPage);
1611 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001612 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001613 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001614 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001615 }
Winson Chung007c6982011-06-14 13:27:53 -07001616
1617 private ImageView getScrollingIndicator() {
1618 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1619 // found
1620 if (mHasScrollIndicator && mScrollIndicator == null) {
1621 ViewGroup parent = (ViewGroup) getParent();
1622 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1623 mHasScrollIndicator = mScrollIndicator != null;
1624 if (mHasScrollIndicator) {
1625 mScrollIndicator.setVisibility(View.VISIBLE);
1626 }
1627 }
1628 return mScrollIndicator;
1629 }
1630
1631 protected boolean isScrollingIndicatorEnabled() {
1632 return true;
1633 }
1634
1635 protected void showScrollingIndicator() {
1636 if (LauncherApplication.isScreenLarge()) return;
1637 if (getChildCount() <= 1) return;
1638 if (!isScrollingIndicatorEnabled()) return;
1639
1640 getScrollingIndicator();
1641 if (mScrollIndicator != null) {
1642 // Update the width of the indicator to the approx. width of each page in the full bar
1643 mScrollIndicator.getLayoutParams().width = getPageWidthForScrollingIndicator() / getChildCount();
1644 mScrollIndicator.requestLayout();
1645
1646 // Fade the indicator in
1647 updateScrollingIndicatorPosition();
1648 mScrollIndicator.animate().alpha(1f).setDuration(sScrollIndicatorFadeInDuration);
1649 }
1650 }
1651
1652 protected void hideScrollingIndicator(boolean immediately) {
1653 if (LauncherApplication.isScreenLarge()) return;
1654 if (getChildCount() <= 1) return;
1655 if (!isScrollingIndicatorEnabled()) return;
1656
1657 getScrollingIndicator();
1658 if (mScrollIndicator != null) {
1659 // Fade the indicator out
1660 updateScrollingIndicatorPosition();
1661 mScrollIndicator.animate().alpha(0f).setDuration(immediately ?
1662 sScrollIndicatorFastFadeOutDuration : sScrollIndicatorFadeOutDuration);
1663 }
1664 }
1665
1666 private void updateScrollingIndicator() {
1667 if (LauncherApplication.isScreenLarge()) return;
1668 if (getChildCount() <= 1) return;
1669 if (!isScrollingIndicatorEnabled()) return;
1670
1671 getScrollingIndicator();
1672 if (mScrollIndicator != null) {
1673 updateScrollingIndicatorPosition();
1674 }
1675 }
1676
1677 protected int getPageWidthForScrollingIndicator() {
1678 return getMeasuredWidth();
1679 }
1680
1681 private void updateScrollingIndicatorPosition() {
1682 // We can make the page width smaller to make it look more centered
1683 int pageWidth = getPageWidthForScrollingIndicator();
1684 int pageOffset = (getMeasuredWidth() - pageWidth) / 2;
1685 int maxPageWidth = getChildCount() * pageWidth;
1686 float offset = (float) getScrollX() / maxPageWidth;
1687 int indicatorWidth = pageWidth / getChildCount();
1688 int indicatorCenterOffset = indicatorWidth / 2 - mScrollIndicator.getMeasuredWidth() / 2;
1689 int indicatorPos = (int) (offset * pageWidth) + pageOffset + indicatorCenterOffset;
1690 mScrollIndicator.setTranslationX(indicatorPos);
1691 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001692}