blob: 83320533fce81cb4c8e6d623a09b3106e46fa5bb [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
Adam Cohen21f12b52010-10-07 17:15:37 -0700652 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700653 (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);
Adam Cohen21f12b52010-10-07 17:15:37 -0700657 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700659 } else if (x > (width - offset + mPageSpacing)) {
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
Adam Cohen21f12b52010-10-07 17:15:37 -0700744 protected boolean handlePagingClicks() {
745 return false;
746 }
747
Winson Chung321e9ee2010-08-09 13:37:56 -0700748 @Override
749 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700750 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700751
752 final int action = ev.getAction();
753
754 switch (action & MotionEvent.ACTION_MASK) {
755 case MotionEvent.ACTION_DOWN:
756 /*
757 * If being flinged and user touches, stop the fling. isFinished
758 * will be false if being flinged.
759 */
760 if (!mScroller.isFinished()) {
761 mScroller.abortAnimation();
762 }
763
764 // Remember where the motion event started
765 mDownMotionX = mLastMotionX = ev.getX();
766 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700767 if (mTouchState == TOUCH_STATE_SCROLLING) {
768 pageBeginMoving();
769 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700770 break;
771
772 case MotionEvent.ACTION_MOVE:
773 if (mTouchState == TOUCH_STATE_SCROLLING) {
774 // Scroll to follow the motion event
775 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
776 final float x = ev.getX(pointerIndex);
777 final int deltaX = (int) (mLastMotionX - x);
778 mLastMotionX = x;
779
780 int sx = getScrollX();
781 if (deltaX < 0) {
782 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700783 mTouchX += Math.max(-mTouchX, deltaX);
784 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
785 if (!mDeferScrollUpdate) {
786 scrollBy(Math.max(-sx, deltaX), 0);
787 } else {
788 // This will trigger a call to computeScroll() on next drawChild() call
789 invalidate();
790 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700791 }
792 } else if (deltaX > 0) {
793 final int lastChildIndex = getChildCount() - 1;
794 final int availableToScroll = getChildOffset(lastChildIndex) -
795 getRelativeChildOffset(lastChildIndex) - sx;
796 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700797 mTouchX += Math.min(availableToScroll, deltaX);
798 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
799 if (!mDeferScrollUpdate) {
800 scrollBy(Math.min(availableToScroll, deltaX), 0);
801 } else {
802 // This will trigger a call to computeScroll() on next drawChild() call
803 invalidate();
804 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700805 }
806 } else {
807 awakenScrollBars();
808 }
809 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
810 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
811 determineScrollingStart(ev);
812 }
813 break;
814
815 case MotionEvent.ACTION_UP:
816 if (mTouchState == TOUCH_STATE_SCROLLING) {
817 final int activePointerId = mActivePointerId;
818 final int pointerIndex = ev.findPointerIndex(activePointerId);
819 final float x = ev.getX(pointerIndex);
820 final VelocityTracker velocityTracker = mVelocityTracker;
821 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
822 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
823 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
824
Michael Jurka0142d492010-08-25 17:46:15 -0700825 final int snapVelocity = mSnapVelocity;
826 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
827 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
828 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700829 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700830 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700831 } else {
832 snapToDestination();
833 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700834 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700835 // at this point we have not moved beyond the touch slop
836 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
837 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700838 int nextPage = Math.max(0, mCurrentPage - 1);
839 if (nextPage != mCurrentPage) {
840 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 } else {
842 snapToDestination();
843 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700844 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 // at this point we have not moved beyond the touch slop
846 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
847 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700848 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
849 if (nextPage != mCurrentPage) {
850 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 } else {
852 snapToDestination();
853 }
854 }
855 mTouchState = TOUCH_STATE_REST;
856 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700857 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 break;
859
860 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700861 if (mTouchState == TOUCH_STATE_SCROLLING) {
862 snapToDestination();
863 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 mTouchState = TOUCH_STATE_REST;
865 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700866 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 break;
868
869 case MotionEvent.ACTION_POINTER_UP:
870 onSecondaryPointerUp(ev);
871 break;
872 }
873
874 return true;
875 }
876
Michael Jurkab8f06722010-10-10 15:58:46 -0700877 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
878 if (mVelocityTracker == null) {
879 mVelocityTracker = VelocityTracker.obtain();
880 }
881 mVelocityTracker.addMovement(ev);
882 }
883
884 private void releaseVelocityTracker() {
885 if (mVelocityTracker != null) {
886 mVelocityTracker.recycle();
887 mVelocityTracker = null;
888 }
889 }
890
Winson Chung321e9ee2010-08-09 13:37:56 -0700891 private void onSecondaryPointerUp(MotionEvent ev) {
892 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
893 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
894 final int pointerId = ev.getPointerId(pointerIndex);
895 if (pointerId == mActivePointerId) {
896 // This was our active pointer going up. Choose a new
897 // active pointer and adjust accordingly.
898 // TODO: Make this decision more intelligent.
899 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
900 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
901 mLastMotionY = ev.getY(newPointerIndex);
902 mActivePointerId = ev.getPointerId(newPointerIndex);
903 if (mVelocityTracker != null) {
904 mVelocityTracker.clear();
905 }
906 }
907 }
908
909 @Override
910 public void requestChildFocus(View child, View focused) {
911 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700912 int page = indexOfChild(child);
913 if (page >= 0 && !isInTouchMode()) {
914 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 }
916 }
917
Winson Chunge3193b92010-09-10 11:44:42 -0700918 protected int getChildIndexForRelativeOffset(int relativeOffset) {
919 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700920 int left;
921 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700922 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700923 left = getRelativeChildOffset(i);
924 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700925 if (left <= relativeOffset && relativeOffset <= right) {
926 return i;
927 }
Winson Chunge3193b92010-09-10 11:44:42 -0700928 }
929 return -1;
930 }
931
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 protected int getRelativeChildOffset(int index) {
933 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
934 }
935
936 protected int getChildOffset(int index) {
937 if (getChildCount() == 0)
938 return 0;
939
940 int offset = getRelativeChildOffset(0);
941 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700942 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 }
944 return offset;
945 }
946
Adam Cohend19d3ca2010-09-15 14:43:42 -0700947 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700948 int minDistanceFromScreenCenter = getMeasuredWidth();
949 int minDistanceFromScreenCenterIndex = -1;
950 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
951 final int childCount = getChildCount();
952 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700953 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700954 int childWidth = layout.getMeasuredWidth();
955 int halfChildWidth = (childWidth / 2);
956 int childCenter = getChildOffset(i) + halfChildWidth;
957 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
958 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
959 minDistanceFromScreenCenter = distanceFromScreenCenter;
960 minDistanceFromScreenCenterIndex = i;
961 }
962 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700963 return minDistanceFromScreenCenterIndex;
964 }
965
966 protected void snapToDestination() {
967 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700968 }
969
Michael Jurka0142d492010-08-25 17:46:15 -0700970 protected void snapToPageWithVelocity(int whichPage, int velocity) {
971 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
972 // can use it
973 snapToPage(whichPage);
974 }
975
976 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700977 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 }
979
Michael Jurka0142d492010-08-25 17:46:15 -0700980 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700981 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700982
Winson Chung86f77532010-08-24 11:08:22 -0700983 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700984 final int sX = getScrollX();
985 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700986 snapToPage(whichPage, delta, duration);
987 }
988
989 protected void snapToPage(int whichPage, int delta, int duration) {
990 mNextPage = whichPage;
991
992 View focusedChild = getFocusedChild();
993 if (focusedChild != null && whichPage != mCurrentPage &&
994 focusedChild == getChildAt(mCurrentPage)) {
995 focusedChild.clearFocus();
996 }
997
998 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 awakenScrollBars(duration);
1000 if (duration == 0) {
1001 duration = Math.abs(delta);
1002 }
1003
1004 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001005 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001006
1007 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001008 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001009 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 invalidate();
1011 }
1012
1013 @Override
1014 protected Parcelable onSaveInstanceState() {
1015 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001016 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 return state;
1018 }
1019
1020 @Override
1021 protected void onRestoreInstanceState(Parcelable state) {
1022 SavedState savedState = (SavedState) state;
1023 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001024 if (savedState.currentPage != -1) {
1025 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001026 }
1027 }
1028
1029 public void scrollLeft() {
1030 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001031 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001032 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001033 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001034 }
1035 }
1036
1037 public void scrollRight() {
1038 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001039 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001041 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 }
1043 }
1044
Winson Chung86f77532010-08-24 11:08:22 -07001045 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001046 int result = -1;
1047 if (v != null) {
1048 ViewParent vp = v.getParent();
1049 int count = getChildCount();
1050 for (int i = 0; i < count; i++) {
1051 if (vp == getChildAt(i)) {
1052 return i;
1053 }
1054 }
1055 }
1056 return result;
1057 }
1058
1059 /**
1060 * @return True is long presses are still allowed for the current touch
1061 */
1062 public boolean allowLongPress() {
1063 return mAllowLongPress;
1064 }
1065
Michael Jurka0142d492010-08-25 17:46:15 -07001066 /**
1067 * Set true to allow long-press events to be triggered, usually checked by
1068 * {@link Launcher} to accept or block dpad-initiated long-presses.
1069 */
1070 public void setAllowLongPress(boolean allowLongPress) {
1071 mAllowLongPress = allowLongPress;
1072 }
1073
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001075 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001076
1077 SavedState(Parcelable superState) {
1078 super(superState);
1079 }
1080
1081 private SavedState(Parcel in) {
1082 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001083 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 }
1085
1086 @Override
1087 public void writeToParcel(Parcel out, int flags) {
1088 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001089 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001090 }
1091
1092 public static final Parcelable.Creator<SavedState> CREATOR =
1093 new Parcelable.Creator<SavedState>() {
1094 public SavedState createFromParcel(Parcel in) {
1095 return new SavedState(in);
1096 }
1097
1098 public SavedState[] newArray(int size) {
1099 return new SavedState[size];
1100 }
1101 };
1102 }
1103
Winson Chung86f77532010-08-24 11:08:22 -07001104 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001105 if (mContentIsRefreshable) {
1106 final int count = getChildCount();
1107 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001108 int lowerPageBound = getAssociatedLowerPageBound(page);
1109 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001110 for (int i = 0; i < count; ++i) {
1111 final ViewGroup layout = (ViewGroup) getChildAt(i);
1112 final int childCount = layout.getChildCount();
1113 if (lowerPageBound <= i && i <= upperPageBound) {
1114 if (mDirtyPageContent.get(i)) {
1115 syncPageItems(i);
1116 mDirtyPageContent.set(i, false);
1117 }
1118 } else {
1119 if (childCount > 0) {
1120 layout.removeAllViews();
1121 }
1122 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001123 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001124 }
1125 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001126 }
1127 }
1128
Winson Chunge3193b92010-09-10 11:44:42 -07001129 protected int getAssociatedLowerPageBound(int page) {
1130 return Math.max(0, page - 1);
1131 }
1132 protected int getAssociatedUpperPageBound(int page) {
1133 final int count = getChildCount();
1134 return Math.min(page + 1, count - 1);
1135 }
1136
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001137 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001138 if (isChoiceMode(CHOICE_MODE_NONE)) {
1139 mChoiceMode = mode;
1140 mActionMode = startActionMode(callback);
1141 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001142 }
1143
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001144 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001145 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001146 mChoiceMode = CHOICE_MODE_NONE;
1147 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001148 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001149 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001150 }
1151 }
1152
1153 protected boolean isChoiceMode(int mode) {
1154 return mChoiceMode == mode;
1155 }
1156
1157 protected ArrayList<Checkable> getCheckedGrandchildren() {
1158 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1159 final int childCount = getChildCount();
1160 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001161 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001162 final int grandChildCount = layout.getChildCount();
1163 for (int j = 0; j < grandChildCount; ++j) {
1164 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001165 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001166 checked.add((Checkable) v);
1167 }
1168 }
1169 }
1170 return checked;
1171 }
1172
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001173 /**
1174 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1175 * Otherwise, returns null.
1176 */
1177 protected Checkable getSingleCheckedGrandchild() {
1178 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1179 final int childCount = getChildCount();
1180 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001181 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001182 final int grandChildCount = layout.getChildCount();
1183 for (int j = 0; j < grandChildCount; ++j) {
1184 final View v = layout.getChildAt(j);
1185 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1186 return (Checkable) v;
1187 }
1188 }
1189 }
1190 }
1191 return null;
1192 }
1193
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001194 public Object getChosenItem() {
1195 View checkedView = (View) getSingleCheckedGrandchild();
1196 if (checkedView != null) {
1197 return checkedView.getTag();
1198 }
1199 return null;
1200 }
1201
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001202 protected void resetCheckedGrandchildren() {
1203 // loop through children, and set all of their children to _not_ be checked
1204 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1205 for (int i = 0; i < checked.size(); ++i) {
1206 final Checkable c = checked.get(i);
1207 c.setChecked(false);
1208 }
1209 }
1210
Winson Chung86f77532010-08-24 11:08:22 -07001211 /**
1212 * This method is called ONLY to synchronize the number of pages that the paged view has.
1213 * To actually fill the pages with information, implement syncPageItems() below. It is
1214 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1215 * and therefore, individual page items do not need to be updated in this method.
1216 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001217 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001218
1219 /**
1220 * This method is called to synchronize the items that are on a particular page. If views on
1221 * the page can be reused, then they should be updated within this method.
1222 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001223 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001224
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001226 if (mContentIsRefreshable) {
1227 // Update all the pages
1228 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001229
Michael Jurka0142d492010-08-25 17:46:15 -07001230 // Mark each of the pages as dirty
1231 final int count = getChildCount();
1232 mDirtyPageContent.clear();
1233 for (int i = 0; i < count; ++i) {
1234 mDirtyPageContent.add(true);
1235 }
1236
1237 // Load any pages that are necessary for the current window of views
1238 loadAssociatedPages(mCurrentPage);
1239 mDirtyPageAlpha = true;
1240 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001241 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001242 }
1243}