blob: 26f0e57a37064a1ca3df556f818639481cb273dd [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
Winson Chung32174c82011-07-19 15:47:55 -0700164 private android.animation.ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700165 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700166 private int mScrollIndicatorPaddingLeft;
167 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700168 private boolean mHasScrollIndicator = true;
169 private static final int sScrollIndicatorFadeInDuration = 150;
Winson Chung007c6982011-06-14 13:27:53 -0700170 private static final int sScrollIndicatorFadeOutDuration = 650;
Winson Chung3ac74c52011-06-30 17:39:37 -0700171 private static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700172
Winson Chungb44b5242011-06-13 11:32:14 -0700173 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700174 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700175
Winson Chung86f77532010-08-24 11:08:22 -0700176 public interface PageSwitchListener {
177 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700178 }
179
Winson Chung321e9ee2010-08-09 13:37:56 -0700180 public PagedView(Context context) {
181 this(context, null);
182 }
183
184 public PagedView(Context context, AttributeSet attrs) {
185 this(context, attrs, 0);
186 }
187
188 public PagedView(Context context, AttributeSet attrs, int defStyle) {
189 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700190 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700191
Adam Cohen9c4949e2010-10-05 12:27:22 -0700192 TypedArray a = context.obtainStyledAttributes(attrs,
193 R.styleable.PagedView, defStyle, 0);
194 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
195 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800196 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700197 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800198 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700199 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800200 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700201 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800202 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700203 mPageLayoutWidthGap = a.getDimensionPixelSize(
204 R.styleable.PagedView_pageLayoutWidthGap, -1);
205 mPageLayoutHeightGap = a.getDimensionPixelSize(
206 R.styleable.PagedView_pageLayoutHeightGap, -1);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700207 mScrollIndicatorPaddingLeft =
208 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
209 mScrollIndicatorPaddingRight =
210 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700211 a.recycle();
212
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700214 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 }
216
217 /**
218 * Initializes various states for this workspace.
219 */
Michael Jurka0142d492010-08-25 17:46:15 -0700220 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700221 mDirtyPageContent = new ArrayList<Boolean>();
222 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800223 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700224 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700225 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700226
227 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
228 mTouchSlop = configuration.getScaledTouchSlop();
229 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
230 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
231 }
232
Winson Chung86f77532010-08-24 11:08:22 -0700233 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
234 mPageSwitchListener = pageSwitchListener;
235 if (mPageSwitchListener != null) {
236 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 }
238 }
239
240 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700241 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
242 * out pages.
243 */
244 protected void setDataIsReady() {
245 mIsDataReady = true;
246 }
247 protected boolean isDataReady() {
248 return mIsDataReady;
249 }
250
251 /**
Winson Chung86f77532010-08-24 11:08:22 -0700252 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700253 *
Winson Chung86f77532010-08-24 11:08:22 -0700254 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700255 */
Winson Chung86f77532010-08-24 11:08:22 -0700256 int getCurrentPage() {
257 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 }
259
Winson Chung86f77532010-08-24 11:08:22 -0700260 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700261 return getChildCount();
262 }
263
Winson Chung86f77532010-08-24 11:08:22 -0700264 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700265 return getChildAt(index);
266 }
267
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800269 * Updates the scroll of the current page immediately to its final scroll position. We use this
270 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
271 * the previous tab page.
272 */
273 protected void updateCurrentPageScroll() {
274 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
275 scrollTo(newX, 0);
276 mScroller.setFinalX(newX);
277 }
278
279 /**
Winson Chung86f77532010-08-24 11:08:22 -0700280 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 */
Winson Chung86f77532010-08-24 11:08:22 -0700282 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800283 if (!mScroller.isFinished()) {
284 mScroller.abortAnimation();
285 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800286 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
287 // the default
288 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800289 return;
290 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700291
Winson Chung86f77532010-08-24 11:08:22 -0700292 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800293 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700294 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700295 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800296 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700297 }
298
Michael Jurka0142d492010-08-25 17:46:15 -0700299 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700300 if (mPageSwitchListener != null) {
301 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 }
303 }
304
Michael Jurkace7e05f2011-02-01 22:02:35 -0800305 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700306 if (!mIsPageMoving) {
307 mIsPageMoving = true;
308 onPageBeginMoving();
309 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700310 }
311
Michael Jurkace7e05f2011-02-01 22:02:35 -0800312 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700313 if (mIsPageMoving) {
314 mIsPageMoving = false;
315 onPageEndMoving();
316 }
Michael Jurka0142d492010-08-25 17:46:15 -0700317 }
318
Adam Cohen26976d92011-03-22 15:33:33 -0700319 protected boolean isPageMoving() {
320 return mIsPageMoving;
321 }
322
Michael Jurka0142d492010-08-25 17:46:15 -0700323 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700324 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700325 showScrollingIndicator();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700326 }
327
328 // a method that subclasses can override to add behavior
329 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700330 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700331 }
332
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 /**
Winson Chung86f77532010-08-24 11:08:22 -0700334 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 *
336 * @param l The listener used to respond to long clicks.
337 */
338 @Override
339 public void setOnLongClickListener(OnLongClickListener l) {
340 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700341 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700343 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 }
345 }
346
347 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800348 public void scrollBy(int x, int y) {
349 scrollTo(mUnboundedScrollX + x, mScrollY + y);
350 }
351
352 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700353 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800354 mUnboundedScrollX = x;
355
356 if (x < 0) {
357 super.scrollTo(0, y);
358 if (mAllowOverScroll) {
359 overScroll(x);
360 }
361 } else if (x > mMaxScrollX) {
362 super.scrollTo(mMaxScrollX, y);
363 if (mAllowOverScroll) {
364 overScroll(x - mMaxScrollX);
365 }
366 } else {
367 super.scrollTo(x, y);
368 }
369
Michael Jurka0142d492010-08-25 17:46:15 -0700370 mTouchX = x;
371 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
372 }
373
374 // we moved this functionality to a helper function so SmoothPagedView can reuse it
375 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700377 // Don't bother scrolling if the page does not need to be moved
378 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
379 mDirtyPageAlpha = true;
380 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
381 }
Michael Jurka0142d492010-08-25 17:46:15 -0700382 invalidate();
383 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700384 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700385 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700386 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700387 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700388 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700389
390 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700391 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700392 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700393 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700394 }
395
Adam Cohen73aa9752010-11-24 16:26:58 -0800396 // We don't want to trigger a page end moving unless the page has settled
397 // and the user has stopped scrolling
398 if (mTouchState == TOUCH_STATE_REST) {
399 pageEndMoving();
400 }
Michael Jurka0142d492010-08-25 17:46:15 -0700401 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 }
Michael Jurka0142d492010-08-25 17:46:15 -0700403 return false;
404 }
405
406 @Override
407 public void computeScroll() {
408 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700409 }
410
411 @Override
412 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700413 if (!mIsDataReady) {
414 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
415 return;
416 }
417
Winson Chung321e9ee2010-08-09 13:37:56 -0700418 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
419 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
420 if (widthMode != MeasureSpec.EXACTLY) {
421 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
422 }
423
Adam Lesinski6b879f02010-11-04 16:15:23 -0700424 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
425 * of the All apps view on XLarge displays to not take up more space then it needs. Width
426 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
427 * each page to have the same width.
428 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700429 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700430 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
431 int maxChildHeight = 0;
432
433 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700434
Michael Jurka36fcb742011-04-06 17:08:58 -0700435
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700437 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700438 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 final int childCount = getChildCount();
440 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700441 // disallowing padding in paged view (just pass 0)
442 final View child = getChildAt(i);
443 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
444
445 int childWidthMode;
446 if (lp.width == LayoutParams.WRAP_CONTENT) {
447 childWidthMode = MeasureSpec.AT_MOST;
448 } else {
449 childWidthMode = MeasureSpec.EXACTLY;
450 }
451
452 int childHeightMode;
453 if (lp.height == LayoutParams.WRAP_CONTENT) {
454 childHeightMode = MeasureSpec.AT_MOST;
455 } else {
456 childHeightMode = MeasureSpec.EXACTLY;
457 }
458
459 final int childWidthMeasureSpec =
460 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
461 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700462 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463
464 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700465 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700466 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
467 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700468 }
469
470 if (heightMode == MeasureSpec.AT_MOST) {
471 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700472 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800473 if (childCount > 0) {
474 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
475 } else {
476 mMaxScrollX = 0;
477 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700478
479 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700480 }
481
Michael Jurka8c920dd2011-01-20 14:16:56 -0800482 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800483 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
484 int delta = newX - mScrollX;
485
Michael Jurka8c920dd2011-01-20 14:16:56 -0800486 final int pageCount = getChildCount();
487 for (int i = 0; i < pageCount; i++) {
488 View page = (View) getChildAt(i);
489 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800490 }
491 setCurrentPage(newCurrentPage);
492 }
493
Michael Jurka8c920dd2011-01-20 14:16:56 -0800494 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
495 // 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 -0800496 // tightens the layout accordingly
497 public void setLayoutScale(float childrenScale) {
498 mLayoutScale = childrenScale;
499
500 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
501 int childCount = getChildCount();
502 float childrenX[] = new float[childCount];
503 float childrenY[] = new float[childCount];
504 for (int i = 0; i < childCount; i++) {
505 final View child = getChildAt(i);
506 childrenX[i] = child.getX();
507 childrenY[i] = child.getY();
508 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700509 // Trigger a full re-layout (never just call onLayout directly!)
510 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
511 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
512 requestLayout();
513 measure(widthSpec, heightSpec);
514 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800515 for (int i = 0; i < childCount; i++) {
516 final View child = getChildAt(i);
517 child.setX(childrenX[i]);
518 child.setY(childrenY[i]);
519 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700520
Michael Jurkad3ef3062010-11-23 16:23:58 -0800521 // Also, the page offset has changed (since the pages are now smaller);
522 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800523 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 }
525
Winson Chung321e9ee2010-08-09 13:37:56 -0700526 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700527 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700528 if (!mIsDataReady) {
529 return;
530 }
531
Winson Chung785d2eb2011-04-14 16:08:02 -0700532 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Michael Jurkacfc62942010-09-14 14:01:07 -0700533 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
534 setHorizontalScrollBarEnabled(false);
535 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
536 scrollTo(newX, 0);
537 mScroller.setFinalX(newX);
538 setHorizontalScrollBarEnabled(true);
539 mFirstLayout = false;
540 }
541
Adam Lesinski6b879f02010-11-04 16:15:23 -0700542 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 final int childCount = getChildCount();
544 int childLeft = 0;
545 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700546 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
547 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700548 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 }
550
551 for (int i = 0; i < childCount; i++) {
552 final View child = getChildAt(i);
553 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800554 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700555 final int childHeight = child.getMeasuredHeight();
556 int childTop = mPaddingTop;
557 if (mCenterPagesVertically) {
558 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
559 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800560
Winson Chung785d2eb2011-04-14 16:08:02 -0700561 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700562 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800563 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700564 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 }
566 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800567 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
568 mFirstLayout = false;
569 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571
Winson Chung03929772011-02-23 17:07:10 -0800572 protected void forceUpdateAdjacentPagesAlpha() {
573 mDirtyPageAlpha = true;
574 updateAdjacentPagesAlpha();
575 }
576
Winson Chunge3193b92010-09-10 11:44:42 -0700577 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700578 if (mFadeInAdjacentScreens) {
579 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chung63257c12011-05-05 17:06:13 -0700580 int screenWidth = getMeasuredWidth();
581 int halfScreenSize = screenWidth / 2;
Winson Chunge3193b92010-09-10 11:44:42 -0700582 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700583 final int childCount = getChildCount();
584 for (int i = 0; i < childCount; ++i) {
585 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800586 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700587 int halfChildWidth = (childWidth / 2);
588 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700589
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700590 // On the first layout, we may not have a width nor a proper offset, so for now
591 // we should just assume full page width (and calculate the offset according to
592 // that).
593 if (childWidth <= 0) {
Winson Chung63257c12011-05-05 17:06:13 -0700594 childWidth = screenWidth;
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700595 childCenter = (i * childWidth) + (childWidth / 2);
596 }
597
Winson Chunge8878e32010-09-15 20:37:09 -0700598 int d = halfChildWidth;
599 int distanceFromScreenCenter = childCenter - screenCenter;
600 if (distanceFromScreenCenter > 0) {
601 if (i > 0) {
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 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700607 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800608 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700609 } else {
610 continue;
Winson Chunge8878e32010-09-15 20:37:09 -0700611 }
Michael Jurka0142d492010-08-25 17:46:15 -0700612 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700613 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700614
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700615 // Preventing potential divide-by-zero
616 d = Math.max(1, d);
617
Winson Chunge8878e32010-09-15 20:37:09 -0700618 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
619 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
620 float alpha = 1.0f - dimAlpha;
621
Adam Cohenf34bab52010-09-30 14:11:56 -0700622 if (alpha < ALPHA_QUANTIZE_LEVEL) {
623 alpha = 0.0f;
624 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
625 alpha = 1.0f;
626 }
627
Michael Jurkaa40829a2011-02-16 18:02:45 -0800628 // Due to the way we're setting alpha on our children in PagedViewCellLayout,
629 // this optimization causes alpha to not be properly updated sometimes (repro
630 // case: in xlarge mode, swipe to second page in All Apps, then click on "My
631 // Apps" tab. the page will have alpha 0 until you swipe it). Removing
632 // optimization fixes the issue, but we should fix this in a better manner
Michael Jurkacfdb0962011-02-04 01:38:00 -0800633 //if (Float.compare(alpha, layout.getAlpha()) != 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700634 layout.setAlpha(alpha);
Michael Jurkacfdb0962011-02-04 01:38:00 -0800635 //}
Winson Chung321e9ee2010-08-09 13:37:56 -0700636 }
Michael Jurka0142d492010-08-25 17:46:15 -0700637 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
639 }
Winson Chunge3193b92010-09-10 11:44:42 -0700640 }
641
Adam Cohenf34bab52010-09-30 14:11:56 -0700642 protected void screenScrolled(int screenCenter) {
Winson Chung007c6982011-06-14 13:27:53 -0700643 updateScrollingIndicator();
Adam Cohenf34bab52010-09-30 14:11:56 -0700644 }
645
Winson Chunge3193b92010-09-10 11:44:42 -0700646 @Override
647 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700648 int halfScreenSize = getMeasuredWidth() / 2;
649 int screenCenter = mScrollX + halfScreenSize;
650
651 if (screenCenter != mLastScreenCenter) {
652 screenScrolled(screenCenter);
653 updateAdjacentPagesAlpha();
654 mLastScreenCenter = screenCenter;
655 }
Michael Jurka0142d492010-08-25 17:46:15 -0700656
657 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700658 // As an optimization, this code assumes that all pages have the same width as the 0th
659 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700660 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700661 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800662 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700663 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700664 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700665 int leftScreen = 0;
666 int rightScreen = 0;
667 while (x <= mScrollX) {
668 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800669 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700670 }
671 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800672 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700673 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800674 if (rightScreen < pageCount) {
675 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
676 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700677 }
678 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700679
Michael Jurkac4fb9172010-09-02 17:19:20 -0700680 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800681 // Clip to the bounds
682 canvas.save();
683 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
684 mScrollY + mBottom - mTop);
685
Michael Jurkac4fb9172010-09-02 17:19:20 -0700686 for (int i = leftScreen; i <= rightScreen; i++) {
687 drawChild(canvas, getChildAt(i), drawingTime);
688 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800689 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700690 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 }
692
693 @Override
694 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700695 int page = indexOfChild(child);
696 if (page != mCurrentPage || !mScroller.isFinished()) {
697 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700698 return true;
699 }
700 return false;
701 }
702
703 @Override
704 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700705 int focusablePage;
706 if (mNextPage != INVALID_PAGE) {
707 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700708 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700709 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 }
Winson Chung86f77532010-08-24 11:08:22 -0700711 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700712 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700713 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 }
715 return false;
716 }
717
718 @Override
719 public boolean dispatchUnhandledMove(View focused, int direction) {
720 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700721 if (getCurrentPage() > 0) {
722 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 return true;
724 }
725 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700726 if (getCurrentPage() < getPageCount() - 1) {
727 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700728 return true;
729 }
730 }
731 return super.dispatchUnhandledMove(focused, direction);
732 }
733
734 @Override
735 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700736 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
737 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 }
739 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700740 if (mCurrentPage > 0) {
741 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 }
743 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700744 if (mCurrentPage < getPageCount() - 1) {
745 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 }
747 }
748 }
749
750 /**
751 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700752 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 *
Winson Chung86f77532010-08-24 11:08:22 -0700754 * This happens when live folders requery, and if they're off page, they
755 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700756 */
757 @Override
758 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700759 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 View v = focused;
761 while (true) {
762 if (v == current) {
763 super.focusableViewAvailable(focused);
764 return;
765 }
766 if (v == this) {
767 return;
768 }
769 ViewParent parent = v.getParent();
770 if (parent instanceof View) {
771 v = (View)v.getParent();
772 } else {
773 return;
774 }
775 }
776 }
777
778 /**
779 * {@inheritDoc}
780 */
781 @Override
782 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
783 if (disallowIntercept) {
784 // We need to make sure to cancel our long press if
785 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700786 final View currentPage = getChildAt(mCurrentPage);
787 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 }
789 super.requestDisallowInterceptTouchEvent(disallowIntercept);
790 }
791
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800792 /**
793 * Return true if a tap at (x, y) should trigger a flip to the previous page.
794 */
795 protected boolean hitsPreviousPage(float x, float y) {
796 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
797 }
798
799 /**
800 * Return true if a tap at (x, y) should trigger a flip to the next page.
801 */
802 protected boolean hitsNextPage(float x, float y) {
803 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
804 }
805
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 @Override
807 public boolean onInterceptTouchEvent(MotionEvent ev) {
808 /*
809 * This method JUST determines whether we want to intercept the motion.
810 * If we return true, onTouchEvent will be called and we do the actual
811 * scrolling there.
812 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800813 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700814
Winson Chung45e1d6e2010-11-09 17:19:49 -0800815 // Skip touch handling if there are no pages to swipe
816 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
817
Winson Chung321e9ee2010-08-09 13:37:56 -0700818 /*
819 * Shortcut the most recurring case: the user is in the dragging
820 * state and he is moving his finger. We want to intercept this
821 * motion.
822 */
823 final int action = ev.getAction();
824 if ((action == MotionEvent.ACTION_MOVE) &&
825 (mTouchState == TOUCH_STATE_SCROLLING)) {
826 return true;
827 }
828
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 switch (action & MotionEvent.ACTION_MASK) {
830 case MotionEvent.ACTION_MOVE: {
831 /*
832 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
833 * whether the user has moved far enough from his original down touch.
834 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700835 if (mActivePointerId != INVALID_POINTER) {
836 determineScrollingStart(ev);
837 break;
838 }
839 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
840 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
841 // i.e. fall through to the next case (don't break)
842 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
843 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 }
845
846 case MotionEvent.ACTION_DOWN: {
847 final float x = ev.getX();
848 final float y = ev.getY();
849 // Remember location of down touch
850 mDownMotionX = x;
851 mLastMotionX = x;
852 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800853 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800854 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 mActivePointerId = ev.getPointerId(0);
856 mAllowLongPress = true;
857
858 /*
859 * If being flinged and user touches the screen, initiate drag;
860 * otherwise don't. mScroller.isFinished should be false when
861 * being flinged.
862 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700863 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700864 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
865 if (finishedScrolling) {
866 mTouchState = TOUCH_STATE_REST;
867 mScroller.abortAnimation();
868 } else {
869 mTouchState = TOUCH_STATE_SCROLLING;
870 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700871
Winson Chung86f77532010-08-24 11:08:22 -0700872 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800874 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700875 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800876 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800878 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 mTouchState = TOUCH_STATE_NEXT_PAGE;
880 }
881 }
882 }
883 break;
884 }
885
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800887 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700888 mTouchState = TOUCH_STATE_REST;
889 mAllowLongPress = false;
890 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800891 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 break;
893
894 case MotionEvent.ACTION_POINTER_UP:
895 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800896 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 break;
898 }
899
900 /*
901 * The only time we want to intercept motion events is if we are in the
902 * drag mode.
903 */
904 return mTouchState != TOUCH_STATE_REST;
905 }
906
Winson Chung80baf5a2010-08-09 16:03:15 -0700907 protected void animateClickFeedback(View v, final Runnable r) {
908 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800909 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
910 loadAnimator(mContext, R.anim.paged_view_click_feedback);
911 anim.setTarget(v);
912 anim.addListener(new AnimatorListenerAdapter() {
913 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700914 r.run();
915 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700916 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800917 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700918 }
919
Adam Cohenf8d28232011-02-01 21:47:00 -0800920 protected void determineScrollingStart(MotionEvent ev) {
921 determineScrollingStart(ev, 1.0f);
922 }
923
Winson Chung321e9ee2010-08-09 13:37:56 -0700924 /*
925 * Determines if we should change the touch state to start scrolling after the
926 * user moves their touch point too far.
927 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800928 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700929 /*
930 * Locally do absolute value. mLastMotionX is set to the y value
931 * of the down event.
932 */
933 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700934 if (pointerIndex == -1) {
935 return;
936 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 final float x = ev.getX(pointerIndex);
938 final float y = ev.getY(pointerIndex);
939 final int xDiff = (int) Math.abs(x - mLastMotionX);
940 final int yDiff = (int) Math.abs(y - mLastMotionY);
941
Adam Cohenf8d28232011-02-01 21:47:00 -0800942 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 boolean xPaged = xDiff > mPagingTouchSlop;
944 boolean xMoved = xDiff > touchSlop;
945 boolean yMoved = yDiff > touchSlop;
946
Adam Cohenf8d28232011-02-01 21:47:00 -0800947 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700948 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 // Scroll if the user moved far enough along the X axis
950 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -0800951 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -0800953 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -0700954 mTouchX = mScrollX;
955 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
956 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 }
958 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -0800959 cancelCurrentPageLongPress();
960 }
961 }
962
963 protected void cancelCurrentPageLongPress() {
964 if (mAllowLongPress) {
965 mAllowLongPress = false;
966 // Try canceling the long press. It could also have been scheduled
967 // by a distant descendant, so use the mAllowLongPress flag to block
968 // everything
969 final View currentPage = getPageAt(mCurrentPage);
970 if (currentPage != null) {
971 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700972 }
973 }
974 }
975
Adam Cohene0f66b52010-11-23 15:06:07 -0800976 // This curve determines how the effect of scrolling over the limits of the page dimishes
977 // as the user pulls further and further from the bounds
978 private float overScrollInfluenceCurve(float f) {
979 f -= 1.0f;
980 return f * f * f + 1.0f;
981 }
982
Adam Cohen68d73932010-11-15 10:50:58 -0800983 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800984 int screenSize = getMeasuredWidth();
985
986 float f = (amount / screenSize);
987
988 if (f == 0) return;
989 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
990
Adam Cohen7bfc9792011-01-28 13:52:37 -0800991 // Clamp this factor, f, to -1 < f < 1
992 if (Math.abs(f) >= 1) {
993 f /= Math.abs(f);
994 }
995
Adam Cohene0f66b52010-11-23 15:06:07 -0800996 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800997 if (amount < 0) {
998 mScrollX = overScrollAmount;
999 } else {
1000 mScrollX = mMaxScrollX + overScrollAmount;
1001 }
1002 invalidate();
1003 }
1004
Michael Jurkac5b262c2011-01-12 20:24:50 -08001005 protected float maxOverScroll() {
1006 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
1007 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
1008 float f = 1.0f;
1009 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1010 return OVERSCROLL_DAMP_FACTOR * f;
1011 }
1012
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 @Override
1014 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001015 // Skip touch handling if there are no pages to swipe
1016 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1017
Michael Jurkab8f06722010-10-10 15:58:46 -07001018 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001019
1020 final int action = ev.getAction();
1021
1022 switch (action & MotionEvent.ACTION_MASK) {
1023 case MotionEvent.ACTION_DOWN:
1024 /*
1025 * If being flinged and user touches, stop the fling. isFinished
1026 * will be false if being flinged.
1027 */
1028 if (!mScroller.isFinished()) {
1029 mScroller.abortAnimation();
1030 }
1031
1032 // Remember where the motion event started
1033 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001034 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001035 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001037 if (mTouchState == TOUCH_STATE_SCROLLING) {
1038 pageBeginMoving();
1039 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 break;
1041
1042 case MotionEvent.ACTION_MOVE:
1043 if (mTouchState == TOUCH_STATE_SCROLLING) {
1044 // Scroll to follow the motion event
1045 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1046 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001047 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001048
Adam Cohenaefd4e12011-02-14 16:39:38 -08001049 mTotalMotionX += Math.abs(deltaX);
1050
Winson Chungc0844aa2011-02-02 15:25:58 -08001051 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1052 // keep the remainder because we are actually testing if we've moved from the last
1053 // scrolled position (which is discrete).
1054 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001055 mTouchX += deltaX;
1056 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1057 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001058 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001059 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001060 } else {
1061 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001063 mLastMotionX = x;
1064 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 } else {
1066 awakenScrollBars();
1067 }
Adam Cohen564976a2010-10-13 18:52:07 -07001068 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 determineScrollingStart(ev);
1070 }
1071 break;
1072
1073 case MotionEvent.ACTION_UP:
1074 if (mTouchState == TOUCH_STATE_SCROLLING) {
1075 final int activePointerId = mActivePointerId;
1076 final int pointerIndex = ev.findPointerIndex(activePointerId);
1077 final float x = ev.getX(pointerIndex);
1078 final VelocityTracker velocityTracker = mVelocityTracker;
1079 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1080 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001081 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001082 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001083 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001084
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001085 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1086
Adam Cohenaefd4e12011-02-14 16:39:38 -08001087 // In the case that the page is moved far to one direction and then is flung
1088 // in the opposite direction, we use a threshold to determine whether we should
1089 // just return to the starting page, or if we should skip one further.
1090 boolean returnToOriginalPage = false;
1091 final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001092 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001093 Math.signum(velocityX) != Math.signum(deltaX)) {
1094 returnToOriginalPage = true;
1095 }
1096
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001097 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001098 Math.abs(velocityX) > snapVelocity;
1099
1100 int finalPage;
1101 // We give flings precedence over large moves, which is why we short-circuit our
1102 // test for a large move if a fling has been registered. That is, a large
1103 // move to the left and fling to the right will register as a fling to the right.
1104 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1105 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1106 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1107 snapToPageWithVelocity(finalPage, velocityX);
1108 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1109 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001110 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001111 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1112 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001113 } else {
1114 snapToDestination();
1115 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001116 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001117 // at this point we have not moved beyond the touch slop
1118 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1119 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001120 int nextPage = Math.max(0, mCurrentPage - 1);
1121 if (nextPage != mCurrentPage) {
1122 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001123 } else {
1124 snapToDestination();
1125 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001126 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001127 // at this point we have not moved beyond the touch slop
1128 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1129 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001130 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1131 if (nextPage != mCurrentPage) {
1132 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001133 } else {
1134 snapToDestination();
1135 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001136 } else {
1137 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001138 }
1139 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_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001145 if (mTouchState == TOUCH_STATE_SCROLLING) {
1146 snapToDestination();
1147 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 mTouchState = TOUCH_STATE_REST;
1149 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001150 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001151 break;
1152
1153 case MotionEvent.ACTION_POINTER_UP:
1154 onSecondaryPointerUp(ev);
1155 break;
1156 }
1157
1158 return true;
1159 }
1160
Winson Chung185d7162011-02-28 13:47:29 -08001161 @Override
1162 public boolean onGenericMotionEvent(MotionEvent event) {
1163 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1164 switch (event.getAction()) {
1165 case MotionEvent.ACTION_SCROLL: {
1166 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1167 final float vscroll;
1168 final float hscroll;
1169 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1170 vscroll = 0;
1171 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1172 } else {
1173 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1174 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1175 }
1176 if (hscroll != 0 || vscroll != 0) {
1177 if (hscroll > 0 || vscroll > 0) {
1178 scrollRight();
1179 } else {
1180 scrollLeft();
1181 }
1182 return true;
1183 }
1184 }
1185 }
1186 }
1187 return super.onGenericMotionEvent(event);
1188 }
1189
Michael Jurkab8f06722010-10-10 15:58:46 -07001190 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1191 if (mVelocityTracker == null) {
1192 mVelocityTracker = VelocityTracker.obtain();
1193 }
1194 mVelocityTracker.addMovement(ev);
1195 }
1196
1197 private void releaseVelocityTracker() {
1198 if (mVelocityTracker != null) {
1199 mVelocityTracker.recycle();
1200 mVelocityTracker = null;
1201 }
1202 }
1203
Winson Chung321e9ee2010-08-09 13:37:56 -07001204 private void onSecondaryPointerUp(MotionEvent ev) {
1205 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1206 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1207 final int pointerId = ev.getPointerId(pointerIndex);
1208 if (pointerId == mActivePointerId) {
1209 // This was our active pointer going up. Choose a new
1210 // active pointer and adjust accordingly.
1211 // TODO: Make this decision more intelligent.
1212 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1213 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1214 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001215 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 mActivePointerId = ev.getPointerId(newPointerIndex);
1217 if (mVelocityTracker != null) {
1218 mVelocityTracker.clear();
1219 }
1220 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001221 if (mTouchState == TOUCH_STATE_REST) {
1222 onWallpaperTap(ev);
1223 }
1224 }
1225
Winson Chungf0ea4d32011-06-06 14:27:16 -07001226 protected void onWallpaperTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001227
1228 @Override
1229 public void requestChildFocus(View child, View focused) {
1230 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001231 int page = indexOfChild(child);
Winson Chung97d85d22011-04-13 11:27:36 -07001232 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001233 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235 }
1236
Winson Chunge3193b92010-09-10 11:44:42 -07001237 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1238 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001239 int left;
1240 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001241 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001242 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001243 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001244 if (left <= relativeOffset && relativeOffset <= right) {
1245 return i;
1246 }
Winson Chunge3193b92010-09-10 11:44:42 -07001247 }
1248 return -1;
1249 }
1250
Winson Chung1908d072011-02-24 18:09:44 -08001251 protected void setMinimumWidthOverride(int minimumWidth) {
1252 mMinimumWidth = minimumWidth;
1253 }
Winson Chung34b23d52011-03-18 11:29:34 -07001254 protected void resetMinimumWidthOverride() {
1255 mMinimumWidth = 0;
1256 }
Winson Chung1908d072011-02-24 18:09:44 -08001257
1258 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001259 // This functions are called enough times that it actually makes a difference in the
1260 // profiler -- so just inline the max() here
1261 final int measuredWidth = getChildAt(index).getMeasuredWidth();
1262 final int minWidth = mMinimumWidth;
1263 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001264 }
1265
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 protected int getRelativeChildOffset(int index) {
Winson Chung1908d072011-02-24 18:09:44 -08001267 return (getMeasuredWidth() - getChildWidth(index)) / 2;
Winson Chung321e9ee2010-08-09 13:37:56 -07001268 }
1269
Winson Chung557d6ed2011-07-08 15:34:52 -07001270 protected int getScaledRelativeChildOffset(int index) {
1271 return (getMeasuredWidth() - getScaledMeasuredWidth(getChildAt(index))) / 2;
1272 }
1273
Winson Chung321e9ee2010-08-09 13:37:56 -07001274 protected int getChildOffset(int index) {
1275 if (getChildCount() == 0)
1276 return 0;
1277
1278 int offset = getRelativeChildOffset(0);
1279 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001280 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001281 }
1282 return offset;
1283 }
1284
Michael Jurkad3ef3062010-11-23 16:23:58 -08001285 protected int getScaledMeasuredWidth(View child) {
Winson Chung63257c12011-05-05 17:06:13 -07001286 // This functions are called enough times that it actually makes a difference in the
1287 // profiler -- so just inline the max() here
1288 final int measuredWidth = child.getMeasuredWidth();
1289 final int minWidth = mMinimumWidth;
1290 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
1291 return (int) (maxWidth * mLayoutScale + 0.5f);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001292 }
1293
Adam Cohend19d3ca2010-09-15 14:43:42 -07001294 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001295 int minDistanceFromScreenCenter = getMeasuredWidth();
1296 int minDistanceFromScreenCenterIndex = -1;
1297 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1298 final int childCount = getChildCount();
1299 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001300 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001301 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001302 int halfChildWidth = (childWidth / 2);
1303 int childCenter = getChildOffset(i) + halfChildWidth;
1304 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1305 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1306 minDistanceFromScreenCenter = distanceFromScreenCenter;
1307 minDistanceFromScreenCenterIndex = i;
1308 }
1309 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001310 return minDistanceFromScreenCenterIndex;
1311 }
1312
1313 protected void snapToDestination() {
1314 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001315 }
1316
Adam Cohene0f66b52010-11-23 15:06:07 -08001317 private static class ScrollInterpolator implements Interpolator {
1318 public ScrollInterpolator() {
1319 }
1320
1321 public float getInterpolation(float t) {
1322 t -= 1.0f;
1323 return t*t*t*t*t + 1;
1324 }
1325 }
1326
1327 // We want the duration of the page snap animation to be influenced by the distance that
1328 // the screen has to travel, however, we don't want this duration to be effected in a
1329 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1330 // of travel has on the overall snap duration.
1331 float distanceInfluenceForSnapDuration(float f) {
1332 f -= 0.5f; // center the values about 0.
1333 f *= 0.3f * Math.PI / 2.0f;
1334 return (float) Math.sin(f);
1335 }
1336
Michael Jurka0142d492010-08-25 17:46:15 -07001337 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001338 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1339 int halfScreenSize = getMeasuredWidth() / 2;
1340
Winson Chung785d2eb2011-04-14 16:08:02 -07001341 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1342 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1343 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001344 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1345 int delta = newX - mUnboundedScrollX;
1346 int duration = 0;
1347
1348 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1349 // If the velocity is low enough, then treat this more as an automatic page advance
1350 // as opposed to an apparent physical response to flinging
1351 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1352 return;
1353 }
1354
1355 // Here we compute a "distance" that will be used in the computation of the overall
1356 // snap duration. This is a function of the actual distance that needs to be traveled;
1357 // we keep this value close to half screen size in order to reduce the variance in snap
1358 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001359 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001360 float distance = halfScreenSize + halfScreenSize *
1361 distanceInfluenceForSnapDuration(distanceRatio);
1362
1363 velocity = Math.abs(velocity);
1364 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1365
1366 // we want the page's snap velocity to approximately match the velocity at which the
1367 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001368 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1369 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001370
1371 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001372 }
1373
1374 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001375 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001376 }
1377
Michael Jurka0142d492010-08-25 17:46:15 -07001378 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001379 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001380
Winson Chung785d2eb2011-04-14 16:08:02 -07001381 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1382 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1383 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001384 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001385 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001386 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001387 snapToPage(whichPage, delta, duration);
1388 }
1389
1390 protected void snapToPage(int whichPage, int delta, int duration) {
1391 mNextPage = whichPage;
1392
1393 View focusedChild = getFocusedChild();
1394 if (focusedChild != null && whichPage != mCurrentPage &&
1395 focusedChild == getChildAt(mCurrentPage)) {
1396 focusedChild.clearFocus();
1397 }
1398
1399 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001400 awakenScrollBars(duration);
1401 if (duration == 0) {
1402 duration = Math.abs(delta);
1403 }
1404
1405 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001406 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001407
Winson Chungb44b5242011-06-13 11:32:14 -07001408 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1409 // loading associated pages until the scroll settles
1410 if (mDeferScrollUpdate) {
1411 loadAssociatedPages(mNextPage);
1412 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001413 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001414 }
Michael Jurka0142d492010-08-25 17:46:15 -07001415 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001416 invalidate();
1417 }
1418
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 public void scrollLeft() {
1420 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001421 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001422 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001423 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001424 }
1425 }
1426
1427 public void scrollRight() {
1428 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001429 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001430 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001431 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 }
1433 }
1434
Winson Chung86f77532010-08-24 11:08:22 -07001435 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001436 int result = -1;
1437 if (v != null) {
1438 ViewParent vp = v.getParent();
1439 int count = getChildCount();
1440 for (int i = 0; i < count; i++) {
1441 if (vp == getChildAt(i)) {
1442 return i;
1443 }
1444 }
1445 }
1446 return result;
1447 }
1448
1449 /**
1450 * @return True is long presses are still allowed for the current touch
1451 */
1452 public boolean allowLongPress() {
1453 return mAllowLongPress;
1454 }
1455
Michael Jurka0142d492010-08-25 17:46:15 -07001456 /**
1457 * Set true to allow long-press events to be triggered, usually checked by
1458 * {@link Launcher} to accept or block dpad-initiated long-presses.
1459 */
1460 public void setAllowLongPress(boolean allowLongPress) {
1461 mAllowLongPress = allowLongPress;
1462 }
1463
Winson Chung321e9ee2010-08-09 13:37:56 -07001464 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001465 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001466
1467 SavedState(Parcelable superState) {
1468 super(superState);
1469 }
1470
1471 private SavedState(Parcel in) {
1472 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001473 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001474 }
1475
1476 @Override
1477 public void writeToParcel(Parcel out, int flags) {
1478 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001479 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001480 }
1481
1482 public static final Parcelable.Creator<SavedState> CREATOR =
1483 new Parcelable.Creator<SavedState>() {
1484 public SavedState createFromParcel(Parcel in) {
1485 return new SavedState(in);
1486 }
1487
1488 public SavedState[] newArray(int size) {
1489 return new SavedState[size];
1490 }
1491 };
1492 }
1493
Winson Chung86f77532010-08-24 11:08:22 -07001494 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001495 if (mContentIsRefreshable) {
1496 final int count = getChildCount();
1497 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001498 int lowerPageBound = getAssociatedLowerPageBound(page);
1499 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001500 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1501 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001502 for (int i = 0; i < count; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001503 Page layout = (Page) getChildAt(i);
1504 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001505 if (lowerPageBound <= i && i <= upperPageBound) {
1506 if (mDirtyPageContent.get(i)) {
1507 syncPageItems(i);
1508 mDirtyPageContent.set(i, false);
1509 }
1510 } else {
1511 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001512 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001513 }
1514 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001515 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001516 }
1517 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001518 }
1519 }
1520
Winson Chunge3193b92010-09-10 11:44:42 -07001521 protected int getAssociatedLowerPageBound(int page) {
1522 return Math.max(0, page - 1);
1523 }
1524 protected int getAssociatedUpperPageBound(int page) {
1525 final int count = getChildCount();
1526 return Math.min(page + 1, count - 1);
1527 }
1528
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001529 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001530 if (isChoiceMode(CHOICE_MODE_NONE)) {
1531 mChoiceMode = mode;
1532 mActionMode = startActionMode(callback);
1533 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001534 }
1535
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001536 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001537 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001538 mChoiceMode = CHOICE_MODE_NONE;
1539 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001540 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001541 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001542 }
1543 }
1544
1545 protected boolean isChoiceMode(int mode) {
1546 return mChoiceMode == mode;
1547 }
1548
1549 protected ArrayList<Checkable> getCheckedGrandchildren() {
1550 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1551 final int childCount = getChildCount();
1552 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001553 Page layout = (Page) getChildAt(i);
1554 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001555 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001556 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001557 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001558 checked.add((Checkable) v);
1559 }
1560 }
1561 }
1562 return checked;
1563 }
1564
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001565 /**
1566 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1567 * Otherwise, returns null.
1568 */
1569 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001570 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001571 final int childCount = getChildCount();
1572 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -08001573 Page layout = (Page) getChildAt(i);
1574 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001575 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001576 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001577 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1578 return (Checkable) v;
1579 }
1580 }
1581 }
1582 }
1583 return null;
1584 }
1585
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001586 protected void resetCheckedGrandchildren() {
1587 // loop through children, and set all of their children to _not_ be checked
1588 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1589 for (int i = 0; i < checked.size(); ++i) {
1590 final Checkable c = checked.get(i);
1591 c.setChecked(false);
1592 }
1593 }
1594
Winson Chung86f77532010-08-24 11:08:22 -07001595 /**
1596 * This method is called ONLY to synchronize the number of pages that the paged view has.
1597 * To actually fill the pages with information, implement syncPageItems() below. It is
1598 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1599 * and therefore, individual page items do not need to be updated in this method.
1600 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001601 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001602
1603 /**
1604 * This method is called to synchronize the items that are on a particular page. If views on
1605 * the page can be reused, then they should be updated within this method.
1606 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001607 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001608
Michael Jurka983e3fd2011-05-26 17:10:29 -07001609 protected void postInvalidatePageData(final boolean clearViews) {
1610 post(new Runnable() {
1611 // post the call to avoid a call to requestLayout from a layout pass
1612 public void run() {
1613 if (clearViews) {
1614 removeAllViews();
1615 }
1616 invalidatePageData();
1617 }
1618 });
1619 }
1620
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001621 protected void invalidatePageData() {
Winson Chung5a808352011-06-27 19:08:49 -07001622 invalidatePageData(-1);
1623 }
1624 protected void invalidatePageData(int currentPage) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001625 if (!mIsDataReady) {
1626 return;
1627 }
1628
Michael Jurka0142d492010-08-25 17:46:15 -07001629 if (mContentIsRefreshable) {
1630 // Update all the pages
1631 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001632
Winson Chung5a808352011-06-27 19:08:49 -07001633 // We must force a measure after we've loaded the pages to update the content width and
1634 // to determine the full scroll width
1635 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1636 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1637
1638 // Set a new page as the current page if necessary
1639 if (currentPage > -1) {
1640 setCurrentPage(currentPage);
1641 }
1642
Michael Jurka0142d492010-08-25 17:46:15 -07001643 // Mark each of the pages as dirty
1644 final int count = getChildCount();
1645 mDirtyPageContent.clear();
1646 for (int i = 0; i < count; ++i) {
1647 mDirtyPageContent.add(true);
1648 }
1649
1650 // Load any pages that are necessary for the current window of views
1651 loadAssociatedPages(mCurrentPage);
1652 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001653 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001654 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001655 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001656 }
Winson Chung007c6982011-06-14 13:27:53 -07001657
1658 private ImageView getScrollingIndicator() {
1659 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1660 // found
1661 if (mHasScrollIndicator && mScrollIndicator == null) {
1662 ViewGroup parent = (ViewGroup) getParent();
1663 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1664 mHasScrollIndicator = mScrollIndicator != null;
1665 if (mHasScrollIndicator) {
1666 mScrollIndicator.setVisibility(View.VISIBLE);
1667 }
1668 }
1669 return mScrollIndicator;
1670 }
1671
1672 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001673 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001674 }
1675
Winson Chung5a808352011-06-27 19:08:49 -07001676 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1677 @Override
1678 public void run() {
1679 hideScrollingIndicator(false);
1680 }
1681 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001682 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001683 removeCallbacks(hideScrollingIndicatorRunnable);
Winson Chung3ac74c52011-06-30 17:39:37 -07001684 showScrollingIndicator();
Winson Chung5a808352011-06-27 19:08:49 -07001685 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001686 }
1687
Winson Chung007c6982011-06-14 13:27:53 -07001688 protected void showScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001689 if (getChildCount() <= 1) return;
1690 if (!isScrollingIndicatorEnabled()) return;
1691
1692 getScrollingIndicator();
1693 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001694 // Fade the indicator in
1695 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001696 mScrollIndicator.setVisibility(View.VISIBLE);
1697 if (mScrollIndicatorAnimator != null) {
1698 mScrollIndicatorAnimator.cancel();
1699 }
1700 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1701 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1702 mScrollIndicatorAnimator.start();
Winson Chung007c6982011-06-14 13:27:53 -07001703 }
1704 }
1705
1706 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001707 if (getChildCount() <= 1) return;
1708 if (!isScrollingIndicatorEnabled()) return;
1709
1710 getScrollingIndicator();
1711 if (mScrollIndicator != null) {
1712 // Fade the indicator out
1713 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001714 if (mScrollIndicatorAnimator != null) {
1715 mScrollIndicatorAnimator.cancel();
1716 }
1717 if (immediately) {
1718 mScrollIndicator.setVisibility(View.GONE);
1719 mScrollIndicator.setAlpha(0f);
1720 } else {
1721 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1722 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1723 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1724 private boolean cancelled = false;
1725 @Override
1726 public void onAnimationCancel(android.animation.Animator animation) {
1727 cancelled = true;
1728 }
1729 @Override
1730 public void onAnimationEnd(Animator animation) {
1731 if (!cancelled) {
1732 mScrollIndicator.setVisibility(View.GONE);
1733 }
1734 }
1735 });
1736 mScrollIndicatorAnimator.start();
1737 }
Winson Chung007c6982011-06-14 13:27:53 -07001738 }
1739 }
1740
Winson Chung32174c82011-07-19 15:47:55 -07001741 /**
1742 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1743 * fill its space on the track or not.
1744 */
1745 protected boolean hasElasticScrollIndicator() {
1746 return false;
1747 }
1748
Winson Chung007c6982011-06-14 13:27:53 -07001749 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001750 if (getChildCount() <= 1) return;
1751 if (!isScrollingIndicatorEnabled()) return;
1752
1753 getScrollingIndicator();
1754 if (mScrollIndicator != null) {
1755 updateScrollingIndicatorPosition();
1756 }
1757 }
1758
Winson Chung007c6982011-06-14 13:27:53 -07001759 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001760 if (!isScrollingIndicatorEnabled()) return;
1761
Winson Chung32174c82011-07-19 15:47:55 -07001762 int numPages = getChildCount();
1763 int pageWidth = getMeasuredWidth();
1764 int maxPageWidth = (numPages * getMeasuredWidth()) + ((numPages - 1) * mPageSpacing);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001765 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001766 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1767 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001768
Winson Chung007c6982011-06-14 13:27:53 -07001769 float offset = (float) getScrollX() / maxPageWidth;
Winson Chung32174c82011-07-19 15:47:55 -07001770 int indicatorSpace = trackWidth / numPages;
Winson Chungf5f8cef2011-07-22 11:16:13 -07001771 int indicatorPos = (int) (offset * trackWidth) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001772 if (hasElasticScrollIndicator()) {
1773 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1774 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1775 mScrollIndicator.requestLayout();
1776 }
1777 } else {
1778 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1779 indicatorPos += indicatorCenterOffset;
1780 }
Winson Chung007c6982011-06-14 13:27:53 -07001781 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001782 mScrollIndicator.invalidate();
1783 }
1784
Winson Chung3ac74c52011-06-30 17:39:37 -07001785 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001786 }
1787
1788 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001789 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001790
1791 /* Accessibility */
1792 @Override
1793 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1794 super.onInitializeAccessibilityNodeInfo(info);
1795 info.setScrollable(true);
1796 }
1797
1798 @Override
1799 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1800 super.onInitializeAccessibilityEvent(event);
1801 event.setScrollable(true);
1802 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1803 event.setFromIndex(mCurrentPage);
1804 event.setToIndex(mCurrentPage);
1805 event.setItemCount(getChildCount());
1806 }
1807 }
1808
1809 @Override
1810 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1811 // Do not append text content to scroll events they are fired frequently
1812 // and the client has already received another event type with the text.
1813 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1814 super.dispatchPopulateAccessibilityEvent(event);
1815 }
1816
1817 onPopulateAccessibilityEvent(event);
1818 return false;
1819 }
1820
1821 @Override
1822 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
1823 super.onPopulateAccessibilityEvent(event);
1824
1825 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1826 event.getText().add(getCurrentPageDescription());
1827 }
1828 }
1829
1830 protected String getCurrentPageDescription() {
1831 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1832 return String.format(mContext.getString(R.string.default_scroll_format),
1833 page + 1, getChildCount());
1834 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001835}