blob: 5509976d01cea16853c7f7f74ab84ceb45827827 [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);
Adam Cohen60b07122011-11-14 17:26:06 -0800202 setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0));
Adam Cohen9c4949e2010-10-05 12:27:22 -0700203 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
Adam Cohen60b07122011-11-14 17:26:06 -0800554 public void setPageSpacing(int pageSpacing) {
555 mPageSpacing = pageSpacing;
556 invalidateCachedOffsets();
557 }
558
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700560 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 if (!mIsDataReady) {
562 return;
563 }
564
Winson Chung785d2eb2011-04-14 16:08:02 -0700565 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Adam Lesinski6b879f02010-11-04 16:15:23 -0700566 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 final int childCount = getChildCount();
568 int childLeft = 0;
569 if (childCount > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700570 if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
571 + getChildWidth(0));
Winson Chunge3193b92010-09-10 11:44:42 -0700572 childLeft = getRelativeChildOffset(0);
Winson Chungae890b82011-09-13 18:08:54 -0700573
574 // Calculate the variable page spacing if necessary
575 if (mPageSpacing < 0) {
Adam Cohen60b07122011-11-14 17:26:06 -0800576 setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
Winson Chungae890b82011-09-13 18:08:54 -0700577 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 }
579
580 for (int i = 0; i < childCount; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700581 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700582 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800583 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700584 final int childHeight = child.getMeasuredHeight();
585 int childTop = mPaddingTop;
586 if (mCenterPagesVertically) {
587 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
588 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800589
Winson Chung785d2eb2011-04-14 16:08:02 -0700590 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700591 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800592 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700593 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
595 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700596
597 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
598 setHorizontalScrollBarEnabled(false);
599 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
600 scrollTo(newX, 0);
601 mScroller.setFinalX(newX);
602 setHorizontalScrollBarEnabled(true);
603 mFirstLayout = false;
604 }
605
Michael Jurkad3ef3062010-11-23 16:23:58 -0800606 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
607 mFirstLayout = false;
608 }
Winson Chunge3193b92010-09-10 11:44:42 -0700609 }
610
Adam Cohenf34bab52010-09-30 14:11:56 -0700611 protected void screenScrolled(int screenCenter) {
Adam Cohen73894962011-10-31 13:17:17 -0700612 if (isScrollingIndicatorEnabled()) {
613 updateScrollingIndicator();
614 }
615 if (mFadeInAdjacentScreens) {
616 for (int i = 0; i < getChildCount(); i++) {
617 View child = getChildAt(i);
618 if (child != null) {
619 float scrollProgress = getScrollProgress(screenCenter, child, i);
620 float alpha = 1 - Math.abs(scrollProgress);
621 child.setFastAlpha(alpha);
622 child.fastInvalidate();
623 }
624 }
625 invalidate();
626 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700627 }
628
Winson Chunge3193b92010-09-10 11:44:42 -0700629 @Override
Adam Cohen2591f6a2011-10-25 14:36:40 -0700630 protected void onViewAdded(View child) {
631 super.onViewAdded(child);
632
633 // This ensures that when children are added, they get the correct transforms / alphas
634 // in accordance with any scroll effects.
635 mForceScreenScrolled = true;
636 invalidate();
Adam Cohen25b29952011-11-02 14:49:06 -0700637 invalidateCachedOffsets();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700638 }
639
Adam Cohen73894962011-10-31 13:17:17 -0700640 protected void invalidateCachedOffsets() {
641 int count = getChildCount();
Adam Cohen25b29952011-11-02 14:49:06 -0700642 if (count == 0) {
643 mChildOffsets = null;
644 mChildRelativeOffsets = null;
645 mChildOffsetsWithLayoutScale = null;
646 return;
647 }
Adam Cohen73894962011-10-31 13:17:17 -0700648
649 mChildOffsets = new int[count];
650 mChildRelativeOffsets = new int[count];
651 mChildOffsetsWithLayoutScale = new int[count];
652 for (int i = 0; i < count; i++) {
653 mChildOffsets[i] = -1;
654 mChildRelativeOffsets[i] = -1;
655 mChildOffsetsWithLayoutScale[i] = -1;
656 }
657 }
658
659 protected int getChildOffset(int index) {
660 int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ?
661 mChildOffsets : mChildOffsetsWithLayoutScale;
662
663 if (childOffsets != null && childOffsets[index] != -1) {
664 return childOffsets[index];
665 } else {
666 if (getChildCount() == 0)
667 return 0;
668
669 int offset = getRelativeChildOffset(0);
670 for (int i = 0; i < index; ++i) {
671 offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing;
672 }
673 if (childOffsets != null) {
674 childOffsets[index] = offset;
675 }
676 return offset;
677 }
678 }
679
680 protected int getRelativeChildOffset(int index) {
681 if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) {
682 return mChildRelativeOffsets[index];
683 } else {
684 final int padding = mPaddingLeft + mPaddingRight;
685 final int offset = mPaddingLeft +
686 (getMeasuredWidth() - padding - getChildWidth(index)) / 2;
687 if (mChildRelativeOffsets != null) {
688 mChildRelativeOffsets[index] = offset;
689 }
690 return offset;
691 }
692 }
693
694 protected int getScaledRelativeChildOffset(int index) {
695 final int padding = mPaddingLeft + mPaddingRight;
696 final int offset = mPaddingLeft + (getMeasuredWidth() - padding -
697 getScaledMeasuredWidth(getPageAt(index))) / 2;
698 return offset;
699 }
700
701 protected int getScaledMeasuredWidth(View child) {
702 // This functions are called enough times that it actually makes a difference in the
703 // profiler -- so just inline the max() here
704 final int measuredWidth = child.getMeasuredWidth();
705 final int minWidth = mMinimumWidth;
706 final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth;
707 return (int) (maxWidth * mLayoutScale + 0.5f);
708 }
709
Michael Jurkadde558b2011-11-09 22:09:06 -0800710 protected void getVisiblePages(int[] range) {
Michael Jurka0142d492010-08-25 17:46:15 -0700711 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700712 if (pageCount > 0) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700713 final int pageWidth = getScaledMeasuredWidth(getPageAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700714 final int screenWidth = getMeasuredWidth();
Winson Chung557d6ed2011-07-08 15:34:52 -0700715 int x = getScaledRelativeChildOffset(0) + pageWidth;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700716 int leftScreen = 0;
717 int rightScreen = 0;
Adam Cohen22f823d2011-09-01 17:22:18 -0700718 while (x <= mScrollX && leftScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700719 leftScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700720 x += getScaledMeasuredWidth(getPageAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700721 }
722 rightScreen = leftScreen;
Adam Cohen22f823d2011-09-01 17:22:18 -0700723 while (x < mScrollX + screenWidth && rightScreen < pageCount - 1) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700724 rightScreen++;
Adam Cohen22f823d2011-09-01 17:22:18 -0700725 x += getScaledMeasuredWidth(getPageAt(rightScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700726 }
Michael Jurkadde558b2011-11-09 22:09:06 -0800727 range[0] = leftScreen;
728 range[1] = rightScreen;
729 } else {
730 range[0] = -1;
731 range[1] = -1;
732 }
733 }
734
735 @Override
736 protected void dispatchDraw(Canvas canvas) {
737 int halfScreenSize = getMeasuredWidth() / 2;
738 int screenCenter = mScrollX + halfScreenSize;
739
740 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
741 screenScrolled(screenCenter);
742 mLastScreenCenter = screenCenter;
743 mForceScreenScrolled = false;
744 }
745
746 // Find out which screens are visible; as an optimization we only call draw on them
747 final int pageCount = getChildCount();
748 if (pageCount > 0) {
749 getVisiblePages(mTempVisiblePagesRange);
750 final int leftScreen = mTempVisiblePagesRange[0];
751 final int rightScreen = mTempVisiblePagesRange[1];
Michael Jurka0142d492010-08-25 17:46:15 -0700752
Michael Jurkac4fb9172010-09-02 17:19:20 -0700753 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800754 // Clip to the bounds
755 canvas.save();
756 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
757 mScrollY + mBottom - mTop);
758
Adam Cohen22f823d2011-09-01 17:22:18 -0700759 for (int i = rightScreen; i >= leftScreen; i--) {
760 drawChild(canvas, getPageAt(i), drawingTime);
Michael Jurkac4fb9172010-09-02 17:19:20 -0700761 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800762 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700763 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765
766 @Override
767 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700768 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -0700769 if (page != mCurrentPage || !mScroller.isFinished()) {
770 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700771 return true;
772 }
773 return false;
774 }
775
776 @Override
777 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700778 int focusablePage;
779 if (mNextPage != INVALID_PAGE) {
780 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700782 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 }
Winson Chung86f77532010-08-24 11:08:22 -0700784 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700785 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700786 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -0700787 }
788 return false;
789 }
790
791 @Override
792 public boolean dispatchUnhandledMove(View focused, int direction) {
793 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700794 if (getCurrentPage() > 0) {
795 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 return true;
797 }
798 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700799 if (getCurrentPage() < getPageCount() - 1) {
800 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700801 return true;
802 }
803 }
804 return super.dispatchUnhandledMove(focused, direction);
805 }
806
807 @Override
808 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700809 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
810 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700811 }
812 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700813 if (mCurrentPage > 0) {
814 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 }
816 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700817 if (mCurrentPage < getPageCount() - 1) {
818 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 }
820 }
821 }
822
823 /**
824 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700825 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700826 *
Winson Chung86f77532010-08-24 11:08:22 -0700827 * This happens when live folders requery, and if they're off page, they
828 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 */
830 @Override
831 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700832 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 View v = focused;
834 while (true) {
835 if (v == current) {
836 super.focusableViewAvailable(focused);
837 return;
838 }
839 if (v == this) {
840 return;
841 }
842 ViewParent parent = v.getParent();
843 if (parent instanceof View) {
844 v = (View)v.getParent();
845 } else {
846 return;
847 }
848 }
849 }
850
851 /**
852 * {@inheritDoc}
853 */
854 @Override
855 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
856 if (disallowIntercept) {
857 // We need to make sure to cancel our long press if
858 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -0700859 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -0700860 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 }
862 super.requestDisallowInterceptTouchEvent(disallowIntercept);
863 }
864
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800865 /**
866 * Return true if a tap at (x, y) should trigger a flip to the previous page.
867 */
868 protected boolean hitsPreviousPage(float x, float y) {
869 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
870 }
871
872 /**
873 * Return true if a tap at (x, y) should trigger a flip to the next page.
874 */
875 protected boolean hitsNextPage(float x, float y) {
876 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
877 }
878
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 @Override
880 public boolean onInterceptTouchEvent(MotionEvent ev) {
881 /*
882 * This method JUST determines whether we want to intercept the motion.
883 * If we return true, onTouchEvent will be called and we do the actual
884 * scrolling there.
885 */
Adam Cohen6342bba2011-03-10 11:33:35 -0800886 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700887
Winson Chung45e1d6e2010-11-09 17:19:49 -0800888 // Skip touch handling if there are no pages to swipe
889 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
890
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 /*
892 * Shortcut the most recurring case: the user is in the dragging
893 * state and he is moving his finger. We want to intercept this
894 * motion.
895 */
896 final int action = ev.getAction();
897 if ((action == MotionEvent.ACTION_MOVE) &&
898 (mTouchState == TOUCH_STATE_SCROLLING)) {
899 return true;
900 }
901
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 switch (action & MotionEvent.ACTION_MASK) {
903 case MotionEvent.ACTION_MOVE: {
904 /*
905 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
906 * whether the user has moved far enough from his original down touch.
907 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700908 if (mActivePointerId != INVALID_POINTER) {
909 determineScrollingStart(ev);
910 break;
911 }
912 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
913 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
914 // i.e. fall through to the next case (don't break)
915 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
916 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 }
918
919 case MotionEvent.ACTION_DOWN: {
920 final float x = ev.getX();
921 final float y = ev.getY();
922 // Remember location of down touch
923 mDownMotionX = x;
924 mLastMotionX = x;
925 mLastMotionY = y;
Winson Chungc0844aa2011-02-02 15:25:58 -0800926 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -0800927 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 mActivePointerId = ev.getPointerId(0);
929 mAllowLongPress = true;
930
931 /*
932 * If being flinged and user touches the screen, initiate drag;
933 * otherwise don't. mScroller.isFinished should be false when
934 * being flinged.
935 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700936 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700937 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
938 if (finishedScrolling) {
939 mTouchState = TOUCH_STATE_REST;
940 mScroller.abortAnimation();
941 } else {
942 mTouchState = TOUCH_STATE_SCROLLING;
943 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700944
Winson Chung86f77532010-08-24 11:08:22 -0700945 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700946 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800947 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800949 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700950 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800951 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 mTouchState = TOUCH_STATE_NEXT_PAGE;
953 }
954 }
955 }
956 break;
957 }
958
Winson Chung321e9ee2010-08-09 13:37:56 -0700959 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800960 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 mTouchState = TOUCH_STATE_REST;
962 mAllowLongPress = false;
963 mActivePointerId = INVALID_POINTER;
Adam Cohen6342bba2011-03-10 11:33:35 -0800964 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 break;
966
967 case MotionEvent.ACTION_POINTER_UP:
968 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -0800969 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700970 break;
971 }
972
973 /*
974 * The only time we want to intercept motion events is if we are in the
975 * drag mode.
976 */
977 return mTouchState != TOUCH_STATE_REST;
978 }
979
Winson Chung80baf5a2010-08-09 16:03:15 -0700980 protected void animateClickFeedback(View v, final Runnable r) {
981 // animate the view slightly to show click feedback running some logic after it is "pressed"
Winson Chung228a0fa2011-01-26 22:14:13 -0800982 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
983 loadAnimator(mContext, R.anim.paged_view_click_feedback);
984 anim.setTarget(v);
985 anim.addListener(new AnimatorListenerAdapter() {
986 public void onAnimationRepeat(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700987 r.run();
988 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700989 });
Winson Chung228a0fa2011-01-26 22:14:13 -0800990 anim.start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700991 }
992
Adam Cohenf8d28232011-02-01 21:47:00 -0800993 protected void determineScrollingStart(MotionEvent ev) {
994 determineScrollingStart(ev, 1.0f);
995 }
996
Winson Chung321e9ee2010-08-09 13:37:56 -0700997 /*
998 * Determines if we should change the touch state to start scrolling after the
999 * user moves their touch point too far.
1000 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001001 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 /*
1003 * Locally do absolute value. mLastMotionX is set to the y value
1004 * of the down event.
1005 */
1006 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Michael Jurka2698db42011-07-13 18:25:14 -07001007 if (pointerIndex == -1) {
1008 return;
1009 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 final float x = ev.getX(pointerIndex);
1011 final float y = ev.getY(pointerIndex);
1012 final int xDiff = (int) Math.abs(x - mLastMotionX);
1013 final int yDiff = (int) Math.abs(y - mLastMotionY);
1014
Adam Cohenf8d28232011-02-01 21:47:00 -08001015 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 boolean xPaged = xDiff > mPagingTouchSlop;
1017 boolean xMoved = xDiff > touchSlop;
1018 boolean yMoved = yDiff > touchSlop;
1019
Adam Cohenf8d28232011-02-01 21:47:00 -08001020 if (xMoved || xPaged || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -07001021 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 // Scroll if the user moved far enough along the X axis
1023 mTouchState = TOUCH_STATE_SCROLLING;
Adam Cohen6342bba2011-03-10 11:33:35 -08001024 mTotalMotionX += Math.abs(mLastMotionX - x);
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 mLastMotionX = x;
Winson Chungc0844aa2011-02-02 15:25:58 -08001026 mLastMotionXRemainder = 0;
Michael Jurka0142d492010-08-25 17:46:15 -07001027 mTouchX = mScrollX;
1028 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1029 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001030 }
1031 // Either way, cancel any pending longpress
Adam Cohenf8d28232011-02-01 21:47:00 -08001032 cancelCurrentPageLongPress();
1033 }
1034 }
1035
1036 protected void cancelCurrentPageLongPress() {
1037 if (mAllowLongPress) {
1038 mAllowLongPress = false;
1039 // Try canceling the long press. It could also have been scheduled
1040 // by a distant descendant, so use the mAllowLongPress flag to block
1041 // everything
1042 final View currentPage = getPageAt(mCurrentPage);
1043 if (currentPage != null) {
1044 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 }
1046 }
1047 }
1048
Adam Cohenb5ba0972011-09-07 18:02:31 -07001049 protected float getScrollProgress(int screenCenter, View v, int page) {
1050 final int halfScreenSize = getMeasuredWidth() / 2;
1051
1052 int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing;
1053 int delta = screenCenter - (getChildOffset(page) -
1054 getRelativeChildOffset(page) + halfScreenSize);
1055
1056 float scrollProgress = delta / (totalDistance * 1.0f);
1057 scrollProgress = Math.min(scrollProgress, 1.0f);
1058 scrollProgress = Math.max(scrollProgress, -1.0f);
1059 return scrollProgress;
1060 }
1061
Adam Cohene0f66b52010-11-23 15:06:07 -08001062 // This curve determines how the effect of scrolling over the limits of the page dimishes
1063 // as the user pulls further and further from the bounds
1064 private float overScrollInfluenceCurve(float f) {
1065 f -= 1.0f;
1066 return f * f * f + 1.0f;
1067 }
1068
Adam Cohenb5ba0972011-09-07 18:02:31 -07001069 protected void acceleratedOverScroll(float amount) {
1070 int screenSize = getMeasuredWidth();
1071
1072 // We want to reach the max over scroll effect when the user has
1073 // over scrolled half the size of the screen
1074 float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize);
1075
1076 if (f == 0) return;
1077
1078 // Clamp this factor, f, to -1 < f < 1
1079 if (Math.abs(f) >= 1) {
1080 f /= Math.abs(f);
1081 }
1082
1083 int overScrollAmount = (int) Math.round(f * screenSize);
1084 if (amount < 0) {
1085 mScrollX = overScrollAmount;
1086 } else {
1087 mScrollX = mMaxScrollX + overScrollAmount;
1088 }
1089 invalidate();
1090 }
1091
1092 protected void dampedOverScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001093 int screenSize = getMeasuredWidth();
1094
1095 float f = (amount / screenSize);
1096
1097 if (f == 0) return;
1098 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1099
Adam Cohen7bfc9792011-01-28 13:52:37 -08001100 // Clamp this factor, f, to -1 < f < 1
1101 if (Math.abs(f) >= 1) {
1102 f /= Math.abs(f);
1103 }
1104
Adam Cohene0f66b52010-11-23 15:06:07 -08001105 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -08001106 if (amount < 0) {
1107 mScrollX = overScrollAmount;
1108 } else {
1109 mScrollX = mMaxScrollX + overScrollAmount;
1110 }
1111 invalidate();
1112 }
1113
Adam Cohenb5ba0972011-09-07 18:02:31 -07001114 protected void overScroll(float amount) {
1115 dampedOverScroll(amount);
1116 }
1117
Michael Jurkac5b262c2011-01-12 20:24:50 -08001118 protected float maxOverScroll() {
1119 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
Adam Cohenb5ba0972011-09-07 18:02:31 -07001120 // exceed). Used to find out how much extra wallpaper we need for the over scroll effect
Michael Jurkac5b262c2011-01-12 20:24:50 -08001121 float f = 1.0f;
1122 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
1123 return OVERSCROLL_DAMP_FACTOR * f;
1124 }
1125
Winson Chung321e9ee2010-08-09 13:37:56 -07001126 @Override
1127 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001128 // Skip touch handling if there are no pages to swipe
1129 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1130
Michael Jurkab8f06722010-10-10 15:58:46 -07001131 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001132
1133 final int action = ev.getAction();
1134
1135 switch (action & MotionEvent.ACTION_MASK) {
1136 case MotionEvent.ACTION_DOWN:
1137 /*
1138 * If being flinged and user touches, stop the fling. isFinished
1139 * will be false if being flinged.
1140 */
1141 if (!mScroller.isFinished()) {
1142 mScroller.abortAnimation();
1143 }
1144
1145 // Remember where the motion event started
1146 mDownMotionX = mLastMotionX = ev.getX();
Winson Chungc0844aa2011-02-02 15:25:58 -08001147 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001148 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001149 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -07001150 if (mTouchState == TOUCH_STATE_SCROLLING) {
1151 pageBeginMoving();
1152 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001153 break;
1154
1155 case MotionEvent.ACTION_MOVE:
1156 if (mTouchState == TOUCH_STATE_SCROLLING) {
1157 // Scroll to follow the motion event
1158 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
1159 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001160 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001161
Adam Cohenaefd4e12011-02-14 16:39:38 -08001162 mTotalMotionX += Math.abs(deltaX);
1163
Winson Chungc0844aa2011-02-02 15:25:58 -08001164 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1165 // keep the remainder because we are actually testing if we've moved from the last
1166 // scrolled position (which is discrete).
1167 if (Math.abs(deltaX) >= 1.0f) {
Adam Cohen68d73932010-11-15 10:50:58 -08001168 mTouchX += deltaX;
1169 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
1170 if (!mDeferScrollUpdate) {
Winson Chungc0844aa2011-02-02 15:25:58 -08001171 scrollBy((int) deltaX, 0);
Winson Chung785d2eb2011-04-14 16:08:02 -07001172 if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
Adam Cohen68d73932010-11-15 10:50:58 -08001173 } else {
1174 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 }
Winson Chungc0844aa2011-02-02 15:25:58 -08001176 mLastMotionX = x;
1177 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001178 } else {
1179 awakenScrollBars();
1180 }
Adam Cohen564976a2010-10-13 18:52:07 -07001181 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001182 determineScrollingStart(ev);
1183 }
1184 break;
1185
1186 case MotionEvent.ACTION_UP:
1187 if (mTouchState == TOUCH_STATE_SCROLLING) {
1188 final int activePointerId = mActivePointerId;
1189 final int pointerIndex = ev.findPointerIndex(activePointerId);
1190 final float x = ev.getX(pointerIndex);
1191 final VelocityTracker velocityTracker = mVelocityTracker;
1192 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1193 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001194 final int deltaX = (int) (x - mDownMotionX);
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001195 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Michael Jurka0142d492010-08-25 17:46:15 -07001196 final int snapVelocity = mSnapVelocity;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001197
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001198 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1199
Adam Cohenaefd4e12011-02-14 16:39:38 -08001200 // In the case that the page is moved far to one direction and then is flung
1201 // in the opposite direction, we use a threshold to determine whether we should
1202 // just return to the starting page, or if we should skip one further.
1203 boolean returnToOriginalPage = false;
Adam Cohen22f823d2011-09-01 17:22:18 -07001204 final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001205 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001206 Math.signum(velocityX) != Math.signum(deltaX)) {
1207 returnToOriginalPage = true;
1208 }
1209
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001210 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohenaefd4e12011-02-14 16:39:38 -08001211 Math.abs(velocityX) > snapVelocity;
1212
1213 int finalPage;
1214 // We give flings precedence over large moves, which is why we short-circuit our
1215 // test for a large move if a fling has been registered. That is, a large
1216 // move to the left and fling to the right will register as a fling to the right.
1217 if (((isSignificantMove && deltaX > 0 && !isFling) ||
1218 (isFling && velocityX > 0)) && mCurrentPage > 0) {
1219 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1220 snapToPageWithVelocity(finalPage, velocityX);
1221 } else if (((isSignificantMove && deltaX < 0 && !isFling) ||
1222 (isFling && velocityX < 0)) &&
Winson Chung86f77532010-08-24 11:08:22 -07001223 mCurrentPage < getChildCount() - 1) {
Adam Cohenaefd4e12011-02-14 16:39:38 -08001224 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1225 snapToPageWithVelocity(finalPage, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 } else {
1227 snapToDestination();
1228 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001229 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 // at this point we have not moved beyond the touch slop
1231 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1232 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001233 int nextPage = Math.max(0, mCurrentPage - 1);
1234 if (nextPage != mCurrentPage) {
1235 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001236 } else {
1237 snapToDestination();
1238 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001239 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 // at this point we have not moved beyond the touch slop
1241 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1242 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001243 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1244 if (nextPage != mCurrentPage) {
1245 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 } else {
1247 snapToDestination();
1248 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001249 } else {
Michael Jurkad771c962011-08-09 15:00:48 -07001250 onUnhandledTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 }
1252 mTouchState = TOUCH_STATE_REST;
1253 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001254 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 break;
1256
1257 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001258 if (mTouchState == TOUCH_STATE_SCROLLING) {
1259 snapToDestination();
1260 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 mTouchState = TOUCH_STATE_REST;
1262 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001263 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001264 break;
1265
1266 case MotionEvent.ACTION_POINTER_UP:
1267 onSecondaryPointerUp(ev);
1268 break;
1269 }
1270
1271 return true;
1272 }
1273
Winson Chung185d7162011-02-28 13:47:29 -08001274 @Override
1275 public boolean onGenericMotionEvent(MotionEvent event) {
1276 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1277 switch (event.getAction()) {
1278 case MotionEvent.ACTION_SCROLL: {
1279 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1280 final float vscroll;
1281 final float hscroll;
1282 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1283 vscroll = 0;
1284 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1285 } else {
1286 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1287 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1288 }
1289 if (hscroll != 0 || vscroll != 0) {
1290 if (hscroll > 0 || vscroll > 0) {
1291 scrollRight();
1292 } else {
1293 scrollLeft();
1294 }
1295 return true;
1296 }
1297 }
1298 }
1299 }
1300 return super.onGenericMotionEvent(event);
1301 }
1302
Michael Jurkab8f06722010-10-10 15:58:46 -07001303 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1304 if (mVelocityTracker == null) {
1305 mVelocityTracker = VelocityTracker.obtain();
1306 }
1307 mVelocityTracker.addMovement(ev);
1308 }
1309
1310 private void releaseVelocityTracker() {
1311 if (mVelocityTracker != null) {
1312 mVelocityTracker.recycle();
1313 mVelocityTracker = null;
1314 }
1315 }
1316
Winson Chung321e9ee2010-08-09 13:37:56 -07001317 private void onSecondaryPointerUp(MotionEvent ev) {
1318 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1319 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1320 final int pointerId = ev.getPointerId(pointerIndex);
1321 if (pointerId == mActivePointerId) {
1322 // This was our active pointer going up. Choose a new
1323 // active pointer and adjust accordingly.
1324 // TODO: Make this decision more intelligent.
1325 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1326 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1327 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001328 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001329 mActivePointerId = ev.getPointerId(newPointerIndex);
1330 if (mVelocityTracker != null) {
1331 mVelocityTracker.clear();
1332 }
1333 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001334 }
1335
Michael Jurkad771c962011-08-09 15:00:48 -07001336 protected void onUnhandledTap(MotionEvent ev) {}
Winson Chung321e9ee2010-08-09 13:37:56 -07001337
1338 @Override
1339 public void requestChildFocus(View child, View focused) {
1340 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001341 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001342 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001343 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001344 }
1345 }
1346
Winson Chunge3193b92010-09-10 11:44:42 -07001347 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1348 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001349 int left;
1350 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001351 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001352 left = getRelativeChildOffset(i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001353 right = (left + getScaledMeasuredWidth(getPageAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001354 if (left <= relativeOffset && relativeOffset <= right) {
1355 return i;
1356 }
Winson Chunge3193b92010-09-10 11:44:42 -07001357 }
1358 return -1;
1359 }
1360
Winson Chung1908d072011-02-24 18:09:44 -08001361 protected int getChildWidth(int index) {
Winson Chung63257c12011-05-05 17:06:13 -07001362 // This functions are called enough times that it actually makes a difference in the
1363 // profiler -- so just inline the max() here
Adam Cohen22f823d2011-09-01 17:22:18 -07001364 final int measuredWidth = getPageAt(index).getMeasuredWidth();
Winson Chung63257c12011-05-05 17:06:13 -07001365 final int minWidth = mMinimumWidth;
1366 return (minWidth > measuredWidth) ? minWidth : measuredWidth;
Winson Chung1908d072011-02-24 18:09:44 -08001367 }
1368
Adam Cohend19d3ca2010-09-15 14:43:42 -07001369 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001370 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 int minDistanceFromScreenCenterIndex = -1;
1372 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1373 final int childCount = getChildCount();
1374 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001375 View layout = (View) getPageAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001376 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001377 int halfChildWidth = (childWidth / 2);
1378 int childCenter = getChildOffset(i) + halfChildWidth;
1379 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1380 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1381 minDistanceFromScreenCenter = distanceFromScreenCenter;
1382 minDistanceFromScreenCenterIndex = i;
1383 }
1384 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001385 return minDistanceFromScreenCenterIndex;
1386 }
1387
1388 protected void snapToDestination() {
1389 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001390 }
1391
Adam Cohene0f66b52010-11-23 15:06:07 -08001392 private static class ScrollInterpolator implements Interpolator {
1393 public ScrollInterpolator() {
1394 }
1395
1396 public float getInterpolation(float t) {
1397 t -= 1.0f;
1398 return t*t*t*t*t + 1;
1399 }
1400 }
1401
1402 // We want the duration of the page snap animation to be influenced by the distance that
1403 // the screen has to travel, however, we don't want this duration to be effected in a
1404 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1405 // of travel has on the overall snap duration.
1406 float distanceInfluenceForSnapDuration(float f) {
1407 f -= 0.5f; // center the values about 0.
1408 f *= 0.3f * Math.PI / 2.0f;
1409 return (float) Math.sin(f);
1410 }
1411
Michael Jurka0142d492010-08-25 17:46:15 -07001412 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001413 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1414 int halfScreenSize = getMeasuredWidth() / 2;
1415
Winson Chung785d2eb2011-04-14 16:08:02 -07001416 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1417 if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): "
1418 + getMeasuredWidth() + ", " + getChildWidth(whichPage));
Adam Cohene0f66b52010-11-23 15:06:07 -08001419 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1420 int delta = newX - mUnboundedScrollX;
1421 int duration = 0;
1422
1423 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1424 // If the velocity is low enough, then treat this more as an automatic page advance
1425 // as opposed to an apparent physical response to flinging
1426 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1427 return;
1428 }
1429
1430 // Here we compute a "distance" that will be used in the computation of the overall
1431 // snap duration. This is a function of the actual distance that needs to be traveled;
1432 // we keep this value close to half screen size in order to reduce the variance in snap
1433 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07001434 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08001435 float distance = halfScreenSize + halfScreenSize *
1436 distanceInfluenceForSnapDuration(distanceRatio);
1437
1438 velocity = Math.abs(velocity);
1439 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1440
1441 // we want the page's snap velocity to approximately match the velocity at which the
1442 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07001443 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
1444 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08001445
1446 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001447 }
1448
1449 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001450 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001451 }
1452
Michael Jurka0142d492010-08-25 17:46:15 -07001453 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001454 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001455
Winson Chung785d2eb2011-04-14 16:08:02 -07001456 if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage));
1457 if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", "
1458 + getChildWidth(whichPage));
Winson Chung86f77532010-08-24 11:08:22 -07001459 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001460 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001461 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001462 snapToPage(whichPage, delta, duration);
1463 }
1464
1465 protected void snapToPage(int whichPage, int delta, int duration) {
1466 mNextPage = whichPage;
1467
1468 View focusedChild = getFocusedChild();
1469 if (focusedChild != null && whichPage != mCurrentPage &&
Adam Cohen22f823d2011-09-01 17:22:18 -07001470 focusedChild == getPageAt(mCurrentPage)) {
Michael Jurka0142d492010-08-25 17:46:15 -07001471 focusedChild.clearFocus();
1472 }
1473
1474 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001475 awakenScrollBars(duration);
1476 if (duration == 0) {
1477 duration = Math.abs(delta);
1478 }
1479
1480 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001481 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001482
Winson Chungb44b5242011-06-13 11:32:14 -07001483 // Load associated pages immediately if someone else is handling the scroll, otherwise defer
1484 // loading associated pages until the scroll settles
1485 if (mDeferScrollUpdate) {
1486 loadAssociatedPages(mNextPage);
1487 } else {
Winson Chung4e076542011-06-23 13:04:10 -07001488 mDeferLoadAssociatedPagesUntilScrollCompletes = true;
Winson Chungb44b5242011-06-13 11:32:14 -07001489 }
Michael Jurka0142d492010-08-25 17:46:15 -07001490 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001491 invalidate();
1492 }
1493
Winson Chung321e9ee2010-08-09 13:37:56 -07001494 public void scrollLeft() {
1495 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001496 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001497 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001498 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001499 }
1500 }
1501
1502 public void scrollRight() {
1503 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001504 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001505 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001506 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001507 }
1508 }
1509
Winson Chung86f77532010-08-24 11:08:22 -07001510 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001511 int result = -1;
1512 if (v != null) {
1513 ViewParent vp = v.getParent();
1514 int count = getChildCount();
1515 for (int i = 0; i < count; i++) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001516 if (vp == getPageAt(i)) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001517 return i;
1518 }
1519 }
1520 }
1521 return result;
1522 }
1523
1524 /**
1525 * @return True is long presses are still allowed for the current touch
1526 */
1527 public boolean allowLongPress() {
1528 return mAllowLongPress;
1529 }
1530
Michael Jurka0142d492010-08-25 17:46:15 -07001531 /**
1532 * Set true to allow long-press events to be triggered, usually checked by
1533 * {@link Launcher} to accept or block dpad-initiated long-presses.
1534 */
1535 public void setAllowLongPress(boolean allowLongPress) {
1536 mAllowLongPress = allowLongPress;
1537 }
1538
Winson Chung321e9ee2010-08-09 13:37:56 -07001539 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001540 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001541
1542 SavedState(Parcelable superState) {
1543 super(superState);
1544 }
1545
1546 private SavedState(Parcel in) {
1547 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001548 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001549 }
1550
1551 @Override
1552 public void writeToParcel(Parcel out, int flags) {
1553 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001554 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001555 }
1556
1557 public static final Parcelable.Creator<SavedState> CREATOR =
1558 new Parcelable.Creator<SavedState>() {
1559 public SavedState createFromParcel(Parcel in) {
1560 return new SavedState(in);
1561 }
1562
1563 public SavedState[] newArray(int size) {
1564 return new SavedState[size];
1565 }
1566 };
1567 }
1568
Winson Chungf314b0e2011-08-16 11:54:27 -07001569 protected void loadAssociatedPages(int page) {
1570 loadAssociatedPages(page, false);
1571 }
1572 protected void loadAssociatedPages(int page, boolean immediateAndOnly) {
Michael Jurka0142d492010-08-25 17:46:15 -07001573 if (mContentIsRefreshable) {
1574 final int count = getChildCount();
1575 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001576 int lowerPageBound = getAssociatedLowerPageBound(page);
1577 int upperPageBound = getAssociatedUpperPageBound(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001578 if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/"
1579 + upperPageBound);
Michael Jurka0142d492010-08-25 17:46:15 -07001580 for (int i = 0; i < count; ++i) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001581 if ((i != page) && immediateAndOnly) {
1582 continue;
1583 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001584 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001585 final int childCount = layout.getPageChildCount();
Michael Jurka0142d492010-08-25 17:46:15 -07001586 if (lowerPageBound <= i && i <= upperPageBound) {
1587 if (mDirtyPageContent.get(i)) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001588 syncPageItems(i, (i == page) && immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001589 mDirtyPageContent.set(i, false);
1590 }
1591 } else {
1592 if (childCount > 0) {
Michael Jurka8245a862011-02-01 17:53:59 -08001593 layout.removeAllViewsOnPage();
Michael Jurka0142d492010-08-25 17:46:15 -07001594 }
1595 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001596 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001597 }
1598 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001599 }
1600 }
1601
Winson Chunge3193b92010-09-10 11:44:42 -07001602 protected int getAssociatedLowerPageBound(int page) {
1603 return Math.max(0, page - 1);
1604 }
1605 protected int getAssociatedUpperPageBound(int page) {
1606 final int count = getChildCount();
1607 return Math.min(page + 1, count - 1);
1608 }
1609
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001610 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001611 if (isChoiceMode(CHOICE_MODE_NONE)) {
1612 mChoiceMode = mode;
1613 mActionMode = startActionMode(callback);
1614 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001615 }
1616
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001617 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001618 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001619 mChoiceMode = CHOICE_MODE_NONE;
1620 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001621 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001622 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001623 }
1624 }
1625
1626 protected boolean isChoiceMode(int mode) {
1627 return mChoiceMode == mode;
1628 }
1629
1630 protected ArrayList<Checkable> getCheckedGrandchildren() {
1631 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1632 final int childCount = getChildCount();
1633 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001634 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001635 final int grandChildCount = layout.getPageChildCount();
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001636 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001637 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001638 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001639 checked.add((Checkable) v);
1640 }
1641 }
1642 }
1643 return checked;
1644 }
1645
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001646 /**
1647 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1648 * Otherwise, returns null.
1649 */
1650 protected Checkable getSingleCheckedGrandchild() {
Patrick Dubroy6f133422011-02-24 12:16:12 -08001651 if (mChoiceMode != CHOICE_MODE_MULTIPLE) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001652 final int childCount = getChildCount();
1653 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001654 Page layout = (Page) getPageAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -08001655 final int grandChildCount = layout.getPageChildCount();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001656 for (int j = 0; j < grandChildCount; ++j) {
Michael Jurka8245a862011-02-01 17:53:59 -08001657 final View v = layout.getChildOnPageAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001658 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1659 return (Checkable) v;
1660 }
1661 }
1662 }
1663 }
1664 return null;
1665 }
1666
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001667 protected void resetCheckedGrandchildren() {
1668 // loop through children, and set all of their children to _not_ be checked
1669 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1670 for (int i = 0; i < checked.size(); ++i) {
1671 final Checkable c = checked.get(i);
1672 c.setChecked(false);
1673 }
1674 }
1675
Winson Chung86f77532010-08-24 11:08:22 -07001676 /**
1677 * This method is called ONLY to synchronize the number of pages that the paged view has.
1678 * To actually fill the pages with information, implement syncPageItems() below. It is
1679 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1680 * and therefore, individual page items do not need to be updated in this method.
1681 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001682 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001683
1684 /**
1685 * This method is called to synchronize the items that are on a particular page. If views on
1686 * the page can be reused, then they should be updated within this method.
1687 */
Winson Chungf314b0e2011-08-16 11:54:27 -07001688 public abstract void syncPageItems(int page, boolean immediate);
Winson Chung86f77532010-08-24 11:08:22 -07001689
Patrick Dubroy244d74c2011-05-19 16:48:48 -07001690 protected void invalidatePageData() {
Winson Chungf314b0e2011-08-16 11:54:27 -07001691 invalidatePageData(-1, false);
Winson Chung5a808352011-06-27 19:08:49 -07001692 }
1693 protected void invalidatePageData(int currentPage) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001694 invalidatePageData(currentPage, false);
1695 }
1696 protected void invalidatePageData(int currentPage, boolean immediateAndOnly) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001697 if (!mIsDataReady) {
1698 return;
1699 }
1700
Michael Jurka0142d492010-08-25 17:46:15 -07001701 if (mContentIsRefreshable) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001702 // Force all scrolling-related behavior to end
1703 mScroller.forceFinished(true);
1704 mNextPage = INVALID_PAGE;
1705
Michael Jurka0142d492010-08-25 17:46:15 -07001706 // Update all the pages
1707 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001708
Winson Chung5a808352011-06-27 19:08:49 -07001709 // We must force a measure after we've loaded the pages to update the content width and
1710 // to determine the full scroll width
1711 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
1712 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
1713
1714 // Set a new page as the current page if necessary
1715 if (currentPage > -1) {
Winson Chung5afbf7b2011-07-25 11:53:08 -07001716 setCurrentPage(Math.min(getPageCount() - 1, currentPage));
Winson Chung5a808352011-06-27 19:08:49 -07001717 }
1718
Michael Jurka0142d492010-08-25 17:46:15 -07001719 // Mark each of the pages as dirty
1720 final int count = getChildCount();
1721 mDirtyPageContent.clear();
1722 for (int i = 0; i < count; ++i) {
1723 mDirtyPageContent.add(true);
1724 }
1725
1726 // Load any pages that are necessary for the current window of views
Winson Chungf314b0e2011-08-16 11:54:27 -07001727 loadAssociatedPages(mCurrentPage, immediateAndOnly);
Michael Jurka0142d492010-08-25 17:46:15 -07001728 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001729 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001730 }
Winson Chung007c6982011-06-14 13:27:53 -07001731
Adam Cohen21b41102011-11-01 17:29:52 -07001732 protected ImageView getScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001733 // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
1734 // found
1735 if (mHasScrollIndicator && mScrollIndicator == null) {
1736 ViewGroup parent = (ViewGroup) getParent();
1737 mScrollIndicator = (ImageView) (parent.findViewById(R.id.paged_view_indicator));
1738 mHasScrollIndicator = mScrollIndicator != null;
1739 if (mHasScrollIndicator) {
1740 mScrollIndicator.setVisibility(View.VISIBLE);
1741 }
1742 }
1743 return mScrollIndicator;
1744 }
1745
1746 protected boolean isScrollingIndicatorEnabled() {
Winson Chung649723c2011-07-06 20:41:23 -07001747 return !LauncherApplication.isScreenLarge();
Winson Chung007c6982011-06-14 13:27:53 -07001748 }
1749
Winson Chung5a808352011-06-27 19:08:49 -07001750 Runnable hideScrollingIndicatorRunnable = new Runnable() {
1751 @Override
1752 public void run() {
1753 hideScrollingIndicator(false);
1754 }
1755 };
Winson Chung3ac74c52011-06-30 17:39:37 -07001756 protected void flashScrollingIndicator() {
Winson Chung5a808352011-06-27 19:08:49 -07001757 removeCallbacks(hideScrollingIndicatorRunnable);
Michael Jurka430e8a52011-08-08 15:52:14 -07001758 showScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -07001759 postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
Winson Chung3ac74c52011-06-30 17:39:37 -07001760 }
1761
Michael Jurka430e8a52011-08-08 15:52:14 -07001762 protected void showScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001763 if (getChildCount() <= 1) return;
1764 if (!isScrollingIndicatorEnabled()) return;
1765
1766 getScrollingIndicator();
1767 if (mScrollIndicator != null) {
Winson Chung007c6982011-06-14 13:27:53 -07001768 // Fade the indicator in
1769 updateScrollingIndicatorPosition();
Winson Chung32174c82011-07-19 15:47:55 -07001770 mScrollIndicator.setVisibility(View.VISIBLE);
Adam Cohen21b41102011-11-01 17:29:52 -07001771 cancelScrollingIndicatorAnimations();
Michael Jurka430e8a52011-08-08 15:52:14 -07001772 if (immediately) {
1773 mScrollIndicator.setAlpha(1f);
1774 } else {
1775 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
1776 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
1777 mScrollIndicatorAnimator.start();
1778 }
Winson Chung007c6982011-06-14 13:27:53 -07001779 }
1780 }
1781
Adam Cohen21b41102011-11-01 17:29:52 -07001782 protected void cancelScrollingIndicatorAnimations() {
1783 if (mScrollIndicatorAnimator != null) {
1784 mScrollIndicatorAnimator.cancel();
1785 }
1786 }
1787
Winson Chung007c6982011-06-14 13:27:53 -07001788 protected void hideScrollingIndicator(boolean immediately) {
Winson Chung007c6982011-06-14 13:27:53 -07001789 if (getChildCount() <= 1) return;
1790 if (!isScrollingIndicatorEnabled()) return;
1791
1792 getScrollingIndicator();
1793 if (mScrollIndicator != null) {
1794 // Fade the indicator out
1795 updateScrollingIndicatorPosition();
Adam Cohen21b41102011-11-01 17:29:52 -07001796 cancelScrollingIndicatorAnimations();
Winson Chung32174c82011-07-19 15:47:55 -07001797 if (immediately) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001798 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001799 mScrollIndicator.setAlpha(0f);
1800 } else {
1801 mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
1802 mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
1803 mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
1804 private boolean cancelled = false;
1805 @Override
1806 public void onAnimationCancel(android.animation.Animator animation) {
1807 cancelled = true;
1808 }
1809 @Override
1810 public void onAnimationEnd(Animator animation) {
1811 if (!cancelled) {
Michael Jurka81efbad2011-11-03 13:50:45 -07001812 mScrollIndicator.setVisibility(View.INVISIBLE);
Winson Chung32174c82011-07-19 15:47:55 -07001813 }
1814 }
1815 });
1816 mScrollIndicatorAnimator.start();
1817 }
Winson Chung007c6982011-06-14 13:27:53 -07001818 }
1819 }
1820
Winson Chung32174c82011-07-19 15:47:55 -07001821 /**
1822 * To be overridden by subclasses to determine whether the scroll indicator should stretch to
1823 * fill its space on the track or not.
1824 */
1825 protected boolean hasElasticScrollIndicator() {
Winson Chungdea74b72011-09-13 18:06:43 -07001826 return true;
Winson Chung32174c82011-07-19 15:47:55 -07001827 }
1828
Winson Chung007c6982011-06-14 13:27:53 -07001829 private void updateScrollingIndicator() {
Winson Chung007c6982011-06-14 13:27:53 -07001830 if (getChildCount() <= 1) return;
1831 if (!isScrollingIndicatorEnabled()) return;
1832
1833 getScrollingIndicator();
1834 if (mScrollIndicator != null) {
1835 updateScrollingIndicatorPosition();
1836 }
1837 }
1838
Winson Chung007c6982011-06-14 13:27:53 -07001839 private void updateScrollingIndicatorPosition() {
Winson Chung649723c2011-07-06 20:41:23 -07001840 if (!isScrollingIndicatorEnabled()) return;
Michael Jurka430e8a52011-08-08 15:52:14 -07001841 if (mScrollIndicator == null) return;
Winson Chung32174c82011-07-19 15:47:55 -07001842 int numPages = getChildCount();
1843 int pageWidth = getMeasuredWidth();
Winson Chungae890b82011-09-13 18:08:54 -07001844 int lastChildIndex = Math.max(0, getChildCount() - 1);
1845 int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
Winson Chungf5f8cef2011-07-22 11:16:13 -07001846 int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
Winson Chung32174c82011-07-19 15:47:55 -07001847 int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
1848 mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
Winson Chung32174c82011-07-19 15:47:55 -07001849
Winson Chungae890b82011-09-13 18:08:54 -07001850 float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX));
Winson Chung32174c82011-07-19 15:47:55 -07001851 int indicatorSpace = trackWidth / numPages;
Winson Chungae890b82011-09-13 18:08:54 -07001852 int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
Winson Chung32174c82011-07-19 15:47:55 -07001853 if (hasElasticScrollIndicator()) {
1854 if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
1855 mScrollIndicator.getLayoutParams().width = indicatorSpace;
1856 mScrollIndicator.requestLayout();
1857 }
1858 } else {
1859 int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
1860 indicatorPos += indicatorCenterOffset;
1861 }
Winson Chung007c6982011-06-14 13:27:53 -07001862 mScrollIndicator.setTranslationX(indicatorPos);
Winson Chung3ac74c52011-06-30 17:39:37 -07001863 mScrollIndicator.invalidate();
1864 }
1865
Winson Chung3ac74c52011-06-30 17:39:37 -07001866 public void showScrollIndicatorTrack() {
Winson Chung3ac74c52011-06-30 17:39:37 -07001867 }
1868
1869 public void hideScrollIndicatorTrack() {
Winson Chung007c6982011-06-14 13:27:53 -07001870 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001871
1872 /* Accessibility */
1873 @Override
1874 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1875 super.onInitializeAccessibilityNodeInfo(info);
1876 info.setScrollable(true);
1877 }
1878
1879 @Override
1880 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1881 super.onInitializeAccessibilityEvent(event);
1882 event.setScrollable(true);
1883 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
1884 event.setFromIndex(mCurrentPage);
1885 event.setToIndex(mCurrentPage);
1886 event.setItemCount(getChildCount());
1887 }
1888 }
1889
Winson Chung6a0f57d2011-06-29 20:10:49 -07001890 protected String getCurrentPageDescription() {
1891 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1892 return String.format(mContext.getString(R.string.default_scroll_format),
1893 page + 1, getChildCount());
1894 }
Winson Chungd11265e2011-08-30 13:37:23 -07001895
1896 @Override
1897 public boolean onHoverEvent(android.view.MotionEvent event) {
1898 return true;
1899 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001900}