blob: fa6a57009181e2ae8793159eb11a4d8f64e50c25 [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
Michael Jurka0142d492010-08-25 17:46:15 -0700142 // If true, syncPages and syncPageItems will be called to refresh pages
143 protected boolean mContentIsRefreshable = true;
144
145 // If true, modify alpha of neighboring pages as user scrolls left/right
146 protected boolean mFadeInAdjacentScreens = true;
147
148 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
149 // to switch to a new page
150 protected boolean mUsePagingTouchSlop = true;
151
152 // If true, the subclass should directly update mScrollX itself in its computeScroll method
153 // (SmoothPagedView does this)
154 protected boolean mDeferScrollUpdate = false;
155
Patrick Dubroy1262e362010-10-06 15:49:50 -0700156 protected boolean mIsPageMoving = false;
157
Winson Chungf0ea4d32011-06-06 14:27:16 -0700158 // All syncs and layout passes are deferred until data is ready.
159 protected boolean mIsDataReady = false;
160
Winson Chung007c6982011-06-14 13:27:53 -0700161 // Scrolling indicator
162 private ImageView mScrollIndicator;
163 private boolean mHasScrollIndicator = true;
164 private static final int sScrollIndicatorFadeInDuration = 150;
165 private static final int sScrollIndicatorFastFadeOutDuration = 50;
166 private static final int sScrollIndicatorFadeOutDuration = 650;
167
Winson Chungb44b5242011-06-13 11:32:14 -0700168 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700169 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700170
Winson Chung86f77532010-08-24 11:08:22 -0700171 public interface PageSwitchListener {
172 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 }
174
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 public PagedView(Context context) {
176 this(context, null);
177 }
178
179 public PagedView(Context context, AttributeSet attrs) {
180 this(context, attrs, 0);
181 }
182
183 public PagedView(Context context, AttributeSet attrs, int defStyle) {
184 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700185 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 TypedArray a = context.obtainStyledAttributes(attrs,
188 R.styleable.PagedView, defStyle, 0);
189 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
190 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800191 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700192 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800193 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700194 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800195 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800197 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700198 mPageLayoutWidthGap = a.getDimensionPixelSize(
199 R.styleable.PagedView_pageLayoutWidthGap, -1);
200 mPageLayoutHeightGap = a.getDimensionPixelSize(
201 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 a.recycle();
203
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700205 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 }
207
208 /**
209 * Initializes various states for this workspace.
210 */
Michael Jurka0142d492010-08-25 17:46:15 -0700211 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700212 mDirtyPageContent = new ArrayList<Boolean>();
213 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800214 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700215 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700216 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700217
218 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
219 mTouchSlop = configuration.getScaledTouchSlop();
220 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
221 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
222 }
223
Winson Chung86f77532010-08-24 11:08:22 -0700224 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
225 mPageSwitchListener = pageSwitchListener;
226 if (mPageSwitchListener != null) {
227 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229 }
230
231 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700232 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
233 * out pages.
234 */
235 protected void setDataIsReady() {
236 mIsDataReady = true;
237 }
238 protected boolean isDataReady() {
239 return mIsDataReady;
240 }
241
242 /**
Winson Chung86f77532010-08-24 11:08:22 -0700243 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 *
Winson Chung86f77532010-08-24 11:08:22 -0700245 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 */
Winson Chung86f77532010-08-24 11:08:22 -0700247 int getCurrentPage() {
248 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 }
250
Winson Chung86f77532010-08-24 11:08:22 -0700251 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 return getChildCount();
253 }
254
Winson Chung86f77532010-08-24 11:08:22 -0700255 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 return getChildAt(index);
257 }
258
259 int getScrollWidth() {
260 return getWidth();
261 }
262
263 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800264 * Updates the scroll of the current page immediately to its final scroll position. We use this
265 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
266 * the previous tab page.
267 */
268 protected void updateCurrentPageScroll() {
269 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
270 scrollTo(newX, 0);
271 mScroller.setFinalX(newX);
272 }
273
274 /**
Winson Chung86f77532010-08-24 11:08:22 -0700275 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700276 */
Winson Chung86f77532010-08-24 11:08:22 -0700277 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800278 if (!mScroller.isFinished()) {
279 mScroller.abortAnimation();
280 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800281 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
282 // the default
283 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800284 return;
285 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700286
Winson Chung86f77532010-08-24 11:08:22 -0700287 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800288 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700289 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800290 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 }
292
Michael Jurka0142d492010-08-25 17:46:15 -0700293 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700294 if (mPageSwitchListener != null) {
295 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 }
297 }
298
Michael Jurkace7e05f2011-02-01 22:02:35 -0800299 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700300 mIsPageMoving = true;
301 onPageBeginMoving();
302 }
303
Michael Jurkace7e05f2011-02-01 22:02:35 -0800304 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700305 onPageEndMoving();
306 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700307 }
308
Adam Cohen26976d92011-03-22 15:33:33 -0700309 protected boolean isPageMoving() {
310 return mIsPageMoving;
311 }
312
Michael Jurka0142d492010-08-25 17:46:15 -0700313 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700314 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700315 showScrollingIndicator();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700316 }
317
318 // a method that subclasses can override to add behavior
319 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700320 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700321 }
322
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 /**
Winson Chung86f77532010-08-24 11:08:22 -0700324 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 *
326 * @param l The listener used to respond to long clicks.
327 */
328 @Override
329 public void setOnLongClickListener(OnLongClickListener l) {
330 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700331 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700332 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700333 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 }
335 }
336
337 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800338 public void scrollBy(int x, int y) {
339 scrollTo(mUnboundedScrollX + x, mScrollY + y);
340 }
341
342 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700343 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800344 mUnboundedScrollX = x;
345
346 if (x < 0) {
347 super.scrollTo(0, y);
348 if (mAllowOverScroll) {
349 overScroll(x);
350 }
351 } else if (x > mMaxScrollX) {
352 super.scrollTo(mMaxScrollX, y);
353 if (mAllowOverScroll) {
354 overScroll(x - mMaxScrollX);
355 }
356 } else {
357 super.scrollTo(x, y);
358 }
359
Michael Jurka0142d492010-08-25 17:46:15 -0700360 mTouchX = x;
361 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
362 }
363
364 // we moved this functionality to a helper function so SmoothPagedView can reuse it
365 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700367 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700368 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700369 invalidate();
370 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700371 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700372 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700373 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700374 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700375 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700376
377 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700378 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700379 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700380 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700381 }
382
Adam Cohen73aa9752010-11-24 16:26:58 -0800383 // We don't want to trigger a page end moving unless the page has settled
384 // and the user has stopped scrolling
385 if (mTouchState == TOUCH_STATE_REST) {
386 pageEndMoving();
387 }
Michael Jurka0142d492010-08-25 17:46:15 -0700388 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
Michael Jurka0142d492010-08-25 17:46:15 -0700390 return false;
391 }
392
393 @Override
394 public void computeScroll() {
395 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 }
397
398 @Override
399 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700400 if (!mIsDataReady) {
401 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
402 return;
403 }
404
Winson Chung321e9ee2010-08-09 13:37:56 -0700405 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
406 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
407 if (widthMode != MeasureSpec.EXACTLY) {
408 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
409 }
410
Adam Lesinski6b879f02010-11-04 16:15:23 -0700411 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
412 * of the All apps view on XLarge displays to not take up more space then it needs. Width
413 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
414 * each page to have the same width.
415 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700417 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
418 int maxChildHeight = 0;
419
420 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700421
Michael Jurka36fcb742011-04-06 17:08:58 -0700422
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700424 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700425 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 final int childCount = getChildCount();
427 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700428 // disallowing padding in paged view (just pass 0)
429 final View child = getChildAt(i);
430 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
431
432 int childWidthMode;
433 if (lp.width == LayoutParams.WRAP_CONTENT) {
434 childWidthMode = MeasureSpec.AT_MOST;
435 } else {
436 childWidthMode = MeasureSpec.EXACTLY;
437 }
438
439 int childHeightMode;
440 if (lp.height == LayoutParams.WRAP_CONTENT) {
441 childHeightMode = MeasureSpec.AT_MOST;
442 } else {
443 childHeightMode = MeasureSpec.EXACTLY;
444 }
445
446 final int childWidthMeasureSpec =
447 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
448 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700449 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700450
451 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700452 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700453 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
454 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700455 }
456
457 if (heightMode == MeasureSpec.AT_MOST) {
458 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800460 if (childCount > 0) {
461 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
462 } else {
463 mMaxScrollX = 0;
464 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700465
466 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700467 }
468
Michael Jurka8c920dd2011-01-20 14:16:56 -0800469 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800470 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
471 int delta = newX - mScrollX;
472
Michael Jurka8c920dd2011-01-20 14:16:56 -0800473 final int pageCount = getChildCount();
474 for (int i = 0; i < pageCount; i++) {
475 View page = (View) getChildAt(i);
476 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800477 }
478 setCurrentPage(newCurrentPage);
479 }
480
Michael Jurka8c920dd2011-01-20 14:16:56 -0800481 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
482 // 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 -0800483 // tightens the layout accordingly
484 public void setLayoutScale(float childrenScale) {
485 mLayoutScale = childrenScale;
486
487 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
488 int childCount = getChildCount();
489 float childrenX[] = new float[childCount];
490 float childrenY[] = new float[childCount];
491 for (int i = 0; i < childCount; i++) {
492 final View child = getChildAt(i);
493 childrenX[i] = child.getX();
494 childrenY[i] = child.getY();
495 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700496 // Trigger a full re-layout (never just call onLayout directly!)
497 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
498 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
499 requestLayout();
500 measure(widthSpec, heightSpec);
501 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800502 for (int i = 0; i < childCount; i++) {
503 final View child = getChildAt(i);
504 child.setX(childrenX[i]);
505 child.setY(childrenY[i]);
506 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700507
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 // Also, the page offset has changed (since the pages are now smaller);
509 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800510 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800511 }
512
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700514 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700515 if (!mIsDataReady) {
516 return;
517 }
518
Winson Chung785d2eb2011-04-14 16:08:02 -0700519 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700520 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
521 setHorizontalScrollBarEnabled(false);
522 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
523 scrollTo(newX, 0);
524 mScroller.setFinalX(newX);
525 setHorizontalScrollBarEnabled(true);
526 mFirstLayout = false;
527 }
528
Adam Lesinski6b879f02010-11-04 16:15:23 -0700529 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 final int childCount = getChildCount();
531 int childLeft = 0;
532 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700533 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
534 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700535 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 }
537
538 for (int i = 0; i < childCount; i++) {
539 final View child = getChildAt(i);
540 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700542 final int childHeight = child.getMeasuredHeight();
543 int childTop = mPaddingTop;
544 if (mCenterPagesVertically) {
545 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
546 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800547
Winson Chung785d2eb2011-04-14 16:08:02 -0700548 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700549 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800550 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700551 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 }
553 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800554 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
555 mFirstLayout = false;
556 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
558
Winson Chung03929772011-02-23 17:07:10 -0800559 protected void forceUpdateAdjacentPagesAlpha() {
560 mDirtyPageAlpha = true;
561 updateAdjacentPagesAlpha();
562 }
563
Winson Chunge3193b92010-09-10 11:44:42 -0700564 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700565 if (mFadeInAdjacentScreens) {
566 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700567 int screenWidth = getMeasuredWidth();
568 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700569 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700570 final int childCount = getChildCount();
571 for (int i = 0; i < childCount; ++i) {
572 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800573 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700574 int halfChildWidth = (childWidth / 2);
575 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700576
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700577 // On the first layout, we may not have a width nor a proper offset, so for now
578 // we should just assume full page width (and calculate the offset according to
579 // that).
580 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700581 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700582 childCenter = (i * childWidth) + (childWidth / 2);
583 }
584
Winson Chunge8878e32010-09-15 20:37:09 -0700585 int d = halfChildWidth;
586 int distanceFromScreenCenter = childCenter - screenCenter;
587 if (distanceFromScreenCenter > 0) {
588 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800589 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700590 } else {
591 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700592 }
Michael Jurka0142d492010-08-25 17:46:15 -0700593 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700594 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700596 } else {
597 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700598 }
Michael Jurka0142d492010-08-25 17:46:15 -0700599 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700600 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700601
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700602 // Preventing potential divide-by-zero
603 d = Math.max(1, d);
604
Winson Chunge8878e32010-09-15 20:37:09 -0700605 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
606 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
607 float alpha = 1.0f - dimAlpha;
608
Adam Cohenf34bab52010-09-30 14:11:56 -0700609 if (alpha < ALPHA_QUANTIZE_LEVEL) {
610 alpha = 0.0f;
611 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
612 alpha = 1.0f;
613 }
614
Michael Jurkaa40829a2011-02-16 18:02:45 -0800615 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
616 // this optimization causes alpha to not be properly updated sometimes (repro
617 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
618 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
619 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800620 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700621 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800622 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700623 }
Michael Jurka0142d492010-08-25 17:46:15 -0700624 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700625 }
626 }
Winson Chunge3193b92010-09-10 11:44:42 -0700627 }
628
Adam Cohenf34bab52010-09-30 14:11:56 -0700629 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700630 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700631 }
632
Winson Chunge3193b92010-09-10 11:44:42 -0700633 @Override
634 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700635 int halfScreenSize = getMeasuredWidth() / 2;
636 int screenCenter = mScrollX + halfScreenSize;
637
638 if (screenCenter != mLastScreenCenter) {
639 screenScrolled(screenCenter);
640 updateAdjacentPagesAlpha();
641 mLastScreenCenter = screenCenter;
642 }
Michael Jurka0142d492010-08-25 17:46:15 -0700643
644 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700645 // As an optimization, this code assumes that all pages have the same width as the 0th
646 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700647 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700648 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800649 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700650 final int screenWidth = getMeasuredWidth();
651 int x = getRelativeChildOffset(0) + pageWidth;
652 int leftScreen = 0;
653 int rightScreen = 0;
654 while (x <= mScrollX) {
655 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800656 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700657 }
658 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800659 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700660 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800661 if (rightScreen < pageCount) {
662 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
663 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700664 }
665 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700666
Michael Jurkac4fb9172010-09-02 17:19:20 -0700667 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800668 // Clip to the bounds
669 canvas.save();
670 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
671 mScrollY + mBottom - mTop);
672
Michael Jurkac4fb9172010-09-02 17:19:20 -0700673 for (int i = leftScreen; i <= rightScreen; i++) {
674 drawChild(canvas, getChildAt(i), drawingTime);
675 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800676 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700677 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700678 }
679
680 @Override
681 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700682 int page = indexOfChild(child);
683 if (page != mCurrentPage || !mScroller.isFinished()) {
684 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 return true;
686 }
687 return false;
688 }
689
690 @Override
691 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700692 int focusablePage;
693 if (mNextPage != INVALID_PAGE) {
694 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700696 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700697 }
Winson Chung86f77532010-08-24 11:08:22 -0700698 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700699 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700700 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 }
702 return false;
703 }
704
705 @Override
706 public boolean dispatchUnhandledMove(View focused, int direction) {
707 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700708 if (getCurrentPage() > 0) {
709 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 return true;
711 }
712 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700713 if (getCurrentPage() < getPageCount() - 1) {
714 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700715 return true;
716 }
717 }
718 return super.dispatchUnhandledMove(focused, direction);
719 }
720
721 @Override
722 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700723 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
724 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 }
726 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700727 if (mCurrentPage > 0) {
728 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 }
730 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700731 if (mCurrentPage < getPageCount() - 1) {
732 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700733 }
734 }
735 }
736
737 /**
738 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700739 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 *
Winson Chung86f77532010-08-24 11:08:22 -0700741 * This happens when live folders requery, and if they're off page, they
742 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700743 */
744 @Override
745 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700746 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 View v = focused;
748 while (true) {
749 if (v == current) {
750 super.focusableViewAvailable(focused);
751 return;
752 }
753 if (v == this) {
754 return;
755 }
756 ViewParent parent = v.getParent();
757 if (parent instanceof View) {
758 v = (View)v.getParent();
759 } else {
760 return;
761 }
762 }
763 }
764
765 /**
766 * {@inheritDoc}
767 */
768 @Override
769 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
770 if (disallowIntercept) {
771 // We need to make sure to cancel our long press if
772 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700773 final View currentPage = getChildAt(mCurrentPage);
774 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 }
776 super.requestDisallowInterceptTouchEvent(disallowIntercept);
777 }
778
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800779 /**
780 * Return true if a tap at (x, y) should trigger a flip to the previous page.
781 */
782 protected boolean hitsPreviousPage(float x, float y) {
783 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
784 }
785
786 /**
787 * Return true if a tap at (x, y) should trigger a flip to the next page.
788 */
789 protected boolean hitsNextPage(float x, float y) {
790 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
791 }
792
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 @Override
794 public boolean onInterceptTouchEvent(MotionEvent ev) {
795 /*
796 * This method JUST determines whether we want to intercept the motion.
797 * If we return true, onTouchEvent will be called and we do the actual
798 * scrolling there.
799 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800800 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801
Winson Chung45e1d6e2010-11-09 17:19:49 -0800802 // Skip touch handling if there are no pages to swipe
803 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
804
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 /*
806 * Shortcut the most recurring case: the user is in the dragging
807 * state and he is moving his finger. We want to intercept this
808 * motion.
809 */
810 final int action = ev.getAction();
811 if ((action == MotionEvent.ACTION_MOVE) &&
812 (mTouchState == TOUCH_STATE_SCROLLING)) {
813 return true;
814 }
815
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 switch (action & MotionEvent.ACTION_MASK) {
817 case MotionEvent.ACTION_MOVE: {
818 /*
819 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
820 * whether the user has moved far enough from his original down touch.
821 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700822 if (mActivePointerId != INVALID_POINTER) {
823 determineScrollingStart(ev);
824 break;
825 }
826 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
827 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
828 // i.e. fall through to the next case (don't break)
829 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
830 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 }
832
833 case MotionEvent.ACTION_DOWN: {
834 final float x = ev.getX();
835 final float y = ev.getY();
836 // Remember location of down touch
837 mDownMotionX = x;
838 mLastMotionX = x;
839 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800840 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800841 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 mActivePointerId = ev.getPointerId(0);
843 mAllowLongPress = true;
844
845 /*
846 * If being flinged and user touches the screen, initiate drag;
847 * otherwise don't. mScroller.isFinished should be false when
848 * being flinged.
849 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700850 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700851 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
852 if (finishedScrolling) {
853 mTouchState = TOUCH_STATE_REST;
854 mScroller.abortAnimation();
855 } else {
856 mTouchState = TOUCH_STATE_SCROLLING;
857 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700858
Winson Chung86f77532010-08-24 11:08:22 -0700859 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700860 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800861 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800863 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800865 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 mTouchState = TOUCH_STATE_NEXT_PAGE;
867 }
868 }
869 }
870 break;
871 }
872
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800874 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 mTouchState = TOUCH_STATE_REST;
876 mAllowLongPress = false;
877 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800878 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 break;
880
881 case MotionEvent.ACTION_POINTER_UP:
882 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800883 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 break;
885 }
886
887 /*
888 * The only time we want to intercept motion events is if we are in the
889 * drag mode.
890 */
891 return mTouchState != TOUCH_STATE_REST;
892 }
893
Winson Chung80baf5a2010-08-09 16:03:15 -0700894 protected void animateClickFeedback(View v, final Runnable r) {
895 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800896 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
897 loadAnimator(mContext, R.anim.paged_view_click_feedback);
898 anim.setTarget(v);
899 anim.addListener(new AnimatorListenerAdapter() {
900 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700901 r.run();
902 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700903 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800904 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700905 }
906
Adam Cohenf8d28232011-02-01 21:47:00 -0800907 protected void determineScrollingStart(MotionEvent ev) {
908 determineScrollingStart(ev, 1.0f);
909 }
910
Winson Chung321e9ee2010-08-09 13:37:56 -0700911 /*
912 * Determines if we should change the touch state to start scrolling after the
913 * user moves their touch point too far.
914 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800915 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700916 /*
917 * Locally do absolute value. mLastMotionX is set to the y value
918 * of the down event.
919 */
920 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
921 final float x = ev.getX(pointerIndex);
922 final float y = ev.getY(pointerIndex);
923 final int xDiff = (int) Math.abs(x - mLastMotionX);
924 final int yDiff = (int) Math.abs(y - mLastMotionY);
925
Adam Cohenf8d28232011-02-01 21:47:00 -0800926 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 boolean xPaged = xDiff > mPagingTouchSlop;
928 boolean xMoved = xDiff > touchSlop;
929 boolean yMoved = yDiff > touchSlop;
930
Adam Cohenf8d28232011-02-01 21:47:00 -0800931 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700932 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 // Scroll if the user moved far enough along the X axis
934 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800935 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700936 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800937 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700938 mTouchX = mScrollX;
939 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
940 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 }
942 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800943 cancelCurrentPageLongPress();
944 }
945 }
946
947 protected void cancelCurrentPageLongPress() {
948 if (mAllowLongPress) {
949 mAllowLongPress = false;
950 // Try canceling the long press. It could also have been scheduled
951 // by a distant descendant, so use the mAllowLongPress flag to block
952 // everything
953 final View currentPage = getPageAt(mCurrentPage);
954 if (currentPage != null) {
955 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 }
957 }
958 }
959
Adam Cohene0f66b52010-11-23 15:06:07 -0800960 // This curve determines how the effect of scrolling over the limits of the page dimishes
961 // as the user pulls further and further from the bounds
962 private float overScrollInfluenceCurve(float f) {
963 f -= 1.0f;
964 return f * f * f + 1.0f;
965 }
966
Adam Cohen68d73932010-11-15 10:50:58 -0800967 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800968 int screenSize = getMeasuredWidth();
969
970 float f = (amount / screenSize);
971
972 if (f == 0) return;
973 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
974
Adam Cohen7bfc9792011-01-28 13:52:37 -0800975 // Clamp this factor, f, to -1 < f < 1
976 if (Math.abs(f) >= 1) {
977 f /= Math.abs(f);
978 }
979
Adam Cohene0f66b52010-11-23 15:06:07 -0800980 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800981 if (amount < 0) {
982 mScrollX = overScrollAmount;
983 } else {
984 mScrollX = mMaxScrollX + overScrollAmount;
985 }
986 invalidate();
987 }
988
Michael Jurkac5b262c2011-01-12 20:24:50 -0800989 protected float maxOverScroll() {
990 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
991 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
992 float f = 1.0f;
993 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
994 return OVERSCROLL_DAMP_FACTOR * f;
995 }
996
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 @Override
998 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800999 // Skip touch handling if there are no pages to swipe
1000 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1001
Michael Jurkab8f06722010-10-10 15:58:46 -07001002 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001003
1004 final int action = ev.getAction();
1005
1006 switch (action & MotionEvent.ACTION_MASK) {
1007 case MotionEvent.ACTION_DOWN:
1008 /*
1009 * If being flinged and user touches, stop the fling. isFinished
1010 * will be false if being flinged.
1011 */
1012 if (!mScroller.isFinished()) {
1013 mScroller.abortAnimation();
1014 }
1015
1016 // Remember where the motion event started
1017 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001018 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001019 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001021 if (mTouchState == TOUCH_STATE_SCROLLING) {
1022 pageBeginMoving();
1023 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 break;
1025
1026 case MotionEvent.ACTION_MOVE:
1027 if (mTouchState == TOUCH_STATE_SCROLLING) {
1028 // Scroll to follow the motion event
1029 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1030 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001031 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001032
Adam Cohenaefd4e12011-02-14 16:39:38 -08001033 mTotalMotionX += Math.abs(deltaX);
1034
Winson Chungc0844aa2011-02-02 15:25:58 -08001035 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1036 // keep the remainder because we are actually testing if we've moved from the last
1037 // scrolled position (which is discrete).
1038 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001039 mTouchX += deltaX;
1040 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1041 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001042 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001043 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001044 } else {
1045 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001047 mLastMotionX = x;
1048 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 } else {
1050 awakenScrollBars();
1051 }
Adam Cohen564976a2010-10-13 18:52:07 -07001052 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 determineScrollingStart(ev);
1054 }
1055 break;
1056
1057 case MotionEvent.ACTION_UP:
1058 if (mTouchState == TOUCH_STATE_SCROLLING) {
1059 final int activePointerId = mActivePointerId;
1060 final int pointerIndex = ev.findPointerIndex(activePointerId);
1061 final float x = ev.getX(pointerIndex);
1062 final VelocityTracker velocityTracker = mVelocityTracker;
1063 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1064 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001065 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001066 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001067 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001068
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001069 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1070
Adam Cohenaefd4e12011-02-14 16:39:38 -08001071 // In the case that the page is moved far to one direction and then is flung
1072 // in the opposite direction, we use a threshold to determine whether we should
1073 // just return to the starting page, or if we should skip one further.
1074 boolean returnToOriginalPage = false;
1075 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001076 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001077 Math.signum(velocityX) != Math.signum(deltaX)) {
1078 returnToOriginalPage = true;
1079 }
1080
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001081 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001082 Math.abs(velocityX) > snapVelocity;
1083
1084 int finalPage;
1085 // We give flings precedence over large moves, which is why we short-circuit our
1086 // test for a large move if a fling has been registered. That is, a large
1087 // move to the left and fling to the right will register as a fling to the right.
1088 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1089 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1090 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1091 snapToPageWithVelocity(finalPage, velocityX);
1092 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1093 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001094 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001095 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1096 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001097 } else {
1098 snapToDestination();
1099 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001100 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001101 // at this point we have not moved beyond the touch slop
1102 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1103 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001104 int nextPage = Math.max(0, mCurrentPage - 1);
1105 if (nextPage != mCurrentPage) {
1106 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 } else {
1108 snapToDestination();
1109 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001110 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 // at this point we have not moved beyond the touch slop
1112 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1113 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001114 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1115 if (nextPage != mCurrentPage) {
1116 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 } else {
1118 snapToDestination();
1119 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001120 } else {
1121 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 }
1123 mTouchState = TOUCH_STATE_REST;
1124 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001125 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 break;
1127
1128 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001129 if (mTouchState == TOUCH_STATE_SCROLLING) {
1130 snapToDestination();
1131 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001132 mTouchState = TOUCH_STATE_REST;
1133 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001134 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 break;
1136
1137 case MotionEvent.ACTION_POINTER_UP:
1138 onSecondaryPointerUp(ev);
1139 break;
1140 }
1141
1142 return true;
1143 }
1144
Winson Chung185d7162011-02-28 13:47:29 -08001145 @Override
1146 public boolean onGenericMotionEvent(MotionEvent event) {
1147 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1148 switch (event.getAction()) {
1149 case MotionEvent.ACTION_SCROLL: {
1150 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1151 final float vscroll;
1152 final float hscroll;
1153 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1154 vscroll = 0;
1155 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1156 } else {
1157 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1158 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1159 }
1160 if (hscroll != 0 || vscroll != 0) {
1161 if (hscroll > 0 || vscroll > 0) {
1162 scrollRight();
1163 } else {
1164 scrollLeft();
1165 }
1166 return true;
1167 }
1168 }
1169 }
1170 }
1171 return super.onGenericMotionEvent(event);
1172 }
1173
Michael Jurkab8f06722010-10-10 15:58:46 -07001174 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1175 if (mVelocityTracker == null) {
1176 mVelocityTracker = VelocityTracker.obtain();
1177 }
1178 mVelocityTracker.addMovement(ev);
1179 }
1180
1181 private void releaseVelocityTracker() {
1182 if (mVelocityTracker != null) {
1183 mVelocityTracker.recycle();
1184 mVelocityTracker = null;
1185 }
1186 }
1187
Winson Chung321e9ee2010-08-09 13:37:56 -07001188 private void onSecondaryPointerUp(MotionEvent ev) {
1189 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1190 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1191 final int pointerId = ev.getPointerId(pointerIndex);
1192 if (pointerId == mActivePointerId) {
1193 // This was our active pointer going up. Choose a new
1194 // active pointer and adjust accordingly.
1195 // TODO: Make this decision more intelligent.
1196 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1197 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1198 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001199 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 mActivePointerId = ev.getPointerId(newPointerIndex);
1201 if (mVelocityTracker != null) {
1202 mVelocityTracker.clear();
1203 }
1204 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001205 if (mTouchState == TOUCH_STATE_REST) {
1206 onWallpaperTap(ev);
1207 }
1208 }
1209
Winson Chungf0ea4d32011-06-06 14:27:16 -07001210 protected void onWallpaperTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001211
1212 @Override
1213 public void requestChildFocus(View child, View focused) {
1214 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001215 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001216 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001217 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 }
1219 }
1220
Winson Chunge3193b92010-09-10 11:44:42 -07001221 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1222 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001223 int left;
1224 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001225 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001226 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001227 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001228 if (left <= relativeOffset && relativeOffset <= right) {
1229 return i;
1230 }
Winson Chunge3193b92010-09-10 11:44:42 -07001231 }
1232 return -1;
1233 }
1234
Winson Chung1908d072011-02-24 18:09:44 -08001235 protected void setMinimumWidthOverride(int minimumWidth) {
1236 mMinimumWidth = minimumWidth;
1237 }
Winson Chung34b23d52011-03-18 11:29:34 -07001238 protected void resetMinimumWidthOverride() {
1239 mMinimumWidth = 0;
1240 }
Winson Chung1908d072011-02-24 18:09:44 -08001241
1242 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001243 // This functions are called enough times that it actually makes a difference in the
1244 // profiler -- so just inline the max() here
1245 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1246 final int minWidth = mMinimumWidth;
1247 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001248 }
1249
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001251 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001252 }
1253
1254 protected int getChildOffset(int index) {
1255 if (getChildCount() == 0)
1256 return 0;
1257
1258 int offset = getRelativeChildOffset(0);
1259 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001260 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 }
1262 return offset;
1263 }
1264
Michael Jurkad3ef3062010-11-23 16:23:58 -08001265 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001266 // This functions are called enough times that it actually makes a difference in the
1267 // profiler -- so just inline the max() here
1268 final int measuredWidth = child.getMeasuredWidth();
1269 final int minWidth = mMinimumWidth;
1270 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1271 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001272 }
1273
Adam Cohend19d3ca2010-09-15 14:43:42 -07001274 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 int minDistanceFromScreenCenter = getMeasuredWidth();
1276 int minDistanceFromScreenCenterIndex = -1;
1277 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1278 final int childCount = getChildCount();
1279 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001280 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001281 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001282 int halfChildWidth = (childWidth / 2);
1283 int childCenter = getChildOffset(i) + halfChildWidth;
1284 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1285 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1286 minDistanceFromScreenCenter = distanceFromScreenCenter;
1287 minDistanceFromScreenCenterIndex = i;
1288 }
1289 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001290 return minDistanceFromScreenCenterIndex;
1291 }
1292
1293 protected void snapToDestination() {
1294 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 }
1296
Adam Cohene0f66b52010-11-23 15:06:07 -08001297 private static class ScrollInterpolator implements Interpolator {
1298 public ScrollInterpolator() {
1299 }
1300
1301 public float getInterpolation(float t) {
1302 t -= 1.0f;
1303 return t*t*t*t*t + 1;
1304 }
1305 }
1306
1307 // We want the duration of the page snap animation to be influenced by the distance that
1308 // the screen has to travel, however, we don't want this duration to be effected in a
1309 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1310 // of travel has on the overall snap duration.
1311 float distanceInfluenceForSnapDuration(float f) {
1312 f -= 0.5f; // center the values about 0.
1313 f *= 0.3f * Math.PI / 2.0f;
1314 return (float) Math.sin(f);
1315 }
1316
Michael Jurka0142d492010-08-25 17:46:15 -07001317 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001318 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1319 int halfScreenSize = getMeasuredWidth() / 2;
1320
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1322 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1323 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001324 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1325 int delta = newX - mUnboundedScrollX;
1326 int duration = 0;
1327
1328 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1329 // If the velocity is low enough, then treat this more as an automatic page advance
1330 // as opposed to an apparent physical response to flinging
1331 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1332 return;
1333 }
1334
1335 // Here we compute a "distance" that will be used in the computation of the overall
1336 // snap duration. This is a function of the actual distance that needs to be traveled;
1337 // we keep this value close to half screen size in order to reduce the variance in snap
1338 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001339 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001340 float distance = halfScreenSize + halfScreenSize *
1341 distanceInfluenceForSnapDuration(distanceRatio);
1342
1343 velocity = Math.abs(velocity);
1344 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1345
1346 // we want the page's snap velocity to approximately match the velocity at which the
1347 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001348 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1349 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001350
1351 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001352 }
1353
1354 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001355 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001356 }
1357
Michael Jurka0142d492010-08-25 17:46:15 -07001358 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001359 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001360
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1362 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1363 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001364 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001365 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001367 snapToPage(whichPage, delta, duration);
1368 }
1369
1370 protected void snapToPage(int whichPage, int delta, int duration) {
1371 mNextPage = whichPage;
1372
1373 View focusedChild = getFocusedChild();
1374 if (focusedChild != null && whichPage != mCurrentPage &&
1375 focusedChild == getChildAt(mCurrentPage)) {
1376 focusedChild.clearFocus();
1377 }
1378
1379 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 awakenScrollBars(duration);
1381 if (duration == 0) {
1382 duration = Math.abs(delta);
1383 }
1384
1385 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001386 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001387
Winson Chungb44b5242011-06-13 11:32:14 -07001388 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1389 // loading associated pages until the scroll settles
1390 if (mDeferScrollUpdate) {
1391 loadAssociatedPages(mNextPage);
1392 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001393 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001394 }
Michael Jurka0142d492010-08-25 17:46:15 -07001395 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001396 invalidate();
1397 }
1398
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 public void scrollLeft() {
1400 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001401 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001403 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001404 }
1405 }
1406
1407 public void scrollRight() {
1408 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001409 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001411 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001412 }
1413 }
1414
Winson Chung86f77532010-08-24 11:08:22 -07001415 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 int result = -1;
1417 if (v != null) {
1418 ViewParent vp = v.getParent();
1419 int count = getChildCount();
1420 for (int i = 0; i < count; i++) {
1421 if (vp == getChildAt(i)) {
1422 return i;
1423 }
1424 }
1425 }
1426 return result;
1427 }
1428
1429 /**
1430 * @return True is long presses are still allowed for the current touch
1431 */
1432 public boolean allowLongPress() {
1433 return mAllowLongPress;
1434 }
1435
Michael Jurka0142d492010-08-25 17:46:15 -07001436 /**
1437 * Set true to allow long-press events to be triggered, usually checked by
1438 * {@link Launcher} to accept or block dpad-initiated long-presses.
1439 */
1440 public void setAllowLongPress(boolean allowLongPress) {
1441 mAllowLongPress = allowLongPress;
1442 }
1443
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001445 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001446
1447 SavedState(Parcelable superState) {
1448 super(superState);
1449 }
1450
1451 private SavedState(Parcel in) {
1452 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001453 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001454 }
1455
1456 @Override
1457 public void writeToParcel(Parcel out, int flags) {
1458 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001459 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001460 }
1461
1462 public static final Parcelable.Creator<SavedState> CREATOR =
1463 new Parcelable.Creator<SavedState>() {
1464 public SavedState createFromParcel(Parcel in) {
1465 return new SavedState(in);
1466 }
1467
1468 public SavedState[] newArray(int size) {
1469 return new SavedState[size];
1470 }
1471 };
1472 }
1473
Winson Chung86f77532010-08-24 11:08:22 -07001474 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001475 if (mContentIsRefreshable) {
1476 final int count = getChildCount();
1477 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001478 int lowerPageBound = getAssociatedLowerPageBound(page);
1479 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001480 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1481 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001482 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001483 Page layout = (Page) getChildAt(i);
1484 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001485 if (lowerPageBound <= i && i <= upperPageBound) {
1486 if (mDirtyPageContent.get(i)) {
1487 syncPageItems(i);
1488 mDirtyPageContent.set(i, false);
1489 }
1490 } else {
1491 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001492 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001493 }
1494 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001495 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001496 }
1497 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001498 }
1499 }
1500
Winson Chunge3193b92010-09-10 11:44:42 -07001501 protected int getAssociatedLowerPageBound(int page) {
1502 return Math.max(0, page - 1);
1503 }
1504 protected int getAssociatedUpperPageBound(int page) {
1505 final int count = getChildCount();
1506 return Math.min(page + 1, count - 1);
1507 }
1508
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001509 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001510 if (isChoiceMode(CHOICE_MODE_NONE)) {
1511 mChoiceMode = mode;
1512 mActionMode = startActionMode(callback);
1513 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001514 }
1515
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001516 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001517 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001518 mChoiceMode = CHOICE_MODE_NONE;
1519 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001520 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001521 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001522 }
1523 }
1524
1525 protected boolean isChoiceMode(int mode) {
1526 return mChoiceMode == mode;
1527 }
1528
1529 protected ArrayList<Checkable> getCheckedGrandchildren() {
1530 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1531 final int childCount = getChildCount();
1532 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001533 Page layout = (Page) getChildAt(i);
1534 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001535 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001536 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001537 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001538 checked.add((Checkable) v);
1539 }
1540 }
1541 }
1542 return checked;
1543 }
1544
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001545 /**
1546 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1547 * Otherwise, returns null.
1548 */
1549 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001550 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001551 final int childCount = getChildCount();
1552 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001553 Page layout = (Page) getChildAt(i);
1554 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001555 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001556 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001557 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1558 return (Checkable) v;
1559 }
1560 }
1561 }
1562 }
1563 return null;
1564 }
1565
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001566 protected void resetCheckedGrandchildren() {
1567 // loop through children, and set all of their children to _not_ be checked
1568 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1569 for (int i = 0; i < checked.size(); ++i) {
1570 final Checkable c = checked.get(i);
1571 c.setChecked(false);
1572 }
1573 }
1574
Winson Chung86f77532010-08-24 11:08:22 -07001575 /**
1576 * This method is called ONLY to synchronize the number of pages that the paged view has.
1577 * To actually fill the pages with information, implement syncPageItems() below. It is
1578 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1579 * and therefore, individual page items do not need to be updated in this method.
1580 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001581 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001582
1583 /**
1584 * This method is called to synchronize the items that are on a particular page. If views on
1585 * the page can be reused, then they should be updated within this method.
1586 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001587 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001588
Michael Jurka983e3fd2011-05-26 17:10:29 -07001589 protected void postInvalidatePageData(final boolean clearViews) {
1590 post(new Runnable() {
1591 // post the call to avoid a call to requestLayout from a layout pass
1592 public void run() {
1593 if (clearViews) {
1594 removeAllViews();
1595 }
1596 invalidatePageData();
1597 }
1598 });
1599 }
1600
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001601 protected void invalidatePageData() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001602 if (!mIsDataReady) {
1603 return;
1604 }
1605
Michael Jurka0142d492010-08-25 17:46:15 -07001606 if (mContentIsRefreshable) {
Winson Chung007c6982011-06-14 13:27:53 -07001607 hideScrollingIndicator(true);
1608
Michael Jurka0142d492010-08-25 17:46:15 -07001609 // Update all the pages
1610 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001611
Michael Jurka0142d492010-08-25 17:46:15 -07001612 // Mark each of the pages as dirty
1613 final int count = getChildCount();
1614 mDirtyPageContent.clear();
1615 for (int i = 0; i < count; ++i) {
1616 mDirtyPageContent.add(true);
1617 }
1618
1619 // Load any pages that are necessary for the current window of views
1620 loadAssociatedPages(mCurrentPage);
1621 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001622 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001623 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001624 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001625 }
Winson Chung007c6982011-06-14 13:27:53 -07001626
1627 private ImageView getScrollingIndicator() {
1628 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1629 // found
1630 if (mHasScrollIndicator && mScrollIndicator == null) {
1631 ViewGroup parent = (ViewGroup) getParent();
1632 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1633 mHasScrollIndicator = mScrollIndicator != null;
1634 if (mHasScrollIndicator) {
1635 mScrollIndicator.setVisibility(View.VISIBLE);
1636 }
1637 }
1638 return mScrollIndicator;
1639 }
1640
1641 protected boolean isScrollingIndicatorEnabled() {
1642 return true;
1643 }
1644
1645 protected void showScrollingIndicator() {
1646 if (LauncherApplication.isScreenLarge()) return;
1647 if (getChildCount() <= 1) return;
1648 if (!isScrollingIndicatorEnabled()) return;
1649
1650 getScrollingIndicator();
1651 if (mScrollIndicator != null) {
1652 // Update the width of the indicator to the approx. width of each page in the full bar
1653 mScrollIndicator.getLayoutParams().width = getPageWidthForScrollingIndicator() / getChildCount();
1654 mScrollIndicator.requestLayout();
1655
1656 // Fade the indicator in
1657 updateScrollingIndicatorPosition();
1658 mScrollIndicator.animate().alpha(1f).setDuration(sScrollIndicatorFadeInDuration);
1659 }
1660 }
1661
1662 protected void hideScrollingIndicator(boolean immediately) {
1663 if (LauncherApplication.isScreenLarge()) return;
1664 if (getChildCount() <= 1) return;
1665 if (!isScrollingIndicatorEnabled()) return;
1666
1667 getScrollingIndicator();
1668 if (mScrollIndicator != null) {
1669 // Fade the indicator out
1670 updateScrollingIndicatorPosition();
1671 mScrollIndicator.animate().alpha(0f).setDuration(immediately ?
1672 sScrollIndicatorFastFadeOutDuration : sScrollIndicatorFadeOutDuration);
1673 }
1674 }
1675
1676 private void updateScrollingIndicator() {
1677 if (LauncherApplication.isScreenLarge()) return;
1678 if (getChildCount() <= 1) return;
1679 if (!isScrollingIndicatorEnabled()) return;
1680
1681 getScrollingIndicator();
1682 if (mScrollIndicator != null) {
1683 updateScrollingIndicatorPosition();
1684 }
1685 }
1686
1687 protected int getPageWidthForScrollingIndicator() {
1688 return getMeasuredWidth();
1689 }
1690
1691 private void updateScrollingIndicatorPosition() {
1692 // We can make the page width smaller to make it look more centered
1693 int pageWidth = getPageWidthForScrollingIndicator();
1694 int pageOffset = (getMeasuredWidth() - pageWidth) / 2;
1695 int maxPageWidth = getChildCount() * pageWidth;
1696 float offset = (float) getScrollX() / maxPageWidth;
1697 int indicatorWidth = pageWidth / getChildCount();
1698 int indicatorCenterOffset = indicatorWidth / 2 - mScrollIndicator.getMeasuredWidth() / 2;
1699 int indicatorPos = (int) (offset * pageWidth) + pageOffset + indicatorCenterOffset;
1700 mScrollIndicator.setTranslationX(indicatorPos);
1701 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001702}