blob: fed0884c3927c7931f6e79daccd2e181e3098e83 [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 Jurka5f1c5092010-09-03 14:15:02 -0700113 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Michael Jurka5f1c5092010-09-03 14:15:02 -0700115 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700116
Winson Chung86f77532010-08-24 11:08:22 -0700117 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700118
Winson Chung86f77532010-08-24 11:08:22 -0700119 private ArrayList<Boolean> mDirtyPageContent;
120 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700121
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700122 // choice modes
123 protected static final int CHOICE_MODE_NONE = 0;
124 protected static final int CHOICE_MODE_SINGLE = 1;
125 // Multiple selection mode is not supported by all Launcher actions atm
126 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700127
Michael Jurkae17e19c2010-09-28 11:01:39 -0700128 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700129 private ActionMode mActionMode;
130
Winson Chung241c3b42010-08-25 16:53:03 -0700131 protected PagedViewIconCache mPageViewIconCache;
132
Michael Jurka0142d492010-08-25 17:46:15 -0700133 // If true, syncPages and syncPageItems will be called to refresh pages
134 protected boolean mContentIsRefreshable = true;
135
136 // If true, modify alpha of neighboring pages as user scrolls left/right
137 protected boolean mFadeInAdjacentScreens = true;
138
139 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
140 // to switch to a new page
141 protected boolean mUsePagingTouchSlop = true;
142
143 // If true, the subclass should directly update mScrollX itself in its computeScroll method
144 // (SmoothPagedView does this)
145 protected boolean mDeferScrollUpdate = false;
146
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147 protected boolean mIsPageMoving = false;
148
Winson Chung241c3b42010-08-25 16:53:03 -0700149 /**
150 * Simple cache mechanism for PagedViewIcon outlines.
151 */
152 class PagedViewIconCache {
153 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
154
155 public void clear() {
156 iconOutlineCache.clear();
157 }
158 public void addOutline(Object key, Bitmap b) {
159 iconOutlineCache.put(key, b);
160 }
161 public void removeOutline(Object key) {
162 if (iconOutlineCache.containsKey(key)) {
163 iconOutlineCache.remove(key);
164 }
165 }
166 public Bitmap getOutline(Object key) {
167 return iconOutlineCache.get(key);
168 }
169 }
170
Winson Chung86f77532010-08-24 11:08:22 -0700171 public interface PageSwitchListener {
172 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 }
174
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 public PagedView(Context context) {
176 this(context, null);
177 }
178
179 public PagedView(Context context, AttributeSet attrs) {
180 this(context, attrs, 0);
181 }
182
183 public PagedView(Context context, AttributeSet attrs, int defStyle) {
184 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700185 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700186
Adam Cohen9c4949e2010-10-05 12:27:22 -0700187 TypedArray a = context.obtainStyledAttributes(attrs,
188 R.styleable.PagedView, defStyle, 0);
189 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
190 mPageLayoutPaddingTop = a.getDimensionPixelSize(
191 R.styleable.PagedView_pageLayoutPaddingTop, 10);
192 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
193 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
194 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
195 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
196 mPageLayoutPaddingRight = a.getDimensionPixelSize(
197 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700198 mPageLayoutWidthGap = a.getDimensionPixelSize(
199 R.styleable.PagedView_pageLayoutWidthGap, -1);
200 mPageLayoutHeightGap = a.getDimensionPixelSize(
201 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700202 a.recycle();
203
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700205 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 }
207
208 /**
209 * Initializes various states for this workspace.
210 */
Michael Jurka0142d492010-08-25 17:46:15 -0700211 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700212 mDirtyPageContent = new ArrayList<Boolean>();
213 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700214 mPageViewIconCache = new PagedViewIconCache();
Adam Cohene0f66b52010-11-23 15:06:07 -0800215 mScroller = new Scroller(getContext(), new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700216 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700217 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700218
219 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
220 mTouchSlop = configuration.getScaledTouchSlop();
221 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
222 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
223 }
224
Winson Chung86f77532010-08-24 11:08:22 -0700225 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
226 mPageSwitchListener = pageSwitchListener;
227 if (mPageSwitchListener != null) {
228 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230 }
231
232 /**
Winson Chung86f77532010-08-24 11:08:22 -0700233 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 *
Winson Chung86f77532010-08-24 11:08:22 -0700235 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 */
Winson Chung86f77532010-08-24 11:08:22 -0700237 int getCurrentPage() {
238 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 }
240
Winson Chung86f77532010-08-24 11:08:22 -0700241 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 return getChildCount();
243 }
244
Winson Chung86f77532010-08-24 11:08:22 -0700245 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 return getChildAt(index);
247 }
248
249 int getScrollWidth() {
250 return getWidth();
251 }
252
253 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800254 * Updates the scroll of the current page immediately to its final scroll position. We use this
255 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
256 * the previous tab page.
257 */
258 protected void updateCurrentPageScroll() {
259 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
260 scrollTo(newX, 0);
261 mScroller.setFinalX(newX);
262 }
263
264 /**
Winson Chung86f77532010-08-24 11:08:22 -0700265 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 */
Winson Chung86f77532010-08-24 11:08:22 -0700267 void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800268 if (!mScroller.isFinished()) {
269 mScroller.abortAnimation();
270 }
271 if (getChildCount() == 0 || currentPage == mCurrentPage) {
272 return;
273 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700274
Winson Chung86f77532010-08-24 11:08:22 -0700275 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Winson Chungbbc60d82010-11-11 16:34:41 -0800276 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700277
Winson Chung321e9ee2010-08-09 13:37:56 -0700278 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700279 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 }
281
Michael Jurka0142d492010-08-25 17:46:15 -0700282 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700283 if (mPageSwitchListener != null) {
284 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 }
286 }
287
Patrick Dubroy1262e362010-10-06 15:49:50 -0700288 private void pageBeginMoving() {
289 mIsPageMoving = true;
290 onPageBeginMoving();
291 }
292
293 private void pageEndMoving() {
294 onPageEndMoving();
295 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700296 }
297
298 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700299 protected void onPageBeginMoving() {
300 }
301
302 // a method that subclasses can override to add behavior
303 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700304 }
305
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 /**
Winson Chung86f77532010-08-24 11:08:22 -0700307 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 *
309 * @param l The listener used to respond to long clicks.
310 */
311 @Override
312 public void setOnLongClickListener(OnLongClickListener l) {
313 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700314 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700315 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700316 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 }
318 }
319
320 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800321 public void scrollBy(int x, int y) {
322 scrollTo(mUnboundedScrollX + x, mScrollY + y);
323 }
324
325 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700326 public void scrollTo(int x, int y) {
Adam Cohen68d73932010-11-15 10:50:58 -0800327 mUnboundedScrollX = x;
328
329 if (x < 0) {
330 super.scrollTo(0, y);
331 if (mAllowOverScroll) {
332 overScroll(x);
333 }
334 } else if (x > mMaxScrollX) {
335 super.scrollTo(mMaxScrollX, y);
336 if (mAllowOverScroll) {
337 overScroll(x - mMaxScrollX);
338 }
339 } else {
340 super.scrollTo(x, y);
341 }
342
Michael Jurka0142d492010-08-25 17:46:15 -0700343 mTouchX = x;
344 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
345 }
346
347 // we moved this functionality to a helper function so SmoothPagedView can reuse it
348 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700350 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700352 invalidate();
353 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700354 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700355 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700356 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700357 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700358 notifyPageSwitchListener();
Adam Cohen73aa9752010-11-24 16:26:58 -0800359 // We don't want to trigger a page end moving unless the page has settled
360 // and the user has stopped scrolling
361 if (mTouchState == TOUCH_STATE_REST) {
362 pageEndMoving();
363 }
Michael Jurka0142d492010-08-25 17:46:15 -0700364 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700365 }
Michael Jurka0142d492010-08-25 17:46:15 -0700366 return false;
367 }
368
369 @Override
370 public void computeScroll() {
371 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373
374 @Override
375 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
376 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
377 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
378 if (widthMode != MeasureSpec.EXACTLY) {
379 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
380 }
381
Adam Lesinski6b879f02010-11-04 16:15:23 -0700382 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
383 * of the All apps view on XLarge displays to not take up more space then it needs. Width
384 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
385 * each page to have the same width.
386 */
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700388 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
389 int maxChildHeight = 0;
390
391 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700392
393 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700394 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 final int childCount = getChildCount();
396 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700397 // disallowing padding in paged view (just pass 0)
398 final View child = getChildAt(i);
399 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
400
401 int childWidthMode;
402 if (lp.width == LayoutParams.WRAP_CONTENT) {
403 childWidthMode = MeasureSpec.AT_MOST;
404 } else {
405 childWidthMode = MeasureSpec.EXACTLY;
406 }
407
408 int childHeightMode;
409 if (lp.height == LayoutParams.WRAP_CONTENT) {
410 childHeightMode = MeasureSpec.AT_MOST;
411 } else {
412 childHeightMode = MeasureSpec.EXACTLY;
413 }
414
415 final int childWidthMeasureSpec =
416 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
417 final int childHeightMeasureSpec =
Adam Lesinski6b879f02010-11-04 16:15:23 -0700418 MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700419
420 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700421 maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
422 }
423
424 if (heightMode == MeasureSpec.AT_MOST) {
425 heightSize = maxChildHeight + verticalPadding;
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 }
Adam Cohenfaa28302010-11-19 12:02:18 -0800427 if (childCount > 0) {
428 mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
429 } else {
430 mMaxScrollX = 0;
431 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700432
433 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700434 }
435
Michael Jurkaaf91de02010-11-23 16:23:58 -0800436 protected void moveToNewPageWithoutMovingCellLayouts(int newCurrentPage) {
437 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
438 int delta = newX - mScrollX;
439
440 final int screenCount = getChildCount();
441 for (int i = 0; i < screenCount; i++) {
442 CellLayout cl = (CellLayout) getChildAt(i);
443 cl.setX(cl.getX() + delta);
444 }
445 setCurrentPage(newCurrentPage);
446 }
447
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700449 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700450 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
451 setHorizontalScrollBarEnabled(false);
452 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
453 scrollTo(newX, 0);
454 mScroller.setFinalX(newX);
455 setHorizontalScrollBarEnabled(true);
456 mFirstLayout = false;
457 }
458
Adam Lesinski6b879f02010-11-04 16:15:23 -0700459 final int verticalPadding = mPaddingTop + mPaddingBottom;
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 final int childCount = getChildCount();
461 int childLeft = 0;
462 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700463 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 }
465
466 for (int i = 0; i < childCount; i++) {
467 final View child = getChildAt(i);
468 if (child.getVisibility() != View.GONE) {
469 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700470 final int childHeight = child.getMeasuredHeight();
471 int childTop = mPaddingTop;
472 if (mCenterPagesVertically) {
473 childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
474 }
475 child.layout(childLeft, childTop,
476 childLeft + childWidth, childTop + childHeight);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700477 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
479 }
480 }
481
Winson Chunge3193b92010-09-10 11:44:42 -0700482 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700483 if (mFadeInAdjacentScreens) {
484 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700485 int halfScreenSize = getMeasuredWidth() / 2;
486 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700487 final int childCount = getChildCount();
488 for (int i = 0; i < childCount; ++i) {
489 View layout = (View) getChildAt(i);
490 int childWidth = layout.getMeasuredWidth();
491 int halfChildWidth = (childWidth / 2);
492 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700493
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700494 // On the first layout, we may not have a width nor a proper offset, so for now
495 // we should just assume full page width (and calculate the offset according to
496 // that).
497 if (childWidth <= 0) {
498 childWidth = getMeasuredWidth();
499 childCenter = (i * childWidth) + (childWidth / 2);
500 }
501
Winson Chunge8878e32010-09-15 20:37:09 -0700502 int d = halfChildWidth;
503 int distanceFromScreenCenter = childCenter - screenCenter;
504 if (distanceFromScreenCenter > 0) {
505 if (i > 0) {
506 d += getChildAt(i - 1).getMeasuredWidth() / 2;
507 }
Michael Jurka0142d492010-08-25 17:46:15 -0700508 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700509 if (i < childCount - 1) {
510 d += getChildAt(i + 1).getMeasuredWidth() / 2;
511 }
Michael Jurka0142d492010-08-25 17:46:15 -0700512 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700513 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700514
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700515 // Preventing potential divide-by-zero
516 d = Math.max(1, d);
517
Winson Chunge8878e32010-09-15 20:37:09 -0700518 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
519 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
520 float alpha = 1.0f - dimAlpha;
521
Adam Cohenf34bab52010-09-30 14:11:56 -0700522 if (alpha < ALPHA_QUANTIZE_LEVEL) {
523 alpha = 0.0f;
524 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
525 alpha = 1.0f;
526 }
527
Michael Jurka0142d492010-08-25 17:46:15 -0700528 if (Float.compare(alpha, layout.getAlpha()) != 0) {
529 layout.setAlpha(alpha);
530 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700531 }
Michael Jurka0142d492010-08-25 17:46:15 -0700532 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 }
534 }
Winson Chunge3193b92010-09-10 11:44:42 -0700535 }
536
Adam Cohenf34bab52010-09-30 14:11:56 -0700537 protected void screenScrolled(int screenCenter) {
538 }
539
Winson Chunge3193b92010-09-10 11:44:42 -0700540 @Override
541 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700542 int halfScreenSize = getMeasuredWidth() / 2;
543 int screenCenter = mScrollX + halfScreenSize;
544
545 if (screenCenter != mLastScreenCenter) {
546 screenScrolled(screenCenter);
547 updateAdjacentPagesAlpha();
548 mLastScreenCenter = screenCenter;
549 }
Michael Jurka0142d492010-08-25 17:46:15 -0700550
551 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700552 // As an optimization, this code assumes that all pages have the same width as the 0th
553 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700554 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700555 if (pageCount > 0) {
556 final int pageWidth = getChildAt(0).getMeasuredWidth();
557 final int screenWidth = getMeasuredWidth();
558 int x = getRelativeChildOffset(0) + pageWidth;
559 int leftScreen = 0;
560 int rightScreen = 0;
561 while (x <= mScrollX) {
562 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700563 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700564 // replace above line with this if you don't assume all pages have same width as 0th
565 // page:
566 // x += getChildAt(leftScreen).getMeasuredWidth();
567 }
568 rightScreen = leftScreen;
569 while (x < mScrollX + screenWidth) {
570 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700571 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700572 // replace above line with this if you don't assume all pages have same width as 0th
573 // page:
574 //if (rightScreen < pageCount) {
575 // x += getChildAt(rightScreen).getMeasuredWidth();
576 //}
577 }
578 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700579
Michael Jurkac4fb9172010-09-02 17:19:20 -0700580 final long drawingTime = getDrawingTime();
Winson Chung29d6fea2010-12-01 15:47:31 -0800581 // Clip to the bounds
582 canvas.save();
583 canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
584 mScrollY + mBottom - mTop);
585
Michael Jurkac4fb9172010-09-02 17:19:20 -0700586 for (int i = leftScreen; i <= rightScreen; i++) {
587 drawChild(canvas, getChildAt(i), drawingTime);
588 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800589 canvas.restore();
Michael Jurka0142d492010-08-25 17:46:15 -0700590 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700591 }
592
593 @Override
594 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700595 int page = indexOfChild(child);
596 if (page != mCurrentPage || !mScroller.isFinished()) {
597 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700598 return true;
599 }
600 return false;
601 }
602
603 @Override
604 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700605 int focusablePage;
606 if (mNextPage != INVALID_PAGE) {
607 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700609 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700610 }
Winson Chung86f77532010-08-24 11:08:22 -0700611 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700612 if (v != null) {
613 v.requestFocus(direction, previouslyFocusedRect);
614 }
615 return false;
616 }
617
618 @Override
619 public boolean dispatchUnhandledMove(View focused, int direction) {
620 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700621 if (getCurrentPage() > 0) {
622 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700623 return true;
624 }
625 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700626 if (getCurrentPage() < getPageCount() - 1) {
627 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 return true;
629 }
630 }
631 return super.dispatchUnhandledMove(focused, direction);
632 }
633
634 @Override
635 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700636 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
637 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700638 }
639 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700640 if (mCurrentPage > 0) {
641 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700642 }
643 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700644 if (mCurrentPage < getPageCount() - 1) {
645 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
647 }
648 }
649
650 /**
651 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700652 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 *
Winson Chung86f77532010-08-24 11:08:22 -0700654 * This happens when live folders requery, and if they're off page, they
655 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 */
657 @Override
658 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700659 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 View v = focused;
661 while (true) {
662 if (v == current) {
663 super.focusableViewAvailable(focused);
664 return;
665 }
666 if (v == this) {
667 return;
668 }
669 ViewParent parent = v.getParent();
670 if (parent instanceof View) {
671 v = (View)v.getParent();
672 } else {
673 return;
674 }
675 }
676 }
677
678 /**
679 * {@inheritDoc}
680 */
681 @Override
682 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
683 if (disallowIntercept) {
684 // We need to make sure to cancel our long press if
685 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700686 final View currentPage = getChildAt(mCurrentPage);
687 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 }
689 super.requestDisallowInterceptTouchEvent(disallowIntercept);
690 }
691
692 @Override
693 public boolean onInterceptTouchEvent(MotionEvent ev) {
694 /*
695 * This method JUST determines whether we want to intercept the motion.
696 * If we return true, onTouchEvent will be called and we do the actual
697 * scrolling there.
698 */
699
Winson Chung45e1d6e2010-11-09 17:19:49 -0800700 // Skip touch handling if there are no pages to swipe
701 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
702
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 /*
704 * Shortcut the most recurring case: the user is in the dragging
705 * state and he is moving his finger. We want to intercept this
706 * motion.
707 */
708 final int action = ev.getAction();
709 if ((action == MotionEvent.ACTION_MOVE) &&
710 (mTouchState == TOUCH_STATE_SCROLLING)) {
711 return true;
712 }
713
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 switch (action & MotionEvent.ACTION_MASK) {
715 case MotionEvent.ACTION_MOVE: {
716 /*
717 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
718 * whether the user has moved far enough from his original down touch.
719 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700720 if (mActivePointerId != INVALID_POINTER) {
721 determineScrollingStart(ev);
722 break;
723 }
724 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
725 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
726 // i.e. fall through to the next case (don't break)
727 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
728 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 }
730
731 case MotionEvent.ACTION_DOWN: {
732 final float x = ev.getX();
733 final float y = ev.getY();
734 // Remember location of down touch
735 mDownMotionX = x;
736 mLastMotionX = x;
737 mLastMotionY = y;
738 mActivePointerId = ev.getPointerId(0);
739 mAllowLongPress = true;
740
741 /*
742 * If being flinged and user touches the screen, initiate drag;
743 * otherwise don't. mScroller.isFinished should be false when
744 * being flinged.
745 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700746 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700747 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
748 if (finishedScrolling) {
749 mTouchState = TOUCH_STATE_REST;
750 mScroller.abortAnimation();
751 } else {
752 mTouchState = TOUCH_STATE_SCROLLING;
753 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700754
Winson Chung86f77532010-08-24 11:08:22 -0700755 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700756 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700757 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700758 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
759 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700760 int width = getMeasuredWidth();
761 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700762 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700763 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700764 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700765 mTouchState = TOUCH_STATE_NEXT_PAGE;
766 }
767 }
768 }
769 break;
770 }
771
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -0800773 onWallpaperTap(ev);
774 case MotionEvent.ACTION_CANCEL:
Winson Chung321e9ee2010-08-09 13:37:56 -0700775 mTouchState = TOUCH_STATE_REST;
776 mAllowLongPress = false;
777 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700778 break;
779
780 case MotionEvent.ACTION_POINTER_UP:
781 onSecondaryPointerUp(ev);
782 break;
783 }
784
785 /*
786 * The only time we want to intercept motion events is if we are in the
787 * drag mode.
788 */
789 return mTouchState != TOUCH_STATE_REST;
790 }
791
Winson Chung80baf5a2010-08-09 16:03:15 -0700792 protected void animateClickFeedback(View v, final Runnable r) {
793 // animate the view slightly to show click feedback running some logic after it is "pressed"
794 Animation anim = AnimationUtils.loadAnimation(getContext(),
795 R.anim.paged_view_click_feedback);
796 anim.setAnimationListener(new AnimationListener() {
797 @Override
798 public void onAnimationStart(Animation animation) {}
799 @Override
800 public void onAnimationRepeat(Animation animation) {
801 r.run();
802 }
803 @Override
804 public void onAnimationEnd(Animation animation) {}
805 });
806 v.startAnimation(anim);
807 }
808
Winson Chung321e9ee2010-08-09 13:37:56 -0700809 /*
810 * Determines if we should change the touch state to start scrolling after the
811 * user moves their touch point too far.
812 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700813 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700814 /*
815 * Locally do absolute value. mLastMotionX is set to the y value
816 * of the down event.
817 */
818 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
819 final float x = ev.getX(pointerIndex);
820 final float y = ev.getY(pointerIndex);
821 final int xDiff = (int) Math.abs(x - mLastMotionX);
822 final int yDiff = (int) Math.abs(y - mLastMotionY);
823
824 final int touchSlop = mTouchSlop;
825 boolean xPaged = xDiff > mPagingTouchSlop;
826 boolean xMoved = xDiff > touchSlop;
827 boolean yMoved = yDiff > touchSlop;
828
829 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700830 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 // Scroll if the user moved far enough along the X axis
832 mTouchState = TOUCH_STATE_SCROLLING;
833 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700834 mTouchX = mScrollX;
835 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
836 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700837 }
838 // Either way, cancel any pending longpress
839 if (mAllowLongPress) {
840 mAllowLongPress = false;
841 // Try canceling the long press. It could also have been scheduled
842 // by a distant descendant, so use the mAllowLongPress flag to block
843 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700844 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700845 if (currentPage != null) {
846 currentPage.cancelLongPress();
847 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700848 }
849 }
850 }
851
Adam Cohen21f12b52010-10-07 17:15:37 -0700852 protected boolean handlePagingClicks() {
853 return false;
854 }
855
Adam Cohene0f66b52010-11-23 15:06:07 -0800856 // This curve determines how the effect of scrolling over the limits of the page dimishes
857 // as the user pulls further and further from the bounds
858 private float overScrollInfluenceCurve(float f) {
859 f -= 1.0f;
860 return f * f * f + 1.0f;
861 }
862
Adam Cohen68d73932010-11-15 10:50:58 -0800863 protected void overScroll(float amount) {
Adam Cohene0f66b52010-11-23 15:06:07 -0800864 int screenSize = getMeasuredWidth();
865
866 float f = (amount / screenSize);
867
868 if (f == 0) return;
869 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
870
871 int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize);
Adam Cohen68d73932010-11-15 10:50:58 -0800872 if (amount < 0) {
873 mScrollX = overScrollAmount;
874 } else {
875 mScrollX = mMaxScrollX + overScrollAmount;
876 }
877 invalidate();
878 }
879
Winson Chung321e9ee2010-08-09 13:37:56 -0700880 @Override
881 public boolean onTouchEvent(MotionEvent ev) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800882 // Skip touch handling if there are no pages to swipe
883 if (getChildCount() <= 0) return super.onTouchEvent(ev);
884
Michael Jurkab8f06722010-10-10 15:58:46 -0700885 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700886
887 final int action = ev.getAction();
888
889 switch (action & MotionEvent.ACTION_MASK) {
890 case MotionEvent.ACTION_DOWN:
891 /*
892 * If being flinged and user touches, stop the fling. isFinished
893 * will be false if being flinged.
894 */
895 if (!mScroller.isFinished()) {
896 mScroller.abortAnimation();
897 }
898
899 // Remember where the motion event started
900 mDownMotionX = mLastMotionX = ev.getX();
901 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700902 if (mTouchState == TOUCH_STATE_SCROLLING) {
903 pageBeginMoving();
904 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700905 break;
906
907 case MotionEvent.ACTION_MOVE:
908 if (mTouchState == TOUCH_STATE_SCROLLING) {
909 // Scroll to follow the motion event
910 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
911 final float x = ev.getX(pointerIndex);
912 final int deltaX = (int) (mLastMotionX - x);
913 mLastMotionX = x;
914
Adam Cohen68d73932010-11-15 10:50:58 -0800915 if (deltaX != 0) {
916 mTouchX += deltaX;
917 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
918 if (!mDeferScrollUpdate) {
919 scrollBy(deltaX, 0);
920 } else {
921 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700922 }
923 } else {
924 awakenScrollBars();
925 }
Adam Cohen564976a2010-10-13 18:52:07 -0700926 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700927 determineScrollingStart(ev);
928 }
929 break;
930
931 case MotionEvent.ACTION_UP:
932 if (mTouchState == TOUCH_STATE_SCROLLING) {
933 final int activePointerId = mActivePointerId;
934 final int pointerIndex = ev.findPointerIndex(activePointerId);
935 final float x = ev.getX(pointerIndex);
936 final VelocityTracker velocityTracker = mVelocityTracker;
937 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
938 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700939 final int deltaX = (int) (x - mDownMotionX);
940 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
941 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700942
Michael Jurka0142d492010-08-25 17:46:15 -0700943 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700944 if ((isSignificantMove && deltaX > 0 ||
Adam Cohene0f66b52010-11-23 15:06:07 -0800945 (isfling && velocityX > snapVelocity)) && mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700946 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700947 } else if ((isSignificantMove && deltaX < 0 ||
948 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700949 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700950 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 } else {
952 snapToDestination();
953 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700954 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 // at this point we have not moved beyond the touch slop
956 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
957 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700958 int nextPage = Math.max(0, mCurrentPage - 1);
959 if (nextPage != mCurrentPage) {
960 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 } else {
962 snapToDestination();
963 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700964 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700965 // at this point we have not moved beyond the touch slop
966 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
967 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700968 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
969 if (nextPage != mCurrentPage) {
970 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 } else {
972 snapToDestination();
973 }
Jeff Brown1d0867c2010-12-02 18:27:39 -0800974 } else {
975 onWallpaperTap(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 }
977 mTouchState = TOUCH_STATE_REST;
978 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700979 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 break;
981
982 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700983 if (mTouchState == TOUCH_STATE_SCROLLING) {
984 snapToDestination();
985 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 mTouchState = TOUCH_STATE_REST;
987 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700988 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700989 break;
990
991 case MotionEvent.ACTION_POINTER_UP:
992 onSecondaryPointerUp(ev);
993 break;
994 }
995
996 return true;
997 }
998
Michael Jurkab8f06722010-10-10 15:58:46 -0700999 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1000 if (mVelocityTracker == null) {
1001 mVelocityTracker = VelocityTracker.obtain();
1002 }
1003 mVelocityTracker.addMovement(ev);
1004 }
1005
1006 private void releaseVelocityTracker() {
1007 if (mVelocityTracker != null) {
1008 mVelocityTracker.recycle();
1009 mVelocityTracker = null;
1010 }
1011 }
1012
Winson Chung321e9ee2010-08-09 13:37:56 -07001013 private void onSecondaryPointerUp(MotionEvent ev) {
1014 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1015 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1016 final int pointerId = ev.getPointerId(pointerIndex);
1017 if (pointerId == mActivePointerId) {
1018 // This was our active pointer going up. Choose a new
1019 // active pointer and adjust accordingly.
1020 // TODO: Make this decision more intelligent.
1021 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1022 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1023 mLastMotionY = ev.getY(newPointerIndex);
1024 mActivePointerId = ev.getPointerId(newPointerIndex);
1025 if (mVelocityTracker != null) {
1026 mVelocityTracker.clear();
1027 }
1028 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001029 if (mTouchState == TOUCH_STATE_REST) {
1030 onWallpaperTap(ev);
1031 }
1032 }
1033
1034 protected void onWallpaperTap(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 }
1036
1037 @Override
1038 public void requestChildFocus(View child, View focused) {
1039 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -07001040 int page = indexOfChild(child);
1041 if (page >= 0 && !isInTouchMode()) {
1042 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001043 }
1044 }
1045
Winson Chunge3193b92010-09-10 11:44:42 -07001046 protected int getChildIndexForRelativeOffset(int relativeOffset) {
1047 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -07001048 int left;
1049 int right;
Winson Chunge3193b92010-09-10 11:44:42 -07001050 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001051 left = getRelativeChildOffset(i);
1052 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -07001053 if (left <= relativeOffset && relativeOffset <= right) {
1054 return i;
1055 }
Winson Chunge3193b92010-09-10 11:44:42 -07001056 }
1057 return -1;
1058 }
1059
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 protected int getRelativeChildOffset(int index) {
1061 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
1062 }
1063
1064 protected int getChildOffset(int index) {
1065 if (getChildCount() == 0)
1066 return 0;
1067
1068 int offset = getRelativeChildOffset(0);
1069 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -07001070 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -07001071 }
1072 return offset;
1073 }
1074
Adam Cohend19d3ca2010-09-15 14:43:42 -07001075 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -07001076 int minDistanceFromScreenCenter = getMeasuredWidth();
1077 int minDistanceFromScreenCenterIndex = -1;
1078 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
1079 final int childCount = getChildCount();
1080 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -07001081 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -07001082 int childWidth = layout.getMeasuredWidth();
1083 int halfChildWidth = (childWidth / 2);
1084 int childCenter = getChildOffset(i) + halfChildWidth;
1085 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1086 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1087 minDistanceFromScreenCenter = distanceFromScreenCenter;
1088 minDistanceFromScreenCenterIndex = i;
1089 }
1090 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001091 return minDistanceFromScreenCenterIndex;
1092 }
1093
1094 protected void snapToDestination() {
1095 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001096 }
1097
Adam Cohene0f66b52010-11-23 15:06:07 -08001098 private static class ScrollInterpolator implements Interpolator {
1099 public ScrollInterpolator() {
1100 }
1101
1102 public float getInterpolation(float t) {
1103 t -= 1.0f;
1104 return t*t*t*t*t + 1;
1105 }
1106 }
1107
1108 // We want the duration of the page snap animation to be influenced by the distance that
1109 // the screen has to travel, however, we don't want this duration to be effected in a
1110 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1111 // of travel has on the overall snap duration.
1112 float distanceInfluenceForSnapDuration(float f) {
1113 f -= 0.5f; // center the values about 0.
1114 f *= 0.3f * Math.PI / 2.0f;
1115 return (float) Math.sin(f);
1116 }
1117
Michael Jurka0142d492010-08-25 17:46:15 -07001118 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001119 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
1120 int halfScreenSize = getMeasuredWidth() / 2;
1121
1122 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
1123 int delta = newX - mUnboundedScrollX;
1124 int duration = 0;
1125
1126 if (Math.abs(velocity) < MIN_FLING_VELOCITY) {
1127 // If the velocity is low enough, then treat this more as an automatic page advance
1128 // as opposed to an apparent physical response to flinging
1129 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
1130 return;
1131 }
1132
1133 // Here we compute a "distance" that will be used in the computation of the overall
1134 // snap duration. This is a function of the actual distance that needs to be traveled;
1135 // we keep this value close to half screen size in order to reduce the variance in snap
1136 // duration as a function of the distance the page needs to travel.
1137 float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize;
1138 float distance = halfScreenSize + halfScreenSize *
1139 distanceInfluenceForSnapDuration(distanceRatio);
1140
1141 velocity = Math.abs(velocity);
1142 velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity);
1143
1144 // we want the page's snap velocity to approximately match the velocity at which the
1145 // user flings, so we scale the duration by a value near to the derivative of the scroll
1146 // interpolator at zero, ie. 5. We use 6 to make it a little slower.
1147 duration = 6 * Math.round(1000 * Math.abs(distance / velocity));
1148
1149 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07001150 }
1151
1152 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001153 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001154 }
1155
Michael Jurka0142d492010-08-25 17:46:15 -07001156 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001157 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001158
Winson Chung86f77532010-08-24 11:08:22 -07001159 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Adam Cohen68d73932010-11-15 10:50:58 -08001160 final int sX = mUnboundedScrollX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001161 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001162 snapToPage(whichPage, delta, duration);
1163 }
1164
1165 protected void snapToPage(int whichPage, int delta, int duration) {
1166 mNextPage = whichPage;
1167
1168 View focusedChild = getFocusedChild();
1169 if (focusedChild != null && whichPage != mCurrentPage &&
1170 focusedChild == getChildAt(mCurrentPage)) {
1171 focusedChild.clearFocus();
1172 }
1173
1174 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001175 awakenScrollBars(duration);
1176 if (duration == 0) {
1177 duration = Math.abs(delta);
1178 }
1179
1180 if (!mScroller.isFinished()) mScroller.abortAnimation();
Adam Cohen68d73932010-11-15 10:50:58 -08001181 mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001182
1183 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001184 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001185 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001186 invalidate();
1187 }
1188
1189 @Override
1190 protected Parcelable onSaveInstanceState() {
1191 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001192 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001193 return state;
1194 }
1195
1196 @Override
1197 protected void onRestoreInstanceState(Parcelable state) {
1198 SavedState savedState = (SavedState) state;
1199 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001200 if (savedState.currentPage != -1) {
1201 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 }
1203 }
1204
1205 public void scrollLeft() {
1206 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001207 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001209 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 }
1211 }
1212
1213 public void scrollRight() {
1214 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001215 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001216 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001217 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001218 }
1219 }
1220
Winson Chung86f77532010-08-24 11:08:22 -07001221 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 int result = -1;
1223 if (v != null) {
1224 ViewParent vp = v.getParent();
1225 int count = getChildCount();
1226 for (int i = 0; i < count; i++) {
1227 if (vp == getChildAt(i)) {
1228 return i;
1229 }
1230 }
1231 }
1232 return result;
1233 }
1234
1235 /**
1236 * @return True is long presses are still allowed for the current touch
1237 */
1238 public boolean allowLongPress() {
1239 return mAllowLongPress;
1240 }
1241
Michael Jurka0142d492010-08-25 17:46:15 -07001242 /**
1243 * Set true to allow long-press events to be triggered, usually checked by
1244 * {@link Launcher} to accept or block dpad-initiated long-presses.
1245 */
1246 public void setAllowLongPress(boolean allowLongPress) {
1247 mAllowLongPress = allowLongPress;
1248 }
1249
Winson Chung321e9ee2010-08-09 13:37:56 -07001250 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001251 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001252
1253 SavedState(Parcelable superState) {
1254 super(superState);
1255 }
1256
1257 private SavedState(Parcel in) {
1258 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001259 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001260 }
1261
1262 @Override
1263 public void writeToParcel(Parcel out, int flags) {
1264 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001265 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 }
1267
1268 public static final Parcelable.Creator<SavedState> CREATOR =
1269 new Parcelable.Creator<SavedState>() {
1270 public SavedState createFromParcel(Parcel in) {
1271 return new SavedState(in);
1272 }
1273
1274 public SavedState[] newArray(int size) {
1275 return new SavedState[size];
1276 }
1277 };
1278 }
1279
Winson Chung86f77532010-08-24 11:08:22 -07001280 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001281 if (mContentIsRefreshable) {
1282 final int count = getChildCount();
1283 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001284 int lowerPageBound = getAssociatedLowerPageBound(page);
1285 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001286 for (int i = 0; i < count; ++i) {
1287 final ViewGroup layout = (ViewGroup) getChildAt(i);
1288 final int childCount = layout.getChildCount();
1289 if (lowerPageBound <= i && i <= upperPageBound) {
1290 if (mDirtyPageContent.get(i)) {
1291 syncPageItems(i);
1292 mDirtyPageContent.set(i, false);
1293 }
1294 } else {
1295 if (childCount > 0) {
1296 layout.removeAllViews();
1297 }
1298 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001299 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001300 }
1301 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001302 }
1303 }
1304
Winson Chunge3193b92010-09-10 11:44:42 -07001305 protected int getAssociatedLowerPageBound(int page) {
1306 return Math.max(0, page - 1);
1307 }
1308 protected int getAssociatedUpperPageBound(int page) {
1309 final int count = getChildCount();
1310 return Math.min(page + 1, count - 1);
1311 }
1312
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001313 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001314 if (isChoiceMode(CHOICE_MODE_NONE)) {
1315 mChoiceMode = mode;
1316 mActionMode = startActionMode(callback);
1317 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001318 }
1319
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001320 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001321 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001322 mChoiceMode = CHOICE_MODE_NONE;
1323 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001324 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001325 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001326 }
1327 }
1328
1329 protected boolean isChoiceMode(int mode) {
1330 return mChoiceMode == mode;
1331 }
1332
1333 protected ArrayList<Checkable> getCheckedGrandchildren() {
1334 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1335 final int childCount = getChildCount();
1336 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001337 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001338 final int grandChildCount = layout.getChildCount();
1339 for (int j = 0; j < grandChildCount; ++j) {
1340 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001341 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001342 checked.add((Checkable) v);
1343 }
1344 }
1345 }
1346 return checked;
1347 }
1348
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001349 /**
1350 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1351 * Otherwise, returns null.
1352 */
1353 protected Checkable getSingleCheckedGrandchild() {
1354 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1355 final int childCount = getChildCount();
1356 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001357 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001358 final int grandChildCount = layout.getChildCount();
1359 for (int j = 0; j < grandChildCount; ++j) {
1360 final View v = layout.getChildAt(j);
1361 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1362 return (Checkable) v;
1363 }
1364 }
1365 }
1366 }
1367 return null;
1368 }
1369
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001370 public Object getChosenItem() {
1371 View checkedView = (View) getSingleCheckedGrandchild();
1372 if (checkedView != null) {
1373 return checkedView.getTag();
1374 }
1375 return null;
1376 }
1377
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001378 protected void resetCheckedGrandchildren() {
1379 // loop through children, and set all of their children to _not_ be checked
1380 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1381 for (int i = 0; i < checked.size(); ++i) {
1382 final Checkable c = checked.get(i);
1383 c.setChecked(false);
1384 }
1385 }
1386
Winson Chung86f77532010-08-24 11:08:22 -07001387 /**
1388 * This method is called ONLY to synchronize the number of pages that the paged view has.
1389 * To actually fill the pages with information, implement syncPageItems() below. It is
1390 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1391 * and therefore, individual page items do not need to be updated in this method.
1392 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001393 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001394
1395 /**
1396 * This method is called to synchronize the items that are on a particular page. If views on
1397 * the page can be reused, then they should be updated within this method.
1398 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001399 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001400
Winson Chung321e9ee2010-08-09 13:37:56 -07001401 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001402 if (mContentIsRefreshable) {
1403 // Update all the pages
1404 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001405
Michael Jurka0142d492010-08-25 17:46:15 -07001406 // Mark each of the pages as dirty
1407 final int count = getChildCount();
1408 mDirtyPageContent.clear();
1409 for (int i = 0; i < count; ++i) {
1410 mDirtyPageContent.add(true);
1411 }
1412
1413 // Load any pages that are necessary for the current window of views
1414 loadAssociatedPages(mCurrentPage);
1415 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001416 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001417 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001418 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001419 }
1420}