blob: 8c74c42e59e0c2ae75b716ae86d09a3b8f0437e5 [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 Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
20import android.animation.AnimatorInflater;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ObjectAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070024import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070031import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080032import android.view.InputDevice;
33import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.view.MotionEvent;
35import android.view.VelocityTracker;
36import android.view.View;
37import android.view.ViewConfiguration;
38import android.view.ViewGroup;
39import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityNodeInfo;
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 Chung6a0f57d2011-06-29 20:10:49 -070049import java.util.ArrayList;
50
Winson Chung321e9ee2010-08-09 13:37:56 -070051/**
52 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070053 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070054 */
55public abstract class PagedView extends ViewGroup {
56 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070057 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070058 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Winson Chung86f77532010-08-24 11:08:22 -070060 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070061 private static final int MIN_LENGTH_FOR_FLING = 25;
62 // The min drag distance to trigger a page shift (regardless of velocity)
63 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Adam Cohene0f66b52010-11-23 15:06:07 -080065 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070066 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Winson Chungb26f3d62011-06-02 10:49:29 -070068 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080069 private static final int MINIMUM_SNAP_VELOCITY = 2200;
70 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080071 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080072
Michael Jurka0142d492010-08-25 17:46:15 -070073 // the velocity at which a fling gesture will cause us to snap to the next page
74 protected int mSnapVelocity = 500;
75
76 protected float mSmoothingTime;
77 protected float mTouchX;
78
79 protected boolean mFirstLayout = true;
80
81 protected int mCurrentPage;
82 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080083 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070084 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070085 private VelocityTracker mVelocityTracker;
86
87 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080088 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080089 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080090 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080091 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070092 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka0142d492010-08-25 17:46:15 -070094 protected final static int TOUCH_STATE_REST = 0;
95 protected final static int TOUCH_STATE_SCROLLING = 1;
96 protected final static int TOUCH_STATE_PREV_PAGE = 2;
97 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070098 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070099
Michael Jurka0142d492010-08-25 17:46:15 -0700100 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka7426c422010-11-11 15:23:47 -0800104 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka7426c422010-11-11 15:23:47 -0800106 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107 private int mPagingTouchSlop;
108 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800109 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700110 protected int mPageSpacing;
111 protected int mPageLayoutPaddingTop;
112 protected int mPageLayoutPaddingBottom;
113 protected int mPageLayoutPaddingLeft;
114 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700115 protected int mPageLayoutWidthGap;
116 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700117 protected int mCellCountX = 0;
118 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700119 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800120 protected boolean mAllowOverScroll = true;
121 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700122
Michael Jurka8c920dd2011-01-20 14:16:56 -0800123 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800124 protected float mLayoutScale = 1.0f;
125
Michael Jurka5f1c5092010-09-03 14:15:02 -0700126 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700127
Michael Jurka5f1c5092010-09-03 14:15:02 -0700128 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700129
Winson Chung86f77532010-08-24 11:08:22 -0700130 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Winson Chung86f77532010-08-24 11:08:22 -0700132 private ArrayList<Boolean> mDirtyPageContent;
133 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700134
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700135 // choice modes
136 protected static final int CHOICE_MODE_NONE = 0;
137 protected static final int CHOICE_MODE_SINGLE = 1;
138 // Multiple selection mode is not supported by all Launcher actions atm
139 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700140
Michael Jurkae17e19c2010-09-28 11:01:39 -0700141 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700142 private ActionMode mActionMode;
143
Michael Jurka0142d492010-08-25 17:46:15 -0700144 // If true, syncPages and syncPageItems will be called to refresh pages
145 protected boolean mContentIsRefreshable = true;
146
147 // If true, modify alpha of neighboring pages as user scrolls left/right
148 protected boolean mFadeInAdjacentScreens = true;
149
150 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
151 // to switch to a new page
152 protected boolean mUsePagingTouchSlop = true;
153
154 // If true, the subclass should directly update mScrollX itself in its computeScroll method
155 // (SmoothPagedView does this)
156 protected boolean mDeferScrollUpdate = false;
157
Patrick Dubroy1262e362010-10-06 15:49:50 -0700158 protected boolean mIsPageMoving = false;
159
Winson Chungf0ea4d32011-06-06 14:27:16 -0700160 // All syncs and layout passes are deferred until data is ready.
161 protected boolean mIsDataReady = false;
162
Winson Chung007c6982011-06-14 13:27:53 -0700163 // Scrolling indicator
164 private ImageView mScrollIndicator;
Winson Chung3ac74c52011-06-30 17:39:37 -0700165 private ImageView mScrollTrack;
Winson Chung007c6982011-06-14 13:27:53 -0700166 private boolean mHasScrollIndicator = true;
167 private static final int sScrollIndicatorFadeInDuration = 150;
168 private static final int sScrollIndicatorFastFadeOutDuration = 50;
169 private static final int sScrollIndicatorFadeOutDuration = 650;
Winson Chung3ac74c52011-06-30 17:39:37 -0700170 private static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700171
Winson Chungb44b5242011-06-13 11:32:14 -0700172 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700173 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700174
Winson Chung86f77532010-08-24 11:08:22 -0700175 public interface PageSwitchListener {
176 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 }
178
Winson Chung321e9ee2010-08-09 13:37:56 -0700179 public PagedView(Context context) {
180 this(context, null);
181 }
182
183 public PagedView(Context context, AttributeSet attrs) {
184 this(context, attrs, 0);
185 }
186
187 public PagedView(Context context, AttributeSet attrs, int defStyle) {
188 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700189 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700190
Adam Cohen9c4949e2010-10-05 12:27:22 -0700191 TypedArray a = context.obtainStyledAttributes(attrs,
192 R.styleable.PagedView, defStyle, 0);
193 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
194 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800195 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800197 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700198 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800199 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800201 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700202 mPageLayoutWidthGap = a.getDimensionPixelSize(
203 R.styleable.PagedView_pageLayoutWidthGap, -1);
204 mPageLayoutHeightGap = a.getDimensionPixelSize(
205 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 a.recycle();
207
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700209 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 }
211
212 /**
213 * Initializes various states for this workspace.
214 */
Michael Jurka0142d492010-08-25 17:46:15 -0700215 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700216 mDirtyPageContent = new ArrayList<Boolean>();
217 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800218 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700219 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700220 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700221
222 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
223 mTouchSlop = configuration.getScaledTouchSlop();
224 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
225 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
226 }
227
Winson Chung86f77532010-08-24 11:08:22 -0700228 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
229 mPageSwitchListener = pageSwitchListener;
230 if (mPageSwitchListener != null) {
231 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 }
233 }
234
235 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700236 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
237 * out pages.
238 */
239 protected void setDataIsReady() {
240 mIsDataReady = true;
241 }
242 protected boolean isDataReady() {
243 return mIsDataReady;
244 }
245
246 /**
Winson Chung86f77532010-08-24 11:08:22 -0700247 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 *
Winson Chung86f77532010-08-24 11:08:22 -0700249 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 */
Winson Chung86f77532010-08-24 11:08:22 -0700251 int getCurrentPage() {
252 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700253 }
254
Winson Chung86f77532010-08-24 11:08:22 -0700255 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 return getChildCount();
257 }
258
Winson Chung86f77532010-08-24 11:08:22 -0700259 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700260 return getChildAt(index);
261 }
262
263 int getScrollWidth() {
264 return getWidth();
265 }
266
267 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800268 * Updates the scroll of the current page immediately to its final scroll position. We use this
269 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
270 * the previous tab page.
271 */
272 protected void updateCurrentPageScroll() {
273 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
274 scrollTo(newX, 0);
275 mScroller.setFinalX(newX);
276 }
277
278 /**
Winson Chung86f77532010-08-24 11:08:22 -0700279 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 */
Winson Chung86f77532010-08-24 11:08:22 -0700281 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800282 if (!mScroller.isFinished()) {
283 mScroller.abortAnimation();
284 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800285 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
286 // the default
287 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800288 return;
289 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700290
Winson Chung86f77532010-08-24 11:08:22 -0700291 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800292 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700293 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800294 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 }
296
Michael Jurka0142d492010-08-25 17:46:15 -0700297 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700298 if (mPageSwitchListener != null) {
299 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 }
301 }
302
Michael Jurkace7e05f2011-02-01 22:02:35 -0800303 protected void pageBeginMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700304 mIsPageMoving = true;
305 onPageBeginMoving();
306 }
307
Michael Jurkace7e05f2011-02-01 22:02:35 -0800308 protected void pageEndMoving() {
Patrick Dubroy1262e362010-10-06 15:49:50 -0700309 onPageEndMoving();
310 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700311 }
312
Adam Cohen26976d92011-03-22 15:33:33 -0700313 protected boolean isPageMoving() {
314 return mIsPageMoving;
315 }
316
Michael Jurka0142d492010-08-25 17:46:15 -0700317 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700318 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700319 showScrollingIndicator();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700320 }
321
322 // a method that subclasses can override to add behavior
323 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700324 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700325 }
326
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 /**
Winson Chung86f77532010-08-24 11:08:22 -0700328 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 *
330 * @param l The listener used to respond to long clicks.
331 */
332 @Override
333 public void setOnLongClickListener(OnLongClickListener l) {
334 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700335 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700337 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700338 }
339 }
340
341 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800342 public void scrollBy(int x, int y) {
343 scrollTo(mUnboundedScrollX + x, mScrollY + y);
344 }
345
346 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700347 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800348 mUnboundedScrollX = x;
349
350 if (x < 0) {
351 super.scrollTo(0, y);
352 if (mAllowOverScroll) {
353 overScroll(x);
354 }
355 } else if (x > mMaxScrollX) {
356 super.scrollTo(mMaxScrollX, y);
357 if (mAllowOverScroll) {
358 overScroll(x - mMaxScrollX);
359 }
360 } else {
361 super.scrollTo(x, y);
362 }
363
Michael Jurka0142d492010-08-25 17:46:15 -0700364 mTouchX = x;
365 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
366 }
367
368 // we moved this functionality to a helper function so SmoothPagedView can reuse it
369 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700371 // Don't bother scrolling if the page does not need to be moved
372 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
373 mDirtyPageAlpha = true;
374 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
375 }
Michael Jurka0142d492010-08-25 17:46:15 -0700376 invalidate();
377 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700378 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700379 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700380 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700381 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700382 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700383
384 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700385 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700386 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700387 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700388 }
389
Adam Cohen73aa9752010-11-24 16:26:58 -0800390 // We don't want to trigger a page end moving unless the page has settled
391 // and the user has stopped scrolling
392 if (mTouchState == TOUCH_STATE_REST) {
393 pageEndMoving();
394 }
Michael Jurka0142d492010-08-25 17:46:15 -0700395 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 }
Michael Jurka0142d492010-08-25 17:46:15 -0700397 return false;
398 }
399
400 @Override
401 public void computeScroll() {
402 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 }
404
405 @Override
406 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700407 if (!mIsDataReady) {
408 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
409 return;
410 }
411
Winson Chung321e9ee2010-08-09 13:37:56 -0700412 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
413 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
414 if (widthMode != MeasureSpec.EXACTLY) {
415 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
416 }
417
Adam Lesinski6b879f02010-11-04 16:15:23 -0700418 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
419 * of the All apps view on XLarge displays to not take up more space then it needs. Width
420 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
421 * each page to have the same width.
422 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700423 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700424 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
425 int maxChildHeight = 0;
426
427 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700428
Michael Jurka36fcb742011-04-06 17:08:58 -0700429
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700431 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700432 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 final int childCount = getChildCount();
434 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700435 // disallowing padding in paged view (just pass 0)
436 final View child = getChildAt(i);
437 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
438
439 int childWidthMode;
440 if (lp.width == LayoutParams.WRAP_CONTENT) {
441 childWidthMode = MeasureSpec.AT_MOST;
442 } else {
443 childWidthMode = MeasureSpec.EXACTLY;
444 }
445
446 int childHeightMode;
447 if (lp.height == LayoutParams.WRAP_CONTENT) {
448 childHeightMode = MeasureSpec.AT_MOST;
449 } else {
450 childHeightMode = MeasureSpec.EXACTLY;
451 }
452
453 final int childWidthMeasureSpec =
454 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
455 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700456 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700457
458 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700459 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700460 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
461 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700462 }
463
464 if (heightMode == MeasureSpec.AT_MOST) {
465 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800467 if (childCount > 0) {
468 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
469 } else {
470 mMaxScrollX = 0;
471 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700472
473 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700474 }
475
Michael Jurka8c920dd2011-01-20 14:16:56 -0800476 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800477 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
478 int delta = newX - mScrollX;
479
Michael Jurka8c920dd2011-01-20 14:16:56 -0800480 final int pageCount = getChildCount();
481 for (int i = 0; i < pageCount; i++) {
482 View page = (View) getChildAt(i);
483 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800484 }
485 setCurrentPage(newCurrentPage);
486 }
487
Michael Jurka8c920dd2011-01-20 14:16:56 -0800488 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
489 // 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 -0800490 // tightens the layout accordingly
491 public void setLayoutScale(float childrenScale) {
492 mLayoutScale = childrenScale;
493
494 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
495 int childCount = getChildCount();
496 float childrenX[] = new float[childCount];
497 float childrenY[] = new float[childCount];
498 for (int i = 0; i < childCount; i++) {
499 final View child = getChildAt(i);
500 childrenX[i] = child.getX();
501 childrenY[i] = child.getY();
502 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700503 // Trigger a full re-layout (never just call onLayout directly!)
504 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
505 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
506 requestLayout();
507 measure(widthSpec, heightSpec);
508 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800509 for (int i = 0; i < childCount; i++) {
510 final View child = getChildAt(i);
511 child.setX(childrenX[i]);
512 child.setY(childrenY[i]);
513 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700514
Michael Jurkad3ef3062010-11-23 16:23:58 -0800515 // Also, the page offset has changed (since the pages are now smaller);
516 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800517 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800518 }
519
Winson Chung321e9ee2010-08-09 13:37:56 -0700520 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700521 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700522 if (!mIsDataReady) {
523 return;
524 }
525
Winson Chung785d2eb2011-04-14 16:08:02 -0700526 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700527 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
528 setHorizontalScrollBarEnabled(false);
529 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
530 scrollTo(newX, 0);
531 mScroller.setFinalX(newX);
532 setHorizontalScrollBarEnabled(true);
533 mFirstLayout = false;
534 }
535
Adam Lesinski6b879f02010-11-04 16:15:23 -0700536 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 final int childCount = getChildCount();
538 int childLeft = 0;
539 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700540 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
541 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700542 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 }
544
545 for (int i = 0; i < childCount; i++) {
546 final View child = getChildAt(i);
547 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800548 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700549 final int childHeight = child.getMeasuredHeight();
550 int childTop = mPaddingTop;
551 if (mCenterPagesVertically) {
552 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
553 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800554
Winson Chung785d2eb2011-04-14 16:08:02 -0700555 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700556 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800557 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700558 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 }
560 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800561 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
562 mFirstLayout = false;
563 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 }
565
Winson Chung03929772011-02-23 17:07:10 -0800566 protected void forceUpdateAdjacentPagesAlpha() {
567 mDirtyPageAlpha = true;
568 updateAdjacentPagesAlpha();
569 }
570
Winson Chunge3193b92010-09-10 11:44:42 -0700571 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700572 if (mFadeInAdjacentScreens) {
573 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700574 int screenWidth = getMeasuredWidth();
575 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700576 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700577 final int childCount = getChildCount();
578 for (int i = 0; i < childCount; ++i) {
579 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800580 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700581 int halfChildWidth = (childWidth / 2);
582 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700583
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700584 // On the first layout, we may not have a width nor a proper offset, so for now
585 // we should just assume full page width (and calculate the offset according to
586 // that).
587 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700588 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700589 childCenter = (i * childWidth) + (childWidth / 2);
590 }
591
Winson Chunge8878e32010-09-15 20:37:09 -0700592 int d = halfChildWidth;
593 int distanceFromScreenCenter = childCenter - screenCenter;
594 if (distanceFromScreenCenter > 0) {
595 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800596 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700597 } else {
598 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700599 }
Michael Jurka0142d492010-08-25 17:46:15 -0700600 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700601 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800602 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700603 } else {
604 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700605 }
Michael Jurka0142d492010-08-25 17:46:15 -0700606 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700607 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700608
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700609 // Preventing potential divide-by-zero
610 d = Math.max(1, d);
611
Winson Chunge8878e32010-09-15 20:37:09 -0700612 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
613 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
614 float alpha = 1.0f - dimAlpha;
615
Adam Cohenf34bab52010-09-30 14:11:56 -0700616 if (alpha < ALPHA_QUANTIZE_LEVEL) {
617 alpha = 0.0f;
618 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
619 alpha = 1.0f;
620 }
621
Michael Jurkaa40829a2011-02-16 18:02:45 -0800622 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
623 // this optimization causes alpha to not be properly updated sometimes (repro
624 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
625 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
626 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800627 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700628 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800629 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700630 }
Michael Jurka0142d492010-08-25 17:46:15 -0700631 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700632 }
633 }
Winson Chunge3193b92010-09-10 11:44:42 -0700634 }
635
Adam Cohenf34bab52010-09-30 14:11:56 -0700636 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700637 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700638 }
639
Winson Chunge3193b92010-09-10 11:44:42 -0700640 @Override
641 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700642 int halfScreenSize = getMeasuredWidth() / 2;
643 int screenCenter = mScrollX + halfScreenSize;
644
645 if (screenCenter != mLastScreenCenter) {
646 screenScrolled(screenCenter);
647 updateAdjacentPagesAlpha();
648 mLastScreenCenter = screenCenter;
649 }
Michael Jurka0142d492010-08-25 17:46:15 -0700650
651 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700652 // As an optimization, this code assumes that all pages have the same width as the 0th
653 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700654 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700655 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800656 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700657 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700658 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700659 int leftScreen = 0;
660 int rightScreen = 0;
661 while (x <= mScrollX) {
662 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800663 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700664 }
665 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800666 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700667 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800668 if (rightScreen < pageCount) {
669 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
670 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700671 }
672 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700673
Michael Jurkac4fb9172010-09-02 17:19:20 -0700674 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800675 // Clip to the bounds
676 canvas.save();
677 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
678 mScrollY + mBottom - mTop);
679
Michael Jurkac4fb9172010-09-02 17:19:20 -0700680 for (int i = leftScreen; i <= rightScreen; i++) {
681 drawChild(canvas, getChildAt(i), drawingTime);
682 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800683 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700684 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 }
686
687 @Override
688 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700689 int page = indexOfChild(child);
690 if (page != mCurrentPage || !mScroller.isFinished()) {
691 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700692 return true;
693 }
694 return false;
695 }
696
697 @Override
698 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700699 int focusablePage;
700 if (mNextPage != INVALID_PAGE) {
701 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700702 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700703 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700704 }
Winson Chung86f77532010-08-24 11:08:22 -0700705 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700706 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700707 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 }
709 return false;
710 }
711
712 @Override
713 public boolean dispatchUnhandledMove(View focused, int direction) {
714 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700715 if (getCurrentPage() > 0) {
716 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700717 return true;
718 }
719 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700720 if (getCurrentPage() < getPageCount() - 1) {
721 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 return true;
723 }
724 }
725 return super.dispatchUnhandledMove(focused, direction);
726 }
727
728 @Override
729 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700730 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
731 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700732 }
733 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700734 if (mCurrentPage > 0) {
735 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 }
737 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700738 if (mCurrentPage < getPageCount() - 1) {
739 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741 }
742 }
743
744 /**
745 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700746 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 *
Winson Chung86f77532010-08-24 11:08:22 -0700748 * This happens when live folders requery, and if they're off page, they
749 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 */
751 @Override
752 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700753 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 View v = focused;
755 while (true) {
756 if (v == current) {
757 super.focusableViewAvailable(focused);
758 return;
759 }
760 if (v == this) {
761 return;
762 }
763 ViewParent parent = v.getParent();
764 if (parent instanceof View) {
765 v = (View)v.getParent();
766 } else {
767 return;
768 }
769 }
770 }
771
772 /**
773 * {@inheritDoc}
774 */
775 @Override
776 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
777 if (disallowIntercept) {
778 // We need to make sure to cancel our long press if
779 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700780 final View currentPage = getChildAt(mCurrentPage);
781 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 }
783 super.requestDisallowInterceptTouchEvent(disallowIntercept);
784 }
785
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800786 /**
787 * Return true if a tap at (x, y) should trigger a flip to the previous page.
788 */
789 protected boolean hitsPreviousPage(float x, float y) {
790 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
791 }
792
793 /**
794 * Return true if a tap at (x, y) should trigger a flip to the next page.
795 */
796 protected boolean hitsNextPage(float x, float y) {
797 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
798 }
799
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 @Override
801 public boolean onInterceptTouchEvent(MotionEvent ev) {
802 /*
803 * This method JUST determines whether we want to intercept the motion.
804 * If we return true, onTouchEvent will be called and we do the actual
805 * scrolling there.
806 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800807 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700808
Winson Chung45e1d6e2010-11-09 17:19:49 -0800809 // Skip touch handling if there are no pages to swipe
810 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
811
Winson Chung321e9ee2010-08-09 13:37:56 -0700812 /*
813 * Shortcut the most recurring case: the user is in the dragging
814 * state and he is moving his finger. We want to intercept this
815 * motion.
816 */
817 final int action = ev.getAction();
818 if ((action == MotionEvent.ACTION_MOVE) &&
819 (mTouchState == TOUCH_STATE_SCROLLING)) {
820 return true;
821 }
822
Winson Chung321e9ee2010-08-09 13:37:56 -0700823 switch (action & MotionEvent.ACTION_MASK) {
824 case MotionEvent.ACTION_MOVE: {
825 /*
826 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
827 * whether the user has moved far enough from his original down touch.
828 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700829 if (mActivePointerId != INVALID_POINTER) {
830 determineScrollingStart(ev);
831 break;
832 }
833 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
834 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
835 // i.e. fall through to the next case (don't break)
836 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
837 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 }
839
840 case MotionEvent.ACTION_DOWN: {
841 final float x = ev.getX();
842 final float y = ev.getY();
843 // Remember location of down touch
844 mDownMotionX = x;
845 mLastMotionX = x;
846 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800847 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800848 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 mActivePointerId = ev.getPointerId(0);
850 mAllowLongPress = true;
851
852 /*
853 * If being flinged and user touches the screen, initiate drag;
854 * otherwise don't. mScroller.isFinished should be false when
855 * being flinged.
856 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700857 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700858 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
859 if (finishedScrolling) {
860 mTouchState = TOUCH_STATE_REST;
861 mScroller.abortAnimation();
862 } else {
863 mTouchState = TOUCH_STATE_SCROLLING;
864 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700865
Winson Chung86f77532010-08-24 11:08:22 -0700866 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800868 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800870 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800872 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 mTouchState = TOUCH_STATE_NEXT_PAGE;
874 }
875 }
876 }
877 break;
878 }
879
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800881 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700882 mTouchState = TOUCH_STATE_REST;
883 mAllowLongPress = false;
884 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800885 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 break;
887
888 case MotionEvent.ACTION_POINTER_UP:
889 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800890 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 break;
892 }
893
894 /*
895 * The only time we want to intercept motion events is if we are in the
896 * drag mode.
897 */
898 return mTouchState != TOUCH_STATE_REST;
899 }
900
Winson Chung80baf5a2010-08-09 16:03:15 -0700901 protected void animateClickFeedback(View v, final Runnable r) {
902 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800903 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
904 loadAnimator(mContext, R.anim.paged_view_click_feedback);
905 anim.setTarget(v);
906 anim.addListener(new AnimatorListenerAdapter() {
907 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700908 r.run();
909 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700910 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800911 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700912 }
913
Adam Cohenf8d28232011-02-01 21:47:00 -0800914 protected void determineScrollingStart(MotionEvent ev) {
915 determineScrollingStart(ev, 1.0f);
916 }
917
Winson Chung321e9ee2010-08-09 13:37:56 -0700918 /*
919 * Determines if we should change the touch state to start scrolling after the
920 * user moves their touch point too far.
921 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800922 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 /*
924 * Locally do absolute value. mLastMotionX is set to the y value
925 * of the down event.
926 */
927 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
928 final float x = ev.getX(pointerIndex);
929 final float y = ev.getY(pointerIndex);
930 final int xDiff = (int) Math.abs(x - mLastMotionX);
931 final int yDiff = (int) Math.abs(y - mLastMotionY);
932
Adam Cohenf8d28232011-02-01 21:47:00 -0800933 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 boolean xPaged = xDiff > mPagingTouchSlop;
935 boolean xMoved = xDiff > touchSlop;
936 boolean yMoved = yDiff > touchSlop;
937
Adam Cohenf8d28232011-02-01 21:47:00 -0800938 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700939 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 // Scroll if the user moved far enough along the X axis
941 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800942 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800944 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700945 mTouchX = mScrollX;
946 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
947 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 }
949 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800950 cancelCurrentPageLongPress();
951 }
952 }
953
954 protected void cancelCurrentPageLongPress() {
955 if (mAllowLongPress) {
956 mAllowLongPress = false;
957 // Try canceling the long press. It could also have been scheduled
958 // by a distant descendant, so use the mAllowLongPress flag to block
959 // everything
960 final View currentPage = getPageAt(mCurrentPage);
961 if (currentPage != null) {
962 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 }
964 }
965 }
966
Adam Cohene0f66b52010-11-23 15:06:07 -0800967 // This curve determines how the effect of scrolling over the limits of the page dimishes
968 // as the user pulls further and further from the bounds
969 private float overScrollInfluenceCurve(float f) {
970 f -= 1.0f;
971 return f * f * f + 1.0f;
972 }
973
Adam Cohen68d73932010-11-15 10:50:58 -0800974 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800975 int screenSize = getMeasuredWidth();
976
977 float f = (amount / screenSize);
978
979 if (f == 0) return;
980 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
981
Adam Cohen7bfc9792011-01-28 13:52:37 -0800982 // Clamp this factor, f, to -1 < f < 1
983 if (Math.abs(f) >= 1) {
984 f /= Math.abs(f);
985 }
986
Adam Cohene0f66b52010-11-23 15:06:07 -0800987 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800988 if (amount < 0) {
989 mScrollX = overScrollAmount;
990 } else {
991 mScrollX = mMaxScrollX + overScrollAmount;
992 }
993 invalidate();
994 }
995
Michael Jurkac5b262c2011-01-12 20:24:50 -0800996 protected float maxOverScroll() {
997 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
998 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
999 float f = 1.0f;
1000 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1001 return OVERSCROLL_DAMP_FACTOR * f;
1002 }
1003
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 @Override
1005 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001006 // Skip touch handling if there are no pages to swipe
1007 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1008
Michael Jurkab8f06722010-10-10 15:58:46 -07001009 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001010
1011 final int action = ev.getAction();
1012
1013 switch (action & MotionEvent.ACTION_MASK) {
1014 case MotionEvent.ACTION_DOWN:
1015 /*
1016 * If being flinged and user touches, stop the fling. isFinished
1017 * will be false if being flinged.
1018 */
1019 if (!mScroller.isFinished()) {
1020 mScroller.abortAnimation();
1021 }
1022
1023 // Remember where the motion event started
1024 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001025 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001026 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001028 if (mTouchState == TOUCH_STATE_SCROLLING) {
1029 pageBeginMoving();
1030 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 break;
1032
1033 case MotionEvent.ACTION_MOVE:
1034 if (mTouchState == TOUCH_STATE_SCROLLING) {
1035 // Scroll to follow the motion event
1036 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1037 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001038 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001039
Adam Cohenaefd4e12011-02-14 16:39:38 -08001040 mTotalMotionX += Math.abs(deltaX);
1041
Winson Chungc0844aa2011-02-02 15:25:58 -08001042 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1043 // keep the remainder because we are actually testing if we've moved from the last
1044 // scrolled position (which is discrete).
1045 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001046 mTouchX += deltaX;
1047 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1048 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001049 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001050 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001051 } else {
1052 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001053 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001054 mLastMotionX = x;
1055 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 } else {
1057 awakenScrollBars();
1058 }
Adam Cohen564976a2010-10-13 18:52:07 -07001059 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 determineScrollingStart(ev);
1061 }
1062 break;
1063
1064 case MotionEvent.ACTION_UP:
1065 if (mTouchState == TOUCH_STATE_SCROLLING) {
1066 final int activePointerId = mActivePointerId;
1067 final int pointerIndex = ev.findPointerIndex(activePointerId);
1068 final float x = ev.getX(pointerIndex);
1069 final VelocityTracker velocityTracker = mVelocityTracker;
1070 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1071 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001072 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001073 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001074 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001075
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001076 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1077
Adam Cohenaefd4e12011-02-14 16:39:38 -08001078 // In the case that the page is moved far to one direction and then is flung
1079 // in the opposite direction, we use a threshold to determine whether we should
1080 // just return to the starting page, or if we should skip one further.
1081 boolean returnToOriginalPage = false;
1082 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001083 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001084 Math.signum(velocityX) != Math.signum(deltaX)) {
1085 returnToOriginalPage = true;
1086 }
1087
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001088 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001089 Math.abs(velocityX) > snapVelocity;
1090
1091 int finalPage;
1092 // We give flings precedence over large moves, which is why we short-circuit our
1093 // test for a large move if a fling has been registered. That is, a large
1094 // move to the left and fling to the right will register as a fling to the right.
1095 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1096 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1097 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1098 snapToPageWithVelocity(finalPage, velocityX);
1099 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1100 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001101 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001102 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1103 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001104 } else {
1105 snapToDestination();
1106 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001107 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001108 // at this point we have not moved beyond the touch slop
1109 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1110 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001111 int nextPage = Math.max(0, mCurrentPage - 1);
1112 if (nextPage != mCurrentPage) {
1113 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001114 } else {
1115 snapToDestination();
1116 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001117 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001118 // at this point we have not moved beyond the touch slop
1119 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1120 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001121 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1122 if (nextPage != mCurrentPage) {
1123 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001124 } else {
1125 snapToDestination();
1126 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001127 } else {
1128 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001129 }
1130 mTouchState = TOUCH_STATE_REST;
1131 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001132 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001133 break;
1134
1135 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001136 if (mTouchState == TOUCH_STATE_SCROLLING) {
1137 snapToDestination();
1138 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001139 mTouchState = TOUCH_STATE_REST;
1140 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001141 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001142 break;
1143
1144 case MotionEvent.ACTION_POINTER_UP:
1145 onSecondaryPointerUp(ev);
1146 break;
1147 }
1148
1149 return true;
1150 }
1151
Winson Chung185d7162011-02-28 13:47:29 -08001152 @Override
1153 public boolean onGenericMotionEvent(MotionEvent event) {
1154 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1155 switch (event.getAction()) {
1156 case MotionEvent.ACTION_SCROLL: {
1157 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1158 final float vscroll;
1159 final float hscroll;
1160 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1161 vscroll = 0;
1162 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1163 } else {
1164 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1165 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1166 }
1167 if (hscroll != 0 || vscroll != 0) {
1168 if (hscroll > 0 || vscroll > 0) {
1169 scrollRight();
1170 } else {
1171 scrollLeft();
1172 }
1173 return true;
1174 }
1175 }
1176 }
1177 }
1178 return super.onGenericMotionEvent(event);
1179 }
1180
Michael Jurkab8f06722010-10-10 15:58:46 -07001181 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1182 if (mVelocityTracker == null) {
1183 mVelocityTracker = VelocityTracker.obtain();
1184 }
1185 mVelocityTracker.addMovement(ev);
1186 }
1187
1188 private void releaseVelocityTracker() {
1189 if (mVelocityTracker != null) {
1190 mVelocityTracker.recycle();
1191 mVelocityTracker = null;
1192 }
1193 }
1194
Winson Chung321e9ee2010-08-09 13:37:56 -07001195 private void onSecondaryPointerUp(MotionEvent ev) {
1196 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1197 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1198 final int pointerId = ev.getPointerId(pointerIndex);
1199 if (pointerId == mActivePointerId) {
1200 // This was our active pointer going up. Choose a new
1201 // active pointer and adjust accordingly.
1202 // TODO: Make this decision more intelligent.
1203 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1204 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1205 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001206 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001207 mActivePointerId = ev.getPointerId(newPointerIndex);
1208 if (mVelocityTracker != null) {
1209 mVelocityTracker.clear();
1210 }
1211 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001212 if (mTouchState == TOUCH_STATE_REST) {
1213 onWallpaperTap(ev);
1214 }
1215 }
1216
Winson Chungf0ea4d32011-06-06 14:27:16 -07001217 protected void onWallpaperTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001218
1219 @Override
1220 public void requestChildFocus(View child, View focused) {
1221 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001222 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001223 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001224 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 }
1226 }
1227
Winson Chunge3193b92010-09-10 11:44:42 -07001228 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1229 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001230 int left;
1231 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001232 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001233 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001234 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001235 if (left <= relativeOffset && relativeOffset <= right) {
1236 return i;
1237 }
Winson Chunge3193b92010-09-10 11:44:42 -07001238 }
1239 return -1;
1240 }
1241
Winson Chung1908d072011-02-24 18:09:44 -08001242 protected void setMinimumWidthOverride(int minimumWidth) {
1243 mMinimumWidth = minimumWidth;
1244 }
Winson Chung34b23d52011-03-18 11:29:34 -07001245 protected void resetMinimumWidthOverride() {
1246 mMinimumWidth = 0;
1247 }
Winson Chung1908d072011-02-24 18:09:44 -08001248
1249 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001250 // This functions are called enough times that it actually makes a difference in the
1251 // profiler -- so just inline the max() here
1252 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1253 final int minWidth = mMinimumWidth;
1254 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001255 }
1256
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001258 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 }
1260
Winson Chung557d6ed2011-07-08 15:34:52 -07001261 protected int getScaledRelativeChildOffset(int index) {
1262 return (getMeasuredWidth() - getScaledMeasuredWidth(getChildAt(index))) / 2;
1263 }
1264
Winson Chung321e9ee2010-08-09 13:37:56 -07001265 protected int getChildOffset(int index) {
1266 if (getChildCount() == 0)
1267 return 0;
1268
1269 int offset = getRelativeChildOffset(0);
1270 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001271 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 }
1273 return offset;
1274 }
1275
Michael Jurkad3ef3062010-11-23 16:23:58 -08001276 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001277 // This functions are called enough times that it actually makes a difference in the
1278 // profiler -- so just inline the max() here
1279 final int measuredWidth = child.getMeasuredWidth();
1280 final int minWidth = mMinimumWidth;
1281 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1282 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001283 }
1284
Adam Cohend19d3ca2010-09-15 14:43:42 -07001285 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 int minDistanceFromScreenCenter = getMeasuredWidth();
1287 int minDistanceFromScreenCenterIndex = -1;
1288 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1289 final int childCount = getChildCount();
1290 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001291 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001292 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 int halfChildWidth = (childWidth / 2);
1294 int childCenter = getChildOffset(i) + halfChildWidth;
1295 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1296 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1297 minDistanceFromScreenCenter = distanceFromScreenCenter;
1298 minDistanceFromScreenCenterIndex = i;
1299 }
1300 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001301 return minDistanceFromScreenCenterIndex;
1302 }
1303
1304 protected void snapToDestination() {
1305 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001306 }
1307
Adam Cohene0f66b52010-11-23 15:06:07 -08001308 private static class ScrollInterpolator implements Interpolator {
1309 public ScrollInterpolator() {
1310 }
1311
1312 public float getInterpolation(float t) {
1313 t -= 1.0f;
1314 return t*t*t*t*t + 1;
1315 }
1316 }
1317
1318 // We want the duration of the page snap animation to be influenced by the distance that
1319 // the screen has to travel, however, we don't want this duration to be effected in a
1320 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1321 // of travel has on the overall snap duration.
1322 float distanceInfluenceForSnapDuration(float f) {
1323 f -= 0.5f; // center the values about 0.
1324 f *= 0.3f * Math.PI / 2.0f;
1325 return (float) Math.sin(f);
1326 }
1327
Michael Jurka0142d492010-08-25 17:46:15 -07001328 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001329 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1330 int halfScreenSize = getMeasuredWidth() / 2;
1331
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1333 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1334 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001335 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1336 int delta = newX - mUnboundedScrollX;
1337 int duration = 0;
1338
1339 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1340 // If the velocity is low enough, then treat this more as an automatic page advance
1341 // as opposed to an apparent physical response to flinging
1342 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1343 return;
1344 }
1345
1346 // Here we compute a "distance" that will be used in the computation of the overall
1347 // snap duration. This is a function of the actual distance that needs to be traveled;
1348 // we keep this value close to half screen size in order to reduce the variance in snap
1349 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001350 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001351 float distance = halfScreenSize + halfScreenSize *
1352 distanceInfluenceForSnapDuration(distanceRatio);
1353
1354 velocity = Math.abs(velocity);
1355 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1356
1357 // we want the page's snap velocity to approximately match the velocity at which the
1358 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001359 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1360 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001361
1362 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001363 }
1364
1365 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001366 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001367 }
1368
Michael Jurka0142d492010-08-25 17:46:15 -07001369 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001370 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001371
Winson Chung785d2eb2011-04-14 16:08:02 -07001372 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1373 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1374 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001375 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001376 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001378 snapToPage(whichPage, delta, duration);
1379 }
1380
1381 protected void snapToPage(int whichPage, int delta, int duration) {
1382 mNextPage = whichPage;
1383
1384 View focusedChild = getFocusedChild();
1385 if (focusedChild != null && whichPage != mCurrentPage &&
1386 focusedChild == getChildAt(mCurrentPage)) {
1387 focusedChild.clearFocus();
1388 }
1389
1390 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001391 awakenScrollBars(duration);
1392 if (duration == 0) {
1393 duration = Math.abs(delta);
1394 }
1395
1396 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001397 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001398
Winson Chungb44b5242011-06-13 11:32:14 -07001399 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1400 // loading associated pages until the scroll settles
1401 if (mDeferScrollUpdate) {
1402 loadAssociatedPages(mNextPage);
1403 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001404 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001405 }
Michael Jurka0142d492010-08-25 17:46:15 -07001406 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 invalidate();
1408 }
1409
Winson Chung321e9ee2010-08-09 13:37:56 -07001410 public void scrollLeft() {
1411 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001412 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001414 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 }
1416 }
1417
1418 public void scrollRight() {
1419 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001420 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001421 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001422 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001423 }
1424 }
1425
Winson Chung86f77532010-08-24 11:08:22 -07001426 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 int result = -1;
1428 if (v != null) {
1429 ViewParent vp = v.getParent();
1430 int count = getChildCount();
1431 for (int i = 0; i < count; i++) {
1432 if (vp == getChildAt(i)) {
1433 return i;
1434 }
1435 }
1436 }
1437 return result;
1438 }
1439
1440 /**
1441 * @return True is long presses are still allowed for the current touch
1442 */
1443 public boolean allowLongPress() {
1444 return mAllowLongPress;
1445 }
1446
Michael Jurka0142d492010-08-25 17:46:15 -07001447 /**
1448 * Set true to allow long-press events to be triggered, usually checked by
1449 * {@link Launcher} to accept or block dpad-initiated long-presses.
1450 */
1451 public void setAllowLongPress(boolean allowLongPress) {
1452 mAllowLongPress = allowLongPress;
1453 }
1454
Winson Chung321e9ee2010-08-09 13:37:56 -07001455 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001456 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001457
1458 SavedState(Parcelable superState) {
1459 super(superState);
1460 }
1461
1462 private SavedState(Parcel in) {
1463 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001464 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001465 }
1466
1467 @Override
1468 public void writeToParcel(Parcel out, int flags) {
1469 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001470 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001471 }
1472
1473 public static final Parcelable.Creator<SavedState> CREATOR =
1474 new Parcelable.Creator<SavedState>() {
1475 public SavedState createFromParcel(Parcel in) {
1476 return new SavedState(in);
1477 }
1478
1479 public SavedState[] newArray(int size) {
1480 return new SavedState[size];
1481 }
1482 };
1483 }
1484
Winson Chung86f77532010-08-24 11:08:22 -07001485 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001486 if (mContentIsRefreshable) {
1487 final int count = getChildCount();
1488 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001489 int lowerPageBound = getAssociatedLowerPageBound(page);
1490 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001491 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1492 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001493 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001494 Page layout = (Page) getChildAt(i);
1495 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001496 if (lowerPageBound <= i && i <= upperPageBound) {
1497 if (mDirtyPageContent.get(i)) {
1498 syncPageItems(i);
1499 mDirtyPageContent.set(i, false);
1500 }
1501 } else {
1502 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001503 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001504 }
1505 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001506 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001507 }
1508 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001509 }
1510 }
1511
Winson Chunge3193b92010-09-10 11:44:42 -07001512 protected int getAssociatedLowerPageBound(int page) {
1513 return Math.max(0, page - 1);
1514 }
1515 protected int getAssociatedUpperPageBound(int page) {
1516 final int count = getChildCount();
1517 return Math.min(page + 1, count - 1);
1518 }
1519
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001520 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001521 if (isChoiceMode(CHOICE_MODE_NONE)) {
1522 mChoiceMode = mode;
1523 mActionMode = startActionMode(callback);
1524 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001525 }
1526
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001527 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001528 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001529 mChoiceMode = CHOICE_MODE_NONE;
1530 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001531 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001532 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001533 }
1534 }
1535
1536 protected boolean isChoiceMode(int mode) {
1537 return mChoiceMode == mode;
1538 }
1539
1540 protected ArrayList<Checkable> getCheckedGrandchildren() {
1541 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1542 final int childCount = getChildCount();
1543 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001544 Page layout = (Page) getChildAt(i);
1545 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001546 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001547 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001548 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001549 checked.add((Checkable) v);
1550 }
1551 }
1552 }
1553 return checked;
1554 }
1555
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001556 /**
1557 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1558 * Otherwise, returns null.
1559 */
1560 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001561 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001562 final int childCount = getChildCount();
1563 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001564 Page layout = (Page) getChildAt(i);
1565 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001566 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001567 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001568 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1569 return (Checkable) v;
1570 }
1571 }
1572 }
1573 }
1574 return null;
1575 }
1576
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001577 protected void resetCheckedGrandchildren() {
1578 // loop through children, and set all of their children to _not_ be checked
1579 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1580 for (int i = 0; i < checked.size(); ++i) {
1581 final Checkable c = checked.get(i);
1582 c.setChecked(false);
1583 }
1584 }
1585
Winson Chung86f77532010-08-24 11:08:22 -07001586 /**
1587 * This method is called ONLY to synchronize the number of pages that the paged view has.
1588 * To actually fill the pages with information, implement syncPageItems() below. It is
1589 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1590 * and therefore, individual page items do not need to be updated in this method.
1591 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001592 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001593
1594 /**
1595 * This method is called to synchronize the items that are on a particular page. If views on
1596 * the page can be reused, then they should be updated within this method.
1597 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001599
Michael Jurka983e3fd2011-05-26 17:10:29 -07001600 protected void postInvalidatePageData(final boolean clearViews) {
1601 post(new Runnable() {
1602 // post the call to avoid a call to requestLayout from a layout pass
1603 public void run() {
1604 if (clearViews) {
1605 removeAllViews();
1606 }
1607 invalidatePageData();
1608 }
1609 });
1610 }
1611
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001612 protected void invalidatePageData() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001613 if (!mIsDataReady) {
1614 return;
1615 }
1616
Michael Jurka0142d492010-08-25 17:46:15 -07001617 if (mContentIsRefreshable) {
1618 // Update all the pages
1619 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001620
Michael Jurka0142d492010-08-25 17:46:15 -07001621 // Mark each of the pages as dirty
1622 final int count = getChildCount();
1623 mDirtyPageContent.clear();
1624 for (int i = 0; i < count; ++i) {
1625 mDirtyPageContent.add(true);
1626 }
1627
1628 // Load any pages that are necessary for the current window of views
1629 loadAssociatedPages(mCurrentPage);
1630 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001631 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001632 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001633 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 }
Winson Chung007c6982011-06-14 13:27:53 -07001635
1636 private ImageView getScrollingIndicator() {
1637 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1638 // found
1639 if (mHasScrollIndicator && mScrollIndicator == null) {
1640 ViewGroup parent = (ViewGroup) getParent();
1641 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1642 mHasScrollIndicator = mScrollIndicator != null;
1643 if (mHasScrollIndicator) {
1644 mScrollIndicator.setVisibility(View.VISIBLE);
1645 }
1646 }
1647 return mScrollIndicator;
1648 }
1649
1650 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001651 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001652 }
1653
Winson Chung3ac74c52011-06-30 17:39:37 -07001654 protected void flashScrollingIndicator() {
1655 showScrollingIndicator();
1656 postDelayed(new Runnable() {
1657 @Override
1658 public void run() {
1659 hideScrollingIndicator(false);
1660 }
1661 }, sScrollIndicatorFlashDuration);
1662 }
1663
Winson Chung007c6982011-06-14 13:27:53 -07001664 protected void showScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001665 if (getChildCount() <= 1) return;
1666 if (!isScrollingIndicatorEnabled()) return;
1667
1668 getScrollingIndicator();
1669 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001670 // Fade the indicator in
1671 updateScrollingIndicatorPosition();
Winson Chung3ac74c52011-06-30 17:39:37 -07001672 mScrollIndicator.animate().alpha(1f).setDuration(sScrollIndicatorFadeInDuration).start();
Winson Chung007c6982011-06-14 13:27:53 -07001673 }
1674 }
1675
1676 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001677 if (getChildCount() <= 1) return;
1678 if (!isScrollingIndicatorEnabled()) return;
1679
1680 getScrollingIndicator();
1681 if (mScrollIndicator != null) {
1682 // Fade the indicator out
1683 updateScrollingIndicatorPosition();
1684 mScrollIndicator.animate().alpha(0f).setDuration(immediately ?
Winson Chung3ac74c52011-06-30 17:39:37 -07001685 sScrollIndicatorFastFadeOutDuration : sScrollIndicatorFadeOutDuration).start();
Winson Chung007c6982011-06-14 13:27:53 -07001686 }
1687 }
1688
1689 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001690 if (getChildCount() <= 1) return;
1691 if (!isScrollingIndicatorEnabled()) return;
1692
1693 getScrollingIndicator();
1694 if (mScrollIndicator != null) {
1695 updateScrollingIndicatorPosition();
1696 }
1697 }
1698
Winson Chung007c6982011-06-14 13:27:53 -07001699 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001700 if (!isScrollingIndicatorEnabled()) return;
1701
Winson Chung007c6982011-06-14 13:27:53 -07001702 // We can make the page width smaller to make it look more centered
Winson Chung649723c2011-07-06 20:41:23 -07001703 getScrollingIndicatorTrack();
1704 int pageWidth = mScrollTrack.getMeasuredWidth() - mScrollTrack.getPaddingLeft() -
1705 mScrollTrack.getPaddingRight();
1706 int maxPageWidth = getChildCount() * getMeasuredWidth();
Winson Chung007c6982011-06-14 13:27:53 -07001707 float offset = (float) getScrollX() / maxPageWidth;
1708 int indicatorWidth = pageWidth / getChildCount();
1709 int indicatorCenterOffset = indicatorWidth / 2 - mScrollIndicator.getMeasuredWidth() / 2;
Winson Chung649723c2011-07-06 20:41:23 -07001710 int indicatorPos = (int) (offset * pageWidth) + mScrollTrack.getPaddingLeft() +
1711 indicatorCenterOffset;
Winson Chung007c6982011-06-14 13:27:53 -07001712 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001713 mScrollIndicator.invalidate();
1714 }
1715
1716 private ImageView getScrollingIndicatorTrack() {
1717 if (mScrollTrack == null) {
1718 ViewGroup parent = (ViewGroup) getParent();
1719 mScrollTrack = (ImageView) (parent.findViewById(R.id.paged_view_indicator_track));
1720 }
1721 return mScrollTrack;
1722 }
1723
1724 public void showScrollIndicatorTrack() {
Winson Chung649723c2011-07-06 20:41:23 -07001725 if (!isScrollingIndicatorEnabled()) return;
1726
1727 getScrollingIndicatorTrack();
1728 if (mScrollTrack != null) {
1729 mScrollTrack.setVisibility(View.VISIBLE);
Winson Chung3ac74c52011-06-30 17:39:37 -07001730 }
1731 }
1732
1733 public void hideScrollIndicatorTrack() {
Winson Chung649723c2011-07-06 20:41:23 -07001734 if (!isScrollingIndicatorEnabled()) return;
1735
1736 getScrollingIndicatorTrack();
1737 if (mScrollTrack != null) {
1738 mScrollTrack.setVisibility(View.GONE);
Winson Chung3ac74c52011-06-30 17:39:37 -07001739 }
Winson Chung007c6982011-06-14 13:27:53 -07001740 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001741
1742 /* Accessibility */
1743 @Override
1744 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1745 super.onInitializeAccessibilityNodeInfo(info);
1746 info.setScrollable(true);
1747 }
1748
1749 @Override
1750 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1751 super.onInitializeAccessibilityEvent(event);
1752 event.setScrollable(true);
1753 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1754 event.setFromIndex(mCurrentPage);
1755 event.setToIndex(mCurrentPage);
1756 event.setItemCount(getChildCount());
1757 }
1758 }
1759
1760 @Override
1761 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1762 // Do not append text content to scroll events they are fired frequently
1763 // and the client has already received another event type with the text.
1764 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1765 super.dispatchPopulateAccessibilityEvent(event);
1766 }
1767
1768 onPopulateAccessibilityEvent(event);
1769 return false;
1770 }
1771
1772 @Override
1773 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1774 super.onPopulateAccessibilityEvent(event);
1775
1776 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1777 event.getText().add(getCurrentPageDescription());
1778 }
1779 }
1780
1781 protected String getCurrentPageDescription() {
1782 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1783 return String.format(mContext.getString(R.string.default_scroll_format),
1784 page + 1, getChildCount());
1785 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001786}