blob: 0ba4f0b47ec26bf3abb61904ae5fddbedeb8f352 [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;
Michael Jurkadde558b2011-11-09 22:09:06 -0800130 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700131
Michael Jurka8c920dd2011-01-20 14:16:56 -0800132 // parameter that adjusts the layout to be optimized for pages with that scale factor
Michael Jurkad3ef3062010-11-23 16:23:58 -0800133 protected float mLayoutScale = 1.0f;
134
Michael Jurka5f1c5092010-09-03 14:15:02 -0700135 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700136
Michael Jurka5f1c5092010-09-03 14:15:02 -0700137 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700138
Winson Chung86f77532010-08-24 11:08:22 -0700139 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Winson Chung86f77532010-08-24 11:08:22 -0700141 private ArrayList<Boolean> mDirtyPageContent;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700143 // choice modes
144 protected static final int CHOICE_MODE_NONE = 0;
145 protected static final int CHOICE_MODE_SINGLE = 1;
146 // Multiple selection mode is not supported by all Launcher actions atm
147 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700148
Michael Jurkae17e19c2010-09-28 11:01:39 -0700149 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700150 private ActionMode mActionMode;
151
Michael Jurka0142d492010-08-25 17:46:15 -0700152 // If true, syncPages and syncPageItems will be called to refresh pages
153 protected boolean mContentIsRefreshable = true;
154
155 // If true, modify alpha of neighboring pages as user scrolls left/right
156 protected boolean mFadeInAdjacentScreens = true;
157
158 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
159 // to switch to a new page
160 protected boolean mUsePagingTouchSlop = true;
161
162 // If true, the subclass should directly update mScrollX itself in its computeScroll method
163 // (SmoothPagedView does this)
164 protected boolean mDeferScrollUpdate = false;
165
Patrick Dubroy1262e362010-10-06 15:49:50 -0700166 protected boolean mIsPageMoving = false;
167
Winson Chungf0ea4d32011-06-06 14:27:16 -0700168 // All syncs and layout passes are deferred until data is ready.
169 protected boolean mIsDataReady = false;
170
Winson Chung007c6982011-06-14 13:27:53 -0700171 // Scrolling indicator
Winson Chungbb6f6a52011-07-25 17:55:44 -0700172 private ValueAnimator mScrollIndicatorAnimator;
Winson Chung007c6982011-06-14 13:27:53 -0700173 private ImageView mScrollIndicator;
Winson Chungf5f8cef2011-07-22 11:16:13 -0700174 private int mScrollIndicatorPaddingLeft;
175 private int mScrollIndicatorPaddingRight;
Winson Chung007c6982011-06-14 13:27:53 -0700176 private boolean mHasScrollIndicator = true;
Winson Chunga6427b12011-07-27 10:53:39 -0700177 protected static final int sScrollIndicatorFadeInDuration = 150;
178 protected static final int sScrollIndicatorFadeOutDuration = 650;
179 protected static final int sScrollIndicatorFlashDuration = 650;
Winson Chung007c6982011-06-14 13:27:53 -0700180
Winson Chungb44b5242011-06-13 11:32:14 -0700181 // If set, will defer loading associated pages until the scrolling settles
Winson Chung4e076542011-06-23 13:04:10 -0700182 private boolean mDeferLoadAssociatedPagesUntilScrollCompletes;
Winson Chungb44b5242011-06-13 11:32:14 -0700183
Winson Chung86f77532010-08-24 11:08:22 -0700184 public interface PageSwitchListener {
185 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700186 }
187
Winson Chung321e9ee2010-08-09 13:37:56 -0700188 public PagedView(Context context) {
189 this(context, null);
190 }
191
192 public PagedView(Context context, AttributeSet attrs) {
193 this(context, attrs, 0);
194 }
195
196 public PagedView(Context context, AttributeSet attrs, int defStyle) {
197 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700198 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700199
Adam Cohen9c4949e2010-10-05 12:27:22 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.PagedView, defStyle, 0);
202 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
203 mPageLayoutPaddingTop = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800204 R.styleable.PagedView_pageLayoutPaddingTop, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800206 R.styleable.PagedView_pageLayoutPaddingBottom, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700207 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800208 R.styleable.PagedView_pageLayoutPaddingLeft, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700209 mPageLayoutPaddingRight = a.getDimensionPixelSize(
Winson Chung1908d072011-02-24 18:09:44 -0800210 R.styleable.PagedView_pageLayoutPaddingRight, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700211 mPageLayoutWidthGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 R.styleable.PagedView_pageLayoutWidthGap, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700213 mPageLayoutHeightGap = a.getDimensionPixelSize(
Winson Chung7d7541e2011-09-16 20:14:36 -0700214 R.styleable.PagedView_pageLayoutHeightGap, 0);
Winson Chungf5f8cef2011-07-22 11:16:13 -0700215 mScrollIndicatorPaddingLeft =
216 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
217 mScrollIndicatorPaddingRight =
218 a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700219 a.recycle();
220
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700222 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224
225 /**
226 * Initializes various states for this workspace.
227 */
Michael Jurka0142d492010-08-25 17:46:15 -0700228 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700229 mDirtyPageContent = new ArrayList<Boolean>();
230 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800231 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700232 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700233 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700234
235 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
236 mTouchSlop = configuration.getScaledTouchSlop();
237 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
238 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700239 mDensity = getResources().getDisplayMetrics().density;
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 }
241
Winson Chung86f77532010-08-24 11:08:22 -0700242 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
243 mPageSwitchListener = pageSwitchListener;
244 if (mPageSwitchListener != null) {
245 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 }
247 }
248
249 /**
Winson Chungf0ea4d32011-06-06 14:27:16 -0700250 * Called by subclasses to mark that data is ready, and that we can begin loading and laying
251 * out pages.
252 */
253 protected void setDataIsReady() {
254 mIsDataReady = true;
255 }
256 protected boolean isDataReady() {
257 return mIsDataReady;
258 }
259
260 /**
Winson Chung86f77532010-08-24 11:08:22 -0700261 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 *
Winson Chung86f77532010-08-24 11:08:22 -0700263 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 */
Winson Chung86f77532010-08-24 11:08:22 -0700265 int getCurrentPage() {
266 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 }
268
Winson Chung86f77532010-08-24 11:08:22 -0700269 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 return getChildCount();
271 }
272
Winson Chung86f77532010-08-24 11:08:22 -0700273 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 return getChildAt(index);
275 }
276
Adam Cohenae4f1552011-10-20 00:15:42 -0700277 protected int indexToPage(int index) {
278 return index;
279 }
280
Winson Chung321e9ee2010-08-09 13:37:56 -0700281 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800282 * Updates the scroll of the current page immediately to its final scroll position. We use this
283 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
284 * the previous tab page.
285 */
286 protected void updateCurrentPageScroll() {
287 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
288 scrollTo(newX, 0);
289 mScroller.setFinalX(newX);
290 }
291
292 /**
Winson Chung86f77532010-08-24 11:08:22 -0700293 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 */
Winson Chung86f77532010-08-24 11:08:22 -0700295 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800296 if (!mScroller.isFinished()) {
297 mScroller.abortAnimation();
298 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800299 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
300 // the default
301 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800302 return;
303 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700304
Winson Chung86f77532010-08-24 11:08:22 -0700305 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800306 updateCurrentPageScroll();
Winson Chung5a808352011-06-27 19:08:49 -0700307 updateScrollingIndicator();
Winson Chung86f77532010-08-24 11:08:22 -0700308 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800309 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 }
311
Michael Jurka0142d492010-08-25 17:46:15 -0700312 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700313 if (mPageSwitchListener != null) {
314 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 }
316 }
317
Michael Jurkace7e05f2011-02-01 22:02:35 -0800318 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700319 if (!mIsPageMoving) {
320 mIsPageMoving = true;
321 onPageBeginMoving();
322 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700323 }
324
Michael Jurkace7e05f2011-02-01 22:02:35 -0800325 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700326 if (mIsPageMoving) {
327 mIsPageMoving = false;
328 onPageEndMoving();
329 }
Michael Jurka0142d492010-08-25 17:46:15 -0700330 }
331
Adam Cohen26976d92011-03-22 15:33:33 -0700332 protected boolean isPageMoving() {
333 return mIsPageMoving;
334 }
335
Michael Jurka0142d492010-08-25 17:46:15 -0700336 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700337 protected void onPageBeginMoving() {
Michael Jurka430e8a52011-08-08 15:52:14 -0700338 showScrollingIndicator(false);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700339 }
340
341 // a method that subclasses can override to add behavior
342 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700343 hideScrollingIndicator(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700344 }
345
Winson Chung321e9ee2010-08-09 13:37:56 -0700346 /**
Winson Chung86f77532010-08-24 11:08:22 -0700347 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 *
349 * @param l The listener used to respond to long clicks.
350 */
351 @Override
352 public void setOnLongClickListener(OnLongClickListener l) {
353 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700354 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700355 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700356 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 }
358 }
359
360 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800361 public void scrollBy(int x, int y) {
362 scrollTo(mUnboundedScrollX + x, mScrollY + y);
363 }
364
365 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700366 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800367 mUnboundedScrollX = x;
368
369 if (x < 0) {
370 super.scrollTo(0, y);
371 if (mAllowOverScroll) {
372 overScroll(x);
373 }
374 } else if (x > mMaxScrollX) {
375 super.scrollTo(mMaxScrollX, y);
376 if (mAllowOverScroll) {
377 overScroll(x - mMaxScrollX);
378 }
379 } else {
380 super.scrollTo(x, y);
381 }
382
Michael Jurka0142d492010-08-25 17:46:15 -0700383 mTouchX = x;
384 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
385 }
386
387 // we moved this functionality to a helper function so SmoothPagedView can reuse it
388 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700390 // Don't bother scrolling if the page does not need to be moved
391 if (mScrollX != mScroller.getCurrX() || mScrollY != mScroller.getCurrY()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700392 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
393 }
Michael Jurka0142d492010-08-25 17:46:15 -0700394 invalidate();
395 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700396 } else if (mNextPage != INVALID_PAGE) {
397 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700398 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700399 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700400
401 // Load the associated pages if necessary
Winson Chung4e076542011-06-23 13:04:10 -0700402 if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
Winson Chungb44b5242011-06-13 11:32:14 -0700403 loadAssociatedPages(mCurrentPage);
Winson Chung4e076542011-06-23 13:04:10 -0700404 mDeferLoadAssociatedPagesUntilScrollCompletes = false;
Winson Chungb44b5242011-06-13 11:32:14 -0700405 }
406
Adam Cohen73aa9752010-11-24 16:26:58 -0800407 // We don't want to trigger a page end moving unless the page has settled
408 // and the user has stopped scrolling
409 if (mTouchState == TOUCH_STATE_REST) {
410 pageEndMoving();
411 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700412
413 // Notify the user when the page changes
414 if (AccessibilityManager.getInstance(getContext()).isEnabled()) {
415 AccessibilityEvent ev =
416 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
417 ev.getText().add(getCurrentPageDescription());
418 sendAccessibilityEventUnchecked(ev);
419 }
Michael Jurka0142d492010-08-25 17:46:15 -0700420 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700421 }
Michael Jurka0142d492010-08-25 17:46:15 -0700422 return false;
423 }
424
425 @Override
426 public void computeScroll() {
427 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
429
430 @Override
431 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700432 if (!mIsDataReady) {
433 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
434 return;
435 }
436
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
438 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
439 if (widthMode != MeasureSpec.EXACTLY) {
440 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
441 }
442
Adam Lesinski6b879f02010-11-04 16:15:23 -0700443 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
444 * of the All apps view on XLarge displays to not take up more space then it needs. Width
445 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
446 * each page to have the same width.
447 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700449 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
450 int maxChildHeight = 0;
451
452 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chungea359c62011-08-03 17:06:35 -0700453 final int horizontalPadding = mPaddingLeft + mPaddingRight;
Winson Chung321e9ee2010-08-09 13:37:56 -0700454
Michael Jurka36fcb742011-04-06 17:08:58 -0700455
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700457 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700458 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 final int childCount = getChildCount();
460 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700461 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700462 final View child = getPageAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700463 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
464
465 int childWidthMode;
466 if (lp.width == LayoutParams.WRAP_CONTENT) {
467 childWidthMode = MeasureSpec.AT_MOST;
468 } else {
469 childWidthMode = MeasureSpec.EXACTLY;
470 }
471
472 int childHeightMode;
473 if (lp.height == LayoutParams.WRAP_CONTENT) {
474 childHeightMode = MeasureSpec.AT_MOST;
475 } else {
476 childHeightMode = MeasureSpec.EXACTLY;
477 }
478
479 final int childWidthMeasureSpec =
Winson Chungea359c62011-08-03 17:06:35 -0700480 MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700481 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700482 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700483
484 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700485 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
Winson Chung785d2eb2011-04-14 16:08:02 -0700486 if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", "
487 + child.getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700488 }
489
490 if (heightMode == MeasureSpec.AT_MOST) {
491 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 }
Winson Chungae890b82011-09-13 18:08:54 -0700493
Winson Chungae890b82011-09-13 18:08:54 -0700494 setMeasuredDimension(widthSize, heightSize);
495
Adam Cohen25b29952011-11-02 14:49:06 -0700496 // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
497 // We also wait until we set the measured dimensions before flushing the cache as well, to
498 // ensure that the cache is filled with good values.
499 invalidateCachedOffsets();
500 updateScrollingIndicatorPosition();
501
Adam Cohenfaa28302010-11-19 12:02:18 -0800502 if (childCount > 0) {
503 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
504 } else {
505 mMaxScrollX = 0;
506 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 }
508
Michael Jurka8c920dd2011-01-20 14:16:56 -0800509 protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800510 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
511 int delta = newX - mScrollX;
512
Michael Jurka8c920dd2011-01-20 14:16:56 -0800513 final int pageCount = getChildCount();
514 for (int i = 0; i < pageCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700515 View page = (View) getPageAt(i);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800516 page.setX(page.getX() + delta);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800517 }
518 setCurrentPage(newCurrentPage);
519 }
520
Michael Jurka8c920dd2011-01-20 14:16:56 -0800521 // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a
522 // 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 -0800523 // tightens the layout accordingly
524 public void setLayoutScale(float childrenScale) {
525 mLayoutScale = childrenScale;
Adam Cohen73894962011-10-31 13:17:17 -0700526 invalidateCachedOffsets();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800527
528 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
529 int childCount = getChildCount();
530 float childrenX[] = new float[childCount];
531 float childrenY[] = new float[childCount];
532 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700533 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800534 childrenX[i] = child.getX();
535 childrenY[i] = child.getY();
536 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700537 // Trigger a full re-layout (never just call onLayout directly!)
538 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
539 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
540 requestLayout();
541 measure(widthSpec, heightSpec);
542 layout(mLeft, mTop, mRight, mBottom);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800543 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700544 final View child = getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800545 child.setX(childrenX[i]);
546 child.setY(childrenY[i]);
547 }
Winson Chungb26f3d62011-06-02 10:49:29 -0700548
Michael Jurkad3ef3062010-11-23 16:23:58 -0800549 // Also, the page offset has changed (since the pages are now smaller);
550 // update the page offset, but again preserving absolute X and Y coordinates
Michael Jurka8c920dd2011-01-20 14:16:56 -0800551 scrollToNewPageWithoutMovingPages(mCurrentPage);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800552 }
553
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700555 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700556 if (!mIsDataReady) {
557 return;
558 }
559
Winson Chung785d2eb2011-04-14 16:08:02 -0700560 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700561 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 final int childCount = getChildCount();
563 int childLeft = 0;
564 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700565 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
566 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700567 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700568
569 // Calculate the variable page spacing if necessary
570 if (mPageSpacing < 0) {
571 mPageSpacing = ((right - left) - getChildAt(0).getMeasuredWidth()) / 2;
572 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 }
574
575 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700576 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800578 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700579 final int childHeight = child.getMeasuredHeight();
580 int childTop = mPaddingTop;
581 if (mCenterPagesVertically) {
582 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
583 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800584
Winson Chung785d2eb2011-04-14 16:08:02 -0700585 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700586 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800587 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700588 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 }
590 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700591
592 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
593 setHorizontalScrollBarEnabled(false);
594 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
595 scrollTo(newX, 0);
596 mScroller.setFinalX(newX);
597 setHorizontalScrollBarEnabled(true);
598 mFirstLayout = false;
599 }
600
Michael Jurkad3ef3062010-11-23 16:23:58 -0800601 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
602 mFirstLayout = false;
603 }
Winson Chunge3193b92010-09-10 11:44:42 -0700604 }
605
Adam Cohenf34bab52010-09-30 14:11:56 -0700606 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700607 if (isScrollingIndicatorEnabled()) {
608 updateScrollingIndicator();
609 }
610 if (mFadeInAdjacentScreens) {
611 for (int i = 0; i < getChildCount(); i++) {
612 View child = getChildAt(i);
613 if (child != null) {
614 float scrollProgress = getScrollProgress(screenCenter, child, i);
615 float alpha = 1 - Math.abs(scrollProgress);
616 child.setFastAlpha(alpha);
617 child.fastInvalidate();
618 }
619 }
620 invalidate();
621 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700622 }
623
Winson Chunge3193b92010-09-10 11:44:42 -0700624 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700625 protected void onViewAdded(View child) {
626 super.onViewAdded(child);
627
628 // This ensures that when children are added, they get the correct transforms / alphas
629 // in accordance with any scroll effects.
630 mForceScreenScrolled = true;
631 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700632 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700633 }
634
Adam Cohen73894962011-10-31 13:17:17 -0700635 protected void invalidateCachedOffsets() {
636 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700637 if (count == 0) {
638 mChildOffsets = null;
639 mChildRelativeOffsets = null;
640 mChildOffsetsWithLayoutScale = null;
641 return;
642 }
Adam Cohen73894962011-10-31 13:17:17 -0700643
644 mChildOffsets = new int[count];
645 mChildRelativeOffsets = new int[count];
646 mChildOffsetsWithLayoutScale = new int[count];
647 for (int i = 0; i < count; i++) {
648 mChildOffsets[i] = -1;
649 mChildRelativeOffsets[i] = -1;
650 mChildOffsetsWithLayoutScale[i] = -1;
651 }
652 }
653
654 protected int getChildOffset(int index) {
655 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
656 mChildOffsets : mChildOffsetsWithLayoutScale;
657
658 if (childOffsets != null && childOffsets[index] != -1) {
659 return childOffsets[index];
660 } else {
661 if (getChildCount() == 0)
662 return 0;
663
664 int offset = getRelativeChildOffset(0);
665 for (int i = 0; i < index; ++i) {
666 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
667 }
668 if (childOffsets != null) {
669 childOffsets[index] = offset;
670 }
671 return offset;
672 }
673 }
674
675 protected int getRelativeChildOffset(int index) {
676 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
677 return mChildRelativeOffsets[index];
678 } else {
679 final int padding = mPaddingLeft + mPaddingRight;
680 final int offset = mPaddingLeft +
681 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
682 if (mChildRelativeOffsets != null) {
683 mChildRelativeOffsets[index] = offset;
684 }
685 return offset;
686 }
687 }
688
689 protected int getScaledRelativeChildOffset(int index) {
690 final int padding = mPaddingLeft + mPaddingRight;
691 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
692 getScaledMeasuredWidth(getPageAt(index))) / 2;
693 return offset;
694 }
695
696 protected int getScaledMeasuredWidth(View child) {
697 // This functions are called enough times that it actually makes a difference in the
698 // profiler -- so just inline the max() here
699 final int measuredWidth = child.getMeasuredWidth();
700 final int minWidth = mMinimumWidth;
701 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
702 return (int) (maxWidth * mLayoutScale + 0.5f);
703 }
704
Michael Jurkadde558b2011-11-09 22:09:06 -0800705 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700706 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700707 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700708 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700709 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700711 int leftScreen = 0;
712 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700713 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700714 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700715 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700716 }
717 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700718 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700719 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700720 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700721 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800722 range[0] = leftScreen;
723 range[1] = rightScreen;
724 } else {
725 range[0] = -1;
726 range[1] = -1;
727 }
728 }
729
730 @Override
731 protected void dispatchDraw(Canvas canvas) {
732 int halfScreenSize = getMeasuredWidth() / 2;
733 int screenCenter = mScrollX + halfScreenSize;
734
735 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
736 screenScrolled(screenCenter);
737 mLastScreenCenter = screenCenter;
738 mForceScreenScrolled = false;
739 }
740
741 // Find out which screens are visible; as an optimization we only call draw on them
742 final int pageCount = getChildCount();
743 if (pageCount > 0) {
744 getVisiblePages(mTempVisiblePagesRange);
745 final int leftScreen = mTempVisiblePagesRange[0];
746 final int rightScreen = mTempVisiblePagesRange[1];
Michael Jurka0142d492010-08-25 17:46:15 -0700747
Michael Jurkac4fb9172010-09-02 17:19:20 -0700748 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800749 // Clip to the bounds
750 canvas.save();
751 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
752 mScrollY + mBottom - mTop);
753
Adam Cohen22f823d2011-09-01 17:22:18 -0700754 for (int i = rightScreen; i >= leftScreen; i--) {
755 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700756 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800757 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700758 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 }
760
761 @Override
762 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700763 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700764 if (page != mCurrentPage || !mScroller.isFinished()) {
765 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700766 return true;
767 }
768 return false;
769 }
770
771 @Override
772 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700773 int focusablePage;
774 if (mNextPage != INVALID_PAGE) {
775 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700777 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 }
Winson Chung86f77532010-08-24 11:08:22 -0700779 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700780 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700781 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700782 }
783 return false;
784 }
785
786 @Override
787 public boolean dispatchUnhandledMove(View focused, int direction) {
788 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700789 if (getCurrentPage() > 0) {
790 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 return true;
792 }
793 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700794 if (getCurrentPage() < getPageCount() - 1) {
795 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 return true;
797 }
798 }
799 return super.dispatchUnhandledMove(focused, direction);
800 }
801
802 @Override
803 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700804 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
805 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700806 }
807 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700808 if (mCurrentPage > 0) {
809 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 }
811 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700812 if (mCurrentPage < getPageCount() - 1) {
813 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 }
815 }
816 }
817
818 /**
819 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700820 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 *
Winson Chung86f77532010-08-24 11:08:22 -0700822 * This happens when live folders requery, and if they're off page, they
823 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 */
825 @Override
826 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700827 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 View v = focused;
829 while (true) {
830 if (v == current) {
831 super.focusableViewAvailable(focused);
832 return;
833 }
834 if (v == this) {
835 return;
836 }
837 ViewParent parent = v.getParent();
838 if (parent instanceof View) {
839 v = (View)v.getParent();
840 } else {
841 return;
842 }
843 }
844 }
845
846 /**
847 * {@inheritDoc}
848 */
849 @Override
850 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
851 if (disallowIntercept) {
852 // We need to make sure to cancel our long press if
853 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700854 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700855 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700856 }
857 super.requestDisallowInterceptTouchEvent(disallowIntercept);
858 }
859
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800860 /**
861 * Return true if a tap at (x, y) should trigger a flip to the previous page.
862 */
863 protected boolean hitsPreviousPage(float x, float y) {
864 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
865 }
866
867 /**
868 * Return true if a tap at (x, y) should trigger a flip to the next page.
869 */
870 protected boolean hitsNextPage(float x, float y) {
871 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
872 }
873
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 @Override
875 public boolean onInterceptTouchEvent(MotionEvent ev) {
876 /*
877 * This method JUST determines whether we want to intercept the motion.
878 * If we return true, onTouchEvent will be called and we do the actual
879 * scrolling there.
880 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800881 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700882
Winson Chung45e1d6e2010-11-09 17:19:49 -0800883 // Skip touch handling if there are no pages to swipe
884 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
885
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 /*
887 * Shortcut the most recurring case: the user is in the dragging
888 * state and he is moving his finger. We want to intercept this
889 * motion.
890 */
891 final int action = ev.getAction();
892 if ((action == MotionEvent.ACTION_MOVE) &&
893 (mTouchState == TOUCH_STATE_SCROLLING)) {
894 return true;
895 }
896
Winson Chung321e9ee2010-08-09 13:37:56 -0700897 switch (action & MotionEvent.ACTION_MASK) {
898 case MotionEvent.ACTION_MOVE: {
899 /*
900 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
901 * whether the user has moved far enough from his original down touch.
902 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700903 if (mActivePointerId != INVALID_POINTER) {
904 determineScrollingStart(ev);
905 break;
906 }
907 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
908 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
909 // i.e. fall through to the next case (don't break)
910 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
911 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 }
913
914 case MotionEvent.ACTION_DOWN: {
915 final float x = ev.getX();
916 final float y = ev.getY();
917 // Remember location of down touch
918 mDownMotionX = x;
919 mLastMotionX = x;
920 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800921 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800922 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 mActivePointerId = ev.getPointerId(0);
924 mAllowLongPress = true;
925
926 /*
927 * If being flinged and user touches the screen, initiate drag;
928 * otherwise don't. mScroller.isFinished should be false when
929 * being flinged.
930 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700931 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700932 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
933 if (finishedScrolling) {
934 mTouchState = TOUCH_STATE_REST;
935 mScroller.abortAnimation();
936 } else {
937 mTouchState = TOUCH_STATE_SCROLLING;
938 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700939
Winson Chung86f77532010-08-24 11:08:22 -0700940 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700941 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800942 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800944 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700945 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800946 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 mTouchState = TOUCH_STATE_NEXT_PAGE;
948 }
949 }
950 }
951 break;
952 }
953
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800955 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700956 mTouchState = TOUCH_STATE_REST;
957 mAllowLongPress = false;
958 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800959 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 break;
961
962 case MotionEvent.ACTION_POINTER_UP:
963 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800964 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 break;
966 }
967
968 /*
969 * The only time we want to intercept motion events is if we are in the
970 * drag mode.
971 */
972 return mTouchState != TOUCH_STATE_REST;
973 }
974
Winson Chung80baf5a2010-08-09 16:03:15 -0700975 protected void animateClickFeedback(View v, final Runnable r) {
976 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800977 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
978 loadAnimator(mContext, R.anim.paged_view_click_feedback);
979 anim.setTarget(v);
980 anim.addListener(new AnimatorListenerAdapter() {
981 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700982 r.run();
983 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700984 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800985 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700986 }
987
Adam Cohenf8d28232011-02-01 21:47:00 -0800988 protected void determineScrollingStart(MotionEvent ev) {
989 determineScrollingStart(ev, 1.0f);
990 }
991
Winson Chung321e9ee2010-08-09 13:37:56 -0700992 /*
993 * Determines if we should change the touch state to start scrolling after the
994 * user moves their touch point too far.
995 */
Adam Cohenf8d28232011-02-01 21:47:00 -0800996 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 /*
998 * Locally do absolute value. mLastMotionX is set to the y value
999 * of the down event.
1000 */
1001 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001002 if (pointerIndex == -1) {
1003 return;
1004 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001005 final float x = ev.getX(pointerIndex);
1006 final float y = ev.getY(pointerIndex);
1007 final int xDiff = (int) Math.abs(x - mLastMotionX);
1008 final int yDiff = (int) Math.abs(y - mLastMotionY);
1009
Adam Cohenf8d28232011-02-01 21:47:00 -08001010 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 boolean xPaged = xDiff > mPagingTouchSlop;
1012 boolean xMoved = xDiff > touchSlop;
1013 boolean yMoved = yDiff > touchSlop;
1014
Adam Cohenf8d28232011-02-01 21:47:00 -08001015 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001016 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 // Scroll if the user moved far enough along the X axis
1018 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001019 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001021 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001022 mTouchX = mScrollX;
1023 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1024 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 }
1026 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001027 cancelCurrentPageLongPress();
1028 }
1029 }
1030
1031 protected void cancelCurrentPageLongPress() {
1032 if (mAllowLongPress) {
1033 mAllowLongPress = false;
1034 // Try canceling the long press. It could also have been scheduled
1035 // by a distant descendant, so use the mAllowLongPress flag to block
1036 // everything
1037 final View currentPage = getPageAt(mCurrentPage);
1038 if (currentPage != null) {
1039 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 }
1041 }
1042 }
1043
Adam Cohenb5ba0972011-09-07 18:02:31 -07001044 protected float getScrollProgress(int screenCenter, View v, int page) {
1045 final int halfScreenSize = getMeasuredWidth() / 2;
1046
1047 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1048 int delta = screenCenter - (getChildOffset(page) -
1049 getRelativeChildOffset(page) + halfScreenSize);
1050
1051 float scrollProgress = delta / (totalDistance * 1.0f);
1052 scrollProgress = Math.min(scrollProgress, 1.0f);
1053 scrollProgress = Math.max(scrollProgress, -1.0f);
1054 return scrollProgress;
1055 }
1056
Adam Cohene0f66b52010-11-23 15:06:07 -08001057 // This curve determines how the effect of scrolling over the limits of the page dimishes
1058 // as the user pulls further and further from the bounds
1059 private float overScrollInfluenceCurve(float f) {
1060 f -= 1.0f;
1061 return f * f * f + 1.0f;
1062 }
1063
Adam Cohenb5ba0972011-09-07 18:02:31 -07001064 protected void acceleratedOverScroll(float amount) {
1065 int screenSize = getMeasuredWidth();
1066
1067 // We want to reach the max over scroll effect when the user has
1068 // over scrolled half the size of the screen
1069 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1070
1071 if (f == 0) return;
1072
1073 // Clamp this factor, f, to -1 < f < 1
1074 if (Math.abs(f) >= 1) {
1075 f /= Math.abs(f);
1076 }
1077
1078 int overScrollAmount = (int) Math.round(f * screenSize);
1079 if (amount < 0) {
1080 mScrollX = overScrollAmount;
1081 } else {
1082 mScrollX = mMaxScrollX + overScrollAmount;
1083 }
1084 invalidate();
1085 }
1086
1087 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001088 int screenSize = getMeasuredWidth();
1089
1090 float f = (amount / screenSize);
1091
1092 if (f == 0) return;
1093 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1094
Adam Cohen7bfc9792011-01-28 13:52:37 -08001095 // Clamp this factor, f, to -1 < f < 1
1096 if (Math.abs(f) >= 1) {
1097 f /= Math.abs(f);
1098 }
1099
Adam Cohene0f66b52010-11-23 15:06:07 -08001100 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001101 if (amount < 0) {
1102 mScrollX = overScrollAmount;
1103 } else {
1104 mScrollX = mMaxScrollX + overScrollAmount;
1105 }
1106 invalidate();
1107 }
1108
Adam Cohenb5ba0972011-09-07 18:02:31 -07001109 protected void overScroll(float amount) {
1110 dampedOverScroll(amount);
1111 }
1112
Michael Jurkac5b262c2011-01-12 20:24:50 -08001113 protected float maxOverScroll() {
1114 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001115 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001116 float f = 1.0f;
1117 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1118 return OVERSCROLL_DAMP_FACTOR * f;
1119 }
1120
Winson Chung321e9ee2010-08-09 13:37:56 -07001121 @Override
1122 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001123 // Skip touch handling if there are no pages to swipe
1124 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1125
Michael Jurkab8f06722010-10-10 15:58:46 -07001126 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001127
1128 final int action = ev.getAction();
1129
1130 switch (action & MotionEvent.ACTION_MASK) {
1131 case MotionEvent.ACTION_DOWN:
1132 /*
1133 * If being flinged and user touches, stop the fling. isFinished
1134 * will be false if being flinged.
1135 */
1136 if (!mScroller.isFinished()) {
1137 mScroller.abortAnimation();
1138 }
1139
1140 // Remember where the motion event started
1141 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001142 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001143 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001144 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001145 if (mTouchState == TOUCH_STATE_SCROLLING) {
1146 pageBeginMoving();
1147 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001148 break;
1149
1150 case MotionEvent.ACTION_MOVE:
1151 if (mTouchState == TOUCH_STATE_SCROLLING) {
1152 // Scroll to follow the motion event
1153 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1154 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001155 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001156
Adam Cohenaefd4e12011-02-14 16:39:38 -08001157 mTotalMotionX += Math.abs(deltaX);
1158
Winson Chungc0844aa2011-02-02 15:25:58 -08001159 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1160 // keep the remainder because we are actually testing if we've moved from the last
1161 // scrolled position (which is discrete).
1162 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001163 mTouchX += deltaX;
1164 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1165 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001166 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001167 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001168 } else {
1169 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001170 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001171 mLastMotionX = x;
1172 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001173 } else {
1174 awakenScrollBars();
1175 }
Adam Cohen564976a2010-10-13 18:52:07 -07001176 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001177 determineScrollingStart(ev);
1178 }
1179 break;
1180
1181 case MotionEvent.ACTION_UP:
1182 if (mTouchState == TOUCH_STATE_SCROLLING) {
1183 final int activePointerId = mActivePointerId;
1184 final int pointerIndex = ev.findPointerIndex(activePointerId);
1185 final float x = ev.getX(pointerIndex);
1186 final VelocityTracker velocityTracker = mVelocityTracker;
1187 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1188 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001189 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001190 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001191 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001192
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001193 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1194
Adam Cohenaefd4e12011-02-14 16:39:38 -08001195 // In the case that the page is moved far to one direction and then is flung
1196 // in the opposite direction, we use a threshold to determine whether we should
1197 // just return to the starting page, or if we should skip one further.
1198 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001199 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001200 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001201 Math.signum(velocityX) != Math.signum(deltaX)) {
1202 returnToOriginalPage = true;
1203 }
1204
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001205 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001206 Math.abs(velocityX) > snapVelocity;
1207
1208 int finalPage;
1209 // We give flings precedence over large moves, which is why we short-circuit our
1210 // test for a large move if a fling has been registered. That is, a large
1211 // move to the left and fling to the right will register as a fling to the right.
1212 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1213 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1214 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1215 snapToPageWithVelocity(finalPage, velocityX);
1216 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1217 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001218 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001219 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1220 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001221 } else {
1222 snapToDestination();
1223 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001224 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 // at this point we have not moved beyond the touch slop
1226 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1227 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001228 int nextPage = Math.max(0, mCurrentPage - 1);
1229 if (nextPage != mCurrentPage) {
1230 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 } else {
1232 snapToDestination();
1233 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001234 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 // at this point we have not moved beyond the touch slop
1236 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1237 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001238 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1239 if (nextPage != mCurrentPage) {
1240 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 } else {
1242 snapToDestination();
1243 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001244 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001245 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 }
1247 mTouchState = TOUCH_STATE_REST;
1248 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001249 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 break;
1251
1252 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001253 if (mTouchState == TOUCH_STATE_SCROLLING) {
1254 snapToDestination();
1255 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 mTouchState = TOUCH_STATE_REST;
1257 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001258 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 break;
1260
1261 case MotionEvent.ACTION_POINTER_UP:
1262 onSecondaryPointerUp(ev);
1263 break;
1264 }
1265
1266 return true;
1267 }
1268
Winson Chung185d7162011-02-28 13:47:29 -08001269 @Override
1270 public boolean onGenericMotionEvent(MotionEvent event) {
1271 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1272 switch (event.getAction()) {
1273 case MotionEvent.ACTION_SCROLL: {
1274 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1275 final float vscroll;
1276 final float hscroll;
1277 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1278 vscroll = 0;
1279 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1280 } else {
1281 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1282 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1283 }
1284 if (hscroll != 0 || vscroll != 0) {
1285 if (hscroll > 0 || vscroll > 0) {
1286 scrollRight();
1287 } else {
1288 scrollLeft();
1289 }
1290 return true;
1291 }
1292 }
1293 }
1294 }
1295 return super.onGenericMotionEvent(event);
1296 }
1297
Michael Jurkab8f06722010-10-10 15:58:46 -07001298 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1299 if (mVelocityTracker == null) {
1300 mVelocityTracker = VelocityTracker.obtain();
1301 }
1302 mVelocityTracker.addMovement(ev);
1303 }
1304
1305 private void releaseVelocityTracker() {
1306 if (mVelocityTracker != null) {
1307 mVelocityTracker.recycle();
1308 mVelocityTracker = null;
1309 }
1310 }
1311
Winson Chung321e9ee2010-08-09 13:37:56 -07001312 private void onSecondaryPointerUp(MotionEvent ev) {
1313 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1314 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1315 final int pointerId = ev.getPointerId(pointerIndex);
1316 if (pointerId == mActivePointerId) {
1317 // This was our active pointer going up. Choose a new
1318 // active pointer and adjust accordingly.
1319 // TODO: Make this decision more intelligent.
1320 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1321 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1322 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001323 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 mActivePointerId = ev.getPointerId(newPointerIndex);
1325 if (mVelocityTracker != null) {
1326 mVelocityTracker.clear();
1327 }
1328 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001329 }
1330
Michael Jurkad771c962011-08-09 15:00:48 -07001331 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001332
1333 @Override
1334 public void requestChildFocus(View child, View focused) {
1335 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001336 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001337 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001338 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001339 }
1340 }
1341
Winson Chunge3193b92010-09-10 11:44:42 -07001342 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1343 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001344 int left;
1345 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001346 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001347 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001348 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001349 if (left <= relativeOffset && relativeOffset <= right) {
1350 return i;
1351 }
Winson Chunge3193b92010-09-10 11:44:42 -07001352 }
1353 return -1;
1354 }
1355
Winson Chung1908d072011-02-24 18:09:44 -08001356 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001357 // This functions are called enough times that it actually makes a difference in the
1358 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001359 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001360 final int minWidth = mMinimumWidth;
1361 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001362 }
1363
Adam Cohend19d3ca2010-09-15 14:43:42 -07001364 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001365 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001366 int minDistanceFromScreenCenterIndex = -1;
1367 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1368 final int childCount = getChildCount();
1369 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001370 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001371 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001372 int halfChildWidth = (childWidth / 2);
1373 int childCenter = getChildOffset(i) + halfChildWidth;
1374 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1375 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1376 minDistanceFromScreenCenter = distanceFromScreenCenter;
1377 minDistanceFromScreenCenterIndex = i;
1378 }
1379 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001380 return minDistanceFromScreenCenterIndex;
1381 }
1382
1383 protected void snapToDestination() {
1384 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001385 }
1386
Adam Cohene0f66b52010-11-23 15:06:07 -08001387 private static class ScrollInterpolator implements Interpolator {
1388 public ScrollInterpolator() {
1389 }
1390
1391 public float getInterpolation(float t) {
1392 t -= 1.0f;
1393 return t*t*t*t*t + 1;
1394 }
1395 }
1396
1397 // We want the duration of the page snap animation to be influenced by the distance that
1398 // the screen has to travel, however, we don't want this duration to be effected in a
1399 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1400 // of travel has on the overall snap duration.
1401 float distanceInfluenceForSnapDuration(float f) {
1402 f -= 0.5f; // center the values about 0.
1403 f *= 0.3f * Math.PI / 2.0f;
1404 return (float) Math.sin(f);
1405 }
1406
Michael Jurka0142d492010-08-25 17:46:15 -07001407 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001408 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1409 int halfScreenSize = getMeasuredWidth() / 2;
1410
Winson Chung785d2eb2011-04-14 16:08:02 -07001411 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1412 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1413 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001414 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1415 int delta = newX - mUnboundedScrollX;
1416 int duration = 0;
1417
1418 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1419 // If the velocity is low enough, then treat this more as an automatic page advance
1420 // as opposed to an apparent physical response to flinging
1421 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1422 return;
1423 }
1424
1425 // Here we compute a "distance" that will be used in the computation of the overall
1426 // snap duration. This is a function of the actual distance that needs to be traveled;
1427 // we keep this value close to half screen size in order to reduce the variance in snap
1428 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001429 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001430 float distance = halfScreenSize + halfScreenSize *
1431 distanceInfluenceForSnapDuration(distanceRatio);
1432
1433 velocity = Math.abs(velocity);
1434 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1435
1436 // we want the page's snap velocity to approximately match the velocity at which the
1437 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001438 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1439 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001440
1441 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001442 }
1443
1444 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001445 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001446 }
1447
Michael Jurka0142d492010-08-25 17:46:15 -07001448 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001449 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001450
Winson Chung785d2eb2011-04-14 16:08:02 -07001451 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1452 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1453 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001454 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001455 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001456 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001457 snapToPage(whichPage, delta, duration);
1458 }
1459
1460 protected void snapToPage(int whichPage, int delta, int duration) {
1461 mNextPage = whichPage;
1462
1463 View focusedChild = getFocusedChild();
1464 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001465 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001466 focusedChild.clearFocus();
1467 }
1468
1469 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001470 awakenScrollBars(duration);
1471 if (duration == 0) {
1472 duration = Math.abs(delta);
1473 }
1474
1475 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001476 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001477
Winson Chungb44b5242011-06-13 11:32:14 -07001478 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1479 // loading associated pages until the scroll settles
1480 if (mDeferScrollUpdate) {
1481 loadAssociatedPages(mNextPage);
1482 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001483 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001484 }
Michael Jurka0142d492010-08-25 17:46:15 -07001485 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001486 invalidate();
1487 }
1488
Winson Chung321e9ee2010-08-09 13:37:56 -07001489 public void scrollLeft() {
1490 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001491 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001492 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001493 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 }
1495 }
1496
1497 public void scrollRight() {
1498 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001499 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001500 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001501 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001502 }
1503 }
1504
Winson Chung86f77532010-08-24 11:08:22 -07001505 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001506 int result = -1;
1507 if (v != null) {
1508 ViewParent vp = v.getParent();
1509 int count = getChildCount();
1510 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001511 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001512 return i;
1513 }
1514 }
1515 }
1516 return result;
1517 }
1518
1519 /**
1520 * @return True is long presses are still allowed for the current touch
1521 */
1522 public boolean allowLongPress() {
1523 return mAllowLongPress;
1524 }
1525
Michael Jurka0142d492010-08-25 17:46:15 -07001526 /**
1527 * Set true to allow long-press events to be triggered, usually checked by
1528 * {@link Launcher} to accept or block dpad-initiated long-presses.
1529 */
1530 public void setAllowLongPress(boolean allowLongPress) {
1531 mAllowLongPress = allowLongPress;
1532 }
1533
Winson Chung321e9ee2010-08-09 13:37:56 -07001534 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001535 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001536
1537 SavedState(Parcelable superState) {
1538 super(superState);
1539 }
1540
1541 private SavedState(Parcel in) {
1542 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001543 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001544 }
1545
1546 @Override
1547 public void writeToParcel(Parcel out, int flags) {
1548 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001549 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001550 }
1551
1552 public static final Parcelable.Creator<SavedState> CREATOR =
1553 new Parcelable.Creator<SavedState>() {
1554 public SavedState createFromParcel(Parcel in) {
1555 return new SavedState(in);
1556 }
1557
1558 public SavedState[] newArray(int size) {
1559 return new SavedState[size];
1560 }
1561 };
1562 }
1563
Winson Chungf314b0e2011-08-16 11:54:27 -07001564 protected void loadAssociatedPages(int page) {
1565 loadAssociatedPages(page, false);
1566 }
1567 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001568 if (mContentIsRefreshable) {
1569 final int count = getChildCount();
1570 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001571 int lowerPageBound = getAssociatedLowerPageBound(page);
1572 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001573 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1574 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001575 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001576 if ((i != page) && immediateAndOnly) {
1577 continue;
1578 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001579 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001580 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001581 if (lowerPageBound <= i && i <= upperPageBound) {
1582 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001583 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001584 mDirtyPageContent.set(i, false);
1585 }
1586 } else {
1587 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001588 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001589 }
1590 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001591 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001592 }
1593 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001594 }
1595 }
1596
Winson Chunge3193b92010-09-10 11:44:42 -07001597 protected int getAssociatedLowerPageBound(int page) {
1598 return Math.max(0, page - 1);
1599 }
1600 protected int getAssociatedUpperPageBound(int page) {
1601 final int count = getChildCount();
1602 return Math.min(page + 1, count - 1);
1603 }
1604
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001605 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001606 if (isChoiceMode(CHOICE_MODE_NONE)) {
1607 mChoiceMode = mode;
1608 mActionMode = startActionMode(callback);
1609 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001610 }
1611
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001612 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001613 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001614 mChoiceMode = CHOICE_MODE_NONE;
1615 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001616 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001617 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001618 }
1619 }
1620
1621 protected boolean isChoiceMode(int mode) {
1622 return mChoiceMode == mode;
1623 }
1624
1625 protected ArrayList<Checkable> getCheckedGrandchildren() {
1626 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1627 final int childCount = getChildCount();
1628 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001629 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001630 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001631 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001632 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001633 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001634 checked.add((Checkable) v);
1635 }
1636 }
1637 }
1638 return checked;
1639 }
1640
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001641 /**
1642 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1643 * Otherwise, returns null.
1644 */
1645 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001646 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001647 final int childCount = getChildCount();
1648 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001649 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001650 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001651 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001652 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001653 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1654 return (Checkable) v;
1655 }
1656 }
1657 }
1658 }
1659 return null;
1660 }
1661
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001662 protected void resetCheckedGrandchildren() {
1663 // loop through children, and set all of their children to _not_ be checked
1664 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1665 for (int i = 0; i < checked.size(); ++i) {
1666 final Checkable c = checked.get(i);
1667 c.setChecked(false);
1668 }
1669 }
1670
Winson Chung86f77532010-08-24 11:08:22 -07001671 /**
1672 * This method is called ONLY to synchronize the number of pages that the paged view has.
1673 * To actually fill the pages with information, implement syncPageItems() below. It is
1674 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1675 * and therefore, individual page items do not need to be updated in this method.
1676 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001677 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001678
1679 /**
1680 * This method is called to synchronize the items that are on a particular page. If views on
1681 * the page can be reused, then they should be updated within this method.
1682 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001683 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001684
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001685 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001686 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001687 }
1688 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001689 invalidatePageData(currentPage, false);
1690 }
1691 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001692 if (!mIsDataReady) {
1693 return;
1694 }
1695
Michael Jurka0142d492010-08-25 17:46:15 -07001696 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001697 // Force all scrolling-related behavior to end
1698 mScroller.forceFinished(true);
1699 mNextPage = INVALID_PAGE;
1700
Michael Jurka0142d492010-08-25 17:46:15 -07001701 // Update all the pages
1702 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001703
Winson Chung5a808352011-06-27 19:08:49 -07001704 // We must force a measure after we've loaded the pages to update the content width and
1705 // to determine the full scroll width
1706 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1707 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1708
1709 // Set a new page as the current page if necessary
1710 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001711 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001712 }
1713
Michael Jurka0142d492010-08-25 17:46:15 -07001714 // Mark each of the pages as dirty
1715 final int count = getChildCount();
1716 mDirtyPageContent.clear();
1717 for (int i = 0; i < count; ++i) {
1718 mDirtyPageContent.add(true);
1719 }
1720
1721 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001722 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001723 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001724 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001725 }
Winson Chung007c6982011-06-14 13:27:53 -07001726
Adam Cohen21b41102011-11-01 17:29:52 -07001727 protected ImageView getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001728 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1729 // found
1730 if (mHasScrollIndicator && mScrollIndicator == null) {
1731 ViewGroup parent = (ViewGroup) getParent();
1732 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1733 mHasScrollIndicator = mScrollIndicator != null;
1734 if (mHasScrollIndicator) {
1735 mScrollIndicator.setVisibility(View.VISIBLE);
1736 }
1737 }
1738 return mScrollIndicator;
1739 }
1740
1741 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001742 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001743 }
1744
Winson Chung5a808352011-06-27 19:08:49 -07001745 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1746 @Override
1747 public void run() {
1748 hideScrollingIndicator(false);
1749 }
1750 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001751 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001752 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001753 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001754 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001755 }
1756
Michael Jurka430e8a52011-08-08 15:52:14 -07001757 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001758 if (getChildCount() <= 1) return;
1759 if (!isScrollingIndicatorEnabled()) return;
1760
1761 getScrollingIndicator();
1762 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001763 // Fade the indicator in
1764 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001765 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001766 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001767 if (immediately) {
1768 mScrollIndicator.setAlpha(1f);
1769 } else {
1770 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1771 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1772 mScrollIndicatorAnimator.start();
1773 }
Winson Chung007c6982011-06-14 13:27:53 -07001774 }
1775 }
1776
Adam Cohen21b41102011-11-01 17:29:52 -07001777 protected void cancelScrollingIndicatorAnimations() {
1778 if (mScrollIndicatorAnimator != null) {
1779 mScrollIndicatorAnimator.cancel();
1780 }
1781 }
1782
Winson Chung007c6982011-06-14 13:27:53 -07001783 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001784 if (getChildCount() <= 1) return;
1785 if (!isScrollingIndicatorEnabled()) return;
1786
1787 getScrollingIndicator();
1788 if (mScrollIndicator != null) {
1789 // Fade the indicator out
1790 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001791 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001792 if (immediately) {
1793 mScrollIndicator.setVisibility(View.GONE);
1794 mScrollIndicator.setAlpha(0f);
1795 } else {
1796 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1797 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1798 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1799 private boolean cancelled = false;
1800 @Override
1801 public void onAnimationCancel(android.animation.Animator animation) {
1802 cancelled = true;
1803 }
1804 @Override
1805 public void onAnimationEnd(Animator animation) {
1806 if (!cancelled) {
1807 mScrollIndicator.setVisibility(View.GONE);
1808 }
1809 }
1810 });
1811 mScrollIndicatorAnimator.start();
1812 }
Winson Chung007c6982011-06-14 13:27:53 -07001813 }
1814 }
1815
Winson Chung32174c82011-07-19 15:47:55 -07001816 /**
1817 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1818 * fill its space on the track or not.
1819 */
1820 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001821 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001822 }
1823
Winson Chung007c6982011-06-14 13:27:53 -07001824 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001825 if (getChildCount() <= 1) return;
1826 if (!isScrollingIndicatorEnabled()) return;
1827
1828 getScrollingIndicator();
1829 if (mScrollIndicator != null) {
1830 updateScrollingIndicatorPosition();
1831 }
1832 }
1833
Winson Chung007c6982011-06-14 13:27:53 -07001834 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001835 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001836 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001837 int numPages = getChildCount();
1838 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001839 int lastChildIndex = Math.max(0, getChildCount() - 1);
1840 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001841 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001842 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1843 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001844
Winson Chungae890b82011-09-13 18:08:54 -07001845 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001846 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001847 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001848 if (hasElasticScrollIndicator()) {
1849 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1850 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1851 mScrollIndicator.requestLayout();
1852 }
1853 } else {
1854 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1855 indicatorPos += indicatorCenterOffset;
1856 }
Winson Chung007c6982011-06-14 13:27:53 -07001857 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001858 mScrollIndicator.invalidate();
1859 }
1860
Winson Chung3ac74c52011-06-30 17:39:37 -07001861 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001862 }
1863
1864 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001865 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001866
1867 /* Accessibility */
1868 @Override
1869 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1870 super.onInitializeAccessibilityNodeInfo(info);
1871 info.setScrollable(true);
1872 }
1873
1874 @Override
1875 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1876 super.onInitializeAccessibilityEvent(event);
1877 event.setScrollable(true);
1878 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1879 event.setFromIndex(mCurrentPage);
1880 event.setToIndex(mCurrentPage);
1881 event.setItemCount(getChildCount());
1882 }
1883 }
1884
Winson Chung6a0f57d2011-06-29 20:10:49 -07001885 protected String getCurrentPageDescription() {
1886 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1887 return String.format(mContext.getString(R.string.default_scroll_format),
1888 page + 1, getChildCount());
1889 }
Winson Chungd11265e2011-08-30 13:37:23 -07001890
1891 @Override
1892 public boolean onHoverEvent(android.view.MotionEvent event) {
1893 return true;
1894 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001895}