blob: 9ef1fa8efe91e056f39f2c1e5290dc17f82725f3 [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() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700304 if (!mIsPageMoving) {
305 mIsPageMoving = true;
306 onPageBeginMoving();
307 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700308 }
309
Michael Jurkace7e05f2011-02-01 22:02:35 -0800310 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700311 if (mIsPageMoving) {
312 mIsPageMoving = false;
313 onPageEndMoving();
314 }
Michael Jurka0142d492010-08-25 17:46:15 -0700315 }
316
Adam Cohen26976d92011-03-22 15:33:33 -0700317 protected boolean isPageMoving() {
318 return mIsPageMoving;
319 }
320
Michael Jurka0142d492010-08-25 17:46:15 -0700321 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700322 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700323 showScrollingIndicator();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700324 }
325
326 // a method that subclasses can override to add behavior
327 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700328 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700329 }
330
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 /**
Winson Chung86f77532010-08-24 11:08:22 -0700332 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 *
334 * @param l The listener used to respond to long clicks.
335 */
336 @Override
337 public void setOnLongClickListener(OnLongClickListener l) {
338 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700339 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700340 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700341 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 }
343 }
344
345 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800346 public void scrollBy(int x, int y) {
347 scrollTo(mUnboundedScrollX + x, mScrollY + y);
348 }
349
350 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700351 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800352 mUnboundedScrollX = x;
353
354 if (x < 0) {
355 super.scrollTo(0, y);
356 if (mAllowOverScroll) {
357 overScroll(x);
358 }
359 } else if (x > mMaxScrollX) {
360 super.scrollTo(mMaxScrollX, y);
361 if (mAllowOverScroll) {
362 overScroll(x - mMaxScrollX);
363 }
364 } else {
365 super.scrollTo(x, y);
366 }
367
Michael Jurka0142d492010-08-25 17:46:15 -0700368 mTouchX = x;
369 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
370 }
371
372 // we moved this functionality to a helper function so SmoothPagedView can reuse it
373 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700375 // Don't bother scrolling if the page does not need to be moved
376 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
377 mDirtyPageAlpha = true;
378 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
379 }
Michael Jurka0142d492010-08-25 17:46:15 -0700380 invalidate();
381 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700382 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700383 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700384 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700385 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700386 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700387
388 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700389 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700390 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700391 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700392 }
393
Adam Cohen73aa9752010-11-24 16:26:58 -0800394 // We don't want to trigger a page end moving unless the page has settled
395 // and the user has stopped scrolling
396 if (mTouchState == TOUCH_STATE_REST) {
397 pageEndMoving();
398 }
Michael Jurka0142d492010-08-25 17:46:15 -0700399 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 }
Michael Jurka0142d492010-08-25 17:46:15 -0700401 return false;
402 }
403
404 @Override
405 public void computeScroll() {
406 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700407 }
408
409 @Override
410 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700411 if (!mIsDataReady) {
412 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
413 return;
414 }
415
Winson Chung321e9ee2010-08-09 13:37:56 -0700416 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
417 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
418 if (widthMode != MeasureSpec.EXACTLY) {
419 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
420 }
421
Adam Lesinski6b879f02010-11-04 16:15:23 -0700422 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
423 * of the All apps view on XLarge displays to not take up more space then it needs. Width
424 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
425 * each page to have the same width.
426 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700428 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
429 int maxChildHeight = 0;
430
431 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700432
Michael Jurka36fcb742011-04-06 17:08:58 -0700433
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700435 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700436 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 final int childCount = getChildCount();
438 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700439 // disallowing padding in paged view (just pass 0)
440 final View child = getChildAt(i);
441 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
442
443 int childWidthMode;
444 if (lp.width == LayoutParams.WRAP_CONTENT) {
445 childWidthMode = MeasureSpec.AT_MOST;
446 } else {
447 childWidthMode = MeasureSpec.EXACTLY;
448 }
449
450 int childHeightMode;
451 if (lp.height == LayoutParams.WRAP_CONTENT) {
452 childHeightMode = MeasureSpec.AT_MOST;
453 } else {
454 childHeightMode = MeasureSpec.EXACTLY;
455 }
456
457 final int childWidthMeasureSpec =
458 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
459 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700460 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700461
462 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700463 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
465 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700466 }
467
468 if (heightMode == MeasureSpec.AT_MOST) {
469 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800471 if (childCount > 0) {
472 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
473 } else {
474 mMaxScrollX = 0;
475 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700476
477 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
479
Michael Jurka8c920dd2011-01-20 14:16:56 -0800480 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800481 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
482 int delta = newX - mScrollX;
483
Michael Jurka8c920dd2011-01-20 14:16:56 -0800484 final int pageCount = getChildCount();
485 for (int i = 0; i < pageCount; i++) {
486 View page = (View) getChildAt(i);
487 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800488 }
489 setCurrentPage(newCurrentPage);
490 }
491
Michael Jurka8c920dd2011-01-20 14:16:56 -0800492 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
493 // 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 -0800494 // tightens the layout accordingly
495 public void setLayoutScale(float childrenScale) {
496 mLayoutScale = childrenScale;
497
498 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
499 int childCount = getChildCount();
500 float childrenX[] = new float[childCount];
501 float childrenY[] = new float[childCount];
502 for (int i = 0; i < childCount; i++) {
503 final View child = getChildAt(i);
504 childrenX[i] = child.getX();
505 childrenY[i] = child.getY();
506 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700507 // Trigger a full re-layout (never just call onLayout directly!)
508 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
509 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
510 requestLayout();
511 measure(widthSpec, heightSpec);
512 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800513 for (int i = 0; i < childCount; i++) {
514 final View child = getChildAt(i);
515 child.setX(childrenX[i]);
516 child.setY(childrenY[i]);
517 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700518
Michael Jurkad3ef3062010-11-23 16:23:58 -0800519 // Also, the page offset has changed (since the pages are now smaller);
520 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800521 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800522 }
523
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700525 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700526 if (!mIsDataReady) {
527 return;
528 }
529
Winson Chung785d2eb2011-04-14 16:08:02 -0700530 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700531 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
532 setHorizontalScrollBarEnabled(false);
533 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
534 scrollTo(newX, 0);
535 mScroller.setFinalX(newX);
536 setHorizontalScrollBarEnabled(true);
537 mFirstLayout = false;
538 }
539
Adam Lesinski6b879f02010-11-04 16:15:23 -0700540 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 final int childCount = getChildCount();
542 int childLeft = 0;
543 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700544 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
545 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700546 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 }
548
549 for (int i = 0; i < childCount; i++) {
550 final View child = getChildAt(i);
551 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800552 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700553 final int childHeight = child.getMeasuredHeight();
554 int childTop = mPaddingTop;
555 if (mCenterPagesVertically) {
556 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
557 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800558
Winson Chung785d2eb2011-04-14 16:08:02 -0700559 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700560 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800561 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700562 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800565 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
566 mFirstLayout = false;
567 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569
Winson Chung03929772011-02-23 17:07:10 -0800570 protected void forceUpdateAdjacentPagesAlpha() {
571 mDirtyPageAlpha = true;
572 updateAdjacentPagesAlpha();
573 }
574
Winson Chunge3193b92010-09-10 11:44:42 -0700575 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700576 if (mFadeInAdjacentScreens) {
577 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700578 int screenWidth = getMeasuredWidth();
579 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700580 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700581 final int childCount = getChildCount();
582 for (int i = 0; i < childCount; ++i) {
583 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800584 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700585 int halfChildWidth = (childWidth / 2);
586 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700587
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700588 // On the first layout, we may not have a width nor a proper offset, so for now
589 // we should just assume full page width (and calculate the offset according to
590 // that).
591 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700592 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700593 childCenter = (i * childWidth) + (childWidth / 2);
594 }
595
Winson Chunge8878e32010-09-15 20:37:09 -0700596 int d = halfChildWidth;
597 int distanceFromScreenCenter = childCenter - screenCenter;
598 if (distanceFromScreenCenter > 0) {
599 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800600 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700601 } else {
602 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700603 }
Michael Jurka0142d492010-08-25 17:46:15 -0700604 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700605 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800606 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700607 } else {
608 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700609 }
Michael Jurka0142d492010-08-25 17:46:15 -0700610 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700611 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700612
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700613 // Preventing potential divide-by-zero
614 d = Math.max(1, d);
615
Winson Chunge8878e32010-09-15 20:37:09 -0700616 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
617 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
618 float alpha = 1.0f - dimAlpha;
619
Adam Cohenf34bab52010-09-30 14:11:56 -0700620 if (alpha < ALPHA_QUANTIZE_LEVEL) {
621 alpha = 0.0f;
622 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
623 alpha = 1.0f;
624 }
625
Michael Jurkaa40829a2011-02-16 18:02:45 -0800626 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
627 // this optimization causes alpha to not be properly updated sometimes (repro
628 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
629 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
630 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800631 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700632 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800633 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700634 }
Michael Jurka0142d492010-08-25 17:46:15 -0700635 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 }
637 }
Winson Chunge3193b92010-09-10 11:44:42 -0700638 }
639
Adam Cohenf34bab52010-09-30 14:11:56 -0700640 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700641 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700642 }
643
Winson Chunge3193b92010-09-10 11:44:42 -0700644 @Override
645 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700646 int halfScreenSize = getMeasuredWidth() / 2;
647 int screenCenter = mScrollX + halfScreenSize;
648
649 if (screenCenter != mLastScreenCenter) {
650 screenScrolled(screenCenter);
651 updateAdjacentPagesAlpha();
652 mLastScreenCenter = screenCenter;
653 }
Michael Jurka0142d492010-08-25 17:46:15 -0700654
655 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700656 // As an optimization, this code assumes that all pages have the same width as the 0th
657 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700658 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700659 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800660 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700661 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700662 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700663 int leftScreen = 0;
664 int rightScreen = 0;
665 while (x <= mScrollX) {
666 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800667 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700668 }
669 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800670 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700671 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800672 if (rightScreen < pageCount) {
673 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
674 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700675 }
676 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700677
Michael Jurkac4fb9172010-09-02 17:19:20 -0700678 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800679 // Clip to the bounds
680 canvas.save();
681 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
682 mScrollY + mBottom - mTop);
683
Michael Jurkac4fb9172010-09-02 17:19:20 -0700684 for (int i = leftScreen; i <= rightScreen; i++) {
685 drawChild(canvas, getChildAt(i), drawingTime);
686 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800687 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700688 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700689 }
690
691 @Override
692 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700693 int page = indexOfChild(child);
694 if (page != mCurrentPage || !mScroller.isFinished()) {
695 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700696 return true;
697 }
698 return false;
699 }
700
701 @Override
702 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700703 int focusablePage;
704 if (mNextPage != INVALID_PAGE) {
705 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700706 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700707 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 }
Winson Chung86f77532010-08-24 11:08:22 -0700709 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700711 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700712 }
713 return false;
714 }
715
716 @Override
717 public boolean dispatchUnhandledMove(View focused, int direction) {
718 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700719 if (getCurrentPage() > 0) {
720 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700721 return true;
722 }
723 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700724 if (getCurrentPage() < getPageCount() - 1) {
725 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700726 return true;
727 }
728 }
729 return super.dispatchUnhandledMove(focused, direction);
730 }
731
732 @Override
733 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700734 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
735 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 }
737 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700738 if (mCurrentPage > 0) {
739 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700742 if (mCurrentPage < getPageCount() - 1) {
743 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700744 }
745 }
746 }
747
748 /**
749 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700750 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 *
Winson Chung86f77532010-08-24 11:08:22 -0700752 * This happens when live folders requery, and if they're off page, they
753 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700754 */
755 @Override
756 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700757 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 View v = focused;
759 while (true) {
760 if (v == current) {
761 super.focusableViewAvailable(focused);
762 return;
763 }
764 if (v == this) {
765 return;
766 }
767 ViewParent parent = v.getParent();
768 if (parent instanceof View) {
769 v = (View)v.getParent();
770 } else {
771 return;
772 }
773 }
774 }
775
776 /**
777 * {@inheritDoc}
778 */
779 @Override
780 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
781 if (disallowIntercept) {
782 // We need to make sure to cancel our long press if
783 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700784 final View currentPage = getChildAt(mCurrentPage);
785 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700786 }
787 super.requestDisallowInterceptTouchEvent(disallowIntercept);
788 }
789
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800790 /**
791 * Return true if a tap at (x, y) should trigger a flip to the previous page.
792 */
793 protected boolean hitsPreviousPage(float x, float y) {
794 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
795 }
796
797 /**
798 * Return true if a tap at (x, y) should trigger a flip to the next page.
799 */
800 protected boolean hitsNextPage(float x, float y) {
801 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
802 }
803
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 @Override
805 public boolean onInterceptTouchEvent(MotionEvent ev) {
806 /*
807 * This method JUST determines whether we want to intercept the motion.
808 * If we return true, onTouchEvent will be called and we do the actual
809 * scrolling there.
810 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800811 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700812
Winson Chung45e1d6e2010-11-09 17:19:49 -0800813 // Skip touch handling if there are no pages to swipe
814 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
815
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 /*
817 * Shortcut the most recurring case: the user is in the dragging
818 * state and he is moving his finger. We want to intercept this
819 * motion.
820 */
821 final int action = ev.getAction();
822 if ((action == MotionEvent.ACTION_MOVE) &&
823 (mTouchState == TOUCH_STATE_SCROLLING)) {
824 return true;
825 }
826
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 switch (action & MotionEvent.ACTION_MASK) {
828 case MotionEvent.ACTION_MOVE: {
829 /*
830 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
831 * whether the user has moved far enough from his original down touch.
832 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700833 if (mActivePointerId != INVALID_POINTER) {
834 determineScrollingStart(ev);
835 break;
836 }
837 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
838 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
839 // i.e. fall through to the next case (don't break)
840 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
841 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700842 }
843
844 case MotionEvent.ACTION_DOWN: {
845 final float x = ev.getX();
846 final float y = ev.getY();
847 // Remember location of down touch
848 mDownMotionX = x;
849 mLastMotionX = x;
850 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800851 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800852 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 mActivePointerId = ev.getPointerId(0);
854 mAllowLongPress = true;
855
856 /*
857 * If being flinged and user touches the screen, initiate drag;
858 * otherwise don't. mScroller.isFinished should be false when
859 * being flinged.
860 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700861 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700862 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
863 if (finishedScrolling) {
864 mTouchState = TOUCH_STATE_REST;
865 mScroller.abortAnimation();
866 } else {
867 mTouchState = TOUCH_STATE_SCROLLING;
868 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700869
Winson Chung86f77532010-08-24 11:08:22 -0700870 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700871 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800872 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800874 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800876 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 mTouchState = TOUCH_STATE_NEXT_PAGE;
878 }
879 }
880 }
881 break;
882 }
883
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800885 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 mTouchState = TOUCH_STATE_REST;
887 mAllowLongPress = false;
888 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800889 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 break;
891
892 case MotionEvent.ACTION_POINTER_UP:
893 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800894 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 break;
896 }
897
898 /*
899 * The only time we want to intercept motion events is if we are in the
900 * drag mode.
901 */
902 return mTouchState != TOUCH_STATE_REST;
903 }
904
Winson Chung80baf5a2010-08-09 16:03:15 -0700905 protected void animateClickFeedback(View v, final Runnable r) {
906 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800907 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
908 loadAnimator(mContext, R.anim.paged_view_click_feedback);
909 anim.setTarget(v);
910 anim.addListener(new AnimatorListenerAdapter() {
911 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700912 r.run();
913 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700914 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800915 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700916 }
917
Adam Cohenf8d28232011-02-01 21:47:00 -0800918 protected void determineScrollingStart(MotionEvent ev) {
919 determineScrollingStart(ev, 1.0f);
920 }
921
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 /*
923 * Determines if we should change the touch state to start scrolling after the
924 * user moves their touch point too far.
925 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800926 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 /*
928 * Locally do absolute value. mLastMotionX is set to the y value
929 * of the down event.
930 */
931 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700932 if (pointerIndex == -1) {
933 return;
934 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700935 final float x = ev.getX(pointerIndex);
936 final float y = ev.getY(pointerIndex);
937 final int xDiff = (int) Math.abs(x - mLastMotionX);
938 final int yDiff = (int) Math.abs(y - mLastMotionY);
939
Adam Cohenf8d28232011-02-01 21:47:00 -0800940 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 boolean xPaged = xDiff > mPagingTouchSlop;
942 boolean xMoved = xDiff > touchSlop;
943 boolean yMoved = yDiff > touchSlop;
944
Adam Cohenf8d28232011-02-01 21:47:00 -0800945 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700946 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 // Scroll if the user moved far enough along the X axis
948 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800949 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800951 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700952 mTouchX = mScrollX;
953 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
954 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 }
956 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800957 cancelCurrentPageLongPress();
958 }
959 }
960
961 protected void cancelCurrentPageLongPress() {
962 if (mAllowLongPress) {
963 mAllowLongPress = false;
964 // Try canceling the long press. It could also have been scheduled
965 // by a distant descendant, so use the mAllowLongPress flag to block
966 // everything
967 final View currentPage = getPageAt(mCurrentPage);
968 if (currentPage != null) {
969 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 }
971 }
972 }
973
Adam Cohene0f66b52010-11-23 15:06:07 -0800974 // This curve determines how the effect of scrolling over the limits of the page dimishes
975 // as the user pulls further and further from the bounds
976 private float overScrollInfluenceCurve(float f) {
977 f -= 1.0f;
978 return f * f * f + 1.0f;
979 }
980
Adam Cohen68d73932010-11-15 10:50:58 -0800981 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800982 int screenSize = getMeasuredWidth();
983
984 float f = (amount / screenSize);
985
986 if (f == 0) return;
987 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
988
Adam Cohen7bfc9792011-01-28 13:52:37 -0800989 // Clamp this factor, f, to -1 < f < 1
990 if (Math.abs(f) >= 1) {
991 f /= Math.abs(f);
992 }
993
Adam Cohene0f66b52010-11-23 15:06:07 -0800994 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800995 if (amount < 0) {
996 mScrollX = overScrollAmount;
997 } else {
998 mScrollX = mMaxScrollX + overScrollAmount;
999 }
1000 invalidate();
1001 }
1002
Michael Jurkac5b262c2011-01-12 20:24:50 -08001003 protected float maxOverScroll() {
1004 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
1005 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
1006 float f = 1.0f;
1007 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1008 return OVERSCROLL_DAMP_FACTOR * f;
1009 }
1010
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 @Override
1012 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001013 // Skip touch handling if there are no pages to swipe
1014 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1015
Michael Jurkab8f06722010-10-10 15:58:46 -07001016 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001017
1018 final int action = ev.getAction();
1019
1020 switch (action & MotionEvent.ACTION_MASK) {
1021 case MotionEvent.ACTION_DOWN:
1022 /*
1023 * If being flinged and user touches, stop the fling. isFinished
1024 * will be false if being flinged.
1025 */
1026 if (!mScroller.isFinished()) {
1027 mScroller.abortAnimation();
1028 }
1029
1030 // Remember where the motion event started
1031 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001032 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001033 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001035 if (mTouchState == TOUCH_STATE_SCROLLING) {
1036 pageBeginMoving();
1037 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 break;
1039
1040 case MotionEvent.ACTION_MOVE:
1041 if (mTouchState == TOUCH_STATE_SCROLLING) {
1042 // Scroll to follow the motion event
1043 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1044 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001045 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001046
Adam Cohenaefd4e12011-02-14 16:39:38 -08001047 mTotalMotionX += Math.abs(deltaX);
1048
Winson Chungc0844aa2011-02-02 15:25:58 -08001049 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1050 // keep the remainder because we are actually testing if we've moved from the last
1051 // scrolled position (which is discrete).
1052 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001053 mTouchX += deltaX;
1054 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1055 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001056 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001057 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001058 } else {
1059 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001061 mLastMotionX = x;
1062 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 } else {
1064 awakenScrollBars();
1065 }
Adam Cohen564976a2010-10-13 18:52:07 -07001066 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 determineScrollingStart(ev);
1068 }
1069 break;
1070
1071 case MotionEvent.ACTION_UP:
1072 if (mTouchState == TOUCH_STATE_SCROLLING) {
1073 final int activePointerId = mActivePointerId;
1074 final int pointerIndex = ev.findPointerIndex(activePointerId);
1075 final float x = ev.getX(pointerIndex);
1076 final VelocityTracker velocityTracker = mVelocityTracker;
1077 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1078 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001079 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001080 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001081 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001082
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001083 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1084
Adam Cohenaefd4e12011-02-14 16:39:38 -08001085 // In the case that the page is moved far to one direction and then is flung
1086 // in the opposite direction, we use a threshold to determine whether we should
1087 // just return to the starting page, or if we should skip one further.
1088 boolean returnToOriginalPage = false;
1089 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001090 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001091 Math.signum(velocityX) != Math.signum(deltaX)) {
1092 returnToOriginalPage = true;
1093 }
1094
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001095 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001096 Math.abs(velocityX) > snapVelocity;
1097
1098 int finalPage;
1099 // We give flings precedence over large moves, which is why we short-circuit our
1100 // test for a large move if a fling has been registered. That is, a large
1101 // move to the left and fling to the right will register as a fling to the right.
1102 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1103 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1104 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1105 snapToPageWithVelocity(finalPage, velocityX);
1106 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1107 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001108 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001109 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1110 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001111 } else {
1112 snapToDestination();
1113 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001114 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 // at this point we have not moved beyond the touch slop
1116 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1117 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001118 int nextPage = Math.max(0, mCurrentPage - 1);
1119 if (nextPage != mCurrentPage) {
1120 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 } else {
1122 snapToDestination();
1123 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001124 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001125 // at this point we have not moved beyond the touch slop
1126 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1127 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001128 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1129 if (nextPage != mCurrentPage) {
1130 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 } else {
1132 snapToDestination();
1133 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001134 } else {
1135 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001136 }
1137 mTouchState = TOUCH_STATE_REST;
1138 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001139 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001140 break;
1141
1142 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001143 if (mTouchState == TOUCH_STATE_SCROLLING) {
1144 snapToDestination();
1145 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001146 mTouchState = TOUCH_STATE_REST;
1147 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001148 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001149 break;
1150
1151 case MotionEvent.ACTION_POINTER_UP:
1152 onSecondaryPointerUp(ev);
1153 break;
1154 }
1155
1156 return true;
1157 }
1158
Winson Chung185d7162011-02-28 13:47:29 -08001159 @Override
1160 public boolean onGenericMotionEvent(MotionEvent event) {
1161 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1162 switch (event.getAction()) {
1163 case MotionEvent.ACTION_SCROLL: {
1164 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1165 final float vscroll;
1166 final float hscroll;
1167 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1168 vscroll = 0;
1169 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1170 } else {
1171 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1172 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1173 }
1174 if (hscroll != 0 || vscroll != 0) {
1175 if (hscroll > 0 || vscroll > 0) {
1176 scrollRight();
1177 } else {
1178 scrollLeft();
1179 }
1180 return true;
1181 }
1182 }
1183 }
1184 }
1185 return super.onGenericMotionEvent(event);
1186 }
1187
Michael Jurkab8f06722010-10-10 15:58:46 -07001188 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1189 if (mVelocityTracker == null) {
1190 mVelocityTracker = VelocityTracker.obtain();
1191 }
1192 mVelocityTracker.addMovement(ev);
1193 }
1194
1195 private void releaseVelocityTracker() {
1196 if (mVelocityTracker != null) {
1197 mVelocityTracker.recycle();
1198 mVelocityTracker = null;
1199 }
1200 }
1201
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 private void onSecondaryPointerUp(MotionEvent ev) {
1203 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1204 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1205 final int pointerId = ev.getPointerId(pointerIndex);
1206 if (pointerId == mActivePointerId) {
1207 // This was our active pointer going up. Choose a new
1208 // active pointer and adjust accordingly.
1209 // TODO: Make this decision more intelligent.
1210 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1211 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1212 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001213 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 mActivePointerId = ev.getPointerId(newPointerIndex);
1215 if (mVelocityTracker != null) {
1216 mVelocityTracker.clear();
1217 }
1218 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001219 if (mTouchState == TOUCH_STATE_REST) {
1220 onWallpaperTap(ev);
1221 }
1222 }
1223
Winson Chungf0ea4d32011-06-06 14:27:16 -07001224 protected void onWallpaperTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001225
1226 @Override
1227 public void requestChildFocus(View child, View focused) {
1228 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001229 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001230 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001231 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 }
1233 }
1234
Winson Chunge3193b92010-09-10 11:44:42 -07001235 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1236 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001237 int left;
1238 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001239 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001240 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001241 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001242 if (left <= relativeOffset && relativeOffset <= right) {
1243 return i;
1244 }
Winson Chunge3193b92010-09-10 11:44:42 -07001245 }
1246 return -1;
1247 }
1248
Winson Chung1908d072011-02-24 18:09:44 -08001249 protected void setMinimumWidthOverride(int minimumWidth) {
1250 mMinimumWidth = minimumWidth;
1251 }
Winson Chung34b23d52011-03-18 11:29:34 -07001252 protected void resetMinimumWidthOverride() {
1253 mMinimumWidth = 0;
1254 }
Winson Chung1908d072011-02-24 18:09:44 -08001255
1256 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001257 // This functions are called enough times that it actually makes a difference in the
1258 // profiler -- so just inline the max() here
1259 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1260 final int minWidth = mMinimumWidth;
1261 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001262 }
1263
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001265 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 }
1267
Winson Chung557d6ed2011-07-08 15:34:52 -07001268 protected int getScaledRelativeChildOffset(int index) {
1269 return (getMeasuredWidth() - getScaledMeasuredWidth(getChildAt(index))) / 2;
1270 }
1271
Winson Chung321e9ee2010-08-09 13:37:56 -07001272 protected int getChildOffset(int index) {
1273 if (getChildCount() == 0)
1274 return 0;
1275
1276 int offset = getRelativeChildOffset(0);
1277 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001278 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001279 }
1280 return offset;
1281 }
1282
Michael Jurkad3ef3062010-11-23 16:23:58 -08001283 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001284 // This functions are called enough times that it actually makes a difference in the
1285 // profiler -- so just inline the max() here
1286 final int measuredWidth = child.getMeasuredWidth();
1287 final int minWidth = mMinimumWidth;
1288 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1289 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001290 }
1291
Adam Cohend19d3ca2010-09-15 14:43:42 -07001292 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001293 int minDistanceFromScreenCenter = getMeasuredWidth();
1294 int minDistanceFromScreenCenterIndex = -1;
1295 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1296 final int childCount = getChildCount();
1297 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001298 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001299 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001300 int halfChildWidth = (childWidth / 2);
1301 int childCenter = getChildOffset(i) + halfChildWidth;
1302 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1303 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1304 minDistanceFromScreenCenter = distanceFromScreenCenter;
1305 minDistanceFromScreenCenterIndex = i;
1306 }
1307 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001308 return minDistanceFromScreenCenterIndex;
1309 }
1310
1311 protected void snapToDestination() {
1312 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001313 }
1314
Adam Cohene0f66b52010-11-23 15:06:07 -08001315 private static class ScrollInterpolator implements Interpolator {
1316 public ScrollInterpolator() {
1317 }
1318
1319 public float getInterpolation(float t) {
1320 t -= 1.0f;
1321 return t*t*t*t*t + 1;
1322 }
1323 }
1324
1325 // We want the duration of the page snap animation to be influenced by the distance that
1326 // the screen has to travel, however, we don't want this duration to be effected in a
1327 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1328 // of travel has on the overall snap duration.
1329 float distanceInfluenceForSnapDuration(float f) {
1330 f -= 0.5f; // center the values about 0.
1331 f *= 0.3f * Math.PI / 2.0f;
1332 return (float) Math.sin(f);
1333 }
1334
Michael Jurka0142d492010-08-25 17:46:15 -07001335 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001336 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1337 int halfScreenSize = getMeasuredWidth() / 2;
1338
Winson Chung785d2eb2011-04-14 16:08:02 -07001339 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1340 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1341 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001342 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1343 int delta = newX - mUnboundedScrollX;
1344 int duration = 0;
1345
1346 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1347 // If the velocity is low enough, then treat this more as an automatic page advance
1348 // as opposed to an apparent physical response to flinging
1349 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1350 return;
1351 }
1352
1353 // Here we compute a "distance" that will be used in the computation of the overall
1354 // snap duration. This is a function of the actual distance that needs to be traveled;
1355 // we keep this value close to half screen size in order to reduce the variance in snap
1356 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001357 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001358 float distance = halfScreenSize + halfScreenSize *
1359 distanceInfluenceForSnapDuration(distanceRatio);
1360
1361 velocity = Math.abs(velocity);
1362 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1363
1364 // we want the page's snap velocity to approximately match the velocity at which the
1365 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001366 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1367 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001368
1369 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001370 }
1371
1372 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001373 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001374 }
1375
Michael Jurka0142d492010-08-25 17:46:15 -07001376 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001377 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001378
Winson Chung785d2eb2011-04-14 16:08:02 -07001379 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1380 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1381 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001382 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001383 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001384 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001385 snapToPage(whichPage, delta, duration);
1386 }
1387
1388 protected void snapToPage(int whichPage, int delta, int duration) {
1389 mNextPage = whichPage;
1390
1391 View focusedChild = getFocusedChild();
1392 if (focusedChild != null && whichPage != mCurrentPage &&
1393 focusedChild == getChildAt(mCurrentPage)) {
1394 focusedChild.clearFocus();
1395 }
1396
1397 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001398 awakenScrollBars(duration);
1399 if (duration == 0) {
1400 duration = Math.abs(delta);
1401 }
1402
1403 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001404 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001405
Winson Chungb44b5242011-06-13 11:32:14 -07001406 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1407 // loading associated pages until the scroll settles
1408 if (mDeferScrollUpdate) {
1409 loadAssociatedPages(mNextPage);
1410 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001411 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001412 }
Michael Jurka0142d492010-08-25 17:46:15 -07001413 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001414 invalidate();
1415 }
1416
Winson Chung321e9ee2010-08-09 13:37:56 -07001417 public void scrollLeft() {
1418 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001419 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001420 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001421 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 }
1423 }
1424
1425 public void scrollRight() {
1426 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001427 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001428 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001429 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 }
1431 }
1432
Winson Chung86f77532010-08-24 11:08:22 -07001433 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001434 int result = -1;
1435 if (v != null) {
1436 ViewParent vp = v.getParent();
1437 int count = getChildCount();
1438 for (int i = 0; i < count; i++) {
1439 if (vp == getChildAt(i)) {
1440 return i;
1441 }
1442 }
1443 }
1444 return result;
1445 }
1446
1447 /**
1448 * @return True is long presses are still allowed for the current touch
1449 */
1450 public boolean allowLongPress() {
1451 return mAllowLongPress;
1452 }
1453
Michael Jurka0142d492010-08-25 17:46:15 -07001454 /**
1455 * Set true to allow long-press events to be triggered, usually checked by
1456 * {@link Launcher} to accept or block dpad-initiated long-presses.
1457 */
1458 public void setAllowLongPress(boolean allowLongPress) {
1459 mAllowLongPress = allowLongPress;
1460 }
1461
Winson Chung321e9ee2010-08-09 13:37:56 -07001462 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001463 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001464
1465 SavedState(Parcelable superState) {
1466 super(superState);
1467 }
1468
1469 private SavedState(Parcel in) {
1470 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001471 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001472 }
1473
1474 @Override
1475 public void writeToParcel(Parcel out, int flags) {
1476 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001477 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001478 }
1479
1480 public static final Parcelable.Creator<SavedState> CREATOR =
1481 new Parcelable.Creator<SavedState>() {
1482 public SavedState createFromParcel(Parcel in) {
1483 return new SavedState(in);
1484 }
1485
1486 public SavedState[] newArray(int size) {
1487 return new SavedState[size];
1488 }
1489 };
1490 }
1491
Winson Chung86f77532010-08-24 11:08:22 -07001492 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001493 if (mContentIsRefreshable) {
1494 final int count = getChildCount();
1495 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001496 int lowerPageBound = getAssociatedLowerPageBound(page);
1497 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001498 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1499 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001500 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001501 Page layout = (Page) getChildAt(i);
1502 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001503 if (lowerPageBound <= i && i <= upperPageBound) {
1504 if (mDirtyPageContent.get(i)) {
1505 syncPageItems(i);
1506 mDirtyPageContent.set(i, false);
1507 }
1508 } else {
1509 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001510 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001511 }
1512 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001513 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001514 }
1515 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001516 }
1517 }
1518
Winson Chunge3193b92010-09-10 11:44:42 -07001519 protected int getAssociatedLowerPageBound(int page) {
1520 return Math.max(0, page - 1);
1521 }
1522 protected int getAssociatedUpperPageBound(int page) {
1523 final int count = getChildCount();
1524 return Math.min(page + 1, count - 1);
1525 }
1526
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001527 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001528 if (isChoiceMode(CHOICE_MODE_NONE)) {
1529 mChoiceMode = mode;
1530 mActionMode = startActionMode(callback);
1531 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001532 }
1533
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001534 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001535 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001536 mChoiceMode = CHOICE_MODE_NONE;
1537 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001538 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001539 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001540 }
1541 }
1542
1543 protected boolean isChoiceMode(int mode) {
1544 return mChoiceMode == mode;
1545 }
1546
1547 protected ArrayList<Checkable> getCheckedGrandchildren() {
1548 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1549 final int childCount = getChildCount();
1550 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001551 Page layout = (Page) getChildAt(i);
1552 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001553 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001554 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001555 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001556 checked.add((Checkable) v);
1557 }
1558 }
1559 }
1560 return checked;
1561 }
1562
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001563 /**
1564 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1565 * Otherwise, returns null.
1566 */
1567 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001568 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001569 final int childCount = getChildCount();
1570 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001571 Page layout = (Page) getChildAt(i);
1572 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001573 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001574 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001575 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1576 return (Checkable) v;
1577 }
1578 }
1579 }
1580 }
1581 return null;
1582 }
1583
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001584 protected void resetCheckedGrandchildren() {
1585 // loop through children, and set all of their children to _not_ be checked
1586 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1587 for (int i = 0; i < checked.size(); ++i) {
1588 final Checkable c = checked.get(i);
1589 c.setChecked(false);
1590 }
1591 }
1592
Winson Chung86f77532010-08-24 11:08:22 -07001593 /**
1594 * This method is called ONLY to synchronize the number of pages that the paged view has.
1595 * To actually fill the pages with information, implement syncPageItems() below. It is
1596 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1597 * and therefore, individual page items do not need to be updated in this method.
1598 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001599 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001600
1601 /**
1602 * This method is called to synchronize the items that are on a particular page. If views on
1603 * the page can be reused, then they should be updated within this method.
1604 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001605 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001606
Michael Jurka983e3fd2011-05-26 17:10:29 -07001607 protected void postInvalidatePageData(final boolean clearViews) {
1608 post(new Runnable() {
1609 // post the call to avoid a call to requestLayout from a layout pass
1610 public void run() {
1611 if (clearViews) {
1612 removeAllViews();
1613 }
1614 invalidatePageData();
1615 }
1616 });
1617 }
1618
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001619 protected void invalidatePageData() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001620 if (!mIsDataReady) {
1621 return;
1622 }
1623
Michael Jurka0142d492010-08-25 17:46:15 -07001624 if (mContentIsRefreshable) {
1625 // Update all the pages
1626 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001627
Michael Jurka0142d492010-08-25 17:46:15 -07001628 // Mark each of the pages as dirty
1629 final int count = getChildCount();
1630 mDirtyPageContent.clear();
1631 for (int i = 0; i < count; ++i) {
1632 mDirtyPageContent.add(true);
1633 }
1634
1635 // Load any pages that are necessary for the current window of views
1636 loadAssociatedPages(mCurrentPage);
1637 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001638 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001639 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001640 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001641 }
Winson Chung007c6982011-06-14 13:27:53 -07001642
1643 private ImageView getScrollingIndicator() {
1644 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1645 // found
1646 if (mHasScrollIndicator && mScrollIndicator == null) {
1647 ViewGroup parent = (ViewGroup) getParent();
1648 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1649 mHasScrollIndicator = mScrollIndicator != null;
1650 if (mHasScrollIndicator) {
1651 mScrollIndicator.setVisibility(View.VISIBLE);
1652 }
1653 }
1654 return mScrollIndicator;
1655 }
1656
1657 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001658 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001659 }
1660
Winson Chung3ac74c52011-06-30 17:39:37 -07001661 protected void flashScrollingIndicator() {
1662 showScrollingIndicator();
1663 postDelayed(new Runnable() {
1664 @Override
1665 public void run() {
1666 hideScrollingIndicator(false);
1667 }
1668 }, sScrollIndicatorFlashDuration);
1669 }
1670
Winson Chung007c6982011-06-14 13:27:53 -07001671 protected void showScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001672 if (getChildCount() <= 1) return;
1673 if (!isScrollingIndicatorEnabled()) return;
1674
1675 getScrollingIndicator();
1676 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001677 // Fade the indicator in
1678 updateScrollingIndicatorPosition();
Winson Chung3ac74c52011-06-30 17:39:37 -07001679 mScrollIndicator.animate().alpha(1f).setDuration(sScrollIndicatorFadeInDuration).start();
Winson Chung007c6982011-06-14 13:27:53 -07001680 }
1681 }
1682
1683 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001684 if (getChildCount() <= 1) return;
1685 if (!isScrollingIndicatorEnabled()) return;
1686
1687 getScrollingIndicator();
1688 if (mScrollIndicator != null) {
1689 // Fade the indicator out
1690 updateScrollingIndicatorPosition();
1691 mScrollIndicator.animate().alpha(0f).setDuration(immediately ?
Winson Chung3ac74c52011-06-30 17:39:37 -07001692 sScrollIndicatorFastFadeOutDuration : sScrollIndicatorFadeOutDuration).start();
Winson Chung007c6982011-06-14 13:27:53 -07001693 }
1694 }
1695
1696 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001697 if (getChildCount() <= 1) return;
1698 if (!isScrollingIndicatorEnabled()) return;
1699
1700 getScrollingIndicator();
1701 if (mScrollIndicator != null) {
1702 updateScrollingIndicatorPosition();
1703 }
1704 }
1705
Winson Chung007c6982011-06-14 13:27:53 -07001706 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001707 if (!isScrollingIndicatorEnabled()) return;
1708
Winson Chung007c6982011-06-14 13:27:53 -07001709 // We can make the page width smaller to make it look more centered
Winson Chung649723c2011-07-06 20:41:23 -07001710 getScrollingIndicatorTrack();
1711 int pageWidth = mScrollTrack.getMeasuredWidth() - mScrollTrack.getPaddingLeft() -
1712 mScrollTrack.getPaddingRight();
1713 int maxPageWidth = getChildCount() * getMeasuredWidth();
Winson Chung007c6982011-06-14 13:27:53 -07001714 float offset = (float) getScrollX() / maxPageWidth;
1715 int indicatorWidth = pageWidth / getChildCount();
1716 int indicatorCenterOffset = indicatorWidth / 2 - mScrollIndicator.getMeasuredWidth() / 2;
Winson Chung649723c2011-07-06 20:41:23 -07001717 int indicatorPos = (int) (offset * pageWidth) + mScrollTrack.getPaddingLeft() +
1718 indicatorCenterOffset;
Winson Chung007c6982011-06-14 13:27:53 -07001719 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001720 mScrollIndicator.invalidate();
1721 }
1722
1723 private ImageView getScrollingIndicatorTrack() {
1724 if (mScrollTrack == null) {
1725 ViewGroup parent = (ViewGroup) getParent();
1726 mScrollTrack = (ImageView) (parent.findViewById(R.id.paged_view_indicator_track));
1727 }
1728 return mScrollTrack;
1729 }
1730
1731 public void showScrollIndicatorTrack() {
Winson Chung649723c2011-07-06 20:41:23 -07001732 if (!isScrollingIndicatorEnabled()) return;
1733
1734 getScrollingIndicatorTrack();
1735 if (mScrollTrack != null) {
1736 mScrollTrack.setVisibility(View.VISIBLE);
Winson Chung3ac74c52011-06-30 17:39:37 -07001737 }
1738 }
1739
1740 public void hideScrollIndicatorTrack() {
Winson Chung649723c2011-07-06 20:41:23 -07001741 if (!isScrollingIndicatorEnabled()) return;
1742
1743 getScrollingIndicatorTrack();
1744 if (mScrollTrack != null) {
1745 mScrollTrack.setVisibility(View.GONE);
Winson Chung3ac74c52011-06-30 17:39:37 -07001746 }
Winson Chung007c6982011-06-14 13:27:53 -07001747 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001748
1749 /* Accessibility */
1750 @Override
1751 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1752 super.onInitializeAccessibilityNodeInfo(info);
1753 info.setScrollable(true);
1754 }
1755
1756 @Override
1757 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1758 super.onInitializeAccessibilityEvent(event);
1759 event.setScrollable(true);
1760 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1761 event.setFromIndex(mCurrentPage);
1762 event.setToIndex(mCurrentPage);
1763 event.setItemCount(getChildCount());
1764 }
1765 }
1766
1767 @Override
1768 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1769 // Do not append text content to scroll events they are fired frequently
1770 // and the client has already received another event type with the text.
1771 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1772 super.dispatchPopulateAccessibilityEvent(event);
1773 }
1774
1775 onPopulateAccessibilityEvent(event);
1776 return false;
1777 }
1778
1779 @Override
1780 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1781 super.onPopulateAccessibilityEvent(event);
1782
1783 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1784 event.getText().add(getCurrentPageDescription());
1785 }
1786 }
1787
1788 protected String getCurrentPageDescription() {
1789 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1790 return String.format(mContext.getString(R.string.default_scroll_format),
1791 page + 1, getChildCount());
1792 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001793}