blob: 4a0f44e2c01810e790e697e0c2dc8023de6ed943 [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 Chung04998342011-01-05 13:54:43 -080019import java.util.ArrayList;
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 Chung321e9ee2010-08-09 13:37:56 -070023import android.graphics.Canvas;
24import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.os.Parcel;
26import android.os.Parcelable;
27import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070028import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.view.MotionEvent;
30import android.view.VelocityTracker;
31import android.view.View;
32import android.view.ViewConfiguration;
33import android.view.ViewGroup;
34import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.view.animation.Animation;
Winson Chung04998342011-01-05 13:54:43 -080036import android.view.animation.Animation.AnimationListener;
Winson Chunge3193b92010-09-10 11:44:42 -070037import android.view.animation.AnimationUtils;
Adam Cohene0f66b52010-11-23 15:06:07 -080038import android.view.animation.Interpolator;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070039import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070040import android.widget.Scroller;
41
Winson Chung04998342011-01-05 13:54:43 -080042import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070043
Winson Chung321e9ee2010-08-09 13:37:56 -070044/**
45 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070046 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070047 */
48public abstract class PagedView extends ViewGroup {
49 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070050 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070051
Winson Chung86f77532010-08-24 11:08:22 -070052 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070053 private static final int MIN_LENGTH_FOR_FLING = 25;
54 // The min drag distance to trigger a page shift (regardless of velocity)
55 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070056
Adam Cohene0f66b52010-11-23 15:06:07 -080057 private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
Michael Jurka0142d492010-08-25 17:46:15 -070058 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Adam Cohene0f66b52010-11-23 15:06:07 -080060 private static final float OVERSCROLL_DAMP_FACTOR = 0.08f;
61 private static final int MINIMUM_SNAP_VELOCITY = 2200;
62 private static final int MIN_FLING_VELOCITY = 250;
Adam Cohen68d73932010-11-15 10:50:58 -080063
Michael Jurka0142d492010-08-25 17:46:15 -070064 // the velocity at which a fling gesture will cause us to snap to the next page
65 protected int mSnapVelocity = 500;
66
67 protected float mSmoothingTime;
68 protected float mTouchX;
69
70 protected boolean mFirstLayout = true;
71
72 protected int mCurrentPage;
73 protected int mNextPage = INVALID_PAGE;
Winson Chunga12a2502010-12-20 14:41:35 -080074 protected int mRestorePage = -1;
Adam Cohen68d73932010-11-15 10:50:58 -080075 protected int mMaxScrollX;
Michael Jurka0142d492010-08-25 17:46:15 -070076 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070077 private VelocityTracker mVelocityTracker;
78
79 private float mDownMotionX;
Michael Jurka7426c422010-11-11 15:23:47 -080080 protected float mLastMotionX;
81 protected float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070082 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070083
Michael Jurka0142d492010-08-25 17:46:15 -070084 protected final static int TOUCH_STATE_REST = 0;
85 protected final static int TOUCH_STATE_SCROLLING = 1;
86 protected final static int TOUCH_STATE_PREV_PAGE = 2;
87 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070088 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070089
Michael Jurka0142d492010-08-25 17:46:15 -070090 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070091
Michael Jurka0142d492010-08-25 17:46:15 -070092 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070093
Michael Jurka7426c422010-11-11 15:23:47 -080094 protected boolean mAllowLongPress = true;
Winson Chung321e9ee2010-08-09 13:37:56 -070095
Michael Jurka7426c422010-11-11 15:23:47 -080096 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -070097 private int mPagingTouchSlop;
98 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070099 protected int mPageSpacing;
100 protected int mPageLayoutPaddingTop;
101 protected int mPageLayoutPaddingBottom;
102 protected int mPageLayoutPaddingLeft;
103 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700104 protected int mPageLayoutWidthGap;
105 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700106 protected int mCellCountX;
107 protected int mCellCountY;
Winson Chung7da10252010-10-28 16:07:04 -0700108 protected boolean mCenterPagesVertically;
Adam Cohen68d73932010-11-15 10:50:58 -0800109 protected boolean mAllowOverScroll = true;
110 protected int mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Michael Jurkad3ef3062010-11-23 16:23:58 -0800112 // parameter that adjusts the layout to be optimized for CellLayouts with that scale factor
113 protected float mLayoutScale = 1.0f;
114
Michael Jurka5f1c5092010-09-03 14:15:02 -0700115 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Michael Jurka5f1c5092010-09-03 14:15:02 -0700117 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Winson Chung86f77532010-08-24 11:08:22 -0700119 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700120
Winson Chung86f77532010-08-24 11:08:22 -0700121 private ArrayList<Boolean> mDirtyPageContent;
122 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700123
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700124 // choice modes
125 protected static final int CHOICE_MODE_NONE = 0;
126 protected static final int CHOICE_MODE_SINGLE = 1;
127 // Multiple selection mode is not supported by all Launcher actions atm
128 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700129
Michael Jurkae17e19c2010-09-28 11:01:39 -0700130 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700131 private ActionMode mActionMode;
132
Winson Chung04998342011-01-05 13:54:43 -0800133 // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in
134 // AllApps and Customize, and allows them to share holographic icons for the application view
135 // (which is in both).
136 protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700137
Michael Jurka0142d492010-08-25 17:46:15 -0700138 // If true, syncPages and syncPageItems will be called to refresh pages
139 protected boolean mContentIsRefreshable = true;
140
141 // If true, modify alpha of neighboring pages as user scrolls left/right
142 protected boolean mFadeInAdjacentScreens = true;
143
144 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
145 // to switch to a new page
146 protected boolean mUsePagingTouchSlop = true;
147
148 // If true, the subclass should directly update mScrollX itself in its computeScroll method
149 // (SmoothPagedView does this)
150 protected boolean mDeferScrollUpdate = false;
151
Patrick Dubroy1262e362010-10-06 15:49:50 -0700152 protected boolean mIsPageMoving = false;
153
Winson Chung86f77532010-08-24 11:08:22 -0700154 public interface PageSwitchListener {
155 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700156 }
157
Winson Chung321e9ee2010-08-09 13:37:56 -0700158 public PagedView(Context context) {
159 this(context, null);
160 }
161
162 public PagedView(Context context, AttributeSet attrs) {
163 this(context, attrs, 0);
164 }
165
166 public PagedView(Context context, AttributeSet attrs, int defStyle) {
167 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700168 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700169
Adam Cohen9c4949e2010-10-05 12:27:22 -0700170 TypedArray a = context.obtainStyledAttributes(attrs,
171 R.styleable.PagedView, defStyle, 0);
172 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
173 mPageLayoutPaddingTop = a.getDimensionPixelSize(
174 R.styleable.PagedView_pageLayoutPaddingTop, 10);
175 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
176 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
177 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
178 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
179 mPageLayoutPaddingRight = a.getDimensionPixelSize(
180 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700181 mPageLayoutWidthGap = a.getDimensionPixelSize(
182 R.styleable.PagedView_pageLayoutWidthGap, -1);
183 mPageLayoutHeightGap = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700185 a.recycle();
186
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700188 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 }
190
191 /**
192 * Initializes various states for this workspace.
193 */
Michael Jurka0142d492010-08-25 17:46:15 -0700194 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700195 mDirtyPageContent = new ArrayList<Boolean>();
196 mDirtyPageContent.ensureCapacity(32);
Adam Cohene0f66b52010-11-23 15:06:07 -0800197 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700198 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700199 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700200
201 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
202 mTouchSlop = configuration.getScaledTouchSlop();
203 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
204 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
205 }
206
Winson Chung86f77532010-08-24 11:08:22 -0700207 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
208 mPageSwitchListener = pageSwitchListener;
209 if (mPageSwitchListener != null) {
210 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 }
212 }
213
214 /**
Winson Chung86f77532010-08-24 11:08:22 -0700215 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 *
Winson Chung86f77532010-08-24 11:08:22 -0700217 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 */
Winson Chung86f77532010-08-24 11:08:22 -0700219 int getCurrentPage() {
220 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222
Winson Chung86f77532010-08-24 11:08:22 -0700223 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 return getChildCount();
225 }
226
Winson Chung86f77532010-08-24 11:08:22 -0700227 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 return getChildAt(index);
229 }
230
231 int getScrollWidth() {
232 return getWidth();
233 }
234
235 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800236 * Updates the scroll of the current page immediately to its final scroll position. We use this
237 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
238 * the previous tab page.
239 */
240 protected void updateCurrentPageScroll() {
241 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
242 scrollTo(newX, 0);
243 mScroller.setFinalX(newX);
244 }
245
246 /**
Winson Chung86f77532010-08-24 11:08:22 -0700247 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 */
Winson Chung86f77532010-08-24 11:08:22 -0700249 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800250 if (!mScroller.isFinished()) {
251 mScroller.abortAnimation();
252 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800253 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
254 // the default
255 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800256 return;
257 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700258
Winson Chung86f77532010-08-24 11:08:22 -0700259 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800260 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700261 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800262 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700263 }
264
Michael Jurka0142d492010-08-25 17:46:15 -0700265 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700266 if (mPageSwitchListener != null) {
267 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 }
269 }
270
Patrick Dubroy1262e362010-10-06 15:49:50 -0700271 private void pageBeginMoving() {
272 mIsPageMoving = true;
273 onPageBeginMoving();
274 }
275
276 private void pageEndMoving() {
277 onPageEndMoving();
278 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700279 }
280
281 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700282 protected void onPageBeginMoving() {
283 }
284
285 // a method that subclasses can override to add behavior
286 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700287 }
288
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 /**
Winson Chung86f77532010-08-24 11:08:22 -0700290 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 *
292 * @param l The listener used to respond to long clicks.
293 */
294 @Override
295 public void setOnLongClickListener(OnLongClickListener l) {
296 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700297 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700299 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 }
301 }
302
303 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800304 public void scrollBy(int x, int y) {
305 scrollTo(mUnboundedScrollX + x, mScrollY + y);
306 }
307
308 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700309 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800310 mUnboundedScrollX = x;
311
312 if (x < 0) {
313 super.scrollTo(0, y);
314 if (mAllowOverScroll) {
315 overScroll(x);
316 }
317 } else if (x > mMaxScrollX) {
318 super.scrollTo(mMaxScrollX, y);
319 if (mAllowOverScroll) {
320 overScroll(x - mMaxScrollX);
321 }
322 } else {
323 super.scrollTo(x, y);
324 }
325
Michael Jurka0142d492010-08-25 17:46:15 -0700326 mTouchX = x;
327 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
328 }
329
330 // we moved this functionality to a helper function so SmoothPagedView can reuse it
331 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700332 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700333 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700334 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700335 invalidate();
336 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700337 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700338 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700339 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700340 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700341 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800342 // We don't want to trigger a page end moving unless the page has settled
343 // and the user has stopped scrolling
344 if (mTouchState == TOUCH_STATE_REST) {
345 pageEndMoving();
346 }
Michael Jurka0142d492010-08-25 17:46:15 -0700347 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 }
Michael Jurka0142d492010-08-25 17:46:15 -0700349 return false;
350 }
351
352 @Override
353 public void computeScroll() {
354 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700355 }
356
357 @Override
358 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
359 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
360 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
361 if (widthMode != MeasureSpec.EXACTLY) {
362 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
363 }
364
Adam Lesinski6b879f02010-11-04 16:15:23 -0700365 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
366 * of the All apps view on XLarge displays to not take up more space then it needs. Width
367 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
368 * each page to have the same width.
369 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700371 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
372 int maxChildHeight = 0;
373
374 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700375
376 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700377 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700378 final int childCount = getChildCount();
379 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700380 // disallowing padding in paged view (just pass 0)
381 final View child = getChildAt(i);
382 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
383
384 int childWidthMode;
385 if (lp.width == LayoutParams.WRAP_CONTENT) {
386 childWidthMode = MeasureSpec.AT_MOST;
387 } else {
388 childWidthMode = MeasureSpec.EXACTLY;
389 }
390
391 int childHeightMode;
392 if (lp.height == LayoutParams.WRAP_CONTENT) {
393 childHeightMode = MeasureSpec.AT_MOST;
394 } else {
395 childHeightMode = MeasureSpec.EXACTLY;
396 }
397
398 final int childWidthMeasureSpec =
399 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
400 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700401 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700402
403 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700404 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
405 }
406
407 if (heightMode == MeasureSpec.AT_MOST) {
408 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700409 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800410 if (childCount > 0) {
411 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
412 } else {
413 mMaxScrollX = 0;
414 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700415
416 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
418
Michael Jurkaaf91de02010-11-23 16:23:58 -0800419 protected void moveToNewPageWithoutMovingCellLayouts(int newCurrentPage) {
420 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
421 int delta = newX - mScrollX;
422
423 final int screenCount = getChildCount();
424 for (int i = 0; i < screenCount; i++) {
425 CellLayout cl = (CellLayout) getChildAt(i);
426 cl.setX(cl.getX() + delta);
427 }
428 setCurrentPage(newCurrentPage);
429 }
430
Michael Jurkad3ef3062010-11-23 16:23:58 -0800431 // A layout scale of 1.0f assumes that the CellLayouts, in their unshrunken state, have a
432 // scale of 1.0f. A layout scale of 0.8f assumes the CellLayouts have a scale of 0.8f, and
433 // tightens the layout accordingly
434 public void setLayoutScale(float childrenScale) {
435 mLayoutScale = childrenScale;
436
437 // Now we need to do a re-layout, but preserving absolute X and Y coordinates
438 int childCount = getChildCount();
439 float childrenX[] = new float[childCount];
440 float childrenY[] = new float[childCount];
441 for (int i = 0; i < childCount; i++) {
442 final View child = getChildAt(i);
443 childrenX[i] = child.getX();
444 childrenY[i] = child.getY();
445 }
446 onLayout(false, mLeft, mTop, mRight, mBottom);
447 for (int i = 0; i < childCount; i++) {
448 final View child = getChildAt(i);
449 child.setX(childrenX[i]);
450 child.setY(childrenY[i]);
451 }
452 // Also, the page offset has changed (since the pages are now smaller);
453 // update the page offset, but again preserving absolute X and Y coordinates
454 moveToNewPageWithoutMovingCellLayouts(mCurrentPage);
455 }
456
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700458 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700459 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
460 setHorizontalScrollBarEnabled(false);
461 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
462 scrollTo(newX, 0);
463 mScroller.setFinalX(newX);
464 setHorizontalScrollBarEnabled(true);
465 mFirstLayout = false;
466 }
467
Adam Lesinski6b879f02010-11-04 16:15:23 -0700468 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700469 final int childCount = getChildCount();
470 int childLeft = 0;
471 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700472 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700473 }
474
475 for (int i = 0; i < childCount; i++) {
476 final View child = getChildAt(i);
477 if (child.getVisibility() != View.GONE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800478 final int childWidth = getScaledMeasuredWidth(child);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700479 final int childHeight = child.getMeasuredHeight();
480 int childTop = mPaddingTop;
481 if (mCenterPagesVertically) {
482 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
483 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800484
Adam Lesinski6b879f02010-11-04 16:15:23 -0700485 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800486 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700487 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 }
489 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800490 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
491 mFirstLayout = false;
492 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700493 }
494
Winson Chunge3193b92010-09-10 11:44:42 -0700495 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700496 if (mFadeInAdjacentScreens) {
497 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700498 int halfScreenSize = getMeasuredWidth() / 2;
499 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700500 final int childCount = getChildCount();
501 for (int i = 0; i < childCount; ++i) {
502 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800503 int childWidth = getScaledMeasuredWidth(layout);
Michael Jurka0142d492010-08-25 17:46:15 -0700504 int halfChildWidth = (childWidth / 2);
505 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700506
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700507 // On the first layout, we may not have a width nor a proper offset, so for now
508 // we should just assume full page width (and calculate the offset according to
509 // that).
510 if (childWidth <= 0) {
511 childWidth = getMeasuredWidth();
512 childCenter = (i * childWidth) + (childWidth / 2);
513 }
514
Winson Chunge8878e32010-09-15 20:37:09 -0700515 int d = halfChildWidth;
516 int distanceFromScreenCenter = childCenter - screenCenter;
517 if (distanceFromScreenCenter > 0) {
518 if (i > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800519 d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700520 }
Michael Jurka0142d492010-08-25 17:46:15 -0700521 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700522 if (i < childCount - 1) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800523 d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2;
Winson Chunge8878e32010-09-15 20:37:09 -0700524 }
Michael Jurka0142d492010-08-25 17:46:15 -0700525 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700526 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700527
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700528 // Preventing potential divide-by-zero
529 d = Math.max(1, d);
530
Winson Chunge8878e32010-09-15 20:37:09 -0700531 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
532 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
533 float alpha = 1.0f - dimAlpha;
534
Adam Cohenf34bab52010-09-30 14:11:56 -0700535 if (alpha < ALPHA_QUANTIZE_LEVEL) {
536 alpha = 0.0f;
537 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
538 alpha = 1.0f;
539 }
540
Michael Jurka0142d492010-08-25 17:46:15 -0700541 if (Float.compare(alpha, layout.getAlpha()) != 0) {
542 layout.setAlpha(alpha);
543 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 }
Michael Jurka0142d492010-08-25 17:46:15 -0700545 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700546 }
547 }
Winson Chunge3193b92010-09-10 11:44:42 -0700548 }
549
Adam Cohenf34bab52010-09-30 14:11:56 -0700550 protected void screenScrolled(int screenCenter) {
551 }
552
Winson Chunge3193b92010-09-10 11:44:42 -0700553 @Override
554 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700555 int halfScreenSize = getMeasuredWidth() / 2;
556 int screenCenter = mScrollX + halfScreenSize;
557
558 if (screenCenter != mLastScreenCenter) {
559 screenScrolled(screenCenter);
560 updateAdjacentPagesAlpha();
561 mLastScreenCenter = screenCenter;
562 }
Michael Jurka0142d492010-08-25 17:46:15 -0700563
564 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700565 // As an optimization, this code assumes that all pages have the same width as the 0th
566 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700567 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700568 if (pageCount > 0) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800569 final int pageWidth = getScaledMeasuredWidth(getChildAt(0));
Michael Jurkac4fb9172010-09-02 17:19:20 -0700570 final int screenWidth = getMeasuredWidth();
571 int x = getRelativeChildOffset(0) + pageWidth;
572 int leftScreen = 0;
573 int rightScreen = 0;
574 while (x <= mScrollX) {
575 leftScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800576 x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700577 }
578 rightScreen = leftScreen;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800579 while (x < mScrollX + screenWidth && rightScreen < pageCount) {
Michael Jurkac4fb9172010-09-02 17:19:20 -0700580 rightScreen++;
Winson Chungc3f9f4f2010-12-14 17:25:59 -0800581 if (rightScreen < pageCount) {
582 x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing;
583 }
Michael Jurkac4fb9172010-09-02 17:19:20 -0700584 }
585 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700586
Michael Jurkac4fb9172010-09-02 17:19:20 -0700587 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800588 // Clip to the bounds
589 canvas.save();
590 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
591 mScrollY + mBottom - mTop);
592
Michael Jurkac4fb9172010-09-02 17:19:20 -0700593 for (int i = leftScreen; i <= rightScreen; i++) {
594 drawChild(canvas, getChildAt(i), drawingTime);
595 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800596 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700597 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 }
599
600 @Override
601 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700602 int page = indexOfChild(child);
603 if (page != mCurrentPage || !mScroller.isFinished()) {
604 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 return true;
606 }
607 return false;
608 }
609
610 @Override
611 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700612 int focusablePage;
613 if (mNextPage != INVALID_PAGE) {
614 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700615 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700616 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700617 }
Winson Chung86f77532010-08-24 11:08:22 -0700618 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700619 if (v != null) {
620 v.requestFocus(direction, previouslyFocusedRect);
621 }
622 return false;
623 }
624
625 @Override
626 public boolean dispatchUnhandledMove(View focused, int direction) {
627 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700628 if (getCurrentPage() > 0) {
629 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700630 return true;
631 }
632 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700633 if (getCurrentPage() < getPageCount() - 1) {
634 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 return true;
636 }
637 }
638 return super.dispatchUnhandledMove(focused, direction);
639 }
640
641 @Override
642 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700643 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
644 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700645 }
646 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700647 if (mCurrentPage > 0) {
648 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 }
650 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700651 if (mCurrentPage < getPageCount() - 1) {
652 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 }
654 }
655 }
656
657 /**
658 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700659 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 *
Winson Chung86f77532010-08-24 11:08:22 -0700661 * This happens when live folders requery, and if they're off page, they
662 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700663 */
664 @Override
665 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700666 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 View v = focused;
668 while (true) {
669 if (v == current) {
670 super.focusableViewAvailable(focused);
671 return;
672 }
673 if (v == this) {
674 return;
675 }
676 ViewParent parent = v.getParent();
677 if (parent instanceof View) {
678 v = (View)v.getParent();
679 } else {
680 return;
681 }
682 }
683 }
684
685 /**
686 * {@inheritDoc}
687 */
688 @Override
689 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
690 if (disallowIntercept) {
691 // We need to make sure to cancel our long press if
692 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700693 final View currentPage = getChildAt(mCurrentPage);
694 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 }
696 super.requestDisallowInterceptTouchEvent(disallowIntercept);
697 }
698
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800699 /**
700 * Return true if a tap at (x, y) should trigger a flip to the previous page.
701 */
702 protected boolean hitsPreviousPage(float x, float y) {
703 return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing);
704 }
705
706 /**
707 * Return true if a tap at (x, y) should trigger a flip to the next page.
708 */
709 protected boolean hitsNextPage(float x, float y) {
710 return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing));
711 }
712
Winson Chung321e9ee2010-08-09 13:37:56 -0700713 @Override
714 public boolean onInterceptTouchEvent(MotionEvent ev) {
715 /*
716 * This method JUST determines whether we want to intercept the motion.
717 * If we return true, onTouchEvent will be called and we do the actual
718 * scrolling there.
719 */
720
Winson Chung45e1d6e2010-11-09 17:19:49 -0800721 // Skip touch handling if there are no pages to swipe
722 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
723
Winson Chung321e9ee2010-08-09 13:37:56 -0700724 /*
725 * Shortcut the most recurring case: the user is in the dragging
726 * state and he is moving his finger. We want to intercept this
727 * motion.
728 */
729 final int action = ev.getAction();
730 if ((action == MotionEvent.ACTION_MOVE) &&
731 (mTouchState == TOUCH_STATE_SCROLLING)) {
732 return true;
733 }
734
Winson Chung321e9ee2010-08-09 13:37:56 -0700735 switch (action & MotionEvent.ACTION_MASK) {
736 case MotionEvent.ACTION_MOVE: {
737 /*
738 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
739 * whether the user has moved far enough from his original down touch.
740 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700741 if (mActivePointerId != INVALID_POINTER) {
742 determineScrollingStart(ev);
743 break;
744 }
745 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
746 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
747 // i.e. fall through to the next case (don't break)
748 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
749 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 }
751
752 case MotionEvent.ACTION_DOWN: {
753 final float x = ev.getX();
754 final float y = ev.getY();
755 // Remember location of down touch
756 mDownMotionX = x;
757 mLastMotionX = x;
758 mLastMotionY = y;
759 mActivePointerId = ev.getPointerId(0);
760 mAllowLongPress = true;
761
762 /*
763 * If being flinged and user touches the screen, initiate drag;
764 * otherwise don't. mScroller.isFinished should be false when
765 * being flinged.
766 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700767 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700768 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
769 if (finishedScrolling) {
770 mTouchState = TOUCH_STATE_REST;
771 mScroller.abortAnimation();
772 } else {
773 mTouchState = TOUCH_STATE_SCROLLING;
774 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700775
Winson Chung86f77532010-08-24 11:08:22 -0700776 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700777 // to scroll the current page
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800778 if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700779 if (getChildCount() > 0) {
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800780 if (hitsPreviousPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 mTouchState = TOUCH_STATE_PREV_PAGE;
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800782 } else if (hitsNextPage(x, y)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700783 mTouchState = TOUCH_STATE_NEXT_PAGE;
784 }
785 }
786 }
787 break;
788 }
789
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800791 onWallpaperTap(ev);
792 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700793 mTouchState = TOUCH_STATE_REST;
794 mAllowLongPress = false;
795 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700796 break;
797
798 case MotionEvent.ACTION_POINTER_UP:
799 onSecondaryPointerUp(ev);
800 break;
801 }
802
803 /*
804 * The only time we want to intercept motion events is if we are in the
805 * drag mode.
806 */
807 return mTouchState != TOUCH_STATE_REST;
808 }
809
Winson Chung80baf5a2010-08-09 16:03:15 -0700810 protected void animateClickFeedback(View v, final Runnable r) {
811 // animate the view slightly to show click feedback running some logic after it is "pressed"
812 Animation anim = AnimationUtils.loadAnimation(getContext(),
813 R.anim.paged_view_click_feedback);
814 anim.setAnimationListener(new AnimationListener() {
815 @Override
816 public void onAnimationStart(Animation animation) {}
817 @Override
818 public void onAnimationRepeat(Animation animation) {
819 r.run();
820 }
821 @Override
822 public void onAnimationEnd(Animation animation) {}
823 });
824 v.startAnimation(anim);
825 }
826
Winson Chung321e9ee2010-08-09 13:37:56 -0700827 /*
828 * Determines if we should change the touch state to start scrolling after the
829 * user moves their touch point too far.
830 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700831 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700832 /*
833 * Locally do absolute value. mLastMotionX is set to the y value
834 * of the down event.
835 */
836 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
837 final float x = ev.getX(pointerIndex);
838 final float y = ev.getY(pointerIndex);
839 final int xDiff = (int) Math.abs(x - mLastMotionX);
840 final int yDiff = (int) Math.abs(y - mLastMotionY);
841
842 final int touchSlop = mTouchSlop;
843 boolean xPaged = xDiff > mPagingTouchSlop;
844 boolean xMoved = xDiff > touchSlop;
845 boolean yMoved = yDiff > touchSlop;
846
847 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700848 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 // Scroll if the user moved far enough along the X axis
850 mTouchState = TOUCH_STATE_SCROLLING;
851 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700852 mTouchX = mScrollX;
853 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
854 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 }
856 // Either way, cancel any pending longpress
857 if (mAllowLongPress) {
858 mAllowLongPress = false;
859 // Try canceling the long press. It could also have been scheduled
860 // by a distant descendant, so use the mAllowLongPress flag to block
861 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700862 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700863 if (currentPage != null) {
864 currentPage.cancelLongPress();
865 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700866 }
867 }
868 }
869
Adam Cohene0f66b52010-11-23 15:06:07 -0800870 // This curve determines how the effect of scrolling over the limits of the page dimishes
871 // as the user pulls further and further from the bounds
872 private float overScrollInfluenceCurve(float f) {
873 f -= 1.0f;
874 return f * f * f + 1.0f;
875 }
876
Adam Cohen68d73932010-11-15 10:50:58 -0800877 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800878 int screenSize = getMeasuredWidth();
879
880 float f = (amount / screenSize);
881
882 if (f == 0) return;
883 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
884
885 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800886 if (amount < 0) {
887 mScrollX = overScrollAmount;
888 } else {
889 mScrollX = mMaxScrollX + overScrollAmount;
890 }
891 invalidate();
892 }
893
Michael Jurkac5b262c2011-01-12 20:24:50 -0800894 protected float maxOverScroll() {
895 // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not
896 // exceed). Used to find out how much extra wallpaper we need for the overscroll effect
897 float f = 1.0f;
898 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
899 return OVERSCROLL_DAMP_FACTOR * f;
900 }
901
Winson Chung321e9ee2010-08-09 13:37:56 -0700902 @Override
903 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800904 // Skip touch handling if there are no pages to swipe
905 if (getChildCount() <= 0) return super.onTouchEvent(ev);
906
Michael Jurkab8f06722010-10-10 15:58:46 -0700907 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700908
909 final int action = ev.getAction();
910
911 switch (action & MotionEvent.ACTION_MASK) {
912 case MotionEvent.ACTION_DOWN:
913 /*
914 * If being flinged and user touches, stop the fling. isFinished
915 * will be false if being flinged.
916 */
917 if (!mScroller.isFinished()) {
918 mScroller.abortAnimation();
919 }
920
921 // Remember where the motion event started
922 mDownMotionX = mLastMotionX = ev.getX();
923 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700924 if (mTouchState == TOUCH_STATE_SCROLLING) {
925 pageBeginMoving();
926 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 break;
928
929 case MotionEvent.ACTION_MOVE:
930 if (mTouchState == TOUCH_STATE_SCROLLING) {
931 // Scroll to follow the motion event
932 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
933 final float x = ev.getX(pointerIndex);
934 final int deltaX = (int) (mLastMotionX - x);
935 mLastMotionX = x;
936
Adam Cohen68d73932010-11-15 10:50:58 -0800937 if (deltaX != 0) {
938 mTouchX += deltaX;
939 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
940 if (!mDeferScrollUpdate) {
941 scrollBy(deltaX, 0);
942 } else {
943 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700944 }
945 } else {
946 awakenScrollBars();
947 }
Adam Cohen564976a2010-10-13 18:52:07 -0700948 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700949 determineScrollingStart(ev);
950 }
951 break;
952
953 case MotionEvent.ACTION_UP:
954 if (mTouchState == TOUCH_STATE_SCROLLING) {
955 final int activePointerId = mActivePointerId;
956 final int pointerIndex = ev.findPointerIndex(activePointerId);
957 final float x = ev.getX(pointerIndex);
958 final VelocityTracker velocityTracker = mVelocityTracker;
959 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
960 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700961 final int deltaX = (int) (x - mDownMotionX);
962 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
963 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700964
Michael Jurka0142d492010-08-25 17:46:15 -0700965 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700966 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800967 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700968 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700969 } else if ((isSignificantMove && deltaX < 0 ||
970 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700971 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700972 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 } else {
974 snapToDestination();
975 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800976 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 // at this point we have not moved beyond the touch slop
978 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
979 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700980 int nextPage = Math.max(0, mCurrentPage - 1);
981 if (nextPage != mCurrentPage) {
982 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700983 } else {
984 snapToDestination();
985 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800986 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700987 // at this point we have not moved beyond the touch slop
988 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
989 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700990 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
991 if (nextPage != mCurrentPage) {
992 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 } else {
994 snapToDestination();
995 }
Jeff Brown1d0867c2010-12-02 18:27:39 -0800996 } else {
997 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700998 }
999 mTouchState = TOUCH_STATE_REST;
1000 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001001 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 break;
1003
1004 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001005 if (mTouchState == TOUCH_STATE_SCROLLING) {
1006 snapToDestination();
1007 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001008 mTouchState = TOUCH_STATE_REST;
1009 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -07001010 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 break;
1012
1013 case MotionEvent.ACTION_POINTER_UP:
1014 onSecondaryPointerUp(ev);
1015 break;
1016 }
1017
1018 return true;
1019 }
1020
Michael Jurkab8f06722010-10-10 15:58:46 -07001021 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1022 if (mVelocityTracker == null) {
1023 mVelocityTracker = VelocityTracker.obtain();
1024 }
1025 mVelocityTracker.addMovement(ev);
1026 }
1027
1028 private void releaseVelocityTracker() {
1029 if (mVelocityTracker != null) {
1030 mVelocityTracker.recycle();
1031 mVelocityTracker = null;
1032 }
1033 }
1034
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 private void onSecondaryPointerUp(MotionEvent ev) {
1036 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1037 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1038 final int pointerId = ev.getPointerId(pointerIndex);
1039 if (pointerId == mActivePointerId) {
1040 // This was our active pointer going up. Choose a new
1041 // active pointer and adjust accordingly.
1042 // TODO: Make this decision more intelligent.
1043 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1044 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1045 mLastMotionY = ev.getY(newPointerIndex);
1046 mActivePointerId = ev.getPointerId(newPointerIndex);
1047 if (mVelocityTracker != null) {
1048 mVelocityTracker.clear();
1049 }
1050 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001051 if (mTouchState == TOUCH_STATE_REST) {
1052 onWallpaperTap(ev);
1053 }
1054 }
1055
1056 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 }
1058
1059 @Override
1060 public void requestChildFocus(View child, View focused) {
1061 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001062 int page = indexOfChild(child);
1063 if (page >= 0 && !isInTouchMode()) {
1064 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 }
1066 }
1067
Winson Chunge3193b92010-09-10 11:44:42 -07001068 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1069 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001070 int left;
1071 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001072 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001073 left = getRelativeChildOffset(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001074 right = (left + getScaledMeasuredWidth(getChildAt(i)));
Winson Chunge3193b92010-09-10 11:44:42 -07001075 if (left <= relativeOffset && relativeOffset <= right) {
1076 return i;
1077 }
Winson Chunge3193b92010-09-10 11:44:42 -07001078 }
1079 return -1;
1080 }
1081
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 protected int getRelativeChildOffset(int index) {
1083 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1084 }
1085
1086 protected int getChildOffset(int index) {
1087 if (getChildCount() == 0)
1088 return 0;
1089
1090 int offset = getRelativeChildOffset(0);
1091 for (int i = 0; i < index; ++i) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001092 offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001093 }
1094 return offset;
1095 }
1096
Michael Jurkad3ef3062010-11-23 16:23:58 -08001097 protected int getScaledMeasuredWidth(View child) {
1098 return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
1099 }
1100
Adam Cohend19d3ca2010-09-15 14:43:42 -07001101 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 int minDistanceFromScreenCenter = getMeasuredWidth();
1103 int minDistanceFromScreenCenterIndex = -1;
1104 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1105 final int childCount = getChildCount();
1106 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001107 View layout = (View) getChildAt(i);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001108 int childWidth = getScaledMeasuredWidth(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -07001109 int halfChildWidth = (childWidth / 2);
1110 int childCenter = getChildOffset(i) + halfChildWidth;
1111 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1112 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1113 minDistanceFromScreenCenter = distanceFromScreenCenter;
1114 minDistanceFromScreenCenterIndex = i;
1115 }
1116 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001117 return minDistanceFromScreenCenterIndex;
1118 }
1119
1120 protected void snapToDestination() {
1121 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 }
1123
Adam Cohene0f66b52010-11-23 15:06:07 -08001124 private static class ScrollInterpolator implements Interpolator {
1125 public ScrollInterpolator() {
1126 }
1127
1128 public float getInterpolation(float t) {
1129 t -= 1.0f;
1130 return t*t*t*t*t + 1;
1131 }
1132 }
1133
1134 // We want the duration of the page snap animation to be influenced by the distance that
1135 // the screen has to travel, however, we don't want this duration to be effected in a
1136 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1137 // of travel has on the overall snap duration.
1138 float distanceInfluenceForSnapDuration(float f) {
1139 f -= 0.5f; // center the values about 0.
1140 f *= 0.3f * Math.PI / 2.0f;
1141 return (float) Math.sin(f);
1142 }
1143
Michael Jurka0142d492010-08-25 17:46:15 -07001144 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001145 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1146 int halfScreenSize = getMeasuredWidth() / 2;
1147
1148 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1149 int delta = newX - mUnboundedScrollX;
1150 int duration = 0;
1151
1152 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1153 // If the velocity is low enough, then treat this more as an automatic page advance
1154 // as opposed to an apparent physical response to flinging
1155 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1156 return;
1157 }
1158
1159 // Here we compute a "distance" that will be used in the computation of the overall
1160 // snap duration. This is a function of the actual distance that needs to be traveled;
1161 // we keep this value close to half screen size in order to reduce the variance in snap
1162 // duration as a function of the distance the page needs to travel.
1163 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1164 float distance = halfScreenSize + halfScreenSize *
1165 distanceInfluenceForSnapDuration(distanceRatio);
1166
1167 velocity = Math.abs(velocity);
1168 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1169
1170 // we want the page's snap velocity to approximately match the velocity at which the
1171 // user flings, so we scale the duration by a value near to the derivative of the scroll
1172 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1173 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1174
1175 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001176 }
1177
1178 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001179 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001180 }
1181
Michael Jurka0142d492010-08-25 17:46:15 -07001182 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001183 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001184
Winson Chung86f77532010-08-24 11:08:22 -07001185 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001186 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001187 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001188 snapToPage(whichPage, delta, duration);
1189 }
1190
1191 protected void snapToPage(int whichPage, int delta, int duration) {
1192 mNextPage = whichPage;
1193
1194 View focusedChild = getFocusedChild();
1195 if (focusedChild != null && whichPage != mCurrentPage &&
1196 focusedChild == getChildAt(mCurrentPage)) {
1197 focusedChild.clearFocus();
1198 }
1199
1200 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 awakenScrollBars(duration);
1202 if (duration == 0) {
1203 duration = Math.abs(delta);
1204 }
1205
1206 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001207 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001208
1209 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001210 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001211 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 invalidate();
1213 }
1214
Winson Chung321e9ee2010-08-09 13:37:56 -07001215 public void scrollLeft() {
1216 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001219 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001220 }
1221 }
1222
1223 public void scrollRight() {
1224 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001225 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001226 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001227 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001228 }
1229 }
1230
Winson Chung86f77532010-08-24 11:08:22 -07001231 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001232 int result = -1;
1233 if (v != null) {
1234 ViewParent vp = v.getParent();
1235 int count = getChildCount();
1236 for (int i = 0; i < count; i++) {
1237 if (vp == getChildAt(i)) {
1238 return i;
1239 }
1240 }
1241 }
1242 return result;
1243 }
1244
1245 /**
1246 * @return True is long presses are still allowed for the current touch
1247 */
1248 public boolean allowLongPress() {
1249 return mAllowLongPress;
1250 }
1251
Michael Jurka0142d492010-08-25 17:46:15 -07001252 /**
1253 * Set true to allow long-press events to be triggered, usually checked by
1254 * {@link Launcher} to accept or block dpad-initiated long-presses.
1255 */
1256 public void setAllowLongPress(boolean allowLongPress) {
1257 mAllowLongPress = allowLongPress;
1258 }
1259
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001261 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001262
1263 SavedState(Parcelable superState) {
1264 super(superState);
1265 }
1266
1267 private SavedState(Parcel in) {
1268 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001269 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001270 }
1271
1272 @Override
1273 public void writeToParcel(Parcel out, int flags) {
1274 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001275 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001276 }
1277
1278 public static final Parcelable.Creator<SavedState> CREATOR =
1279 new Parcelable.Creator<SavedState>() {
1280 public SavedState createFromParcel(Parcel in) {
1281 return new SavedState(in);
1282 }
1283
1284 public SavedState[] newArray(int size) {
1285 return new SavedState[size];
1286 }
1287 };
1288 }
1289
Winson Chung86f77532010-08-24 11:08:22 -07001290 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001291 if (mContentIsRefreshable) {
1292 final int count = getChildCount();
1293 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001294 int lowerPageBound = getAssociatedLowerPageBound(page);
1295 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001296 for (int i = 0; i < count; ++i) {
1297 final ViewGroup layout = (ViewGroup) getChildAt(i);
1298 final int childCount = layout.getChildCount();
1299 if (lowerPageBound <= i && i <= upperPageBound) {
1300 if (mDirtyPageContent.get(i)) {
1301 syncPageItems(i);
1302 mDirtyPageContent.set(i, false);
1303 }
1304 } else {
1305 if (childCount > 0) {
1306 layout.removeAllViews();
1307 }
1308 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001309 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001310 }
1311 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001312 }
1313 }
1314
Winson Chunge3193b92010-09-10 11:44:42 -07001315 protected int getAssociatedLowerPageBound(int page) {
1316 return Math.max(0, page - 1);
1317 }
1318 protected int getAssociatedUpperPageBound(int page) {
1319 final int count = getChildCount();
1320 return Math.min(page + 1, count - 1);
1321 }
1322
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001323 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001324 if (isChoiceMode(CHOICE_MODE_NONE)) {
1325 mChoiceMode = mode;
1326 mActionMode = startActionMode(callback);
1327 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001328 }
1329
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001330 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001331 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001332 mChoiceMode = CHOICE_MODE_NONE;
1333 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001334 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001335 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001336 }
1337 }
1338
1339 protected boolean isChoiceMode(int mode) {
1340 return mChoiceMode == mode;
1341 }
1342
1343 protected ArrayList<Checkable> getCheckedGrandchildren() {
1344 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1345 final int childCount = getChildCount();
1346 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001347 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001348 final int grandChildCount = layout.getChildCount();
1349 for (int j = 0; j < grandChildCount; ++j) {
1350 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001351 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001352 checked.add((Checkable) v);
1353 }
1354 }
1355 }
1356 return checked;
1357 }
1358
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001359 /**
1360 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1361 * Otherwise, returns null.
1362 */
1363 protected Checkable getSingleCheckedGrandchild() {
1364 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1365 final int childCount = getChildCount();
1366 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001367 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001368 final int grandChildCount = layout.getChildCount();
1369 for (int j = 0; j < grandChildCount; ++j) {
1370 final View v = layout.getChildAt(j);
1371 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1372 return (Checkable) v;
1373 }
1374 }
1375 }
1376 }
1377 return null;
1378 }
1379
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001380 public Object getChosenItem() {
1381 View checkedView = (View) getSingleCheckedGrandchild();
1382 if (checkedView != null) {
1383 return checkedView.getTag();
1384 }
1385 return null;
1386 }
1387
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001388 protected void resetCheckedGrandchildren() {
1389 // loop through children, and set all of their children to _not_ be checked
1390 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1391 for (int i = 0; i < checked.size(); ++i) {
1392 final Checkable c = checked.get(i);
1393 c.setChecked(false);
1394 }
1395 }
1396
Winson Chunga12a2502010-12-20 14:41:35 -08001397 public void setRestorePage(int restorePage) {
1398 mRestorePage = restorePage;
1399 }
1400
Winson Chung86f77532010-08-24 11:08:22 -07001401 /**
1402 * This method is called ONLY to synchronize the number of pages that the paged view has.
1403 * To actually fill the pages with information, implement syncPageItems() below. It is
1404 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1405 * and therefore, individual page items do not need to be updated in this method.
1406 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001407 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001408
1409 /**
1410 * This method is called to synchronize the items that are on a particular page. If views on
1411 * the page can be reused, then they should be updated within this method.
1412 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001414
Winson Chung321e9ee2010-08-09 13:37:56 -07001415 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001416 if (mContentIsRefreshable) {
1417 // Update all the pages
1418 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001419
Michael Jurka0142d492010-08-25 17:46:15 -07001420 // Mark each of the pages as dirty
1421 final int count = getChildCount();
1422 mDirtyPageContent.clear();
1423 for (int i = 0; i < count; ++i) {
1424 mDirtyPageContent.add(true);
1425 }
1426
Winson Chunga12a2502010-12-20 14:41:35 -08001427 // Use the restore page if necessary
1428 if (mRestorePage > -1) {
1429 mCurrentPage = mRestorePage;
1430 mRestorePage = -1;
1431 }
1432
Michael Jurka0142d492010-08-25 17:46:15 -07001433 // Load any pages that are necessary for the current window of views
1434 loadAssociatedPages(mCurrentPage);
1435 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001436 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001437 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001438 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001439 }
1440}