blob: 2a96736bdf6f1f8a455830cacef2af738c0aa5ee [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) {
750 if (mVelocityTracker == null) {
751 mVelocityTracker = VelocityTracker.obtain();
752 }
753 mVelocityTracker.addMovement(ev);
754
755 final int action = ev.getAction();
756
757 switch (action & MotionEvent.ACTION_MASK) {
758 case MotionEvent.ACTION_DOWN:
759 /*
760 * If being flinged and user touches, stop the fling. isFinished
761 * will be false if being flinged.
762 */
763 if (!mScroller.isFinished()) {
764 mScroller.abortAnimation();
765 }
766
767 // Remember where the motion event started
768 mDownMotionX = mLastMotionX = ev.getX();
769 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700770 if (mTouchState == TOUCH_STATE_SCROLLING) {
771 pageBeginMoving();
772 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700773 break;
774
775 case MotionEvent.ACTION_MOVE:
776 if (mTouchState == TOUCH_STATE_SCROLLING) {
777 // Scroll to follow the motion event
778 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
779 final float x = ev.getX(pointerIndex);
780 final int deltaX = (int) (mLastMotionX - x);
781 mLastMotionX = x;
782
783 int sx = getScrollX();
784 if (deltaX < 0) {
785 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700786 mTouchX += Math.max(-mTouchX, deltaX);
787 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
788 if (!mDeferScrollUpdate) {
789 scrollBy(Math.max(-sx, deltaX), 0);
790 } else {
791 // This will trigger a call to computeScroll() on next drawChild() call
792 invalidate();
793 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 }
795 } else if (deltaX > 0) {
796 final int lastChildIndex = getChildCount() - 1;
797 final int availableToScroll = getChildOffset(lastChildIndex) -
798 getRelativeChildOffset(lastChildIndex) - sx;
799 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700800 mTouchX += Math.min(availableToScroll, deltaX);
801 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
802 if (!mDeferScrollUpdate) {
803 scrollBy(Math.min(availableToScroll, deltaX), 0);
804 } else {
805 // This will trigger a call to computeScroll() on next drawChild() call
806 invalidate();
807 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700808 }
809 } else {
810 awakenScrollBars();
811 }
812 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
813 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
814 determineScrollingStart(ev);
815 }
816 break;
817
818 case MotionEvent.ACTION_UP:
819 if (mTouchState == TOUCH_STATE_SCROLLING) {
820 final int activePointerId = mActivePointerId;
821 final int pointerIndex = ev.findPointerIndex(activePointerId);
822 final float x = ev.getX(pointerIndex);
823 final VelocityTracker velocityTracker = mVelocityTracker;
824 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
825 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
826 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
827
Michael Jurka0142d492010-08-25 17:46:15 -0700828 final int snapVelocity = mSnapVelocity;
829 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
830 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
831 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700832 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700833 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 } else {
835 snapToDestination();
836 }
837
838 if (mVelocityTracker != null) {
839 mVelocityTracker.recycle();
840 mVelocityTracker = null;
841 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700842 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 // at this point we have not moved beyond the touch slop
844 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
845 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700846 int nextPage = Math.max(0, mCurrentPage - 1);
847 if (nextPage != mCurrentPage) {
848 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700849 } else {
850 snapToDestination();
851 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700852 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 // at this point we have not moved beyond the touch slop
854 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
855 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700856 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
857 if (nextPage != mCurrentPage) {
858 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 } else {
860 snapToDestination();
861 }
862 }
863 mTouchState = TOUCH_STATE_REST;
864 mActivePointerId = INVALID_POINTER;
865 break;
866
867 case MotionEvent.ACTION_CANCEL:
868 mTouchState = TOUCH_STATE_REST;
869 mActivePointerId = INVALID_POINTER;
870 break;
871
872 case MotionEvent.ACTION_POINTER_UP:
873 onSecondaryPointerUp(ev);
874 break;
875 }
876
877 return true;
878 }
879
880 private void onSecondaryPointerUp(MotionEvent ev) {
881 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
882 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
883 final int pointerId = ev.getPointerId(pointerIndex);
884 if (pointerId == mActivePointerId) {
885 // This was our active pointer going up. Choose a new
886 // active pointer and adjust accordingly.
887 // TODO: Make this decision more intelligent.
888 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
889 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
890 mLastMotionY = ev.getY(newPointerIndex);
891 mActivePointerId = ev.getPointerId(newPointerIndex);
892 if (mVelocityTracker != null) {
893 mVelocityTracker.clear();
894 }
895 }
896 }
897
898 @Override
899 public void requestChildFocus(View child, View focused) {
900 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700901 int page = indexOfChild(child);
902 if (page >= 0 && !isInTouchMode()) {
903 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700904 }
905 }
906
Winson Chunge3193b92010-09-10 11:44:42 -0700907 protected int getChildIndexForRelativeOffset(int relativeOffset) {
908 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700909 int left;
910 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700911 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700912 left = getRelativeChildOffset(i);
913 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700914 if (left <= relativeOffset && relativeOffset <= right) {
915 return i;
916 }
Winson Chunge3193b92010-09-10 11:44:42 -0700917 }
918 return -1;
919 }
920
Winson Chung321e9ee2010-08-09 13:37:56 -0700921 protected int getRelativeChildOffset(int index) {
922 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
923 }
924
925 protected int getChildOffset(int index) {
926 if (getChildCount() == 0)
927 return 0;
928
929 int offset = getRelativeChildOffset(0);
930 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700931 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700932 }
933 return offset;
934 }
935
Adam Cohend19d3ca2010-09-15 14:43:42 -0700936 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 int minDistanceFromScreenCenter = getMeasuredWidth();
938 int minDistanceFromScreenCenterIndex = -1;
939 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
940 final int childCount = getChildCount();
941 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700942 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 int childWidth = layout.getMeasuredWidth();
944 int halfChildWidth = (childWidth / 2);
945 int childCenter = getChildOffset(i) + halfChildWidth;
946 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
947 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
948 minDistanceFromScreenCenter = distanceFromScreenCenter;
949 minDistanceFromScreenCenterIndex = i;
950 }
951 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700952 return minDistanceFromScreenCenterIndex;
953 }
954
955 protected void snapToDestination() {
956 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700957 }
958
Michael Jurka0142d492010-08-25 17:46:15 -0700959 protected void snapToPageWithVelocity(int whichPage, int velocity) {
960 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
961 // can use it
962 snapToPage(whichPage);
963 }
964
965 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700966 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 }
968
Michael Jurka0142d492010-08-25 17:46:15 -0700969 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700970 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700971
Winson Chung86f77532010-08-24 11:08:22 -0700972 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700973 final int sX = getScrollX();
974 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700975 snapToPage(whichPage, delta, duration);
976 }
977
978 protected void snapToPage(int whichPage, int delta, int duration) {
979 mNextPage = whichPage;
980
981 View focusedChild = getFocusedChild();
982 if (focusedChild != null && whichPage != mCurrentPage &&
983 focusedChild == getChildAt(mCurrentPage)) {
984 focusedChild.clearFocus();
985 }
986
987 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 awakenScrollBars(duration);
989 if (duration == 0) {
990 duration = Math.abs(delta);
991 }
992
993 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700994 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700995
996 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700997 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700998 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700999 invalidate();
1000 }
1001
1002 @Override
1003 protected Parcelable onSaveInstanceState() {
1004 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001005 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 return state;
1007 }
1008
1009 @Override
1010 protected void onRestoreInstanceState(Parcelable state) {
1011 SavedState savedState = (SavedState) state;
1012 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001013 if (savedState.currentPage != -1) {
1014 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 }
1016 }
1017
1018 public void scrollLeft() {
1019 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001020 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001021 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001022 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 }
1024 }
1025
1026 public void scrollRight() {
1027 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001028 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001030 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 }
1032 }
1033
Winson Chung86f77532010-08-24 11:08:22 -07001034 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001035 int result = -1;
1036 if (v != null) {
1037 ViewParent vp = v.getParent();
1038 int count = getChildCount();
1039 for (int i = 0; i < count; i++) {
1040 if (vp == getChildAt(i)) {
1041 return i;
1042 }
1043 }
1044 }
1045 return result;
1046 }
1047
1048 /**
1049 * @return True is long presses are still allowed for the current touch
1050 */
1051 public boolean allowLongPress() {
1052 return mAllowLongPress;
1053 }
1054
Michael Jurka0142d492010-08-25 17:46:15 -07001055 /**
1056 * Set true to allow long-press events to be triggered, usually checked by
1057 * {@link Launcher} to accept or block dpad-initiated long-presses.
1058 */
1059 public void setAllowLongPress(boolean allowLongPress) {
1060 mAllowLongPress = allowLongPress;
1061 }
1062
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001064 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001065
1066 SavedState(Parcelable superState) {
1067 super(superState);
1068 }
1069
1070 private SavedState(Parcel in) {
1071 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001072 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074
1075 @Override
1076 public void writeToParcel(Parcel out, int flags) {
1077 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001078 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001079 }
1080
1081 public static final Parcelable.Creator<SavedState> CREATOR =
1082 new Parcelable.Creator<SavedState>() {
1083 public SavedState createFromParcel(Parcel in) {
1084 return new SavedState(in);
1085 }
1086
1087 public SavedState[] newArray(int size) {
1088 return new SavedState[size];
1089 }
1090 };
1091 }
1092
Winson Chung86f77532010-08-24 11:08:22 -07001093 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001094 if (mContentIsRefreshable) {
1095 final int count = getChildCount();
1096 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001097 int lowerPageBound = getAssociatedLowerPageBound(page);
1098 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001099 for (int i = 0; i < count; ++i) {
1100 final ViewGroup layout = (ViewGroup) getChildAt(i);
1101 final int childCount = layout.getChildCount();
1102 if (lowerPageBound <= i && i <= upperPageBound) {
1103 if (mDirtyPageContent.get(i)) {
1104 syncPageItems(i);
1105 mDirtyPageContent.set(i, false);
1106 }
1107 } else {
1108 if (childCount > 0) {
1109 layout.removeAllViews();
1110 }
1111 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001112 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001113 }
1114 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001115 }
1116 }
1117
Winson Chunge3193b92010-09-10 11:44:42 -07001118 protected int getAssociatedLowerPageBound(int page) {
1119 return Math.max(0, page - 1);
1120 }
1121 protected int getAssociatedUpperPageBound(int page) {
1122 final int count = getChildCount();
1123 return Math.min(page + 1, count - 1);
1124 }
1125
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001126 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001127 if (isChoiceMode(CHOICE_MODE_NONE)) {
1128 mChoiceMode = mode;
1129 mActionMode = startActionMode(callback);
1130 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001131 }
1132
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001133 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001134 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001135 mChoiceMode = CHOICE_MODE_NONE;
1136 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001137 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001138 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001139 }
1140 }
1141
1142 protected boolean isChoiceMode(int mode) {
1143 return mChoiceMode == mode;
1144 }
1145
1146 protected ArrayList<Checkable> getCheckedGrandchildren() {
1147 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1148 final int childCount = getChildCount();
1149 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001150 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001151 final int grandChildCount = layout.getChildCount();
1152 for (int j = 0; j < grandChildCount; ++j) {
1153 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001154 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001155 checked.add((Checkable) v);
1156 }
1157 }
1158 }
1159 return checked;
1160 }
1161
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001162 /**
1163 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1164 * Otherwise, returns null.
1165 */
1166 protected Checkable getSingleCheckedGrandchild() {
1167 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1168 final int childCount = getChildCount();
1169 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001170 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001171 final int grandChildCount = layout.getChildCount();
1172 for (int j = 0; j < grandChildCount; ++j) {
1173 final View v = layout.getChildAt(j);
1174 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1175 return (Checkable) v;
1176 }
1177 }
1178 }
1179 }
1180 return null;
1181 }
1182
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001183 public Object getChosenItem() {
1184 View checkedView = (View) getSingleCheckedGrandchild();
1185 if (checkedView != null) {
1186 return checkedView.getTag();
1187 }
1188 return null;
1189 }
1190
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001191 protected void resetCheckedGrandchildren() {
1192 // loop through children, and set all of their children to _not_ be checked
1193 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1194 for (int i = 0; i < checked.size(); ++i) {
1195 final Checkable c = checked.get(i);
1196 c.setChecked(false);
1197 }
1198 }
1199
Winson Chung86f77532010-08-24 11:08:22 -07001200 /**
1201 * This method is called ONLY to synchronize the number of pages that the paged view has.
1202 * To actually fill the pages with information, implement syncPageItems() below. It is
1203 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1204 * and therefore, individual page items do not need to be updated in this method.
1205 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001207
1208 /**
1209 * This method is called to synchronize the items that are on a particular page. If views on
1210 * the page can be reused, then they should be updated within this method.
1211 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001212 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001213
Winson Chung321e9ee2010-08-09 13:37:56 -07001214 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001215 if (mContentIsRefreshable) {
1216 // Update all the pages
1217 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001218
Michael Jurka0142d492010-08-25 17:46:15 -07001219 // Mark each of the pages as dirty
1220 final int count = getChildCount();
1221 mDirtyPageContent.clear();
1222 for (int i = 0; i < count; ++i) {
1223 mDirtyPageContent.add(true);
1224 }
1225
1226 // Load any pages that are necessary for the current window of views
1227 loadAssociatedPages(mCurrentPage);
1228 mDirtyPageAlpha = true;
1229 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001230 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001231 }
1232}