blob: 578bbcc1431918e737c1ff977a1637d8091bd06c [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chunge3193b92010-09-10 11:44:42 -070019import java.util.ArrayList;
20import java.util.HashMap;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070021
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070023import android.content.res.TypedArray;
Winson Chung241c3b42010-08-25 16:53:03 -070024import android.graphics.Bitmap;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.graphics.Canvas;
26import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
Winson Chunge3193b92010-09-10 11:44:42 -070030import android.util.Log;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070031import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.MotionEvent;
33import android.view.VelocityTracker;
34import android.view.View;
35import android.view.ViewConfiguration;
36import android.view.ViewGroup;
37import android.view.ViewParent;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.view.animation.Animation;
Michael Jurka5f1c5092010-09-03 14:15:02 -070039import android.view.animation.Animation.AnimationListener;
Winson Chunge3193b92010-09-10 11:44:42 -070040import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070041import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070042import android.widget.LinearLayout;
Winson Chung321e9ee2010-08-09 13:37:56 -070043import android.widget.Scroller;
44
Winson Chunge3193b92010-09-10 11:44:42 -070045import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070046
Winson Chung321e9ee2010-08-09 13:37:56 -070047/**
48 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070049 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070050 */
51public abstract class PagedView extends ViewGroup {
52 private static final String TAG = "PagedView";
Michael Jurka0142d492010-08-25 17:46:15 -070053 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Winson Chung86f77532010-08-24 11:08:22 -070055 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung321e9ee2010-08-09 13:37:56 -070056 private static final int MIN_LENGTH_FOR_FLING = 50;
57
Winson Chung5f2aa4e2010-08-20 14:49:25 -070058 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070059 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070060
Michael Jurka0142d492010-08-25 17:46:15 -070061 // the velocity at which a fling gesture will cause us to snap to the next page
62 protected int mSnapVelocity = 500;
63
64 protected float mSmoothingTime;
65 protected float mTouchX;
66
67 protected boolean mFirstLayout = true;
68
69 protected int mCurrentPage;
70 protected int mNextPage = INVALID_PAGE;
71 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070072 private VelocityTracker mVelocityTracker;
73
74 private float mDownMotionX;
75 private float mLastMotionX;
76 private float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070077 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070078
Michael Jurka0142d492010-08-25 17:46:15 -070079 protected final static int TOUCH_STATE_REST = 0;
80 protected final static int TOUCH_STATE_SCROLLING = 1;
81 protected final static int TOUCH_STATE_PREV_PAGE = 2;
82 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohenf34bab52010-09-30 14:11:56 -070083 protected final static float ALPHA_QUANTIZE_LEVEL = 0.01f;
Winson Chung321e9ee2010-08-09 13:37:56 -070084
Michael Jurka0142d492010-08-25 17:46:15 -070085 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070086
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070088
89 private boolean mAllowLongPress = true;
90
91 private int mTouchSlop;
92 private int mPagingTouchSlop;
93 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070094 protected int mPageSpacing;
95 protected int mPageLayoutPaddingTop;
96 protected int mPageLayoutPaddingBottom;
97 protected int mPageLayoutPaddingLeft;
98 protected int mPageLayoutPaddingRight;
99 protected int mCellCountX;
100 protected int mCellCountY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700101
Michael Jurka5f1c5092010-09-03 14:15:02 -0700102 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700103
Michael Jurka5f1c5092010-09-03 14:15:02 -0700104 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Winson Chung86f77532010-08-24 11:08:22 -0700106 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Winson Chung86f77532010-08-24 11:08:22 -0700108 private ArrayList<Boolean> mDirtyPageContent;
109 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700111 // choice modes
112 protected static final int CHOICE_MODE_NONE = 0;
113 protected static final int CHOICE_MODE_SINGLE = 1;
114 // Multiple selection mode is not supported by all Launcher actions atm
115 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700116
Michael Jurkae17e19c2010-09-28 11:01:39 -0700117 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700118 private ActionMode mActionMode;
119
Winson Chung241c3b42010-08-25 16:53:03 -0700120 protected PagedViewIconCache mPageViewIconCache;
121
Michael Jurka0142d492010-08-25 17:46:15 -0700122 // If true, syncPages and syncPageItems will be called to refresh pages
123 protected boolean mContentIsRefreshable = true;
124
125 // If true, modify alpha of neighboring pages as user scrolls left/right
126 protected boolean mFadeInAdjacentScreens = true;
127
128 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
129 // to switch to a new page
130 protected boolean mUsePagingTouchSlop = true;
131
132 // If true, the subclass should directly update mScrollX itself in its computeScroll method
133 // (SmoothPagedView does this)
134 protected boolean mDeferScrollUpdate = false;
135
Patrick Dubroy1262e362010-10-06 15:49:50 -0700136 protected boolean mIsPageMoving = false;
137
Winson Chung241c3b42010-08-25 16:53:03 -0700138 /**
139 * Simple cache mechanism for PagedViewIcon outlines.
140 */
141 class PagedViewIconCache {
142 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
143
144 public void clear() {
145 iconOutlineCache.clear();
146 }
147 public void addOutline(Object key, Bitmap b) {
148 iconOutlineCache.put(key, b);
149 }
150 public void removeOutline(Object key) {
151 if (iconOutlineCache.containsKey(key)) {
152 iconOutlineCache.remove(key);
153 }
154 }
155 public Bitmap getOutline(Object key) {
156 return iconOutlineCache.get(key);
157 }
158 }
159
Winson Chung86f77532010-08-24 11:08:22 -0700160 public interface PageSwitchListener {
161 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 }
163
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 public PagedView(Context context) {
165 this(context, null);
166 }
167
168 public PagedView(Context context, AttributeSet attrs) {
169 this(context, attrs, 0);
170 }
171
172 public PagedView(Context context, AttributeSet attrs, int defStyle) {
173 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700174 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700175
Adam Cohen9c4949e2010-10-05 12:27:22 -0700176 TypedArray a = context.obtainStyledAttributes(attrs,
177 R.styleable.PagedView, defStyle, 0);
178 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
179 mPageLayoutPaddingTop = a.getDimensionPixelSize(
180 R.styleable.PagedView_pageLayoutPaddingTop, 10);
181 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
182 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
183 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
185 mPageLayoutPaddingRight = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutPaddingRight, 10);
187 a.recycle();
188
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700190 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700191 }
192
193 /**
194 * Initializes various states for this workspace.
195 */
Michael Jurka0142d492010-08-25 17:46:15 -0700196 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700197 mDirtyPageContent = new ArrayList<Boolean>();
198 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700199 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700201 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700202
203 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
204 mTouchSlop = configuration.getScaledTouchSlop();
205 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
206 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
207 }
208
Winson Chung86f77532010-08-24 11:08:22 -0700209 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
210 mPageSwitchListener = pageSwitchListener;
211 if (mPageSwitchListener != null) {
212 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 }
214 }
215
216 /**
Winson Chung86f77532010-08-24 11:08:22 -0700217 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 *
Winson Chung86f77532010-08-24 11:08:22 -0700219 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 */
Winson Chung86f77532010-08-24 11:08:22 -0700221 int getCurrentPage() {
222 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224
Winson Chung86f77532010-08-24 11:08:22 -0700225 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 return getChildCount();
227 }
228
Winson Chung86f77532010-08-24 11:08:22 -0700229 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 return getChildAt(index);
231 }
232
233 int getScrollWidth() {
234 return getWidth();
235 }
236
237 /**
Winson Chung86f77532010-08-24 11:08:22 -0700238 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 */
Winson Chung86f77532010-08-24 11:08:22 -0700240 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 if (!mScroller.isFinished()) mScroller.abortAnimation();
242 if (getChildCount() == 0) return;
243
Winson Chung86f77532010-08-24 11:08:22 -0700244 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700245 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
246 scrollTo(newX, 0);
247 mScroller.setFinalX(newX);
Winson Chung80baf5a2010-08-09 16:03:15 -0700248
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700250 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 }
252
Michael Jurka0142d492010-08-25 17:46:15 -0700253 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700254 if (mPageSwitchListener != null) {
255 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 }
257 }
258
Patrick Dubroy1262e362010-10-06 15:49:50 -0700259 private void pageBeginMoving() {
260 mIsPageMoving = true;
261 onPageBeginMoving();
262 }
263
264 private void pageEndMoving() {
265 onPageEndMoving();
266 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700267 }
268
269 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700270 protected void onPageBeginMoving() {
271 }
272
273 // a method that subclasses can override to add behavior
274 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700275 }
276
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 /**
Winson Chung86f77532010-08-24 11:08:22 -0700278 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700279 *
280 * @param l The listener used to respond to long clicks.
281 */
282 @Override
283 public void setOnLongClickListener(OnLongClickListener l) {
284 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700285 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700287 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700288 }
289 }
290
291 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700292 public void scrollTo(int x, int y) {
293 super.scrollTo(x, y);
294 mTouchX = x;
295 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
296 }
297
298 // we moved this functionality to a helper function so SmoothPagedView can reuse it
299 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700301 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700303 invalidate();
304 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700305 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700306 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700307 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700308 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700309 notifyPageSwitchListener();
310 pageEndMoving();
311 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 }
Michael Jurka0142d492010-08-25 17:46:15 -0700313 return false;
314 }
315
316 @Override
317 public void computeScroll() {
318 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700319 }
320
321 @Override
322 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
323 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
324 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
325 if (widthMode != MeasureSpec.EXACTLY) {
326 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
327 }
328
329 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
330 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
331 if (heightMode != MeasureSpec.EXACTLY) {
332 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
333 }
334
335 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700336 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700337 final int childCount = getChildCount();
338 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700339 // disallowing padding in paged view (just pass 0)
340 final View child = getChildAt(i);
341 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
342
343 int childWidthMode;
344 if (lp.width == LayoutParams.WRAP_CONTENT) {
345 childWidthMode = MeasureSpec.AT_MOST;
346 } else {
347 childWidthMode = MeasureSpec.EXACTLY;
348 }
349
350 int childHeightMode;
351 if (lp.height == LayoutParams.WRAP_CONTENT) {
352 childHeightMode = MeasureSpec.AT_MOST;
353 } else {
354 childHeightMode = MeasureSpec.EXACTLY;
355 }
356
357 final int childWidthMeasureSpec =
358 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
359 final int childHeightMeasureSpec =
360 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
361
362 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700363 }
364
365 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700366 }
367
368 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700369 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700370 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
371 setHorizontalScrollBarEnabled(false);
372 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
373 scrollTo(newX, 0);
374 mScroller.setFinalX(newX);
375 setHorizontalScrollBarEnabled(true);
376 mFirstLayout = false;
377 }
378
Winson Chung321e9ee2010-08-09 13:37:56 -0700379 final int childCount = getChildCount();
380 int childLeft = 0;
381 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700382 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700383 }
384
385 for (int i = 0; i < childCount; i++) {
386 final View child = getChildAt(i);
387 if (child.getVisibility() != View.GONE) {
388 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700389 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
390 child.layout(childLeft, childHeight,
391 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700392 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 }
394 }
395 }
396
Winson Chunge3193b92010-09-10 11:44:42 -0700397 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700398 if (mFadeInAdjacentScreens) {
399 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700400 int halfScreenSize = getMeasuredWidth() / 2;
401 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700402 final int childCount = getChildCount();
403 for (int i = 0; i < childCount; ++i) {
404 View layout = (View) getChildAt(i);
405 int childWidth = layout.getMeasuredWidth();
406 int halfChildWidth = (childWidth / 2);
407 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700408
409 int d = halfChildWidth;
410 int distanceFromScreenCenter = childCenter - screenCenter;
411 if (distanceFromScreenCenter > 0) {
412 if (i > 0) {
413 d += getChildAt(i - 1).getMeasuredWidth() / 2;
414 }
Michael Jurka0142d492010-08-25 17:46:15 -0700415 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700416 if (i < childCount - 1) {
417 d += getChildAt(i + 1).getMeasuredWidth() / 2;
418 }
Michael Jurka0142d492010-08-25 17:46:15 -0700419 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700420 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700421
422 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
423 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
424 float alpha = 1.0f - dimAlpha;
425
Adam Cohenf34bab52010-09-30 14:11:56 -0700426 if (alpha < ALPHA_QUANTIZE_LEVEL) {
427 alpha = 0.0f;
428 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
429 alpha = 1.0f;
430 }
431
Michael Jurka0142d492010-08-25 17:46:15 -0700432 if (Float.compare(alpha, layout.getAlpha()) != 0) {
433 layout.setAlpha(alpha);
434 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
Michael Jurka0142d492010-08-25 17:46:15 -0700436 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 }
438 }
Winson Chunge3193b92010-09-10 11:44:42 -0700439 }
440
Adam Cohenf34bab52010-09-30 14:11:56 -0700441 protected void screenScrolled(int screenCenter) {
442 }
443
Winson Chunge3193b92010-09-10 11:44:42 -0700444 @Override
445 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700446 int halfScreenSize = getMeasuredWidth() / 2;
447 int screenCenter = mScrollX + halfScreenSize;
448
449 if (screenCenter != mLastScreenCenter) {
450 screenScrolled(screenCenter);
451 updateAdjacentPagesAlpha();
452 mLastScreenCenter = screenCenter;
453 }
Michael Jurka0142d492010-08-25 17:46:15 -0700454
455 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700456 // As an optimization, this code assumes that all pages have the same width as the 0th
457 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700458 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700459 if (pageCount > 0) {
460 final int pageWidth = getChildAt(0).getMeasuredWidth();
461 final int screenWidth = getMeasuredWidth();
462 int x = getRelativeChildOffset(0) + pageWidth;
463 int leftScreen = 0;
464 int rightScreen = 0;
465 while (x <= mScrollX) {
466 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700467 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700468 // replace above line with this if you don't assume all pages have same width as 0th
469 // page:
470 // x += getChildAt(leftScreen).getMeasuredWidth();
471 }
472 rightScreen = leftScreen;
473 while (x < mScrollX + screenWidth) {
474 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700475 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700476 // replace above line with this if you don't assume all pages have same width as 0th
477 // page:
478 //if (rightScreen < pageCount) {
479 // x += getChildAt(rightScreen).getMeasuredWidth();
480 //}
481 }
482 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700483
Michael Jurkac4fb9172010-09-02 17:19:20 -0700484 final long drawingTime = getDrawingTime();
485 for (int i = leftScreen; i <= rightScreen; i++) {
486 drawChild(canvas, getChildAt(i), drawingTime);
487 }
Michael Jurka0142d492010-08-25 17:46:15 -0700488 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
490
491 @Override
492 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700493 int page = indexOfChild(child);
494 if (page != mCurrentPage || !mScroller.isFinished()) {
495 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700496 return true;
497 }
498 return false;
499 }
500
501 @Override
502 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700503 int focusablePage;
504 if (mNextPage != INVALID_PAGE) {
505 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700507 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 }
Winson Chung86f77532010-08-24 11:08:22 -0700509 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 if (v != null) {
511 v.requestFocus(direction, previouslyFocusedRect);
512 }
513 return false;
514 }
515
516 @Override
517 public boolean dispatchUnhandledMove(View focused, int direction) {
518 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700519 if (getCurrentPage() > 0) {
520 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 return true;
522 }
523 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700524 if (getCurrentPage() < getPageCount() - 1) {
525 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700526 return true;
527 }
528 }
529 return super.dispatchUnhandledMove(focused, direction);
530 }
531
532 @Override
533 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700534 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
535 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 }
537 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700538 if (mCurrentPage > 0) {
539 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700540 }
541 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700542 if (mCurrentPage < getPageCount() - 1) {
543 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700544 }
545 }
546 }
547
548 /**
549 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700550 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700551 *
Winson Chung86f77532010-08-24 11:08:22 -0700552 * This happens when live folders requery, and if they're off page, they
553 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700554 */
555 @Override
556 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700557 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 View v = focused;
559 while (true) {
560 if (v == current) {
561 super.focusableViewAvailable(focused);
562 return;
563 }
564 if (v == this) {
565 return;
566 }
567 ViewParent parent = v.getParent();
568 if (parent instanceof View) {
569 v = (View)v.getParent();
570 } else {
571 return;
572 }
573 }
574 }
575
576 /**
577 * {@inheritDoc}
578 */
579 @Override
580 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
581 if (disallowIntercept) {
582 // We need to make sure to cancel our long press if
583 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700584 final View currentPage = getChildAt(mCurrentPage);
585 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700586 }
587 super.requestDisallowInterceptTouchEvent(disallowIntercept);
588 }
589
590 @Override
591 public boolean onInterceptTouchEvent(MotionEvent ev) {
592 /*
593 * This method JUST determines whether we want to intercept the motion.
594 * If we return true, onTouchEvent will be called and we do the actual
595 * scrolling there.
596 */
597
598 /*
599 * Shortcut the most recurring case: the user is in the dragging
600 * state and he is moving his finger. We want to intercept this
601 * motion.
602 */
603 final int action = ev.getAction();
604 if ((action == MotionEvent.ACTION_MOVE) &&
605 (mTouchState == TOUCH_STATE_SCROLLING)) {
606 return true;
607 }
608
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 switch (action & MotionEvent.ACTION_MASK) {
610 case MotionEvent.ACTION_MOVE: {
611 /*
612 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
613 * whether the user has moved far enough from his original down touch.
614 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700615 if (mActivePointerId != INVALID_POINTER) {
616 determineScrollingStart(ev);
617 break;
618 }
619 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
620 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
621 // i.e. fall through to the next case (don't break)
622 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
623 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700624 }
625
626 case MotionEvent.ACTION_DOWN: {
627 final float x = ev.getX();
628 final float y = ev.getY();
629 // Remember location of down touch
630 mDownMotionX = x;
631 mLastMotionX = x;
632 mLastMotionY = y;
633 mActivePointerId = ev.getPointerId(0);
634 mAllowLongPress = true;
635
636 /*
637 * If being flinged and user touches the screen, initiate drag;
638 * otherwise don't. mScroller.isFinished should be false when
639 * being flinged.
640 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700641 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
642 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
643 if (finishedScrolling) {
644 mTouchState = TOUCH_STATE_REST;
645 mScroller.abortAnimation();
646 } else {
647 mTouchState = TOUCH_STATE_SCROLLING;
648 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700649
Winson Chung86f77532010-08-24 11:08:22 -0700650 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700651 // to scroll the current page
652 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
653 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
654 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700655 int width = getMeasuredWidth();
656 int offset = getRelativeChildOffset(mCurrentPage);
657 if (x < offset) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 mTouchState = TOUCH_STATE_PREV_PAGE;
Winson Chunge3193b92010-09-10 11:44:42 -0700659 } else if (x > (width - offset)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700660 mTouchState = TOUCH_STATE_NEXT_PAGE;
661 }
662 }
663 }
664 break;
665 }
666
667 case MotionEvent.ACTION_CANCEL:
668 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700669 mTouchState = TOUCH_STATE_REST;
670 mAllowLongPress = false;
671 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 break;
673
674 case MotionEvent.ACTION_POINTER_UP:
675 onSecondaryPointerUp(ev);
676 break;
677 }
678
679 /*
680 * The only time we want to intercept motion events is if we are in the
681 * drag mode.
682 */
683 return mTouchState != TOUCH_STATE_REST;
684 }
685
Winson Chung80baf5a2010-08-09 16:03:15 -0700686 protected void animateClickFeedback(View v, final Runnable r) {
687 // animate the view slightly to show click feedback running some logic after it is "pressed"
688 Animation anim = AnimationUtils.loadAnimation(getContext(),
689 R.anim.paged_view_click_feedback);
690 anim.setAnimationListener(new AnimationListener() {
691 @Override
692 public void onAnimationStart(Animation animation) {}
693 @Override
694 public void onAnimationRepeat(Animation animation) {
695 r.run();
696 }
697 @Override
698 public void onAnimationEnd(Animation animation) {}
699 });
700 v.startAnimation(anim);
701 }
702
Winson Chung321e9ee2010-08-09 13:37:56 -0700703 /*
704 * Determines if we should change the touch state to start scrolling after the
705 * user moves their touch point too far.
706 */
707 private void determineScrollingStart(MotionEvent ev) {
708 /*
709 * Locally do absolute value. mLastMotionX is set to the y value
710 * of the down event.
711 */
712 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
713 final float x = ev.getX(pointerIndex);
714 final float y = ev.getY(pointerIndex);
715 final int xDiff = (int) Math.abs(x - mLastMotionX);
716 final int yDiff = (int) Math.abs(y - mLastMotionY);
717
718 final int touchSlop = mTouchSlop;
719 boolean xPaged = xDiff > mPagingTouchSlop;
720 boolean xMoved = xDiff > touchSlop;
721 boolean yMoved = yDiff > touchSlop;
722
723 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700724 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 // Scroll if the user moved far enough along the X axis
726 mTouchState = TOUCH_STATE_SCROLLING;
727 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700728 mTouchX = mScrollX;
729 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
730 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700731 }
732 // Either way, cancel any pending longpress
733 if (mAllowLongPress) {
734 mAllowLongPress = false;
735 // Try canceling the long press. It could also have been scheduled
736 // by a distant descendant, so use the mAllowLongPress flag to block
737 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700738 final View currentPage = getPageAt(mCurrentPage);
739 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700740 }
741 }
742 }
743
744 @Override
745 public boolean onTouchEvent(MotionEvent ev) {
746 if (mVelocityTracker == null) {
747 mVelocityTracker = VelocityTracker.obtain();
748 }
749 mVelocityTracker.addMovement(ev);
750
751 final int action = ev.getAction();
752
753 switch (action & MotionEvent.ACTION_MASK) {
754 case MotionEvent.ACTION_DOWN:
755 /*
756 * If being flinged and user touches, stop the fling. isFinished
757 * will be false if being flinged.
758 */
759 if (!mScroller.isFinished()) {
760 mScroller.abortAnimation();
761 }
762
763 // Remember where the motion event started
764 mDownMotionX = mLastMotionX = ev.getX();
765 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700766 if (mTouchState == TOUCH_STATE_SCROLLING) {
767 pageBeginMoving();
768 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700769 break;
770
771 case MotionEvent.ACTION_MOVE:
772 if (mTouchState == TOUCH_STATE_SCROLLING) {
773 // Scroll to follow the motion event
774 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
775 final float x = ev.getX(pointerIndex);
776 final int deltaX = (int) (mLastMotionX - x);
777 mLastMotionX = x;
778
779 int sx = getScrollX();
780 if (deltaX < 0) {
781 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700782 mTouchX += Math.max(-mTouchX, deltaX);
783 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
784 if (!mDeferScrollUpdate) {
785 scrollBy(Math.max(-sx, deltaX), 0);
786 } else {
787 // This will trigger a call to computeScroll() on next drawChild() call
788 invalidate();
789 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700790 }
791 } else if (deltaX > 0) {
792 final int lastChildIndex = getChildCount() - 1;
793 final int availableToScroll = getChildOffset(lastChildIndex) -
794 getRelativeChildOffset(lastChildIndex) - sx;
795 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700796 mTouchX += Math.min(availableToScroll, deltaX);
797 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
798 if (!mDeferScrollUpdate) {
799 scrollBy(Math.min(availableToScroll, deltaX), 0);
800 } else {
801 // This will trigger a call to computeScroll() on next drawChild() call
802 invalidate();
803 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700804 }
805 } else {
806 awakenScrollBars();
807 }
808 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
809 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
810 determineScrollingStart(ev);
811 }
812 break;
813
814 case MotionEvent.ACTION_UP:
815 if (mTouchState == TOUCH_STATE_SCROLLING) {
816 final int activePointerId = mActivePointerId;
817 final int pointerIndex = ev.findPointerIndex(activePointerId);
818 final float x = ev.getX(pointerIndex);
819 final VelocityTracker velocityTracker = mVelocityTracker;
820 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
821 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
822 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
823
Michael Jurka0142d492010-08-25 17:46:15 -0700824 final int snapVelocity = mSnapVelocity;
825 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
826 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
827 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700828 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700829 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700830 } else {
831 snapToDestination();
832 }
833
834 if (mVelocityTracker != null) {
835 mVelocityTracker.recycle();
836 mVelocityTracker = null;
837 }
838 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
839 // at this point we have not moved beyond the touch slop
840 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
841 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700842 int nextPage = Math.max(0, mCurrentPage - 1);
843 if (nextPage != mCurrentPage) {
844 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 } else {
846 snapToDestination();
847 }
848 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
849 // at this point we have not moved beyond the touch slop
850 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
851 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700852 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
853 if (nextPage != mCurrentPage) {
854 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 } else {
856 snapToDestination();
857 }
858 }
859 mTouchState = TOUCH_STATE_REST;
860 mActivePointerId = INVALID_POINTER;
861 break;
862
863 case MotionEvent.ACTION_CANCEL:
864 mTouchState = TOUCH_STATE_REST;
865 mActivePointerId = INVALID_POINTER;
866 break;
867
868 case MotionEvent.ACTION_POINTER_UP:
869 onSecondaryPointerUp(ev);
870 break;
871 }
872
873 return true;
874 }
875
876 private void onSecondaryPointerUp(MotionEvent ev) {
877 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
878 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
879 final int pointerId = ev.getPointerId(pointerIndex);
880 if (pointerId == mActivePointerId) {
881 // This was our active pointer going up. Choose a new
882 // active pointer and adjust accordingly.
883 // TODO: Make this decision more intelligent.
884 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
885 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
886 mLastMotionY = ev.getY(newPointerIndex);
887 mActivePointerId = ev.getPointerId(newPointerIndex);
888 if (mVelocityTracker != null) {
889 mVelocityTracker.clear();
890 }
891 }
892 }
893
894 @Override
895 public void requestChildFocus(View child, View focused) {
896 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700897 int page = indexOfChild(child);
898 if (page >= 0 && !isInTouchMode()) {
899 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700900 }
901 }
902
Winson Chunge3193b92010-09-10 11:44:42 -0700903 protected int getChildIndexForRelativeOffset(int relativeOffset) {
904 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700905 int left;
906 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700907 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700908 left = getRelativeChildOffset(i);
909 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700910 if (left <= relativeOffset && relativeOffset <= right) {
911 return i;
912 }
Winson Chunge3193b92010-09-10 11:44:42 -0700913 }
914 return -1;
915 }
916
Winson Chung321e9ee2010-08-09 13:37:56 -0700917 protected int getRelativeChildOffset(int index) {
918 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
919 }
920
921 protected int getChildOffset(int index) {
922 if (getChildCount() == 0)
923 return 0;
924
925 int offset = getRelativeChildOffset(0);
926 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700927 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700928 }
929 return offset;
930 }
931
Adam Cohend19d3ca2010-09-15 14:43:42 -0700932 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700933 int minDistanceFromScreenCenter = getMeasuredWidth();
934 int minDistanceFromScreenCenterIndex = -1;
935 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
936 final int childCount = getChildCount();
937 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700938 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700939 int childWidth = layout.getMeasuredWidth();
940 int halfChildWidth = (childWidth / 2);
941 int childCenter = getChildOffset(i) + halfChildWidth;
942 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
943 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
944 minDistanceFromScreenCenter = distanceFromScreenCenter;
945 minDistanceFromScreenCenterIndex = i;
946 }
947 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700948 return minDistanceFromScreenCenterIndex;
949 }
950
951 protected void snapToDestination() {
952 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 }
954
Michael Jurka0142d492010-08-25 17:46:15 -0700955 protected void snapToPageWithVelocity(int whichPage, int velocity) {
956 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
957 // can use it
958 snapToPage(whichPage);
959 }
960
961 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700962 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700963 }
964
Michael Jurka0142d492010-08-25 17:46:15 -0700965 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700966 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700967
Winson Chung86f77532010-08-24 11:08:22 -0700968 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700969 final int sX = getScrollX();
970 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700971 snapToPage(whichPage, delta, duration);
972 }
973
974 protected void snapToPage(int whichPage, int delta, int duration) {
975 mNextPage = whichPage;
976
977 View focusedChild = getFocusedChild();
978 if (focusedChild != null && whichPage != mCurrentPage &&
979 focusedChild == getChildAt(mCurrentPage)) {
980 focusedChild.clearFocus();
981 }
982
983 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 awakenScrollBars(duration);
985 if (duration == 0) {
986 duration = Math.abs(delta);
987 }
988
989 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700990 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700991
992 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700993 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700994 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700995 invalidate();
996 }
997
998 @Override
999 protected Parcelable onSaveInstanceState() {
1000 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001001 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001002 return state;
1003 }
1004
1005 @Override
1006 protected void onRestoreInstanceState(Parcelable state) {
1007 SavedState savedState = (SavedState) state;
1008 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001009 if (savedState.currentPage != -1) {
1010 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001011 }
1012 }
1013
1014 public void scrollLeft() {
1015 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001016 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001018 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001019 }
1020 }
1021
1022 public void scrollRight() {
1023 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001024 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001026 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 }
1028 }
1029
Winson Chung86f77532010-08-24 11:08:22 -07001030 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 int result = -1;
1032 if (v != null) {
1033 ViewParent vp = v.getParent();
1034 int count = getChildCount();
1035 for (int i = 0; i < count; i++) {
1036 if (vp == getChildAt(i)) {
1037 return i;
1038 }
1039 }
1040 }
1041 return result;
1042 }
1043
1044 /**
1045 * @return True is long presses are still allowed for the current touch
1046 */
1047 public boolean allowLongPress() {
1048 return mAllowLongPress;
1049 }
1050
Michael Jurka0142d492010-08-25 17:46:15 -07001051 /**
1052 * Set true to allow long-press events to be triggered, usually checked by
1053 * {@link Launcher} to accept or block dpad-initiated long-presses.
1054 */
1055 public void setAllowLongPress(boolean allowLongPress) {
1056 mAllowLongPress = allowLongPress;
1057 }
1058
Winson Chung321e9ee2010-08-09 13:37:56 -07001059 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001060 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001061
1062 SavedState(Parcelable superState) {
1063 super(superState);
1064 }
1065
1066 private SavedState(Parcel in) {
1067 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001068 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 }
1070
1071 @Override
1072 public void writeToParcel(Parcel out, int flags) {
1073 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001074 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001075 }
1076
1077 public static final Parcelable.Creator<SavedState> CREATOR =
1078 new Parcelable.Creator<SavedState>() {
1079 public SavedState createFromParcel(Parcel in) {
1080 return new SavedState(in);
1081 }
1082
1083 public SavedState[] newArray(int size) {
1084 return new SavedState[size];
1085 }
1086 };
1087 }
1088
Winson Chung86f77532010-08-24 11:08:22 -07001089 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001090 if (mContentIsRefreshable) {
1091 final int count = getChildCount();
1092 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001093 int lowerPageBound = getAssociatedLowerPageBound(page);
1094 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001095 for (int i = 0; i < count; ++i) {
1096 final ViewGroup layout = (ViewGroup) getChildAt(i);
1097 final int childCount = layout.getChildCount();
1098 if (lowerPageBound <= i && i <= upperPageBound) {
1099 if (mDirtyPageContent.get(i)) {
1100 syncPageItems(i);
1101 mDirtyPageContent.set(i, false);
1102 }
1103 } else {
1104 if (childCount > 0) {
1105 layout.removeAllViews();
1106 }
1107 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001108 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001109 }
1110 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001111 }
1112 }
1113
Winson Chunge3193b92010-09-10 11:44:42 -07001114 protected int getAssociatedLowerPageBound(int page) {
1115 return Math.max(0, page - 1);
1116 }
1117 protected int getAssociatedUpperPageBound(int page) {
1118 final int count = getChildCount();
1119 return Math.min(page + 1, count - 1);
1120 }
1121
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001122 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001123 if (isChoiceMode(CHOICE_MODE_NONE)) {
1124 mChoiceMode = mode;
1125 mActionMode = startActionMode(callback);
1126 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001127 }
1128
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001129 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001130 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001131 mChoiceMode = CHOICE_MODE_NONE;
1132 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001133 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001134 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001135 }
1136 }
1137
1138 protected boolean isChoiceMode(int mode) {
1139 return mChoiceMode == mode;
1140 }
1141
1142 protected ArrayList<Checkable> getCheckedGrandchildren() {
1143 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1144 final int childCount = getChildCount();
1145 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001146 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001147 final int grandChildCount = layout.getChildCount();
1148 for (int j = 0; j < grandChildCount; ++j) {
1149 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001150 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001151 checked.add((Checkable) v);
1152 }
1153 }
1154 }
1155 return checked;
1156 }
1157
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001158 /**
1159 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1160 * Otherwise, returns null.
1161 */
1162 protected Checkable getSingleCheckedGrandchild() {
1163 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1164 final int childCount = getChildCount();
1165 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001166 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001167 final int grandChildCount = layout.getChildCount();
1168 for (int j = 0; j < grandChildCount; ++j) {
1169 final View v = layout.getChildAt(j);
1170 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1171 return (Checkable) v;
1172 }
1173 }
1174 }
1175 }
1176 return null;
1177 }
1178
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001179 public Object getChosenItem() {
1180 View checkedView = (View) getSingleCheckedGrandchild();
1181 if (checkedView != null) {
1182 return checkedView.getTag();
1183 }
1184 return null;
1185 }
1186
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001187 protected void resetCheckedGrandchildren() {
1188 // loop through children, and set all of their children to _not_ be checked
1189 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1190 for (int i = 0; i < checked.size(); ++i) {
1191 final Checkable c = checked.get(i);
1192 c.setChecked(false);
1193 }
1194 }
1195
Winson Chung86f77532010-08-24 11:08:22 -07001196 /**
1197 * This method is called ONLY to synchronize the number of pages that the paged view has.
1198 * To actually fill the pages with information, implement syncPageItems() below. It is
1199 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1200 * and therefore, individual page items do not need to be updated in this method.
1201 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001202 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001203
1204 /**
1205 * This method is called to synchronize the items that are on a particular page. If views on
1206 * the page can be reused, then they should be updated within this method.
1207 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001209
Winson Chung321e9ee2010-08-09 13:37:56 -07001210 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001211 if (mContentIsRefreshable) {
1212 // Update all the pages
1213 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001214
Michael Jurka0142d492010-08-25 17:46:15 -07001215 // Mark each of the pages as dirty
1216 final int count = getChildCount();
1217 mDirtyPageContent.clear();
1218 for (int i = 0; i < count; ++i) {
1219 mDirtyPageContent.add(true);
1220 }
1221
1222 // Load any pages that are necessary for the current window of views
1223 loadAssociatedPages(mCurrentPage);
1224 mDirtyPageAlpha = true;
1225 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001226 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 }
1228}