blob: fb8b7d64d9b3b21ee44bbc708dcaf36f9b0c70b1 [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 Chunge3193b92010-09-10 11:44:42 -070019import java.util.ArrayList;
20import java.util.HashMap;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070021
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung241c3b42010-08-25 16:53:03 -070024import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chunge3193b92010-09-10 11:44:42 -070030import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070031import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.MotionEvent;
33import android.view.VelocityTracker;
34import android.view.View;
35import android.view.ViewConfiguration;
36import android.view.ViewGroup;
37import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.view.animation.Animation;
Michael Jurka5f1c5092010-09-03 14:15:02 -070039import android.view.animation.Animation.AnimationListener;
Winson Chunge3193b92010-09-10 11:44:42 -070040import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070041import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070042import android.widget.LinearLayout;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chunge3193b92010-09-10 11:44:42 -070045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung321e9ee2010-08-09 13:37:56 -070047/**
48 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070049 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070050 */
51public abstract class PagedView extends ViewGroup {
52 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070053 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Winson Chung86f77532010-08-24 11:08:22 -070055 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070056 private static final int MIN_LENGTH_FOR_FLING = 25;
57 // The min drag distance to trigger a page shift (regardless of velocity)
58 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Winson Chunge22a8e92010-11-12 13:40:58 -080060 private static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Michael Jurka0142d492010-08-25 17:46:15 -070063 // the velocity at which a fling gesture will cause us to snap to the next page
64 protected int mSnapVelocity = 500;
65
66 protected float mSmoothingTime;
67 protected float mTouchX;
68
69 protected boolean mFirstLayout = true;
70
71 protected int mCurrentPage;
72 protected int mNextPage = INVALID_PAGE;
73 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070074 private VelocityTracker mVelocityTracker;
75
76 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080077 protected float mLastMotionX;
78 protected float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070079 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Michael Jurka0142d492010-08-25 17:46:15 -070081 protected final static int TOUCH_STATE_REST = 0;
82 protected final static int TOUCH_STATE_SCROLLING = 1;
83 protected final static int TOUCH_STATE_PREV_PAGE = 2;
84 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070085 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070086
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070088
Michael Jurka0142d492010-08-25 17:46:15 -070089 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070090
Michael Jurka7426c422010-11-11 15:23:47 -080091 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070092
Michael Jurka7426c422010-11-11 15:23:47 -080093 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070094 private int mPagingTouchSlop;
95 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070096 protected int mPageSpacing;
97 protected int mPageLayoutPaddingTop;
98 protected int mPageLayoutPaddingBottom;
99 protected int mPageLayoutPaddingLeft;
100 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700101 protected int mPageLayoutWidthGap;
102 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700103 protected int mCellCountX;
104 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700105 protected boolean mCenterPagesVertically;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka5f1c5092010-09-03 14:15:02 -0700107 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Michael Jurka5f1c5092010-09-03 14:15:02 -0700109 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Winson Chung86f77532010-08-24 11:08:22 -0700111 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Winson Chung86f77532010-08-24 11:08:22 -0700113 private ArrayList<Boolean> mDirtyPageContent;
114 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700116 // choice modes
117 protected static final int CHOICE_MODE_NONE = 0;
118 protected static final int CHOICE_MODE_SINGLE = 1;
119 // Multiple selection mode is not supported by all Launcher actions atm
120 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700121
Michael Jurkae17e19c2010-09-28 11:01:39 -0700122 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700123 private ActionMode mActionMode;
124
Winson Chung241c3b42010-08-25 16:53:03 -0700125 protected PagedViewIconCache mPageViewIconCache;
126
Michael Jurka0142d492010-08-25 17:46:15 -0700127 // If true, syncPages and syncPageItems will be called to refresh pages
128 protected boolean mContentIsRefreshable = true;
129
130 // If true, modify alpha of neighboring pages as user scrolls left/right
131 protected boolean mFadeInAdjacentScreens = true;
132
133 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
134 // to switch to a new page
135 protected boolean mUsePagingTouchSlop = true;
136
137 // If true, the subclass should directly update mScrollX itself in its computeScroll method
138 // (SmoothPagedView does this)
139 protected boolean mDeferScrollUpdate = false;
140
Patrick Dubroy1262e362010-10-06 15:49:50 -0700141 protected boolean mIsPageMoving = false;
142
Winson Chung241c3b42010-08-25 16:53:03 -0700143 /**
144 * Simple cache mechanism for PagedViewIcon outlines.
145 */
146 class PagedViewIconCache {
147 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
148
149 public void clear() {
150 iconOutlineCache.clear();
151 }
152 public void addOutline(Object key, Bitmap b) {
153 iconOutlineCache.put(key, b);
154 }
155 public void removeOutline(Object key) {
156 if (iconOutlineCache.containsKey(key)) {
157 iconOutlineCache.remove(key);
158 }
159 }
160 public Bitmap getOutline(Object key) {
161 return iconOutlineCache.get(key);
162 }
163 }
164
Winson Chung86f77532010-08-24 11:08:22 -0700165 public interface PageSwitchListener {
166 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 }
168
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 public PagedView(Context context) {
170 this(context, null);
171 }
172
173 public PagedView(Context context, AttributeSet attrs) {
174 this(context, attrs, 0);
175 }
176
177 public PagedView(Context context, AttributeSet attrs, int defStyle) {
178 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Adam Cohen9c4949e2010-10-05 12:27:22 -0700181 TypedArray a = context.obtainStyledAttributes(attrs,
182 R.styleable.PagedView, defStyle, 0);
183 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
184 mPageLayoutPaddingTop = a.getDimensionPixelSize(
185 R.styleable.PagedView_pageLayoutPaddingTop, 10);
186 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
187 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
188 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
189 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
190 mPageLayoutPaddingRight = a.getDimensionPixelSize(
191 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700192 mPageLayoutWidthGap = a.getDimensionPixelSize(
193 R.styleable.PagedView_pageLayoutWidthGap, -1);
194 mPageLayoutHeightGap = a.getDimensionPixelSize(
195 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 a.recycle();
197
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700199 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
202 /**
203 * Initializes various states for this workspace.
204 */
Michael Jurka0142d492010-08-25 17:46:15 -0700205 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700206 mDirtyPageContent = new ArrayList<Boolean>();
207 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700208 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700210 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700211 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
217 }
218
Winson Chung86f77532010-08-24 11:08:22 -0700219 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
220 mPageSwitchListener = pageSwitchListener;
221 if (mPageSwitchListener != null) {
222 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224 }
225
226 /**
Winson Chung86f77532010-08-24 11:08:22 -0700227 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 *
Winson Chung86f77532010-08-24 11:08:22 -0700229 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 */
Winson Chung86f77532010-08-24 11:08:22 -0700231 int getCurrentPage() {
232 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 }
234
Winson Chung86f77532010-08-24 11:08:22 -0700235 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Winson Chung86f77532010-08-24 11:08:22 -0700239 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
243 int getScrollWidth() {
244 return getWidth();
245 }
246
247 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800248 * Updates the scroll of the current page immediately to its final scroll position. We use this
249 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
250 * the previous tab page.
251 */
252 protected void updateCurrentPageScroll() {
253 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
254 scrollTo(newX, 0);
255 mScroller.setFinalX(newX);
256 }
257
258 /**
Winson Chung86f77532010-08-24 11:08:22 -0700259 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700260 */
Winson Chung86f77532010-08-24 11:08:22 -0700261 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800262 if (!mScroller.isFinished()) {
263 mScroller.abortAnimation();
264 }
265 if (getChildCount() == 0 || currentPage == mCurrentPage) {
266 return;
267 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700268
Winson Chung86f77532010-08-24 11:08:22 -0700269 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800270 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700271
Winson Chung321e9ee2010-08-09 13:37:56 -0700272 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700273 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 }
275
Michael Jurka0142d492010-08-25 17:46:15 -0700276 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700277 if (mPageSwitchListener != null) {
278 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 }
280 }
281
Patrick Dubroy1262e362010-10-06 15:49:50 -0700282 private void pageBeginMoving() {
283 mIsPageMoving = true;
284 onPageBeginMoving();
285 }
286
287 private void pageEndMoving() {
288 onPageEndMoving();
289 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700290 }
291
292 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 protected void onPageBeginMoving() {
294 }
295
296 // a method that subclasses can override to add behavior
297 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700298 }
299
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 /**
Winson Chung86f77532010-08-24 11:08:22 -0700301 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 *
303 * @param l The listener used to respond to long clicks.
304 */
305 @Override
306 public void setOnLongClickListener(OnLongClickListener l) {
307 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700308 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700310 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 }
312 }
313
314 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700315 public void scrollTo(int x, int y) {
316 super.scrollTo(x, y);
317 mTouchX = x;
318 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
319 }
320
321 // we moved this functionality to a helper function so SmoothPagedView can reuse it
322 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700324 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700326 invalidate();
327 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700328 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700329 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700330 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700331 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700332 notifyPageSwitchListener();
333 pageEndMoving();
334 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 }
Michael Jurka0142d492010-08-25 17:46:15 -0700336 return false;
337 }
338
339 @Override
340 public void computeScroll() {
341 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 }
343
344 @Override
345 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
346 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
347 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
348 if (widthMode != MeasureSpec.EXACTLY) {
349 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
350 }
351
Adam Lesinski6b879f02010-11-04 16:15:23 -0700352 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
353 * of the All apps view on XLarge displays to not take up more space then it needs. Width
354 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
355 * each page to have the same width.
356 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700357 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700358 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
359 int maxChildHeight = 0;
360
361 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700362
363 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700364 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700365 final int childCount = getChildCount();
366 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700367 // disallowing padding in paged view (just pass 0)
368 final View child = getChildAt(i);
369 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
370
371 int childWidthMode;
372 if (lp.width == LayoutParams.WRAP_CONTENT) {
373 childWidthMode = MeasureSpec.AT_MOST;
374 } else {
375 childWidthMode = MeasureSpec.EXACTLY;
376 }
377
378 int childHeightMode;
379 if (lp.height == LayoutParams.WRAP_CONTENT) {
380 childHeightMode = MeasureSpec.AT_MOST;
381 } else {
382 childHeightMode = MeasureSpec.EXACTLY;
383 }
384
385 final int childWidthMeasureSpec =
386 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
387 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700388 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700389
390 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700391 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
392 }
393
394 if (heightMode == MeasureSpec.AT_MOST) {
395 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 }
397
398 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 }
400
401 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700402 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700403 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
404 setHorizontalScrollBarEnabled(false);
405 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
406 scrollTo(newX, 0);
407 mScroller.setFinalX(newX);
408 setHorizontalScrollBarEnabled(true);
409 mFirstLayout = false;
410 }
411
Adam Lesinski6b879f02010-11-04 16:15:23 -0700412 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700413 final int childCount = getChildCount();
414 int childLeft = 0;
415 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700416 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
418
419 for (int i = 0; i < childCount; i++) {
420 final View child = getChildAt(i);
421 if (child.getVisibility() != View.GONE) {
422 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700423 final int childHeight = child.getMeasuredHeight();
424 int childTop = mPaddingTop;
425 if (mCenterPagesVertically) {
426 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
427 }
428 child.layout(childLeft, childTop,
429 childLeft + childWidth, childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700430 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 }
432 }
433 }
434
Winson Chunge3193b92010-09-10 11:44:42 -0700435 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700436 if (mFadeInAdjacentScreens) {
437 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700438 int halfScreenSize = getMeasuredWidth() / 2;
439 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700440 final int childCount = getChildCount();
441 for (int i = 0; i < childCount; ++i) {
442 View layout = (View) getChildAt(i);
443 int childWidth = layout.getMeasuredWidth();
444 int halfChildWidth = (childWidth / 2);
445 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700446
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700447 // On the first layout, we may not have a width nor a proper offset, so for now
448 // we should just assume full page width (and calculate the offset according to
449 // that).
450 if (childWidth <= 0) {
451 childWidth = getMeasuredWidth();
452 childCenter = (i * childWidth) + (childWidth / 2);
453 }
454
Winson Chunge8878e32010-09-15 20:37:09 -0700455 int d = halfChildWidth;
456 int distanceFromScreenCenter = childCenter - screenCenter;
457 if (distanceFromScreenCenter > 0) {
458 if (i > 0) {
459 d += getChildAt(i - 1).getMeasuredWidth() / 2;
460 }
Michael Jurka0142d492010-08-25 17:46:15 -0700461 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700462 if (i < childCount - 1) {
463 d += getChildAt(i + 1).getMeasuredWidth() / 2;
464 }
Michael Jurka0142d492010-08-25 17:46:15 -0700465 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700466 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700467
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700468 // Preventing potential divide-by-zero
469 d = Math.max(1, d);
470
Winson Chunge8878e32010-09-15 20:37:09 -0700471 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
472 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
473 float alpha = 1.0f - dimAlpha;
474
Adam Cohenf34bab52010-09-30 14:11:56 -0700475 if (alpha < ALPHA_QUANTIZE_LEVEL) {
476 alpha = 0.0f;
477 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
478 alpha = 1.0f;
479 }
480
Michael Jurka0142d492010-08-25 17:46:15 -0700481 if (Float.compare(alpha, layout.getAlpha()) != 0) {
482 layout.setAlpha(alpha);
483 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700484 }
Michael Jurka0142d492010-08-25 17:46:15 -0700485 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
487 }
Winson Chunge3193b92010-09-10 11:44:42 -0700488 }
489
Adam Cohenf34bab52010-09-30 14:11:56 -0700490 protected void screenScrolled(int screenCenter) {
491 }
492
Winson Chunge3193b92010-09-10 11:44:42 -0700493 @Override
494 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700495 int halfScreenSize = getMeasuredWidth() / 2;
496 int screenCenter = mScrollX + halfScreenSize;
497
498 if (screenCenter != mLastScreenCenter) {
499 screenScrolled(screenCenter);
500 updateAdjacentPagesAlpha();
501 mLastScreenCenter = screenCenter;
502 }
Michael Jurka0142d492010-08-25 17:46:15 -0700503
504 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700505 // As an optimization, this code assumes that all pages have the same width as the 0th
506 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700507 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700508 if (pageCount > 0) {
509 final int pageWidth = getChildAt(0).getMeasuredWidth();
510 final int screenWidth = getMeasuredWidth();
511 int x = getRelativeChildOffset(0) + pageWidth;
512 int leftScreen = 0;
513 int rightScreen = 0;
514 while (x <= mScrollX) {
515 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700516 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700517 // replace above line with this if you don't assume all pages have same width as 0th
518 // page:
519 // x += getChildAt(leftScreen).getMeasuredWidth();
520 }
521 rightScreen = leftScreen;
522 while (x < mScrollX + screenWidth) {
523 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700524 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700525 // replace above line with this if you don't assume all pages have same width as 0th
526 // page:
527 //if (rightScreen < pageCount) {
528 // x += getChildAt(rightScreen).getMeasuredWidth();
529 //}
530 }
531 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700532
Michael Jurkac4fb9172010-09-02 17:19:20 -0700533 final long drawingTime = getDrawingTime();
534 for (int i = leftScreen; i <= rightScreen; i++) {
535 drawChild(canvas, getChildAt(i), drawingTime);
536 }
Michael Jurka0142d492010-08-25 17:46:15 -0700537 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700538 }
539
540 @Override
541 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700542 int page = indexOfChild(child);
543 if (page != mCurrentPage || !mScroller.isFinished()) {
544 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 return true;
546 }
547 return false;
548 }
549
550 @Override
551 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700552 int focusablePage;
553 if (mNextPage != INVALID_PAGE) {
554 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700556 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700557 }
Winson Chung86f77532010-08-24 11:08:22 -0700558 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 if (v != null) {
560 v.requestFocus(direction, previouslyFocusedRect);
561 }
562 return false;
563 }
564
565 @Override
566 public boolean dispatchUnhandledMove(View focused, int direction) {
567 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700568 if (getCurrentPage() > 0) {
569 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 return true;
571 }
572 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700573 if (getCurrentPage() < getPageCount() - 1) {
574 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700575 return true;
576 }
577 }
578 return super.dispatchUnhandledMove(focused, direction);
579 }
580
581 @Override
582 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700583 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
584 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700585 }
586 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700587 if (mCurrentPage > 0) {
588 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 }
590 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700591 if (mCurrentPage < getPageCount() - 1) {
592 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700593 }
594 }
595 }
596
597 /**
598 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700599 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 *
Winson Chung86f77532010-08-24 11:08:22 -0700601 * This happens when live folders requery, and if they're off page, they
602 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700603 */
604 @Override
605 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700606 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700607 View v = focused;
608 while (true) {
609 if (v == current) {
610 super.focusableViewAvailable(focused);
611 return;
612 }
613 if (v == this) {
614 return;
615 }
616 ViewParent parent = v.getParent();
617 if (parent instanceof View) {
618 v = (View)v.getParent();
619 } else {
620 return;
621 }
622 }
623 }
624
625 /**
626 * {@inheritDoc}
627 */
628 @Override
629 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
630 if (disallowIntercept) {
631 // We need to make sure to cancel our long press if
632 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700633 final View currentPage = getChildAt(mCurrentPage);
634 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 }
636 super.requestDisallowInterceptTouchEvent(disallowIntercept);
637 }
638
639 @Override
640 public boolean onInterceptTouchEvent(MotionEvent ev) {
641 /*
642 * This method JUST determines whether we want to intercept the motion.
643 * If we return true, onTouchEvent will be called and we do the actual
644 * scrolling there.
645 */
646
Winson Chung45e1d6e2010-11-09 17:19:49 -0800647 // Skip touch handling if there are no pages to swipe
648 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
649
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 /*
651 * Shortcut the most recurring case: the user is in the dragging
652 * state and he is moving his finger. We want to intercept this
653 * motion.
654 */
655 final int action = ev.getAction();
656 if ((action == MotionEvent.ACTION_MOVE) &&
657 (mTouchState == TOUCH_STATE_SCROLLING)) {
658 return true;
659 }
660
Winson Chung321e9ee2010-08-09 13:37:56 -0700661 switch (action & MotionEvent.ACTION_MASK) {
662 case MotionEvent.ACTION_MOVE: {
663 /*
664 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
665 * whether the user has moved far enough from his original down touch.
666 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700667 if (mActivePointerId != INVALID_POINTER) {
668 determineScrollingStart(ev);
669 break;
670 }
671 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
672 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
673 // i.e. fall through to the next case (don't break)
674 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
675 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700676 }
677
678 case MotionEvent.ACTION_DOWN: {
679 final float x = ev.getX();
680 final float y = ev.getY();
681 // Remember location of down touch
682 mDownMotionX = x;
683 mLastMotionX = x;
684 mLastMotionY = y;
685 mActivePointerId = ev.getPointerId(0);
686 mAllowLongPress = true;
687
688 /*
689 * If being flinged and user touches the screen, initiate drag;
690 * otherwise don't. mScroller.isFinished should be false when
691 * being flinged.
692 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700693 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700694 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
695 if (finishedScrolling) {
696 mTouchState = TOUCH_STATE_REST;
697 mScroller.abortAnimation();
698 } else {
699 mTouchState = TOUCH_STATE_SCROLLING;
700 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700701
Winson Chung86f77532010-08-24 11:08:22 -0700702 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700704 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700705 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
706 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700707 int width = getMeasuredWidth();
708 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700709 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700710 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700711 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700712 mTouchState = TOUCH_STATE_NEXT_PAGE;
713 }
714 }
715 }
716 break;
717 }
718
719 case MotionEvent.ACTION_CANCEL:
720 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700721 mTouchState = TOUCH_STATE_REST;
722 mAllowLongPress = false;
723 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 break;
725
726 case MotionEvent.ACTION_POINTER_UP:
727 onSecondaryPointerUp(ev);
728 break;
729 }
730
731 /*
732 * The only time we want to intercept motion events is if we are in the
733 * drag mode.
734 */
735 return mTouchState != TOUCH_STATE_REST;
736 }
737
Winson Chung80baf5a2010-08-09 16:03:15 -0700738 protected void animateClickFeedback(View v, final Runnable r) {
739 // animate the view slightly to show click feedback running some logic after it is "pressed"
740 Animation anim = AnimationUtils.loadAnimation(getContext(),
741 R.anim.paged_view_click_feedback);
742 anim.setAnimationListener(new AnimationListener() {
743 @Override
744 public void onAnimationStart(Animation animation) {}
745 @Override
746 public void onAnimationRepeat(Animation animation) {
747 r.run();
748 }
749 @Override
750 public void onAnimationEnd(Animation animation) {}
751 });
752 v.startAnimation(anim);
753 }
754
Winson Chung321e9ee2010-08-09 13:37:56 -0700755 /*
756 * Determines if we should change the touch state to start scrolling after the
757 * user moves their touch point too far.
758 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700759 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700760 /*
761 * Locally do absolute value. mLastMotionX is set to the y value
762 * of the down event.
763 */
764 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
765 final float x = ev.getX(pointerIndex);
766 final float y = ev.getY(pointerIndex);
767 final int xDiff = (int) Math.abs(x - mLastMotionX);
768 final int yDiff = (int) Math.abs(y - mLastMotionY);
769
770 final int touchSlop = mTouchSlop;
771 boolean xPaged = xDiff > mPagingTouchSlop;
772 boolean xMoved = xDiff > touchSlop;
773 boolean yMoved = yDiff > touchSlop;
774
775 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700776 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 // Scroll if the user moved far enough along the X axis
778 mTouchState = TOUCH_STATE_SCROLLING;
779 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700780 mTouchX = mScrollX;
781 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
782 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 }
784 // Either way, cancel any pending longpress
785 if (mAllowLongPress) {
786 mAllowLongPress = false;
787 // Try canceling the long press. It could also have been scheduled
788 // by a distant descendant, so use the mAllowLongPress flag to block
789 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700790 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700791 if (currentPage != null) {
792 currentPage.cancelLongPress();
793 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 }
795 }
796 }
797
Adam Cohen21f12b52010-10-07 17:15:37 -0700798 protected boolean handlePagingClicks() {
799 return false;
800 }
801
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 @Override
803 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800804 // Skip touch handling if there are no pages to swipe
805 if (getChildCount() <= 0) return super.onTouchEvent(ev);
806
Michael Jurkab8f06722010-10-10 15:58:46 -0700807 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700808
809 final int action = ev.getAction();
810
811 switch (action & MotionEvent.ACTION_MASK) {
812 case MotionEvent.ACTION_DOWN:
813 /*
814 * If being flinged and user touches, stop the fling. isFinished
815 * will be false if being flinged.
816 */
817 if (!mScroller.isFinished()) {
818 mScroller.abortAnimation();
819 }
820
821 // Remember where the motion event started
822 mDownMotionX = mLastMotionX = ev.getX();
823 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700824 if (mTouchState == TOUCH_STATE_SCROLLING) {
825 pageBeginMoving();
826 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 break;
828
829 case MotionEvent.ACTION_MOVE:
830 if (mTouchState == TOUCH_STATE_SCROLLING) {
831 // Scroll to follow the motion event
832 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
833 final float x = ev.getX(pointerIndex);
834 final int deltaX = (int) (mLastMotionX - x);
835 mLastMotionX = x;
836
837 int sx = getScrollX();
838 if (deltaX < 0) {
839 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700840 mTouchX += Math.max(-mTouchX, deltaX);
841 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
842 if (!mDeferScrollUpdate) {
843 scrollBy(Math.max(-sx, deltaX), 0);
844 } else {
845 // This will trigger a call to computeScroll() on next drawChild() call
846 invalidate();
847 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 }
849 } else if (deltaX > 0) {
850 final int lastChildIndex = getChildCount() - 1;
851 final int availableToScroll = getChildOffset(lastChildIndex) -
852 getRelativeChildOffset(lastChildIndex) - sx;
853 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700854 mTouchX += Math.min(availableToScroll, deltaX);
855 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
856 if (!mDeferScrollUpdate) {
857 scrollBy(Math.min(availableToScroll, deltaX), 0);
858 } else {
859 // This will trigger a call to computeScroll() on next drawChild() call
860 invalidate();
861 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700862 }
863 } else {
864 awakenScrollBars();
865 }
Adam Cohen564976a2010-10-13 18:52:07 -0700866 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 determineScrollingStart(ev);
868 }
869 break;
870
871 case MotionEvent.ACTION_UP:
872 if (mTouchState == TOUCH_STATE_SCROLLING) {
873 final int activePointerId = mActivePointerId;
874 final int pointerIndex = ev.findPointerIndex(activePointerId);
875 final float x = ev.getX(pointerIndex);
876 final VelocityTracker velocityTracker = mVelocityTracker;
877 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
878 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700879 final int deltaX = (int) (x - mDownMotionX);
880 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
881 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700882
Michael Jurka0142d492010-08-25 17:46:15 -0700883 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700884 if ((isSignificantMove && deltaX > 0 ||
885 (isfling && velocityX > snapVelocity)) &&
886 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700887 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700888 } else if ((isSignificantMove && deltaX < 0 ||
889 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700890 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700891 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 } else {
893 snapToDestination();
894 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700895 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 // at this point we have not moved beyond the touch slop
897 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
898 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700899 int nextPage = Math.max(0, mCurrentPage - 1);
900 if (nextPage != mCurrentPage) {
901 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 } else {
903 snapToDestination();
904 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700905 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700906 // at this point we have not moved beyond the touch slop
907 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
908 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700909 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
910 if (nextPage != mCurrentPage) {
911 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700912 } else {
913 snapToDestination();
914 }
915 }
916 mTouchState = TOUCH_STATE_REST;
917 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700918 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 break;
920
921 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700922 if (mTouchState == TOUCH_STATE_SCROLLING) {
923 snapToDestination();
924 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 mTouchState = TOUCH_STATE_REST;
926 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700927 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 break;
929
930 case MotionEvent.ACTION_POINTER_UP:
931 onSecondaryPointerUp(ev);
932 break;
933 }
934
935 return true;
936 }
937
Michael Jurkab8f06722010-10-10 15:58:46 -0700938 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
939 if (mVelocityTracker == null) {
940 mVelocityTracker = VelocityTracker.obtain();
941 }
942 mVelocityTracker.addMovement(ev);
943 }
944
945 private void releaseVelocityTracker() {
946 if (mVelocityTracker != null) {
947 mVelocityTracker.recycle();
948 mVelocityTracker = null;
949 }
950 }
951
Winson Chung321e9ee2010-08-09 13:37:56 -0700952 private void onSecondaryPointerUp(MotionEvent ev) {
953 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
954 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
955 final int pointerId = ev.getPointerId(pointerIndex);
956 if (pointerId == mActivePointerId) {
957 // This was our active pointer going up. Choose a new
958 // active pointer and adjust accordingly.
959 // TODO: Make this decision more intelligent.
960 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
961 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
962 mLastMotionY = ev.getY(newPointerIndex);
963 mActivePointerId = ev.getPointerId(newPointerIndex);
964 if (mVelocityTracker != null) {
965 mVelocityTracker.clear();
966 }
967 }
968 }
969
970 @Override
971 public void requestChildFocus(View child, View focused) {
972 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700973 int page = indexOfChild(child);
974 if (page >= 0 && !isInTouchMode()) {
975 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 }
977 }
978
Winson Chunge3193b92010-09-10 11:44:42 -0700979 protected int getChildIndexForRelativeOffset(int relativeOffset) {
980 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700981 int left;
982 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700983 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700984 left = getRelativeChildOffset(i);
985 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700986 if (left <= relativeOffset && relativeOffset <= right) {
987 return i;
988 }
Winson Chunge3193b92010-09-10 11:44:42 -0700989 }
990 return -1;
991 }
992
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 protected int getRelativeChildOffset(int index) {
994 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
995 }
996
997 protected int getChildOffset(int index) {
998 if (getChildCount() == 0)
999 return 0;
1000
1001 int offset = getRelativeChildOffset(0);
1002 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001003 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001004 }
1005 return offset;
1006 }
1007
Adam Cohend19d3ca2010-09-15 14:43:42 -07001008 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 int minDistanceFromScreenCenter = getMeasuredWidth();
1010 int minDistanceFromScreenCenterIndex = -1;
1011 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1012 final int childCount = getChildCount();
1013 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001014 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 int childWidth = layout.getMeasuredWidth();
1016 int halfChildWidth = (childWidth / 2);
1017 int childCenter = getChildOffset(i) + halfChildWidth;
1018 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1019 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1020 minDistanceFromScreenCenter = distanceFromScreenCenter;
1021 minDistanceFromScreenCenterIndex = i;
1022 }
1023 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001024 return minDistanceFromScreenCenterIndex;
1025 }
1026
1027 protected void snapToDestination() {
1028 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 }
1030
Michael Jurka0142d492010-08-25 17:46:15 -07001031 protected void snapToPageWithVelocity(int whichPage, int velocity) {
1032 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
1033 // can use it
1034 snapToPage(whichPage);
1035 }
1036
1037 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001038 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001039 }
1040
Michael Jurka0142d492010-08-25 17:46:15 -07001041 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001042 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001043
Winson Chung86f77532010-08-24 11:08:22 -07001044 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 final int sX = getScrollX();
1046 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001047 snapToPage(whichPage, delta, duration);
1048 }
1049
1050 protected void snapToPage(int whichPage, int delta, int duration) {
1051 mNextPage = whichPage;
1052
1053 View focusedChild = getFocusedChild();
1054 if (focusedChild != null && whichPage != mCurrentPage &&
1055 focusedChild == getChildAt(mCurrentPage)) {
1056 focusedChild.clearFocus();
1057 }
1058
1059 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 awakenScrollBars(duration);
1061 if (duration == 0) {
1062 duration = Math.abs(delta);
1063 }
1064
1065 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001066 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001067
1068 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001069 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001070 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 invalidate();
1072 }
1073
1074 @Override
1075 protected Parcelable onSaveInstanceState() {
1076 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001077 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 return state;
1079 }
1080
1081 @Override
1082 protected void onRestoreInstanceState(Parcelable state) {
1083 SavedState savedState = (SavedState) state;
1084 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001085 if (savedState.currentPage != -1) {
1086 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001087 }
1088 }
1089
1090 public void scrollLeft() {
1091 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001092 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001094 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 }
1096 }
1097
1098 public void scrollRight() {
1099 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001100 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001101 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001102 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001103 }
1104 }
1105
Winson Chung86f77532010-08-24 11:08:22 -07001106 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 int result = -1;
1108 if (v != null) {
1109 ViewParent vp = v.getParent();
1110 int count = getChildCount();
1111 for (int i = 0; i < count; i++) {
1112 if (vp == getChildAt(i)) {
1113 return i;
1114 }
1115 }
1116 }
1117 return result;
1118 }
1119
1120 /**
1121 * @return True is long presses are still allowed for the current touch
1122 */
1123 public boolean allowLongPress() {
1124 return mAllowLongPress;
1125 }
1126
Michael Jurka0142d492010-08-25 17:46:15 -07001127 /**
1128 * Set true to allow long-press events to be triggered, usually checked by
1129 * {@link Launcher} to accept or block dpad-initiated long-presses.
1130 */
1131 public void setAllowLongPress(boolean allowLongPress) {
1132 mAllowLongPress = allowLongPress;
1133 }
1134
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001136 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001137
1138 SavedState(Parcelable superState) {
1139 super(superState);
1140 }
1141
1142 private SavedState(Parcel in) {
1143 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001144 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001145 }
1146
1147 @Override
1148 public void writeToParcel(Parcel out, int flags) {
1149 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001150 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001151 }
1152
1153 public static final Parcelable.Creator<SavedState> CREATOR =
1154 new Parcelable.Creator<SavedState>() {
1155 public SavedState createFromParcel(Parcel in) {
1156 return new SavedState(in);
1157 }
1158
1159 public SavedState[] newArray(int size) {
1160 return new SavedState[size];
1161 }
1162 };
1163 }
1164
Winson Chung86f77532010-08-24 11:08:22 -07001165 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001166 if (mContentIsRefreshable) {
1167 final int count = getChildCount();
1168 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001169 int lowerPageBound = getAssociatedLowerPageBound(page);
1170 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001171 for (int i = 0; i < count; ++i) {
1172 final ViewGroup layout = (ViewGroup) getChildAt(i);
1173 final int childCount = layout.getChildCount();
1174 if (lowerPageBound <= i && i <= upperPageBound) {
1175 if (mDirtyPageContent.get(i)) {
1176 syncPageItems(i);
1177 mDirtyPageContent.set(i, false);
1178 }
1179 } else {
1180 if (childCount > 0) {
1181 layout.removeAllViews();
1182 }
1183 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001184 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001185 }
1186 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001187 }
1188 }
1189
Winson Chunge3193b92010-09-10 11:44:42 -07001190 protected int getAssociatedLowerPageBound(int page) {
1191 return Math.max(0, page - 1);
1192 }
1193 protected int getAssociatedUpperPageBound(int page) {
1194 final int count = getChildCount();
1195 return Math.min(page + 1, count - 1);
1196 }
1197
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001198 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001199 if (isChoiceMode(CHOICE_MODE_NONE)) {
1200 mChoiceMode = mode;
1201 mActionMode = startActionMode(callback);
1202 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001203 }
1204
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001205 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001206 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001207 mChoiceMode = CHOICE_MODE_NONE;
1208 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001209 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001210 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001211 }
1212 }
1213
1214 protected boolean isChoiceMode(int mode) {
1215 return mChoiceMode == mode;
1216 }
1217
1218 protected ArrayList<Checkable> getCheckedGrandchildren() {
1219 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1220 final int childCount = getChildCount();
1221 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001222 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001223 final int grandChildCount = layout.getChildCount();
1224 for (int j = 0; j < grandChildCount; ++j) {
1225 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001226 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001227 checked.add((Checkable) v);
1228 }
1229 }
1230 }
1231 return checked;
1232 }
1233
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001234 /**
1235 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1236 * Otherwise, returns null.
1237 */
1238 protected Checkable getSingleCheckedGrandchild() {
1239 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1240 final int childCount = getChildCount();
1241 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001242 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001243 final int grandChildCount = layout.getChildCount();
1244 for (int j = 0; j < grandChildCount; ++j) {
1245 final View v = layout.getChildAt(j);
1246 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1247 return (Checkable) v;
1248 }
1249 }
1250 }
1251 }
1252 return null;
1253 }
1254
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001255 public Object getChosenItem() {
1256 View checkedView = (View) getSingleCheckedGrandchild();
1257 if (checkedView != null) {
1258 return checkedView.getTag();
1259 }
1260 return null;
1261 }
1262
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001263 protected void resetCheckedGrandchildren() {
1264 // loop through children, and set all of their children to _not_ be checked
1265 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1266 for (int i = 0; i < checked.size(); ++i) {
1267 final Checkable c = checked.get(i);
1268 c.setChecked(false);
1269 }
1270 }
1271
Winson Chung86f77532010-08-24 11:08:22 -07001272 /**
1273 * This method is called ONLY to synchronize the number of pages that the paged view has.
1274 * To actually fill the pages with information, implement syncPageItems() below. It is
1275 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1276 * and therefore, individual page items do not need to be updated in this method.
1277 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001278 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001279
1280 /**
1281 * This method is called to synchronize the items that are on a particular page. If views on
1282 * the page can be reused, then they should be updated within this method.
1283 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001284 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001285
Winson Chung321e9ee2010-08-09 13:37:56 -07001286 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001287 if (mContentIsRefreshable) {
1288 // Update all the pages
1289 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001290
Michael Jurka0142d492010-08-25 17:46:15 -07001291 // Mark each of the pages as dirty
1292 final int count = getChildCount();
1293 mDirtyPageContent.clear();
1294 for (int i = 0; i < count; ++i) {
1295 mDirtyPageContent.add(true);
1296 }
1297
1298 // Load any pages that are necessary for the current window of views
1299 loadAssociatedPages(mCurrentPage);
1300 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001301 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001302 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001303 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001304 }
1305}