blob: 2de7d4a4b33f4c12ce3ba5348f72ad50f635cf74 [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 Chungbb6f6a52011-07-25 17:55:44 -070023import android.animation.ValueAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070025import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.graphics.Canvas;
27import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.view.ActionMode;
Winson Chung185d7162011-02-28 13:47:29 -080033import android.view.InputDevice;
34import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070035import android.view.MotionEvent;
36import android.view.VelocityTracker;
37import android.view.View;
38import android.view.ViewConfiguration;
39import android.view.ViewGroup;
40import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070041import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070042import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070043import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080044import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070045import android.widget.Checkable;
Winson Chung007c6982011-06-14 13:27:53 -070046import android.widget.ImageView;
Winson Chung321e9ee2010-08-09 13:37:56 -070047import android.widget.Scroller;
48
Winson Chung04998342011-01-05 13:54:43 -080049import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070050
Winson Chung6a0f57d2011-06-29 20:10:49 -070051import java.util.ArrayList;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053/**
54 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070055 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070056 */
57public abstract class PagedView extends ViewGroup {
58 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070059 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Winson Chung86f77532010-08-24 11:08:22 -070062 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070063 private static final int MIN_LENGTH_FOR_FLING = 25;
64 // The min drag distance to trigger a page shift (regardless of velocity)
65 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070066
Adam Cohene0f66b52010-11-23 15:06:07 -080067 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070068 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070069
Adam Cohenb5ba0972011-09-07 18:02:31 -070070 private static final float OVERSCROLL_ACCELERATE_FACTOR = 2;
Winson Chungb26f3d62011-06-02 10:49:29 -070071 private static final float OVERSCROLL_DAMP_FACTOR = 0.14f;
Adam Cohene0f66b52010-11-23 15:06:07 -080072 private static final int MINIMUM_SNAP_VELOCITY = 2200;
73 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohenb64cb5a2011-02-15 13:53:42 -080074 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen68d73932010-11-15 10:50:58 -080075
Michael Jurka0142d492010-08-25 17:46:15 -070076 // the velocity at which a fling gesture will cause us to snap to the next page
77 protected int mSnapVelocity = 500;
78
Adam Cohenb5ba0972011-09-07 18:02:31 -070079 protected float mDensity;
Michael Jurka0142d492010-08-25 17:46:15 -070080 protected float mSmoothingTime;
81 protected float mTouchX;
82
83 protected boolean mFirstLayout = true;
84
85 protected int mCurrentPage;
86 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080087 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070088 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070089 private VelocityTracker mVelocityTracker;
90
91 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080092 protected float mLastMotionX;
Winson Chungc0844aa2011-02-02 15:25:58 -080093 protected float mLastMotionXRemainder;
Michael Jurka7426c422010-11-11 15:23:47 -080094 protected float mLastMotionY;
Adam Cohenaefd4e12011-02-14 16:39:38 -080095 protected float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -070096 private int mLastScreenCenter = -1;
Adam Cohen73894962011-10-31 13:17:17 -070097 private int[] mChildOffsets;
98 private int[] mChildRelativeOffsets;
99 private int[] mChildOffsetsWithLayoutScale;
Winson Chung321e9ee2010-08-09 13:37:56 -0700100
Michael Jurka0142d492010-08-25 17:46:15 -0700101 protected final static int TOUCH_STATE_REST = 0;
102 protected final static int TOUCH_STATE_SCROLLING = 1;
103 protected final static int TOUCH_STATE_PREV_PAGE = 2;
104 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -0700105 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka0142d492010-08-25 17:46:15 -0700107 protected int mTouchState = TOUCH_STATE_REST;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700108 protected boolean mForceScreenScrolled = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Michael Jurka0142d492010-08-25 17:46:15 -0700110 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurka7426c422010-11-11 15:23:47 -0800112 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700113
Michael Jurka7426c422010-11-11 15:23:47 -0800114 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115 private int mPagingTouchSlop;
116 private int mMaximumVelocity;
Winson Chung1908d072011-02-24 18:09:44 -0800117 private int mMinimumWidth;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700118 protected int mPageSpacing;
119 protected int mPageLayoutPaddingTop;
120 protected int mPageLayoutPaddingBottom;
121 protected int mPageLayoutPaddingLeft;
122 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700123 protected int mPageLayoutWidthGap;
124 protected int mPageLayoutHeightGap;
Michael Jurka87b14902011-05-25 22:13:09 -0700125 protected int mCellCountX = 0;
126 protected int mCellCountY = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700127 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800128 protected boolean mAllowOverScroll = true;
129 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700130
Michael Jurka8c920dd2011-01-20 14:16:56 -0800131 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800132 protected float mLayoutScale = 1.0f;
133
Michael Jurka5f1c5092010-09-03 14:15:02 -0700134 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka5f1c5092010-09-03 14:15:02 -0700136 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137
Winson Chung86f77532010-08-24 11:08:22 -0700138 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700139
Winson Chung86f77532010-08-24 11:08:22 -0700140 private ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700141
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700142 // choice modes
143 protected static final int CHOICE_MODE_NONE = 0;
144 protected static final int CHOICE_MODE_SINGLE = 1;
145 // Multiple selection mode is not supported by all Launcher actions atm
146 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700147
Michael Jurkae17e19c2010-09-28 11:01:39 -0700148 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700149 private ActionMode mActionMode;
150
Michael Jurka0142d492010-08-25 17:46:15 -0700151 // If true, syncPages and syncPageItems will be called to refresh pages
152 protected boolean mContentIsRefreshable = true;
153
154 // If true, modify alpha of neighboring pages as user scrolls left/right
155 protected boolean mFadeInAdjacentScreens = true;
156
157 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
158 // to switch to a new page
159 protected boolean mUsePagingTouchSlop = true;
160
161 // If true, the subclass should directly update mScrollX itself in its computeScroll method
162 // (SmoothPagedView does this)
163 protected boolean mDeferScrollUpdate = false;
164
Patrick Dubroy1262e362010-10-06 15:49:50 -0700165 protected boolean mIsPageMoving = false;
166
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 // All syncs and layout passes are deferred until data is ready.
168 protected boolean mIsDataReady = false;
169
Winson Chung007c6982011-06-14 13:27:53 -0700170 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700171 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700172 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700173 private int mScrollIndicatorPaddingLeft;
174 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700175 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700176 protected static final int sScrollIndicatorFadeInDuration = 150;
177 protected static final int sScrollIndicatorFadeOutDuration = 650;
178 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700179
Winson Chungb44b5242011-06-13 11:32:14 -0700180 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700181 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700182
Winson Chung86f77532010-08-24 11:08:22 -0700183 public interface PageSwitchListener {
184 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700185 }
186
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 public PagedView(Context context) {
188 this(context, null);
189 }
190
191 public PagedView(Context context, AttributeSet attrs) {
192 this(context, attrs, 0);
193 }
194
195 public PagedView(Context context, AttributeSet attrs, int defStyle) {
196 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700197 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700198
Adam Cohen9c4949e2010-10-05 12:27:22 -0700199 TypedArray a = context.obtainStyledAttributes(attrs,
200 R.styleable.PagedView, defStyle, 0);
201 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
202 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800203 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700204 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800205 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700206 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800207 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700208 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800209 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700210 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700211 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700212 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700213 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700214 mScrollIndicatorPaddingLeft =
215 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
216 mScrollIndicatorPaddingRight =
217 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700218 a.recycle();
219
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700221 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 }
223
224 /**
225 * Initializes various states for this workspace.
226 */
Michael Jurka0142d492010-08-25 17:46:15 -0700227 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700228 mDirtyPageContent = new ArrayList<Boolean>();
229 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800230 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700231 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700232 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700233
234 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
235 mTouchSlop = configuration.getScaledTouchSlop();
236 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
237 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700238 mDensity = getResources().getDisplayMetrics().density;
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 }
240
Winson Chung86f77532010-08-24 11:08:22 -0700241 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
242 mPageSwitchListener = pageSwitchListener;
243 if (mPageSwitchListener != null) {
244 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246 }
247
248 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700249 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
250 * out pages.
251 */
252 protected void setDataIsReady() {
253 mIsDataReady = true;
254 }
255 protected boolean isDataReady() {
256 return mIsDataReady;
257 }
258
259 /**
Winson Chung86f77532010-08-24 11:08:22 -0700260 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700261 *
Winson Chung86f77532010-08-24 11:08:22 -0700262 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700263 */
Winson Chung86f77532010-08-24 11:08:22 -0700264 int getCurrentPage() {
265 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 }
267
Winson Chung86f77532010-08-24 11:08:22 -0700268 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 return getChildCount();
270 }
271
Winson Chung86f77532010-08-24 11:08:22 -0700272 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700273 return getChildAt(index);
274 }
275
Adam Cohenae4f1552011-10-20 00:15:42 -0700276 protected int indexToPage(int index) {
277 return index;
278 }
279
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800281 * Updates the scroll of the current page immediately to its final scroll position. We use this
282 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
283 * the previous tab page.
284 */
285 protected void updateCurrentPageScroll() {
286 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
287 scrollTo(newX, 0);
288 mScroller.setFinalX(newX);
289 }
290
291 /**
Winson Chung86f77532010-08-24 11:08:22 -0700292 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 */
Winson Chung86f77532010-08-24 11:08:22 -0700294 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800295 if (!mScroller.isFinished()) {
296 mScroller.abortAnimation();
297 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800298 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
299 // the default
300 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800301 return;
302 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700303
Winson Chung86f77532010-08-24 11:08:22 -0700304 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800305 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700306 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700307 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800308 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 }
310
Michael Jurka0142d492010-08-25 17:46:15 -0700311 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700312 if (mPageSwitchListener != null) {
313 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 }
315 }
316
Michael Jurkace7e05f2011-02-01 22:02:35 -0800317 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700318 if (!mIsPageMoving) {
319 mIsPageMoving = true;
320 onPageBeginMoving();
321 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700322 }
323
Michael Jurkace7e05f2011-02-01 22:02:35 -0800324 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700325 if (mIsPageMoving) {
326 mIsPageMoving = false;
327 onPageEndMoving();
328 }
Michael Jurka0142d492010-08-25 17:46:15 -0700329 }
330
Adam Cohen26976d92011-03-22 15:33:33 -0700331 protected boolean isPageMoving() {
332 return mIsPageMoving;
333 }
334
Michael Jurka0142d492010-08-25 17:46:15 -0700335 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700336 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700337 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700338 }
339
340 // a method that subclasses can override to add behavior
341 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700342 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700343 }
344
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 /**
Winson Chung86f77532010-08-24 11:08:22 -0700346 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 *
348 * @param l The listener used to respond to long clicks.
349 */
350 @Override
351 public void setOnLongClickListener(OnLongClickListener l) {
352 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700353 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700355 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
357 }
358
359 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800360 public void scrollBy(int x, int y) {
361 scrollTo(mUnboundedScrollX + x, mScrollY + y);
362 }
363
364 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700365 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800366 mUnboundedScrollX = x;
367
368 if (x < 0) {
369 super.scrollTo(0, y);
370 if (mAllowOverScroll) {
371 overScroll(x);
372 }
373 } else if (x > mMaxScrollX) {
374 super.scrollTo(mMaxScrollX, y);
375 if (mAllowOverScroll) {
376 overScroll(x - mMaxScrollX);
377 }
378 } else {
379 super.scrollTo(x, y);
380 }
381
Michael Jurka0142d492010-08-25 17:46:15 -0700382 mTouchX = x;
383 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
384 }
385
386 // we moved this functionality to a helper function so SmoothPagedView can reuse it
387 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700388 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700389 // Don't bother scrolling if the page does not need to be moved
390 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700391 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
392 }
Michael Jurka0142d492010-08-25 17:46:15 -0700393 invalidate();
394 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700395 } else if (mNextPage != INVALID_PAGE) {
396 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700397 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700398 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700399
400 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700401 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700402 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700403 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700404 }
405
Adam Cohen73aa9752010-11-24 16:26:58 -0800406 // We don't want to trigger a page end moving unless the page has settled
407 // and the user has stopped scrolling
408 if (mTouchState == TOUCH_STATE_REST) {
409 pageEndMoving();
410 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700411
412 // Notify the user when the page changes
413 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
414 AccessibilityEvent ev =
415 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
416 ev.getText().add(getCurrentPageDescription());
417 sendAccessibilityEventUnchecked(ev);
418 }
Michael Jurka0142d492010-08-25 17:46:15 -0700419 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700420 }
Michael Jurka0142d492010-08-25 17:46:15 -0700421 return false;
422 }
423
424 @Override
425 public void computeScroll() {
426 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
428
429 @Override
430 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700431 if (!mIsDataReady) {
432 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
433 return;
434 }
435
Winson Chung321e9ee2010-08-09 13:37:56 -0700436 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
437 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
438 if (widthMode != MeasureSpec.EXACTLY) {
439 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
440 }
441
Adam Lesinski6b879f02010-11-04 16:15:23 -0700442 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
443 * of the All apps view on XLarge displays to not take up more space then it needs. Width
444 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
445 * each page to have the same width.
446 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700447 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700448 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
449 int maxChildHeight = 0;
450
451 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700452 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700453
Michael Jurka36fcb742011-04-06 17:08:58 -0700454
Winson Chung321e9ee2010-08-09 13:37:56 -0700455 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700456 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700457 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700458 final int childCount = getChildCount();
459 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700460 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700461 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700462 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
463
464 int childWidthMode;
465 if (lp.width == LayoutParams.WRAP_CONTENT) {
466 childWidthMode = MeasureSpec.AT_MOST;
467 } else {
468 childWidthMode = MeasureSpec.EXACTLY;
469 }
470
471 int childHeightMode;
472 if (lp.height == LayoutParams.WRAP_CONTENT) {
473 childHeightMode = MeasureSpec.AT_MOST;
474 } else {
475 childHeightMode = MeasureSpec.EXACTLY;
476 }
477
478 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700479 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700480 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700481 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700482
483 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700484 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700485 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
486 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700487 }
488
489 if (heightMode == MeasureSpec.AT_MOST) {
490 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 }
Winson Chungae890b82011-09-13 18:08:54 -0700492
Winson Chungae890b82011-09-13 18:08:54 -0700493 setMeasuredDimension(widthSize, heightSize);
494
Adam Cohen25b29952011-11-02 14:49:06 -0700495 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
496 // We also wait until we set the measured dimensions before flushing the cache as well, to
497 // ensure that the cache is filled with good values.
498 invalidateCachedOffsets();
499 updateScrollingIndicatorPosition();
500
Adam Cohenfaa28302010-11-19 12:02:18 -0800501 if (childCount > 0) {
502 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
503 } else {
504 mMaxScrollX = 0;
505 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
507
Michael Jurka8c920dd2011-01-20 14:16:56 -0800508 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800509 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
510 int delta = newX - mScrollX;
511
Michael Jurka8c920dd2011-01-20 14:16:56 -0800512 final int pageCount = getChildCount();
513 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700514 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800515 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800516 }
517 setCurrentPage(newCurrentPage);
518 }
519
Michael Jurka8c920dd2011-01-20 14:16:56 -0800520 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
521 // 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 -0800522 // tightens the layout accordingly
523 public void setLayoutScale(float childrenScale) {
524 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700525 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526
527 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
528 int childCount = getChildCount();
529 float childrenX[] = new float[childCount];
530 float childrenY[] = new float[childCount];
531 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700532 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800533 childrenX[i] = child.getX();
534 childrenY[i] = child.getY();
535 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700536 // Trigger a full re-layout (never just call onLayout directly!)
537 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
538 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
539 requestLayout();
540 measure(widthSpec, heightSpec);
541 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700543 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800544 child.setX(childrenX[i]);
545 child.setY(childrenY[i]);
546 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700547
Michael Jurkad3ef3062010-11-23 16:23:58 -0800548 // Also, the page offset has changed (since the pages are now smaller);
549 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800550 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800551 }
552
Winson Chung321e9ee2010-08-09 13:37:56 -0700553 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700554 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700555 if (!mIsDataReady) {
556 return;
557 }
558
Winson Chung785d2eb2011-04-14 16:08:02 -0700559 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700560 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700561 final int childCount = getChildCount();
562 int childLeft = 0;
563 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700564 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
565 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700566 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700567
568 // Calculate the variable page spacing if necessary
569 if (mPageSpacing < 0) {
570 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
571 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700572 }
573
574 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700575 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700576 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800577 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700578 final int childHeight = child.getMeasuredHeight();
579 int childTop = mPaddingTop;
580 if (mCenterPagesVertically) {
581 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
582 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800583
Winson Chung785d2eb2011-04-14 16:08:02 -0700584 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700585 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800586 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700587 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700588 }
589 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700590
591 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
592 setHorizontalScrollBarEnabled(false);
593 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
594 scrollTo(newX, 0);
595 mScroller.setFinalX(newX);
596 setHorizontalScrollBarEnabled(true);
597 mFirstLayout = false;
598 }
599
Michael Jurkad3ef3062010-11-23 16:23:58 -0800600 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
601 mFirstLayout = false;
602 }
Winson Chunge3193b92010-09-10 11:44:42 -0700603 }
604
Adam Cohenf34bab52010-09-30 14:11:56 -0700605 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700606 if (isScrollingIndicatorEnabled()) {
607 updateScrollingIndicator();
608 }
609 if (mFadeInAdjacentScreens) {
610 for (int i = 0; i < getChildCount(); i++) {
611 View child = getChildAt(i);
612 if (child != null) {
613 float scrollProgress = getScrollProgress(screenCenter, child, i);
614 float alpha = 1 - Math.abs(scrollProgress);
615 child.setFastAlpha(alpha);
616 child.fastInvalidate();
617 }
618 }
619 invalidate();
620 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700621 }
622
Winson Chunge3193b92010-09-10 11:44:42 -0700623 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700624 protected void onViewAdded(View child) {
625 super.onViewAdded(child);
626
627 // This ensures that when children are added, they get the correct transforms / alphas
628 // in accordance with any scroll effects.
629 mForceScreenScrolled = true;
630 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700631 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700632 }
633
Adam Cohen73894962011-10-31 13:17:17 -0700634 protected void invalidateCachedOffsets() {
635 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700636 if (count == 0) {
637 mChildOffsets = null;
638 mChildRelativeOffsets = null;
639 mChildOffsetsWithLayoutScale = null;
640 return;
641 }
Adam Cohen73894962011-10-31 13:17:17 -0700642
643 mChildOffsets = new int[count];
644 mChildRelativeOffsets = new int[count];
645 mChildOffsetsWithLayoutScale = new int[count];
646 for (int i = 0; i < count; i++) {
647 mChildOffsets[i] = -1;
648 mChildRelativeOffsets[i] = -1;
649 mChildOffsetsWithLayoutScale[i] = -1;
650 }
651 }
652
653 protected int getChildOffset(int index) {
654 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
655 mChildOffsets : mChildOffsetsWithLayoutScale;
656
657 if (childOffsets != null && childOffsets[index] != -1) {
658 return childOffsets[index];
659 } else {
660 if (getChildCount() == 0)
661 return 0;
662
663 int offset = getRelativeChildOffset(0);
664 for (int i = 0; i < index; ++i) {
665 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
666 }
667 if (childOffsets != null) {
668 childOffsets[index] = offset;
669 }
670 return offset;
671 }
672 }
673
674 protected int getRelativeChildOffset(int index) {
675 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
676 return mChildRelativeOffsets[index];
677 } else {
678 final int padding = mPaddingLeft + mPaddingRight;
679 final int offset = mPaddingLeft +
680 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
681 if (mChildRelativeOffsets != null) {
682 mChildRelativeOffsets[index] = offset;
683 }
684 return offset;
685 }
686 }
687
688 protected int getScaledRelativeChildOffset(int index) {
689 final int padding = mPaddingLeft + mPaddingRight;
690 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
691 getScaledMeasuredWidth(getPageAt(index))) / 2;
692 return offset;
693 }
694
695 protected int getScaledMeasuredWidth(View child) {
696 // This functions are called enough times that it actually makes a difference in the
697 // profiler -- so just inline the max() here
698 final int measuredWidth = child.getMeasuredWidth();
699 final int minWidth = mMinimumWidth;
700 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
701 return (int) (maxWidth * mLayoutScale + 0.5f);
702 }
703
Adam Cohen2591f6a2011-10-25 14:36:40 -0700704 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700705 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700706 int halfScreenSize = getMeasuredWidth() / 2;
707 int screenCenter = mScrollX + halfScreenSize;
708
Adam Cohen2591f6a2011-10-25 14:36:40 -0700709 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700710 screenScrolled(screenCenter);
Adam Cohenf34bab52010-09-30 14:11:56 -0700711 mLastScreenCenter = screenCenter;
Adam Cohen2591f6a2011-10-25 14:36:40 -0700712 mForceScreenScrolled = false;
Adam Cohenf34bab52010-09-30 14:11:56 -0700713 }
Michael Jurka0142d492010-08-25 17:46:15 -0700714
715 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700716 // As an optimization, this code assumes that all pages have the same width as the 0th
717 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700718 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700719 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700720 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700721 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700722 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700723 int leftScreen = 0;
724 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700725 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700726 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700727 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700728 }
729 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700730 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700731 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700732 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700733 }
Michael Jurka0142d492010-08-25 17:46:15 -0700734
Michael Jurkac4fb9172010-09-02 17:19:20 -0700735 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800736 // Clip to the bounds
737 canvas.save();
738 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
739 mScrollY + mBottom - mTop);
740
Adam Cohen22f823d2011-09-01 17:22:18 -0700741 for (int i = rightScreen; i >= leftScreen; i--) {
742 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700743 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800744 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700745 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700746 }
747
748 @Override
749 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700750 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700751 if (page != mCurrentPage || !mScroller.isFinished()) {
752 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 return true;
754 }
755 return false;
756 }
757
758 @Override
759 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700760 int focusablePage;
761 if (mNextPage != INVALID_PAGE) {
762 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700764 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 }
Winson Chung86f77532010-08-24 11:08:22 -0700766 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700768 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 }
770 return false;
771 }
772
773 @Override
774 public boolean dispatchUnhandledMove(View focused, int direction) {
775 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700776 if (getCurrentPage() > 0) {
777 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 return true;
779 }
780 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700781 if (getCurrentPage() < getPageCount() - 1) {
782 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 return true;
784 }
785 }
786 return super.dispatchUnhandledMove(focused, direction);
787 }
788
789 @Override
790 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700791 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
792 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 }
794 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700795 if (mCurrentPage > 0) {
796 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700797 }
798 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700799 if (mCurrentPage < getPageCount() - 1) {
800 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 }
802 }
803 }
804
805 /**
806 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700807 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 *
Winson Chung86f77532010-08-24 11:08:22 -0700809 * This happens when live folders requery, and if they're off page, they
810 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 */
812 @Override
813 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700814 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 View v = focused;
816 while (true) {
817 if (v == current) {
818 super.focusableViewAvailable(focused);
819 return;
820 }
821 if (v == this) {
822 return;
823 }
824 ViewParent parent = v.getParent();
825 if (parent instanceof View) {
826 v = (View)v.getParent();
827 } else {
828 return;
829 }
830 }
831 }
832
833 /**
834 * {@inheritDoc}
835 */
836 @Override
837 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
838 if (disallowIntercept) {
839 // We need to make sure to cancel our long press if
840 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700841 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700842 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 }
844 super.requestDisallowInterceptTouchEvent(disallowIntercept);
845 }
846
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800847 /**
848 * Return true if a tap at (x, y) should trigger a flip to the previous page.
849 */
850 protected boolean hitsPreviousPage(float x, float y) {
851 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
852 }
853
854 /**
855 * Return true if a tap at (x, y) should trigger a flip to the next page.
856 */
857 protected boolean hitsNextPage(float x, float y) {
858 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
859 }
860
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 @Override
862 public boolean onInterceptTouchEvent(MotionEvent ev) {
863 /*
864 * This method JUST determines whether we want to intercept the motion.
865 * If we return true, onTouchEvent will be called and we do the actual
866 * scrolling there.
867 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800868 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700869
Winson Chung45e1d6e2010-11-09 17:19:49 -0800870 // Skip touch handling if there are no pages to swipe
871 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
872
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 /*
874 * Shortcut the most recurring case: the user is in the dragging
875 * state and he is moving his finger. We want to intercept this
876 * motion.
877 */
878 final int action = ev.getAction();
879 if ((action == MotionEvent.ACTION_MOVE) &&
880 (mTouchState == TOUCH_STATE_SCROLLING)) {
881 return true;
882 }
883
Winson Chung321e9ee2010-08-09 13:37:56 -0700884 switch (action & MotionEvent.ACTION_MASK) {
885 case MotionEvent.ACTION_MOVE: {
886 /*
887 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
888 * whether the user has moved far enough from his original down touch.
889 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700890 if (mActivePointerId != INVALID_POINTER) {
891 determineScrollingStart(ev);
892 break;
893 }
894 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
895 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
896 // i.e. fall through to the next case (don't break)
897 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
898 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 }
900
901 case MotionEvent.ACTION_DOWN: {
902 final float x = ev.getX();
903 final float y = ev.getY();
904 // Remember location of down touch
905 mDownMotionX = x;
906 mLastMotionX = x;
907 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800908 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800909 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700910 mActivePointerId = ev.getPointerId(0);
911 mAllowLongPress = true;
912
913 /*
914 * If being flinged and user touches the screen, initiate drag;
915 * otherwise don't. mScroller.isFinished should be false when
916 * being flinged.
917 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700918 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700919 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
920 if (finishedScrolling) {
921 mTouchState = TOUCH_STATE_REST;
922 mScroller.abortAnimation();
923 } else {
924 mTouchState = TOUCH_STATE_SCROLLING;
925 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700926
Winson Chung86f77532010-08-24 11:08:22 -0700927 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800929 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700930 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800931 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800933 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700934 mTouchState = TOUCH_STATE_NEXT_PAGE;
935 }
936 }
937 }
938 break;
939 }
940
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800942 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 mTouchState = TOUCH_STATE_REST;
944 mAllowLongPress = false;
945 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800946 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 break;
948
949 case MotionEvent.ACTION_POINTER_UP:
950 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800951 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 break;
953 }
954
955 /*
956 * The only time we want to intercept motion events is if we are in the
957 * drag mode.
958 */
959 return mTouchState != TOUCH_STATE_REST;
960 }
961
Winson Chung80baf5a2010-08-09 16:03:15 -0700962 protected void animateClickFeedback(View v, final Runnable r) {
963 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800964 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
965 loadAnimator(mContext, R.anim.paged_view_click_feedback);
966 anim.setTarget(v);
967 anim.addListener(new AnimatorListenerAdapter() {
968 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700969 r.run();
970 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700971 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800972 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700973 }
974
Adam Cohenf8d28232011-02-01 21:47:00 -0800975 protected void determineScrollingStart(MotionEvent ev) {
976 determineScrollingStart(ev, 1.0f);
977 }
978
Winson Chung321e9ee2010-08-09 13:37:56 -0700979 /*
980 * Determines if we should change the touch state to start scrolling after the
981 * user moves their touch point too far.
982 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800983 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 /*
985 * Locally do absolute value. mLastMotionX is set to the y value
986 * of the down event.
987 */
988 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -0700989 if (pointerIndex == -1) {
990 return;
991 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 final float x = ev.getX(pointerIndex);
993 final float y = ev.getY(pointerIndex);
994 final int xDiff = (int) Math.abs(x - mLastMotionX);
995 final int yDiff = (int) Math.abs(y - mLastMotionY);
996
Adam Cohenf8d28232011-02-01 21:47:00 -0800997 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 boolean xPaged = xDiff > mPagingTouchSlop;
999 boolean xMoved = xDiff > touchSlop;
1000 boolean yMoved = yDiff > touchSlop;
1001
Adam Cohenf8d28232011-02-01 21:47:00 -08001002 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001003 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 // Scroll if the user moved far enough along the X axis
1005 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001006 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001008 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001009 mTouchX = mScrollX;
1010 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1011 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 }
1013 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001014 cancelCurrentPageLongPress();
1015 }
1016 }
1017
1018 protected void cancelCurrentPageLongPress() {
1019 if (mAllowLongPress) {
1020 mAllowLongPress = false;
1021 // Try canceling the long press. It could also have been scheduled
1022 // by a distant descendant, so use the mAllowLongPress flag to block
1023 // everything
1024 final View currentPage = getPageAt(mCurrentPage);
1025 if (currentPage != null) {
1026 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 }
1028 }
1029 }
1030
Adam Cohenb5ba0972011-09-07 18:02:31 -07001031 protected float getScrollProgress(int screenCenter, View v, int page) {
1032 final int halfScreenSize = getMeasuredWidth() / 2;
1033
1034 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1035 int delta = screenCenter - (getChildOffset(page) -
1036 getRelativeChildOffset(page) + halfScreenSize);
1037
1038 float scrollProgress = delta / (totalDistance * 1.0f);
1039 scrollProgress = Math.min(scrollProgress, 1.0f);
1040 scrollProgress = Math.max(scrollProgress, -1.0f);
1041 return scrollProgress;
1042 }
1043
Adam Cohene0f66b52010-11-23 15:06:07 -08001044 // This curve determines how the effect of scrolling over the limits of the page dimishes
1045 // as the user pulls further and further from the bounds
1046 private float overScrollInfluenceCurve(float f) {
1047 f -= 1.0f;
1048 return f * f * f + 1.0f;
1049 }
1050
Adam Cohenb5ba0972011-09-07 18:02:31 -07001051 protected void acceleratedOverScroll(float amount) {
1052 int screenSize = getMeasuredWidth();
1053
1054 // We want to reach the max over scroll effect when the user has
1055 // over scrolled half the size of the screen
1056 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1057
1058 if (f == 0) return;
1059
1060 // Clamp this factor, f, to -1 < f < 1
1061 if (Math.abs(f) >= 1) {
1062 f /= Math.abs(f);
1063 }
1064
1065 int overScrollAmount = (int) Math.round(f * screenSize);
1066 if (amount < 0) {
1067 mScrollX = overScrollAmount;
1068 } else {
1069 mScrollX = mMaxScrollX + overScrollAmount;
1070 }
1071 invalidate();
1072 }
1073
1074 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001075 int screenSize = getMeasuredWidth();
1076
1077 float f = (amount / screenSize);
1078
1079 if (f == 0) return;
1080 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1081
Adam Cohen7bfc9792011-01-28 13:52:37 -08001082 // Clamp this factor, f, to -1 < f < 1
1083 if (Math.abs(f) >= 1) {
1084 f /= Math.abs(f);
1085 }
1086
Adam Cohene0f66b52010-11-23 15:06:07 -08001087 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001088 if (amount < 0) {
1089 mScrollX = overScrollAmount;
1090 } else {
1091 mScrollX = mMaxScrollX + overScrollAmount;
1092 }
1093 invalidate();
1094 }
1095
Adam Cohenb5ba0972011-09-07 18:02:31 -07001096 protected void overScroll(float amount) {
1097 dampedOverScroll(amount);
1098 }
1099
Michael Jurkac5b262c2011-01-12 20:24:50 -08001100 protected float maxOverScroll() {
1101 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001102 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001103 float f = 1.0f;
1104 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1105 return OVERSCROLL_DAMP_FACTOR * f;
1106 }
1107
Winson Chung321e9ee2010-08-09 13:37:56 -07001108 @Override
1109 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001110 // Skip touch handling if there are no pages to swipe
1111 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1112
Michael Jurkab8f06722010-10-10 15:58:46 -07001113 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001114
1115 final int action = ev.getAction();
1116
1117 switch (action & MotionEvent.ACTION_MASK) {
1118 case MotionEvent.ACTION_DOWN:
1119 /*
1120 * If being flinged and user touches, stop the fling. isFinished
1121 * will be false if being flinged.
1122 */
1123 if (!mScroller.isFinished()) {
1124 mScroller.abortAnimation();
1125 }
1126
1127 // Remember where the motion event started
1128 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001129 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001130 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001131 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001132 if (mTouchState == TOUCH_STATE_SCROLLING) {
1133 pageBeginMoving();
1134 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 break;
1136
1137 case MotionEvent.ACTION_MOVE:
1138 if (mTouchState == TOUCH_STATE_SCROLLING) {
1139 // Scroll to follow the motion event
1140 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1141 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001142 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001143
Adam Cohenaefd4e12011-02-14 16:39:38 -08001144 mTotalMotionX += Math.abs(deltaX);
1145
Winson Chungc0844aa2011-02-02 15:25:58 -08001146 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1147 // keep the remainder because we are actually testing if we've moved from the last
1148 // scrolled position (which is discrete).
1149 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001150 mTouchX += deltaX;
1151 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1152 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001153 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001154 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001155 } else {
1156 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001157 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001158 mLastMotionX = x;
1159 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001160 } else {
1161 awakenScrollBars();
1162 }
Adam Cohen564976a2010-10-13 18:52:07 -07001163 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001164 determineScrollingStart(ev);
1165 }
1166 break;
1167
1168 case MotionEvent.ACTION_UP:
1169 if (mTouchState == TOUCH_STATE_SCROLLING) {
1170 final int activePointerId = mActivePointerId;
1171 final int pointerIndex = ev.findPointerIndex(activePointerId);
1172 final float x = ev.getX(pointerIndex);
1173 final VelocityTracker velocityTracker = mVelocityTracker;
1174 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1175 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001176 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001177 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001178 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001179
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001180 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1181
Adam Cohenaefd4e12011-02-14 16:39:38 -08001182 // In the case that the page is moved far to one direction and then is flung
1183 // in the opposite direction, we use a threshold to determine whether we should
1184 // just return to the starting page, or if we should skip one further.
1185 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001186 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001187 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001188 Math.signum(velocityX) != Math.signum(deltaX)) {
1189 returnToOriginalPage = true;
1190 }
1191
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001192 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001193 Math.abs(velocityX) > snapVelocity;
1194
1195 int finalPage;
1196 // We give flings precedence over large moves, which is why we short-circuit our
1197 // test for a large move if a fling has been registered. That is, a large
1198 // move to the left and fling to the right will register as a fling to the right.
1199 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1200 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1201 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1202 snapToPageWithVelocity(finalPage, velocityX);
1203 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1204 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001205 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001206 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1207 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 } else {
1209 snapToDestination();
1210 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001211 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 // at this point we have not moved beyond the touch slop
1213 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1214 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001215 int nextPage = Math.max(0, mCurrentPage - 1);
1216 if (nextPage != mCurrentPage) {
1217 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 } else {
1219 snapToDestination();
1220 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001221 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 // at this point we have not moved beyond the touch slop
1223 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1224 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001225 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1226 if (nextPage != mCurrentPage) {
1227 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 } else {
1229 snapToDestination();
1230 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001231 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001232 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 }
1234 mTouchState = TOUCH_STATE_REST;
1235 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001236 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001237 break;
1238
1239 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001240 if (mTouchState == TOUCH_STATE_SCROLLING) {
1241 snapToDestination();
1242 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001243 mTouchState = TOUCH_STATE_REST;
1244 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001245 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 break;
1247
1248 case MotionEvent.ACTION_POINTER_UP:
1249 onSecondaryPointerUp(ev);
1250 break;
1251 }
1252
1253 return true;
1254 }
1255
Winson Chung185d7162011-02-28 13:47:29 -08001256 @Override
1257 public boolean onGenericMotionEvent(MotionEvent event) {
1258 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1259 switch (event.getAction()) {
1260 case MotionEvent.ACTION_SCROLL: {
1261 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1262 final float vscroll;
1263 final float hscroll;
1264 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1265 vscroll = 0;
1266 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1267 } else {
1268 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1269 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1270 }
1271 if (hscroll != 0 || vscroll != 0) {
1272 if (hscroll > 0 || vscroll > 0) {
1273 scrollRight();
1274 } else {
1275 scrollLeft();
1276 }
1277 return true;
1278 }
1279 }
1280 }
1281 }
1282 return super.onGenericMotionEvent(event);
1283 }
1284
Michael Jurkab8f06722010-10-10 15:58:46 -07001285 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1286 if (mVelocityTracker == null) {
1287 mVelocityTracker = VelocityTracker.obtain();
1288 }
1289 mVelocityTracker.addMovement(ev);
1290 }
1291
1292 private void releaseVelocityTracker() {
1293 if (mVelocityTracker != null) {
1294 mVelocityTracker.recycle();
1295 mVelocityTracker = null;
1296 }
1297 }
1298
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 private void onSecondaryPointerUp(MotionEvent ev) {
1300 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1301 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1302 final int pointerId = ev.getPointerId(pointerIndex);
1303 if (pointerId == mActivePointerId) {
1304 // This was our active pointer going up. Choose a new
1305 // active pointer and adjust accordingly.
1306 // TODO: Make this decision more intelligent.
1307 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1308 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1309 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001310 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001311 mActivePointerId = ev.getPointerId(newPointerIndex);
1312 if (mVelocityTracker != null) {
1313 mVelocityTracker.clear();
1314 }
1315 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001316 }
1317
Michael Jurkad771c962011-08-09 15:00:48 -07001318 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001319
1320 @Override
1321 public void requestChildFocus(View child, View focused) {
1322 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001323 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001324 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001325 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001326 }
1327 }
1328
Winson Chunge3193b92010-09-10 11:44:42 -07001329 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1330 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001331 int left;
1332 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001333 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001334 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001335 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001336 if (left <= relativeOffset && relativeOffset <= right) {
1337 return i;
1338 }
Winson Chunge3193b92010-09-10 11:44:42 -07001339 }
1340 return -1;
1341 }
1342
Winson Chung1908d072011-02-24 18:09:44 -08001343 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001344 // This functions are called enough times that it actually makes a difference in the
1345 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001346 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001347 final int minWidth = mMinimumWidth;
1348 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001349 }
1350
Adam Cohend19d3ca2010-09-15 14:43:42 -07001351 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001352 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001353 int minDistanceFromScreenCenterIndex = -1;
1354 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1355 final int childCount = getChildCount();
1356 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001357 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001358 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001359 int halfChildWidth = (childWidth / 2);
1360 int childCenter = getChildOffset(i) + halfChildWidth;
1361 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1362 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1363 minDistanceFromScreenCenter = distanceFromScreenCenter;
1364 minDistanceFromScreenCenterIndex = i;
1365 }
1366 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001367 return minDistanceFromScreenCenterIndex;
1368 }
1369
1370 protected void snapToDestination() {
1371 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 }
1373
Adam Cohene0f66b52010-11-23 15:06:07 -08001374 private static class ScrollInterpolator implements Interpolator {
1375 public ScrollInterpolator() {
1376 }
1377
1378 public float getInterpolation(float t) {
1379 t -= 1.0f;
1380 return t*t*t*t*t + 1;
1381 }
1382 }
1383
1384 // We want the duration of the page snap animation to be influenced by the distance that
1385 // the screen has to travel, however, we don't want this duration to be effected in a
1386 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1387 // of travel has on the overall snap duration.
1388 float distanceInfluenceForSnapDuration(float f) {
1389 f -= 0.5f; // center the values about 0.
1390 f *= 0.3f * Math.PI / 2.0f;
1391 return (float) Math.sin(f);
1392 }
1393
Michael Jurka0142d492010-08-25 17:46:15 -07001394 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001395 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1396 int halfScreenSize = getMeasuredWidth() / 2;
1397
Winson Chung785d2eb2011-04-14 16:08:02 -07001398 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1399 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1400 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001401 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1402 int delta = newX - mUnboundedScrollX;
1403 int duration = 0;
1404
1405 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1406 // If the velocity is low enough, then treat this more as an automatic page advance
1407 // as opposed to an apparent physical response to flinging
1408 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1409 return;
1410 }
1411
1412 // Here we compute a "distance" that will be used in the computation of the overall
1413 // snap duration. This is a function of the actual distance that needs to be traveled;
1414 // we keep this value close to half screen size in order to reduce the variance in snap
1415 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001416 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001417 float distance = halfScreenSize + halfScreenSize *
1418 distanceInfluenceForSnapDuration(distanceRatio);
1419
1420 velocity = Math.abs(velocity);
1421 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1422
1423 // we want the page's snap velocity to approximately match the velocity at which the
1424 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001425 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1426 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001427
1428 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001429 }
1430
1431 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001432 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 }
1434
Michael Jurka0142d492010-08-25 17:46:15 -07001435 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001436 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001437
Winson Chung785d2eb2011-04-14 16:08:02 -07001438 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1439 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1440 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001441 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001442 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001443 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001444 snapToPage(whichPage, delta, duration);
1445 }
1446
1447 protected void snapToPage(int whichPage, int delta, int duration) {
1448 mNextPage = whichPage;
1449
1450 View focusedChild = getFocusedChild();
1451 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001452 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001453 focusedChild.clearFocus();
1454 }
1455
1456 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001457 awakenScrollBars(duration);
1458 if (duration == 0) {
1459 duration = Math.abs(delta);
1460 }
1461
1462 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001463 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001464
Winson Chungb44b5242011-06-13 11:32:14 -07001465 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1466 // loading associated pages until the scroll settles
1467 if (mDeferScrollUpdate) {
1468 loadAssociatedPages(mNextPage);
1469 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001470 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001471 }
Michael Jurka0142d492010-08-25 17:46:15 -07001472 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001473 invalidate();
1474 }
1475
Winson Chung321e9ee2010-08-09 13:37:56 -07001476 public void scrollLeft() {
1477 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001478 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001479 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001480 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001481 }
1482 }
1483
1484 public void scrollRight() {
1485 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001486 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001487 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001488 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 }
1490 }
1491
Winson Chung86f77532010-08-24 11:08:22 -07001492 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001493 int result = -1;
1494 if (v != null) {
1495 ViewParent vp = v.getParent();
1496 int count = getChildCount();
1497 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001498 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 return i;
1500 }
1501 }
1502 }
1503 return result;
1504 }
1505
1506 /**
1507 * @return True is long presses are still allowed for the current touch
1508 */
1509 public boolean allowLongPress() {
1510 return mAllowLongPress;
1511 }
1512
Michael Jurka0142d492010-08-25 17:46:15 -07001513 /**
1514 * Set true to allow long-press events to be triggered, usually checked by
1515 * {@link Launcher} to accept or block dpad-initiated long-presses.
1516 */
1517 public void setAllowLongPress(boolean allowLongPress) {
1518 mAllowLongPress = allowLongPress;
1519 }
1520
Winson Chung321e9ee2010-08-09 13:37:56 -07001521 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001522 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001523
1524 SavedState(Parcelable superState) {
1525 super(superState);
1526 }
1527
1528 private SavedState(Parcel in) {
1529 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001530 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001531 }
1532
1533 @Override
1534 public void writeToParcel(Parcel out, int flags) {
1535 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001536 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001537 }
1538
1539 public static final Parcelable.Creator<SavedState> CREATOR =
1540 new Parcelable.Creator<SavedState>() {
1541 public SavedState createFromParcel(Parcel in) {
1542 return new SavedState(in);
1543 }
1544
1545 public SavedState[] newArray(int size) {
1546 return new SavedState[size];
1547 }
1548 };
1549 }
1550
Winson Chungf314b0e2011-08-16 11:54:27 -07001551 protected void loadAssociatedPages(int page) {
1552 loadAssociatedPages(page, false);
1553 }
1554 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001555 if (mContentIsRefreshable) {
1556 final int count = getChildCount();
1557 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001558 int lowerPageBound = getAssociatedLowerPageBound(page);
1559 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001560 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1561 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001562 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001563 if ((i != page) && immediateAndOnly) {
1564 continue;
1565 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001566 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001567 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001568 if (lowerPageBound <= i && i <= upperPageBound) {
1569 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001570 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001571 mDirtyPageContent.set(i, false);
1572 }
1573 } else {
1574 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001575 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001576 }
1577 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001578 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001579 }
1580 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001581 }
1582 }
1583
Winson Chunge3193b92010-09-10 11:44:42 -07001584 protected int getAssociatedLowerPageBound(int page) {
1585 return Math.max(0, page - 1);
1586 }
1587 protected int getAssociatedUpperPageBound(int page) {
1588 final int count = getChildCount();
1589 return Math.min(page + 1, count - 1);
1590 }
1591
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001592 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001593 if (isChoiceMode(CHOICE_MODE_NONE)) {
1594 mChoiceMode = mode;
1595 mActionMode = startActionMode(callback);
1596 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001597 }
1598
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001599 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001600 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001601 mChoiceMode = CHOICE_MODE_NONE;
1602 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001603 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001604 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001605 }
1606 }
1607
1608 protected boolean isChoiceMode(int mode) {
1609 return mChoiceMode == mode;
1610 }
1611
1612 protected ArrayList<Checkable> getCheckedGrandchildren() {
1613 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1614 final int childCount = getChildCount();
1615 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001616 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001617 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001618 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001619 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001620 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001621 checked.add((Checkable) v);
1622 }
1623 }
1624 }
1625 return checked;
1626 }
1627
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001628 /**
1629 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1630 * Otherwise, returns null.
1631 */
1632 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001633 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001634 final int childCount = getChildCount();
1635 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001636 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001637 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001638 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001639 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001640 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1641 return (Checkable) v;
1642 }
1643 }
1644 }
1645 }
1646 return null;
1647 }
1648
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001649 protected void resetCheckedGrandchildren() {
1650 // loop through children, and set all of their children to _not_ be checked
1651 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1652 for (int i = 0; i < checked.size(); ++i) {
1653 final Checkable c = checked.get(i);
1654 c.setChecked(false);
1655 }
1656 }
1657
Winson Chung86f77532010-08-24 11:08:22 -07001658 /**
1659 * This method is called ONLY to synchronize the number of pages that the paged view has.
1660 * To actually fill the pages with information, implement syncPageItems() below. It is
1661 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1662 * and therefore, individual page items do not need to be updated in this method.
1663 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001664 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001665
1666 /**
1667 * This method is called to synchronize the items that are on a particular page. If views on
1668 * the page can be reused, then they should be updated within this method.
1669 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001670 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001671
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001672 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001673 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001674 }
1675 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001676 invalidatePageData(currentPage, false);
1677 }
1678 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001679 if (!mIsDataReady) {
1680 return;
1681 }
1682
Michael Jurka0142d492010-08-25 17:46:15 -07001683 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001684 // Force all scrolling-related behavior to end
1685 mScroller.forceFinished(true);
1686 mNextPage = INVALID_PAGE;
1687
Michael Jurka0142d492010-08-25 17:46:15 -07001688 // Update all the pages
1689 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001690
Winson Chung5a808352011-06-27 19:08:49 -07001691 // We must force a measure after we've loaded the pages to update the content width and
1692 // to determine the full scroll width
1693 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1694 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1695
1696 // Set a new page as the current page if necessary
1697 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001698 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001699 }
1700
Michael Jurka0142d492010-08-25 17:46:15 -07001701 // Mark each of the pages as dirty
1702 final int count = getChildCount();
1703 mDirtyPageContent.clear();
1704 for (int i = 0; i < count; ++i) {
1705 mDirtyPageContent.add(true);
1706 }
1707
1708 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001709 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001710 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001711 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001712 }
Winson Chung007c6982011-06-14 13:27:53 -07001713
Adam Cohen21b41102011-11-01 17:29:52 -07001714 protected ImageView getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001715 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1716 // found
1717 if (mHasScrollIndicator && mScrollIndicator == null) {
1718 ViewGroup parent = (ViewGroup) getParent();
1719 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1720 mHasScrollIndicator = mScrollIndicator != null;
1721 if (mHasScrollIndicator) {
1722 mScrollIndicator.setVisibility(View.VISIBLE);
1723 }
1724 }
1725 return mScrollIndicator;
1726 }
1727
1728 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001729 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001730 }
1731
Winson Chung5a808352011-06-27 19:08:49 -07001732 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1733 @Override
1734 public void run() {
1735 hideScrollingIndicator(false);
1736 }
1737 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001738 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001739 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001740 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001741 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001742 }
1743
Michael Jurka430e8a52011-08-08 15:52:14 -07001744 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001745 if (getChildCount() <= 1) return;
1746 if (!isScrollingIndicatorEnabled()) return;
1747
1748 getScrollingIndicator();
1749 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001750 // Fade the indicator in
1751 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001752 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001753 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001754 if (immediately) {
1755 mScrollIndicator.setAlpha(1f);
1756 } else {
1757 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1758 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1759 mScrollIndicatorAnimator.start();
1760 }
Winson Chung007c6982011-06-14 13:27:53 -07001761 }
1762 }
1763
Adam Cohen21b41102011-11-01 17:29:52 -07001764 protected void cancelScrollingIndicatorAnimations() {
1765 if (mScrollIndicatorAnimator != null) {
1766 mScrollIndicatorAnimator.cancel();
1767 }
1768 }
1769
Winson Chung007c6982011-06-14 13:27:53 -07001770 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001771 if (getChildCount() <= 1) return;
1772 if (!isScrollingIndicatorEnabled()) return;
1773
1774 getScrollingIndicator();
1775 if (mScrollIndicator != null) {
1776 // Fade the indicator out
1777 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001778 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001779 if (immediately) {
1780 mScrollIndicator.setVisibility(View.GONE);
1781 mScrollIndicator.setAlpha(0f);
1782 } else {
1783 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1784 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1785 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1786 private boolean cancelled = false;
1787 @Override
1788 public void onAnimationCancel(android.animation.Animator animation) {
1789 cancelled = true;
1790 }
1791 @Override
1792 public void onAnimationEnd(Animator animation) {
1793 if (!cancelled) {
1794 mScrollIndicator.setVisibility(View.GONE);
1795 }
1796 }
1797 });
1798 mScrollIndicatorAnimator.start();
1799 }
Winson Chung007c6982011-06-14 13:27:53 -07001800 }
1801 }
1802
Winson Chung32174c82011-07-19 15:47:55 -07001803 /**
1804 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1805 * fill its space on the track or not.
1806 */
1807 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001808 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001809 }
1810
Winson Chung007c6982011-06-14 13:27:53 -07001811 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001812 if (getChildCount() <= 1) return;
1813 if (!isScrollingIndicatorEnabled()) return;
1814
1815 getScrollingIndicator();
1816 if (mScrollIndicator != null) {
1817 updateScrollingIndicatorPosition();
1818 }
1819 }
1820
Winson Chung007c6982011-06-14 13:27:53 -07001821 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001822 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001823 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001824 int numPages = getChildCount();
1825 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001826 int lastChildIndex = Math.max(0, getChildCount() - 1);
1827 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001828 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001829 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1830 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001831
Winson Chungae890b82011-09-13 18:08:54 -07001832 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001833 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001834 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001835 if (hasElasticScrollIndicator()) {
1836 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1837 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1838 mScrollIndicator.requestLayout();
1839 }
1840 } else {
1841 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1842 indicatorPos += indicatorCenterOffset;
1843 }
Winson Chung007c6982011-06-14 13:27:53 -07001844 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001845 mScrollIndicator.invalidate();
1846 }
1847
Winson Chung3ac74c52011-06-30 17:39:37 -07001848 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001849 }
1850
1851 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001852 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001853
1854 /* Accessibility */
1855 @Override
1856 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1857 super.onInitializeAccessibilityNodeInfo(info);
1858 info.setScrollable(true);
1859 }
1860
1861 @Override
1862 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1863 super.onInitializeAccessibilityEvent(event);
1864 event.setScrollable(true);
1865 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1866 event.setFromIndex(mCurrentPage);
1867 event.setToIndex(mCurrentPage);
1868 event.setItemCount(getChildCount());
1869 }
1870 }
1871
Winson Chung6a0f57d2011-06-29 20:10:49 -07001872 protected String getCurrentPageDescription() {
1873 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1874 return String.format(mContext.getString(R.string.default_scroll_format),
1875 page + 1, getChildCount());
1876 }
Winson Chungd11265e2011-08-30 13:37:23 -07001877
1878 @Override
1879 public boolean onHoverEvent(android.view.MotionEvent event) {
1880 return true;
1881 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001882}