blob: 856507d648169923284b52774a14da2aac14854e [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
Michael Jurkaaf91de02010-11-23 16:23:58 -080019import com.android.launcher.R;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070020
Winson Chung321e9ee2010-08-09 13:37:56 -070021import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070022import android.content.res.TypedArray;
Winson Chung241c3b42010-08-25 16:53:03 -070023import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.graphics.Canvas;
25import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.os.Parcel;
27import android.os.Parcelable;
28import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070029import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.view.MotionEvent;
31import android.view.VelocityTracker;
32import android.view.View;
33import android.view.ViewConfiguration;
34import android.view.ViewGroup;
35import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070036import android.view.animation.Animation;
Winson Chunge3193b92010-09-10 11:44:42 -070037import android.view.animation.AnimationUtils;
Adam Cohene0f66b52010-11-23 15:06:07 -080038import android.view.animation.Interpolator;
Adam Cohen68d73932010-11-15 10:50:58 -080039import android.view.animation.Animation.AnimationListener;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070040import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.widget.Scroller;
42
Michael Jurkaaf91de02010-11-23 16:23:58 -080043import java.util.ArrayList;
44import java.util.HashMap;
Winson Chung80baf5a2010-08-09 16:03:15 -070045
Winson Chung321e9ee2010-08-09 13:37:56 -070046/**
47 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070048 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070049 */
50public abstract class PagedView extends ViewGroup {
51 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070052 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070053
Winson Chung86f77532010-08-24 11:08:22 -070054 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070055 private static final int MIN_LENGTH_FOR_FLING = 25;
56 // The min drag distance to trigger a page shift (regardless of velocity)
57 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070058
Adam Cohene0f66b52010-11-23 15:06:07 -080059 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070060 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070061
Adam Cohene0f66b52010-11-23 15:06:07 -080062 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
63 private static final int MINIMUM_SNAP_VELOCITY = 2200;
64 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohen68d73932010-11-15 10:50:58 -080065
Michael Jurka0142d492010-08-25 17:46:15 -070066 // the velocity at which a fling gesture will cause us to snap to the next page
67 protected int mSnapVelocity = 500;
68
69 protected float mSmoothingTime;
70 protected float mTouchX;
71
72 protected boolean mFirstLayout = true;
73
74 protected int mCurrentPage;
75 protected int mNextPage = INVALID_PAGE;
Adam Cohen68d73932010-11-15 10:50:58 -080076 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070077 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070078 private VelocityTracker mVelocityTracker;
79
80 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080081 protected float mLastMotionX;
82 protected float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070083 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070084
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected final static int TOUCH_STATE_REST = 0;
86 protected final static int TOUCH_STATE_SCROLLING = 1;
87 protected final static int TOUCH_STATE_PREV_PAGE = 2;
88 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070089 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070090
Michael Jurka0142d492010-08-25 17:46:15 -070091 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070094
Michael Jurka7426c422010-11-11 15:23:47 -080095 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070096
Michael Jurka7426c422010-11-11 15:23:47 -080097 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070098 private int mPagingTouchSlop;
99 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700100 protected int mPageSpacing;
101 protected int mPageLayoutPaddingTop;
102 protected int mPageLayoutPaddingBottom;
103 protected int mPageLayoutPaddingLeft;
104 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700105 protected int mPageLayoutWidthGap;
106 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700107 protected int mCellCountX;
108 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700109 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800110 protected boolean mAllowOverScroll = true;
111 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Michael Jurkad3ef3062010-11-23 16:23:58 -0800113 // parameter that adjusts the layout to be optimized for CellLayouts with that scale factor
114 protected float mLayoutScale = 1.0f;
115
Michael Jurka5f1c5092010-09-03 14:15:02 -0700116 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700117
Michael Jurka5f1c5092010-09-03 14:15:02 -0700118 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700119
Winson Chung86f77532010-08-24 11:08:22 -0700120 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Winson Chung86f77532010-08-24 11:08:22 -0700122 private ArrayList<Boolean> mDirtyPageContent;
123 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700125 // choice modes
126 protected static final int CHOICE_MODE_NONE = 0;
127 protected static final int CHOICE_MODE_SINGLE = 1;
128 // Multiple selection mode is not supported by all Launcher actions atm
129 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700130
Michael Jurkae17e19c2010-09-28 11:01:39 -0700131 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700132 private ActionMode mActionMode;
133
Winson Chung241c3b42010-08-25 16:53:03 -0700134 protected PagedViewIconCache mPageViewIconCache;
135
Michael Jurka0142d492010-08-25 17:46:15 -0700136 // If true, syncPages and syncPageItems will be called to refresh pages
137 protected boolean mContentIsRefreshable = true;
138
139 // If true, modify alpha of neighboring pages as user scrolls left/right
140 protected boolean mFadeInAdjacentScreens = true;
141
142 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
143 // to switch to a new page
144 protected boolean mUsePagingTouchSlop = true;
145
146 // If true, the subclass should directly update mScrollX itself in its computeScroll method
147 // (SmoothPagedView does this)
148 protected boolean mDeferScrollUpdate = false;
149
Patrick Dubroy1262e362010-10-06 15:49:50 -0700150 protected boolean mIsPageMoving = false;
151
Winson Chung241c3b42010-08-25 16:53:03 -0700152 /**
153 * Simple cache mechanism for PagedViewIcon outlines.
154 */
155 class PagedViewIconCache {
156 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
157
158 public void clear() {
159 iconOutlineCache.clear();
160 }
161 public void addOutline(Object key, Bitmap b) {
162 iconOutlineCache.put(key, b);
163 }
164 public void removeOutline(Object key) {
165 if (iconOutlineCache.containsKey(key)) {
166 iconOutlineCache.remove(key);
167 }
168 }
169 public Bitmap getOutline(Object key) {
170 return iconOutlineCache.get(key);
171 }
172 }
173
Winson Chung86f77532010-08-24 11:08:22 -0700174 public interface PageSwitchListener {
175 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700176 }
177
Winson Chung321e9ee2010-08-09 13:37:56 -0700178 public PagedView(Context context) {
179 this(context, null);
180 }
181
182 public PagedView(Context context, AttributeSet attrs) {
183 this(context, attrs, 0);
184 }
185
186 public PagedView(Context context, AttributeSet attrs, int defStyle) {
187 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700188 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700189
Adam Cohen9c4949e2010-10-05 12:27:22 -0700190 TypedArray a = context.obtainStyledAttributes(attrs,
191 R.styleable.PagedView, defStyle, 0);
192 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
193 mPageLayoutPaddingTop = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutPaddingTop, 10);
195 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
196 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
197 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
198 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
199 mPageLayoutPaddingRight = a.getDimensionPixelSize(
200 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700201 mPageLayoutWidthGap = a.getDimensionPixelSize(
202 R.styleable.PagedView_pageLayoutWidthGap, -1);
203 mPageLayoutHeightGap = a.getDimensionPixelSize(
204 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700205 a.recycle();
206
Winson Chung321e9ee2010-08-09 13:37:56 -0700207 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700208 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 }
210
211 /**
212 * Initializes various states for this workspace.
213 */
Michael Jurka0142d492010-08-25 17:46:15 -0700214 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700215 mDirtyPageContent = new ArrayList<Boolean>();
216 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700217 mPageViewIconCache = new PagedViewIconCache();
Adam Cohene0f66b52010-11-23 15:06:07 -0800218 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700219 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700220 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700221
222 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
223 mTouchSlop = configuration.getScaledTouchSlop();
224 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
225 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
226 }
227
Winson Chung86f77532010-08-24 11:08:22 -0700228 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
229 mPageSwitchListener = pageSwitchListener;
230 if (mPageSwitchListener != null) {
231 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 }
233 }
234
235 /**
Winson Chung86f77532010-08-24 11:08:22 -0700236 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 *
Winson Chung86f77532010-08-24 11:08:22 -0700238 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 */
Winson Chung86f77532010-08-24 11:08:22 -0700240 int getCurrentPage() {
241 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 }
243
Winson Chung86f77532010-08-24 11:08:22 -0700244 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 return getChildCount();
246 }
247
Winson Chung86f77532010-08-24 11:08:22 -0700248 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 return getChildAt(index);
250 }
251
252 int getScrollWidth() {
253 return getWidth();
254 }
255
256 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800257 * Updates the scroll of the current page immediately to its final scroll position. We use this
258 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
259 * the previous tab page.
260 */
261 protected void updateCurrentPageScroll() {
262 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
263 scrollTo(newX, 0);
264 mScroller.setFinalX(newX);
265 }
266
267 /**
Winson Chung86f77532010-08-24 11:08:22 -0700268 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 */
Winson Chung86f77532010-08-24 11:08:22 -0700270 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800271 if (!mScroller.isFinished()) {
272 mScroller.abortAnimation();
273 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800274 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
275 // the default
276 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800277 return;
278 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700279
Winson Chung86f77532010-08-24 11:08:22 -0700280 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800281 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700282
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700284 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 }
286
Michael Jurka0142d492010-08-25 17:46:15 -0700287 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700288 if (mPageSwitchListener != null) {
289 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 }
291 }
292
Patrick Dubroy1262e362010-10-06 15:49:50 -0700293 private void pageBeginMoving() {
294 mIsPageMoving = true;
295 onPageBeginMoving();
296 }
297
298 private void pageEndMoving() {
299 onPageEndMoving();
300 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700301 }
302
303 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700304 protected void onPageBeginMoving() {
305 }
306
307 // a method that subclasses can override to add behavior
308 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700309 }
310
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 /**
Winson Chung86f77532010-08-24 11:08:22 -0700312 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700313 *
314 * @param l The listener used to respond to long clicks.
315 */
316 @Override
317 public void setOnLongClickListener(OnLongClickListener l) {
318 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700319 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700321 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700322 }
323 }
324
325 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800326 public void scrollBy(int x, int y) {
327 scrollTo(mUnboundedScrollX + x, mScrollY + y);
328 }
329
330 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700331 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800332 mUnboundedScrollX = x;
333
334 if (x < 0) {
335 super.scrollTo(0, y);
336 if (mAllowOverScroll) {
337 overScroll(x);
338 }
339 } else if (x > mMaxScrollX) {
340 super.scrollTo(mMaxScrollX, y);
341 if (mAllowOverScroll) {
342 overScroll(x - mMaxScrollX);
343 }
344 } else {
345 super.scrollTo(x, y);
346 }
347
Michael Jurka0142d492010-08-25 17:46:15 -0700348 mTouchX = x;
349 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
350 }
351
352 // we moved this functionality to a helper function so SmoothPagedView can reuse it
353 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700355 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700357 invalidate();
358 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700359 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700360 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700361 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700362 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700363 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800364 // We don't want to trigger a page end moving unless the page has settled
365 // and the user has stopped scrolling
366 if (mTouchState == TOUCH_STATE_REST) {
367 pageEndMoving();
368 }
Michael Jurka0142d492010-08-25 17:46:15 -0700369 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 }
Michael Jurka0142d492010-08-25 17:46:15 -0700371 return false;
372 }
373
374 @Override
375 public void computeScroll() {
376 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 }
378
379 @Override
380 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
381 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
382 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
383 if (widthMode != MeasureSpec.EXACTLY) {
384 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
385 }
386
Adam Lesinski6b879f02010-11-04 16:15:23 -0700387 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
388 * of the All apps view on XLarge displays to not take up more space then it needs. Width
389 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
390 * each page to have the same width.
391 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700393 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
394 int maxChildHeight = 0;
395
396 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700397
398 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700399 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 final int childCount = getChildCount();
401 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700402 // disallowing padding in paged view (just pass 0)
403 final View child = getChildAt(i);
404 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
405
406 int childWidthMode;
407 if (lp.width == LayoutParams.WRAP_CONTENT) {
408 childWidthMode = MeasureSpec.AT_MOST;
409 } else {
410 childWidthMode = MeasureSpec.EXACTLY;
411 }
412
413 int childHeightMode;
414 if (lp.height == LayoutParams.WRAP_CONTENT) {
415 childHeightMode = MeasureSpec.AT_MOST;
416 } else {
417 childHeightMode = MeasureSpec.EXACTLY;
418 }
419
420 final int childWidthMeasureSpec =
421 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
422 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700423 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700424
425 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700426 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
427 }
428
429 if (heightMode == MeasureSpec.AT_MOST) {
430 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800432 if (childCount > 0) {
433 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
434 } else {
435 mMaxScrollX = 0;
436 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700437
438 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 }
440
Michael Jurkaaf91de02010-11-23 16:23:58 -0800441 protected void moveToNewPageWithoutMovingCellLayouts(int newCurrentPage) {
442 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
443 int delta = newX - mScrollX;
444
445 final int screenCount = getChildCount();
446 for (int i = 0; i < screenCount; i++) {
447 CellLayout cl = (CellLayout) getChildAt(i);
448 cl.setX(cl.getX() + delta);
449 }
450 setCurrentPage(newCurrentPage);
451 }
452
Michael Jurkad3ef3062010-11-23 16:23:58 -0800453 // A layout scale of 1.0f assumes that the CellLayouts, in their unshrunken state, have a
454 // scale of 1.0f. A layout scale of 0.8f assumes the CellLayouts have a scale of 0.8f, and
455 // tightens the layout accordingly
456 public void setLayoutScale(float childrenScale) {
457 mLayoutScale = childrenScale;
458
459 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
460 int childCount = getChildCount();
461 float childrenX[] = new float[childCount];
462 float childrenY[] = new float[childCount];
463 for (int i = 0; i < childCount; i++) {
464 final View child = getChildAt(i);
465 childrenX[i] = child.getX();
466 childrenY[i] = child.getY();
467 }
468 onLayout(false, mLeft, mTop, mRight, mBottom);
469 for (int i = 0; i < childCount; i++) {
470 final View child = getChildAt(i);
471 child.setX(childrenX[i]);
472 child.setY(childrenY[i]);
473 }
474 // Also, the page offset has changed (since the pages are now smaller);
475 // update the page offset, but again preserving absolute X and Y coordinates
476 moveToNewPageWithoutMovingCellLayouts(mCurrentPage);
477 }
478
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700480 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700481 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
482 setHorizontalScrollBarEnabled(false);
483 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
484 scrollTo(newX, 0);
485 mScroller.setFinalX(newX);
486 setHorizontalScrollBarEnabled(true);
487 mFirstLayout = false;
488 }
489
Adam Lesinski6b879f02010-11-04 16:15:23 -0700490 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 final int childCount = getChildCount();
492 int childLeft = 0;
493 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700494 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700495 }
496
497 for (int i = 0; i < childCount; i++) {
498 final View child = getChildAt(i);
499 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800500 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700501 final int childHeight = child.getMeasuredHeight();
502 int childTop = mPaddingTop;
503 if (mCenterPagesVertically) {
504 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
505 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800506
Adam Lesinski6b879f02010-11-04 16:15:23 -0700507 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700509 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 }
511 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800512 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
513 mFirstLayout = false;
514 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
516
Winson Chunge3193b92010-09-10 11:44:42 -0700517 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700518 if (mFadeInAdjacentScreens) {
519 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700520 int halfScreenSize = getMeasuredWidth() / 2;
521 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700522 final int childCount = getChildCount();
523 for (int i = 0; i < childCount; ++i) {
524 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800525 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700526 int halfChildWidth = (childWidth / 2);
527 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700528
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700529 // On the first layout, we may not have a width nor a proper offset, so for now
530 // we should just assume full page width (and calculate the offset according to
531 // that).
532 if (childWidth <= 0) {
533 childWidth = getMeasuredWidth();
534 childCenter = (i * childWidth) + (childWidth / 2);
535 }
536
Winson Chunge8878e32010-09-15 20:37:09 -0700537 int d = halfChildWidth;
538 int distanceFromScreenCenter = childCenter - screenCenter;
539 if (distanceFromScreenCenter > 0) {
540 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700542 }
Michael Jurka0142d492010-08-25 17:46:15 -0700543 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700544 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800545 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700546 }
Michael Jurka0142d492010-08-25 17:46:15 -0700547 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700548 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700549
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700550 // Preventing potential divide-by-zero
551 d = Math.max(1, d);
552
Winson Chunge8878e32010-09-15 20:37:09 -0700553 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
554 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
555 float alpha = 1.0f - dimAlpha;
556
Adam Cohenf34bab52010-09-30 14:11:56 -0700557 if (alpha < ALPHA_QUANTIZE_LEVEL) {
558 alpha = 0.0f;
559 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
560 alpha = 1.0f;
561 }
562
Michael Jurka0142d492010-08-25 17:46:15 -0700563 if (Float.compare(alpha, layout.getAlpha()) != 0) {
564 layout.setAlpha(alpha);
565 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 }
Michael Jurka0142d492010-08-25 17:46:15 -0700567 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700568 }
569 }
Winson Chunge3193b92010-09-10 11:44:42 -0700570 }
571
Adam Cohenf34bab52010-09-30 14:11:56 -0700572 protected void screenScrolled(int screenCenter) {
573 }
574
Winson Chunge3193b92010-09-10 11:44:42 -0700575 @Override
576 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700577 int halfScreenSize = getMeasuredWidth() / 2;
578 int screenCenter = mScrollX + halfScreenSize;
579
580 if (screenCenter != mLastScreenCenter) {
581 screenScrolled(screenCenter);
582 updateAdjacentPagesAlpha();
583 mLastScreenCenter = screenCenter;
584 }
Michael Jurka0142d492010-08-25 17:46:15 -0700585
586 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700587 // As an optimization, this code assumes that all pages have the same width as the 0th
588 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700589 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700590 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800591 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700592 final int screenWidth = getMeasuredWidth();
593 int x = getRelativeChildOffset(0) + pageWidth;
594 int leftScreen = 0;
595 int rightScreen = 0;
596 while (x <= mScrollX) {
597 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700598 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700599 // replace above line with this if you don't assume all pages have same width as 0th
600 // page:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800601 // x += getScaledMeasuredWidth(getChildAt(leftScreen));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700602 }
603 rightScreen = leftScreen;
604 while (x < mScrollX + screenWidth) {
605 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700606 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700607 // replace above line with this if you don't assume all pages have same width as 0th
608 // page:
609 //if (rightScreen < pageCount) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800610 // x += getScaledMeasuredWidth(getChildAt(rightScreen));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700611 //}
612 }
613 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700614
Michael Jurkac4fb9172010-09-02 17:19:20 -0700615 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800616 // Clip to the bounds
617 canvas.save();
618 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
619 mScrollY + mBottom - mTop);
620
Michael Jurkac4fb9172010-09-02 17:19:20 -0700621 for (int i = leftScreen; i <= rightScreen; i++) {
622 drawChild(canvas, getChildAt(i), drawingTime);
623 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800624 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700625 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700626 }
627
628 @Override
629 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700630 int page = indexOfChild(child);
631 if (page != mCurrentPage || !mScroller.isFinished()) {
632 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700633 return true;
634 }
635 return false;
636 }
637
638 @Override
639 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700640 int focusablePage;
641 if (mNextPage != INVALID_PAGE) {
642 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700644 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 }
Winson Chung86f77532010-08-24 11:08:22 -0700646 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700647 if (v != null) {
648 v.requestFocus(direction, previouslyFocusedRect);
649 }
650 return false;
651 }
652
653 @Override
654 public boolean dispatchUnhandledMove(View focused, int direction) {
655 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700656 if (getCurrentPage() > 0) {
657 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 return true;
659 }
660 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700661 if (getCurrentPage() < getPageCount() - 1) {
662 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700663 return true;
664 }
665 }
666 return super.dispatchUnhandledMove(focused, direction);
667 }
668
669 @Override
670 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700671 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
672 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 }
674 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700675 if (mCurrentPage > 0) {
676 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 }
678 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700679 if (mCurrentPage < getPageCount() - 1) {
680 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700681 }
682 }
683 }
684
685 /**
686 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700687 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 *
Winson Chung86f77532010-08-24 11:08:22 -0700689 * This happens when live folders requery, and if they're off page, they
690 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 */
692 @Override
693 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700694 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 View v = focused;
696 while (true) {
697 if (v == current) {
698 super.focusableViewAvailable(focused);
699 return;
700 }
701 if (v == this) {
702 return;
703 }
704 ViewParent parent = v.getParent();
705 if (parent instanceof View) {
706 v = (View)v.getParent();
707 } else {
708 return;
709 }
710 }
711 }
712
713 /**
714 * {@inheritDoc}
715 */
716 @Override
717 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
718 if (disallowIntercept) {
719 // We need to make sure to cancel our long press if
720 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700721 final View currentPage = getChildAt(mCurrentPage);
722 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 }
724 super.requestDisallowInterceptTouchEvent(disallowIntercept);
725 }
726
727 @Override
728 public boolean onInterceptTouchEvent(MotionEvent ev) {
729 /*
730 * This method JUST determines whether we want to intercept the motion.
731 * If we return true, onTouchEvent will be called and we do the actual
732 * scrolling there.
733 */
734
Winson Chung45e1d6e2010-11-09 17:19:49 -0800735 // Skip touch handling if there are no pages to swipe
736 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
737
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 /*
739 * Shortcut the most recurring case: the user is in the dragging
740 * state and he is moving his finger. We want to intercept this
741 * motion.
742 */
743 final int action = ev.getAction();
744 if ((action == MotionEvent.ACTION_MOVE) &&
745 (mTouchState == TOUCH_STATE_SCROLLING)) {
746 return true;
747 }
748
Winson Chung321e9ee2010-08-09 13:37:56 -0700749 switch (action & MotionEvent.ACTION_MASK) {
750 case MotionEvent.ACTION_MOVE: {
751 /*
752 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
753 * whether the user has moved far enough from his original down touch.
754 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700755 if (mActivePointerId != INVALID_POINTER) {
756 determineScrollingStart(ev);
757 break;
758 }
759 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
760 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
761 // i.e. fall through to the next case (don't break)
762 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
763 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765
766 case MotionEvent.ACTION_DOWN: {
767 final float x = ev.getX();
768 final float y = ev.getY();
769 // Remember location of down touch
770 mDownMotionX = x;
771 mLastMotionX = x;
772 mLastMotionY = y;
773 mActivePointerId = ev.getPointerId(0);
774 mAllowLongPress = true;
775
776 /*
777 * If being flinged and user touches the screen, initiate drag;
778 * otherwise don't. mScroller.isFinished should be false when
779 * being flinged.
780 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700781 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700782 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
783 if (finishedScrolling) {
784 mTouchState = TOUCH_STATE_REST;
785 mScroller.abortAnimation();
786 } else {
787 mTouchState = TOUCH_STATE_SCROLLING;
788 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700789
Winson Chung86f77532010-08-24 11:08:22 -0700790 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700792 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
794 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700795 int width = getMeasuredWidth();
796 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700797 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700799 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700800 mTouchState = TOUCH_STATE_NEXT_PAGE;
801 }
802 }
803 }
804 break;
805 }
806
Winson Chung321e9ee2010-08-09 13:37:56 -0700807 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800808 onWallpaperTap(ev);
809 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 mTouchState = TOUCH_STATE_REST;
811 mAllowLongPress = false;
812 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700813 break;
814
815 case MotionEvent.ACTION_POINTER_UP:
816 onSecondaryPointerUp(ev);
817 break;
818 }
819
820 /*
821 * The only time we want to intercept motion events is if we are in the
822 * drag mode.
823 */
824 return mTouchState != TOUCH_STATE_REST;
825 }
826
Winson Chung80baf5a2010-08-09 16:03:15 -0700827 protected void animateClickFeedback(View v, final Runnable r) {
828 // animate the view slightly to show click feedback running some logic after it is "pressed"
829 Animation anim = AnimationUtils.loadAnimation(getContext(),
830 R.anim.paged_view_click_feedback);
831 anim.setAnimationListener(new AnimationListener() {
832 @Override
833 public void onAnimationStart(Animation animation) {}
834 @Override
835 public void onAnimationRepeat(Animation animation) {
836 r.run();
837 }
838 @Override
839 public void onAnimationEnd(Animation animation) {}
840 });
841 v.startAnimation(anim);
842 }
843
Winson Chung321e9ee2010-08-09 13:37:56 -0700844 /*
845 * Determines if we should change the touch state to start scrolling after the
846 * user moves their touch point too far.
847 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700848 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 /*
850 * Locally do absolute value. mLastMotionX is set to the y value
851 * of the down event.
852 */
853 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
854 final float x = ev.getX(pointerIndex);
855 final float y = ev.getY(pointerIndex);
856 final int xDiff = (int) Math.abs(x - mLastMotionX);
857 final int yDiff = (int) Math.abs(y - mLastMotionY);
858
859 final int touchSlop = mTouchSlop;
860 boolean xPaged = xDiff > mPagingTouchSlop;
861 boolean xMoved = xDiff > touchSlop;
862 boolean yMoved = yDiff > touchSlop;
863
864 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700865 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 // Scroll if the user moved far enough along the X axis
867 mTouchState = TOUCH_STATE_SCROLLING;
868 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700869 mTouchX = mScrollX;
870 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
871 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700872 }
873 // Either way, cancel any pending longpress
874 if (mAllowLongPress) {
875 mAllowLongPress = false;
876 // Try canceling the long press. It could also have been scheduled
877 // by a distant descendant, so use the mAllowLongPress flag to block
878 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700879 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700880 if (currentPage != null) {
881 currentPage.cancelLongPress();
882 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 }
884 }
885 }
886
Adam Cohen21f12b52010-10-07 17:15:37 -0700887 protected boolean handlePagingClicks() {
888 return false;
889 }
890
Adam Cohene0f66b52010-11-23 15:06:07 -0800891 // This curve determines how the effect of scrolling over the limits of the page dimishes
892 // as the user pulls further and further from the bounds
893 private float overScrollInfluenceCurve(float f) {
894 f -= 1.0f;
895 return f * f * f + 1.0f;
896 }
897
Adam Cohen68d73932010-11-15 10:50:58 -0800898 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800899 int screenSize = getMeasuredWidth();
900
901 float f = (amount / screenSize);
902
903 if (f == 0) return;
904 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
905
906 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800907 if (amount < 0) {
908 mScrollX = overScrollAmount;
909 } else {
910 mScrollX = mMaxScrollX + overScrollAmount;
911 }
912 invalidate();
913 }
914
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 @Override
916 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800917 // Skip touch handling if there are no pages to swipe
918 if (getChildCount() <= 0) return super.onTouchEvent(ev);
919
Michael Jurkab8f06722010-10-10 15:58:46 -0700920 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700921
922 final int action = ev.getAction();
923
924 switch (action & MotionEvent.ACTION_MASK) {
925 case MotionEvent.ACTION_DOWN:
926 /*
927 * If being flinged and user touches, stop the fling. isFinished
928 * will be false if being flinged.
929 */
930 if (!mScroller.isFinished()) {
931 mScroller.abortAnimation();
932 }
933
934 // Remember where the motion event started
935 mDownMotionX = mLastMotionX = ev.getX();
936 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700937 if (mTouchState == TOUCH_STATE_SCROLLING) {
938 pageBeginMoving();
939 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700940 break;
941
942 case MotionEvent.ACTION_MOVE:
943 if (mTouchState == TOUCH_STATE_SCROLLING) {
944 // Scroll to follow the motion event
945 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
946 final float x = ev.getX(pointerIndex);
947 final int deltaX = (int) (mLastMotionX - x);
948 mLastMotionX = x;
949
Adam Cohen68d73932010-11-15 10:50:58 -0800950 if (deltaX != 0) {
951 mTouchX += deltaX;
952 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
953 if (!mDeferScrollUpdate) {
954 scrollBy(deltaX, 0);
955 } else {
956 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 }
958 } else {
959 awakenScrollBars();
960 }
Adam Cohen564976a2010-10-13 18:52:07 -0700961 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700962 determineScrollingStart(ev);
963 }
964 break;
965
966 case MotionEvent.ACTION_UP:
967 if (mTouchState == TOUCH_STATE_SCROLLING) {
968 final int activePointerId = mActivePointerId;
969 final int pointerIndex = ev.findPointerIndex(activePointerId);
970 final float x = ev.getX(pointerIndex);
971 final VelocityTracker velocityTracker = mVelocityTracker;
972 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
973 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700974 final int deltaX = (int) (x - mDownMotionX);
975 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
976 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700977
Michael Jurka0142d492010-08-25 17:46:15 -0700978 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700979 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800980 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700981 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700982 } else if ((isSignificantMove && deltaX < 0 ||
983 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700984 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700985 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 } else {
987 snapToDestination();
988 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700989 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700990 // at this point we have not moved beyond the touch slop
991 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
992 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700993 int nextPage = Math.max(0, mCurrentPage - 1);
994 if (nextPage != mCurrentPage) {
995 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 } else {
997 snapToDestination();
998 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700999 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 // at this point we have not moved beyond the touch slop
1001 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1002 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001003 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1004 if (nextPage != mCurrentPage) {
1005 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 } else {
1007 snapToDestination();
1008 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001009 } else {
1010 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 }
1012 mTouchState = TOUCH_STATE_REST;
1013 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001014 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 break;
1016
1017 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001018 if (mTouchState == TOUCH_STATE_SCROLLING) {
1019 snapToDestination();
1020 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 mTouchState = TOUCH_STATE_REST;
1022 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001023 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001024 break;
1025
1026 case MotionEvent.ACTION_POINTER_UP:
1027 onSecondaryPointerUp(ev);
1028 break;
1029 }
1030
1031 return true;
1032 }
1033
Michael Jurkab8f06722010-10-10 15:58:46 -07001034 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1035 if (mVelocityTracker == null) {
1036 mVelocityTracker = VelocityTracker.obtain();
1037 }
1038 mVelocityTracker.addMovement(ev);
1039 }
1040
1041 private void releaseVelocityTracker() {
1042 if (mVelocityTracker != null) {
1043 mVelocityTracker.recycle();
1044 mVelocityTracker = null;
1045 }
1046 }
1047
Winson Chung321e9ee2010-08-09 13:37:56 -07001048 private void onSecondaryPointerUp(MotionEvent ev) {
1049 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1050 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1051 final int pointerId = ev.getPointerId(pointerIndex);
1052 if (pointerId == mActivePointerId) {
1053 // This was our active pointer going up. Choose a new
1054 // active pointer and adjust accordingly.
1055 // TODO: Make this decision more intelligent.
1056 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1057 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1058 mLastMotionY = ev.getY(newPointerIndex);
1059 mActivePointerId = ev.getPointerId(newPointerIndex);
1060 if (mVelocityTracker != null) {
1061 mVelocityTracker.clear();
1062 }
1063 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001064 if (mTouchState == TOUCH_STATE_REST) {
1065 onWallpaperTap(ev);
1066 }
1067 }
1068
1069 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 }
1071
1072 @Override
1073 public void requestChildFocus(View child, View focused) {
1074 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001075 int page = indexOfChild(child);
1076 if (page >= 0 && !isInTouchMode()) {
1077 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 }
1079 }
1080
Winson Chunge3193b92010-09-10 11:44:42 -07001081 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1082 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001083 int left;
1084 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001085 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001086 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001087 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001088 if (left <= relativeOffset && relativeOffset <= right) {
1089 return i;
1090 }
Winson Chunge3193b92010-09-10 11:44:42 -07001091 }
1092 return -1;
1093 }
1094
Winson Chung321e9ee2010-08-09 13:37:56 -07001095 protected int getRelativeChildOffset(int index) {
1096 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1097 }
1098
1099 protected int getChildOffset(int index) {
1100 if (getChildCount() == 0)
1101 return 0;
1102
1103 int offset = getRelativeChildOffset(0);
1104 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001105 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 }
1107 return offset;
1108 }
1109
Michael Jurkad3ef3062010-11-23 16:23:58 -08001110 protected int getScaledMeasuredWidth(View child) {
1111 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1112 }
1113
Adam Cohend19d3ca2010-09-15 14:43:42 -07001114 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001115 int minDistanceFromScreenCenter = getMeasuredWidth();
1116 int minDistanceFromScreenCenterIndex = -1;
1117 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1118 final int childCount = getChildCount();
1119 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001120 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001121 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 int halfChildWidth = (childWidth / 2);
1123 int childCenter = getChildOffset(i) + halfChildWidth;
1124 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1125 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1126 minDistanceFromScreenCenter = distanceFromScreenCenter;
1127 minDistanceFromScreenCenterIndex = i;
1128 }
1129 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001130 return minDistanceFromScreenCenterIndex;
1131 }
1132
1133 protected void snapToDestination() {
1134 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001135 }
1136
Adam Cohene0f66b52010-11-23 15:06:07 -08001137 private static class ScrollInterpolator implements Interpolator {
1138 public ScrollInterpolator() {
1139 }
1140
1141 public float getInterpolation(float t) {
1142 t -= 1.0f;
1143 return t*t*t*t*t + 1;
1144 }
1145 }
1146
1147 // We want the duration of the page snap animation to be influenced by the distance that
1148 // the screen has to travel, however, we don't want this duration to be effected in a
1149 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1150 // of travel has on the overall snap duration.
1151 float distanceInfluenceForSnapDuration(float f) {
1152 f -= 0.5f; // center the values about 0.
1153 f *= 0.3f * Math.PI / 2.0f;
1154 return (float) Math.sin(f);
1155 }
1156
Michael Jurka0142d492010-08-25 17:46:15 -07001157 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001158 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1159 int halfScreenSize = getMeasuredWidth() / 2;
1160
1161 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1162 int delta = newX - mUnboundedScrollX;
1163 int duration = 0;
1164
1165 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1166 // If the velocity is low enough, then treat this more as an automatic page advance
1167 // as opposed to an apparent physical response to flinging
1168 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1169 return;
1170 }
1171
1172 // Here we compute a "distance" that will be used in the computation of the overall
1173 // snap duration. This is a function of the actual distance that needs to be traveled;
1174 // we keep this value close to half screen size in order to reduce the variance in snap
1175 // duration as a function of the distance the page needs to travel.
1176 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1177 float distance = halfScreenSize + halfScreenSize *
1178 distanceInfluenceForSnapDuration(distanceRatio);
1179
1180 velocity = Math.abs(velocity);
1181 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1182
1183 // we want the page's snap velocity to approximately match the velocity at which the
1184 // user flings, so we scale the duration by a value near to the derivative of the scroll
1185 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1186 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1187
1188 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001189 }
1190
1191 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001192 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 }
1194
Michael Jurka0142d492010-08-25 17:46:15 -07001195 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001196 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001197
Winson Chung86f77532010-08-24 11:08:22 -07001198 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001199 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001201 snapToPage(whichPage, delta, duration);
1202 }
1203
1204 protected void snapToPage(int whichPage, int delta, int duration) {
1205 mNextPage = whichPage;
1206
1207 View focusedChild = getFocusedChild();
1208 if (focusedChild != null && whichPage != mCurrentPage &&
1209 focusedChild == getChildAt(mCurrentPage)) {
1210 focusedChild.clearFocus();
1211 }
1212
1213 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 awakenScrollBars(duration);
1215 if (duration == 0) {
1216 duration = Math.abs(delta);
1217 }
1218
1219 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001220 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001221
1222 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001223 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001224 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 invalidate();
1226 }
1227
1228 @Override
1229 protected Parcelable onSaveInstanceState() {
1230 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001231 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 return state;
1233 }
1234
1235 @Override
1236 protected void onRestoreInstanceState(Parcelable state) {
1237 SavedState savedState = (SavedState) state;
1238 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001239 if (savedState.currentPage != -1) {
1240 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 }
1242 }
1243
1244 public void scrollLeft() {
1245 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001246 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001247 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001248 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 }
1250 }
1251
1252 public void scrollRight() {
1253 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001254 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001256 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 }
1258 }
1259
Winson Chung86f77532010-08-24 11:08:22 -07001260 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001261 int result = -1;
1262 if (v != null) {
1263 ViewParent vp = v.getParent();
1264 int count = getChildCount();
1265 for (int i = 0; i < count; i++) {
1266 if (vp == getChildAt(i)) {
1267 return i;
1268 }
1269 }
1270 }
1271 return result;
1272 }
1273
1274 /**
1275 * @return True is long presses are still allowed for the current touch
1276 */
1277 public boolean allowLongPress() {
1278 return mAllowLongPress;
1279 }
1280
Michael Jurka0142d492010-08-25 17:46:15 -07001281 /**
1282 * Set true to allow long-press events to be triggered, usually checked by
1283 * {@link Launcher} to accept or block dpad-initiated long-presses.
1284 */
1285 public void setAllowLongPress(boolean allowLongPress) {
1286 mAllowLongPress = allowLongPress;
1287 }
1288
Winson Chung321e9ee2010-08-09 13:37:56 -07001289 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001290 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001291
1292 SavedState(Parcelable superState) {
1293 super(superState);
1294 }
1295
1296 private SavedState(Parcel in) {
1297 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001298 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001299 }
1300
1301 @Override
1302 public void writeToParcel(Parcel out, int flags) {
1303 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001304 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001305 }
1306
1307 public static final Parcelable.Creator<SavedState> CREATOR =
1308 new Parcelable.Creator<SavedState>() {
1309 public SavedState createFromParcel(Parcel in) {
1310 return new SavedState(in);
1311 }
1312
1313 public SavedState[] newArray(int size) {
1314 return new SavedState[size];
1315 }
1316 };
1317 }
1318
Winson Chung86f77532010-08-24 11:08:22 -07001319 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001320 if (mContentIsRefreshable) {
1321 final int count = getChildCount();
1322 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001323 int lowerPageBound = getAssociatedLowerPageBound(page);
1324 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001325 for (int i = 0; i < count; ++i) {
1326 final ViewGroup layout = (ViewGroup) getChildAt(i);
1327 final int childCount = layout.getChildCount();
1328 if (lowerPageBound <= i && i <= upperPageBound) {
1329 if (mDirtyPageContent.get(i)) {
1330 syncPageItems(i);
1331 mDirtyPageContent.set(i, false);
1332 }
1333 } else {
1334 if (childCount > 0) {
1335 layout.removeAllViews();
1336 }
1337 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001338 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001339 }
1340 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001341 }
1342 }
1343
Winson Chunge3193b92010-09-10 11:44:42 -07001344 protected int getAssociatedLowerPageBound(int page) {
1345 return Math.max(0, page - 1);
1346 }
1347 protected int getAssociatedUpperPageBound(int page) {
1348 final int count = getChildCount();
1349 return Math.min(page + 1, count - 1);
1350 }
1351
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001352 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001353 if (isChoiceMode(CHOICE_MODE_NONE)) {
1354 mChoiceMode = mode;
1355 mActionMode = startActionMode(callback);
1356 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001357 }
1358
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001359 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001360 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001361 mChoiceMode = CHOICE_MODE_NONE;
1362 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001363 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001364 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001365 }
1366 }
1367
1368 protected boolean isChoiceMode(int mode) {
1369 return mChoiceMode == mode;
1370 }
1371
1372 protected ArrayList<Checkable> getCheckedGrandchildren() {
1373 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1374 final int childCount = getChildCount();
1375 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001376 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001377 final int grandChildCount = layout.getChildCount();
1378 for (int j = 0; j < grandChildCount; ++j) {
1379 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001380 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001381 checked.add((Checkable) v);
1382 }
1383 }
1384 }
1385 return checked;
1386 }
1387
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001388 /**
1389 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1390 * Otherwise, returns null.
1391 */
1392 protected Checkable getSingleCheckedGrandchild() {
1393 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1394 final int childCount = getChildCount();
1395 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001396 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001397 final int grandChildCount = layout.getChildCount();
1398 for (int j = 0; j < grandChildCount; ++j) {
1399 final View v = layout.getChildAt(j);
1400 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1401 return (Checkable) v;
1402 }
1403 }
1404 }
1405 }
1406 return null;
1407 }
1408
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001409 public Object getChosenItem() {
1410 View checkedView = (View) getSingleCheckedGrandchild();
1411 if (checkedView != null) {
1412 return checkedView.getTag();
1413 }
1414 return null;
1415 }
1416
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001417 protected void resetCheckedGrandchildren() {
1418 // loop through children, and set all of their children to _not_ be checked
1419 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1420 for (int i = 0; i < checked.size(); ++i) {
1421 final Checkable c = checked.get(i);
1422 c.setChecked(false);
1423 }
1424 }
1425
Winson Chung86f77532010-08-24 11:08:22 -07001426 /**
1427 * This method is called ONLY to synchronize the number of pages that the paged view has.
1428 * To actually fill the pages with information, implement syncPageItems() below. It is
1429 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1430 * and therefore, individual page items do not need to be updated in this method.
1431 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001432 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001433
1434 /**
1435 * This method is called to synchronize the items that are on a particular page. If views on
1436 * the page can be reused, then they should be updated within this method.
1437 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001438 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001439
Winson Chung321e9ee2010-08-09 13:37:56 -07001440 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001441 if (mContentIsRefreshable) {
1442 // Update all the pages
1443 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001444
Michael Jurka0142d492010-08-25 17:46:15 -07001445 // Mark each of the pages as dirty
1446 final int count = getChildCount();
1447 mDirtyPageContent.clear();
1448 for (int i = 0; i < count; ++i) {
1449 mDirtyPageContent.add(true);
1450 }
1451
1452 // Load any pages that are necessary for the current window of views
1453 loadAssociatedPages(mCurrentPage);
1454 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001455 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001456 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001457 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001458 }
1459}