blob: bf0eb12874410fd951f328012e952d120e3afe1b [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));
245 scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700246
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700248 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 }
250
Michael Jurka0142d492010-08-25 17:46:15 -0700251 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700252 if (mPageSwitchListener != null) {
253 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 }
255 }
256
Patrick Dubroy1262e362010-10-06 15:49:50 -0700257 private void pageBeginMoving() {
258 mIsPageMoving = true;
259 onPageBeginMoving();
260 }
261
262 private void pageEndMoving() {
263 onPageEndMoving();
264 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700265 }
266
267 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700268 protected void onPageBeginMoving() {
269 }
270
271 // a method that subclasses can override to add behavior
272 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700273 }
274
Winson Chung321e9ee2010-08-09 13:37:56 -0700275 /**
Winson Chung86f77532010-08-24 11:08:22 -0700276 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 *
278 * @param l The listener used to respond to long clicks.
279 */
280 @Override
281 public void setOnLongClickListener(OnLongClickListener l) {
282 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700283 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700284 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700285 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 }
287 }
288
289 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700290 public void scrollTo(int x, int y) {
291 super.scrollTo(x, y);
292 mTouchX = x;
293 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
294 }
295
296 // we moved this functionality to a helper function so SmoothPagedView can reuse it
297 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700299 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700301 invalidate();
302 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700303 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700304 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700305 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700306 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700307 notifyPageSwitchListener();
308 pageEndMoving();
309 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 }
Michael Jurka0142d492010-08-25 17:46:15 -0700311 return false;
312 }
313
314 @Override
315 public void computeScroll() {
316 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 }
318
319 @Override
320 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
321 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
322 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
323 if (widthMode != MeasureSpec.EXACTLY) {
324 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
325 }
326
327 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
328 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
329 if (heightMode != MeasureSpec.EXACTLY) {
330 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
331 }
332
333 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700334 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 final int childCount = getChildCount();
336 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700337 // disallowing padding in paged view (just pass 0)
338 final View child = getChildAt(i);
339 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
340
341 int childWidthMode;
342 if (lp.width == LayoutParams.WRAP_CONTENT) {
343 childWidthMode = MeasureSpec.AT_MOST;
344 } else {
345 childWidthMode = MeasureSpec.EXACTLY;
346 }
347
348 int childHeightMode;
349 if (lp.height == LayoutParams.WRAP_CONTENT) {
350 childHeightMode = MeasureSpec.AT_MOST;
351 } else {
352 childHeightMode = MeasureSpec.EXACTLY;
353 }
354
355 final int childWidthMeasureSpec =
356 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
357 final int childHeightMeasureSpec =
358 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
359
360 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700361 }
362
363 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 }
365
366 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700367 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700368 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
369 setHorizontalScrollBarEnabled(false);
370 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
371 scrollTo(newX, 0);
372 mScroller.setFinalX(newX);
373 setHorizontalScrollBarEnabled(true);
374 mFirstLayout = false;
375 }
376
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 final int childCount = getChildCount();
378 int childLeft = 0;
379 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700380 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 }
382
383 for (int i = 0; i < childCount; i++) {
384 final View child = getChildAt(i);
385 if (child.getVisibility() != View.GONE) {
386 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700387 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
388 child.layout(childLeft, childHeight,
389 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700390 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700391 }
392 }
393 }
394
Winson Chunge3193b92010-09-10 11:44:42 -0700395 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700396 if (mFadeInAdjacentScreens) {
397 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700398 int halfScreenSize = getMeasuredWidth() / 2;
399 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700400 final int childCount = getChildCount();
401 for (int i = 0; i < childCount; ++i) {
402 View layout = (View) getChildAt(i);
403 int childWidth = layout.getMeasuredWidth();
404 int halfChildWidth = (childWidth / 2);
405 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700406
407 int d = halfChildWidth;
408 int distanceFromScreenCenter = childCenter - screenCenter;
409 if (distanceFromScreenCenter > 0) {
410 if (i > 0) {
411 d += getChildAt(i - 1).getMeasuredWidth() / 2;
412 }
Michael Jurka0142d492010-08-25 17:46:15 -0700413 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700414 if (i < childCount - 1) {
415 d += getChildAt(i + 1).getMeasuredWidth() / 2;
416 }
Michael Jurka0142d492010-08-25 17:46:15 -0700417 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700418 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700419
420 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
421 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
422 float alpha = 1.0f - dimAlpha;
423
Adam Cohenf34bab52010-09-30 14:11:56 -0700424 if (alpha < ALPHA_QUANTIZE_LEVEL) {
425 alpha = 0.0f;
426 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
427 alpha = 1.0f;
428 }
429
Michael Jurka0142d492010-08-25 17:46:15 -0700430 if (Float.compare(alpha, layout.getAlpha()) != 0) {
431 layout.setAlpha(alpha);
432 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 }
Michael Jurka0142d492010-08-25 17:46:15 -0700434 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 }
436 }
Winson Chunge3193b92010-09-10 11:44:42 -0700437 }
438
Adam Cohenf34bab52010-09-30 14:11:56 -0700439 protected void screenScrolled(int screenCenter) {
440 }
441
Winson Chunge3193b92010-09-10 11:44:42 -0700442 @Override
443 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700444 int halfScreenSize = getMeasuredWidth() / 2;
445 int screenCenter = mScrollX + halfScreenSize;
446
447 if (screenCenter != mLastScreenCenter) {
448 screenScrolled(screenCenter);
449 updateAdjacentPagesAlpha();
450 mLastScreenCenter = screenCenter;
451 }
Michael Jurka0142d492010-08-25 17:46:15 -0700452
453 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700454 // As an optimization, this code assumes that all pages have the same width as the 0th
455 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700456 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700457 if (pageCount > 0) {
458 final int pageWidth = getChildAt(0).getMeasuredWidth();
459 final int screenWidth = getMeasuredWidth();
460 int x = getRelativeChildOffset(0) + pageWidth;
461 int leftScreen = 0;
462 int rightScreen = 0;
463 while (x <= mScrollX) {
464 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700465 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700466 // replace above line with this if you don't assume all pages have same width as 0th
467 // page:
468 // x += getChildAt(leftScreen).getMeasuredWidth();
469 }
470 rightScreen = leftScreen;
471 while (x < mScrollX + screenWidth) {
472 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700473 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700474 // replace above line with this if you don't assume all pages have same width as 0th
475 // page:
476 //if (rightScreen < pageCount) {
477 // x += getChildAt(rightScreen).getMeasuredWidth();
478 //}
479 }
480 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700481
Michael Jurkac4fb9172010-09-02 17:19:20 -0700482 final long drawingTime = getDrawingTime();
483 for (int i = leftScreen; i <= rightScreen; i++) {
484 drawChild(canvas, getChildAt(i), drawingTime);
485 }
Michael Jurka0142d492010-08-25 17:46:15 -0700486 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 }
488
489 @Override
490 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700491 int page = indexOfChild(child);
492 if (page != mCurrentPage || !mScroller.isFinished()) {
493 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 return true;
495 }
496 return false;
497 }
498
499 @Override
500 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700501 int focusablePage;
502 if (mNextPage != INVALID_PAGE) {
503 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700504 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700505 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
Winson Chung86f77532010-08-24 11:08:22 -0700507 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 if (v != null) {
509 v.requestFocus(direction, previouslyFocusedRect);
510 }
511 return false;
512 }
513
514 @Override
515 public boolean dispatchUnhandledMove(View focused, int direction) {
516 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700517 if (getCurrentPage() > 0) {
518 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 return true;
520 }
521 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700522 if (getCurrentPage() < getPageCount() - 1) {
523 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 return true;
525 }
526 }
527 return super.dispatchUnhandledMove(focused, direction);
528 }
529
530 @Override
531 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700532 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
533 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700534 }
535 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700536 if (mCurrentPage > 0) {
537 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700538 }
539 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700540 if (mCurrentPage < getPageCount() - 1) {
541 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700542 }
543 }
544 }
545
546 /**
547 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700548 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 *
Winson Chung86f77532010-08-24 11:08:22 -0700550 * This happens when live folders requery, and if they're off page, they
551 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 */
553 @Override
554 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700555 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700556 View v = focused;
557 while (true) {
558 if (v == current) {
559 super.focusableViewAvailable(focused);
560 return;
561 }
562 if (v == this) {
563 return;
564 }
565 ViewParent parent = v.getParent();
566 if (parent instanceof View) {
567 v = (View)v.getParent();
568 } else {
569 return;
570 }
571 }
572 }
573
574 /**
575 * {@inheritDoc}
576 */
577 @Override
578 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
579 if (disallowIntercept) {
580 // We need to make sure to cancel our long press if
581 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700582 final View currentPage = getChildAt(mCurrentPage);
583 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 }
585 super.requestDisallowInterceptTouchEvent(disallowIntercept);
586 }
587
588 @Override
589 public boolean onInterceptTouchEvent(MotionEvent ev) {
590 /*
591 * This method JUST determines whether we want to intercept the motion.
592 * If we return true, onTouchEvent will be called and we do the actual
593 * scrolling there.
594 */
595
596 /*
597 * Shortcut the most recurring case: the user is in the dragging
598 * state and he is moving his finger. We want to intercept this
599 * motion.
600 */
601 final int action = ev.getAction();
602 if ((action == MotionEvent.ACTION_MOVE) &&
603 (mTouchState == TOUCH_STATE_SCROLLING)) {
604 return true;
605 }
606
Winson Chung321e9ee2010-08-09 13:37:56 -0700607 switch (action & MotionEvent.ACTION_MASK) {
608 case MotionEvent.ACTION_MOVE: {
609 /*
610 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
611 * whether the user has moved far enough from his original down touch.
612 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700613 if (mActivePointerId != INVALID_POINTER) {
614 determineScrollingStart(ev);
615 break;
616 }
617 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
618 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
619 // i.e. fall through to the next case (don't break)
620 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
621 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700622 }
623
624 case MotionEvent.ACTION_DOWN: {
625 final float x = ev.getX();
626 final float y = ev.getY();
627 // Remember location of down touch
628 mDownMotionX = x;
629 mLastMotionX = x;
630 mLastMotionY = y;
631 mActivePointerId = ev.getPointerId(0);
632 mAllowLongPress = true;
633
634 /*
635 * If being flinged and user touches the screen, initiate drag;
636 * otherwise don't. mScroller.isFinished should be false when
637 * being flinged.
638 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700639 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
640 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
641 if (finishedScrolling) {
642 mTouchState = TOUCH_STATE_REST;
643 mScroller.abortAnimation();
644 } else {
645 mTouchState = TOUCH_STATE_SCROLLING;
646 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700647
Winson Chung86f77532010-08-24 11:08:22 -0700648 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700649 // to scroll the current page
650 if ((mTouchState != TOUCH_STATE_PREV_PAGE) &&
651 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
652 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700653 int width = getMeasuredWidth();
654 int offset = getRelativeChildOffset(mCurrentPage);
655 if (x < offset) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700656 mTouchState = TOUCH_STATE_PREV_PAGE;
Winson Chunge3193b92010-09-10 11:44:42 -0700657 } else if (x > (width - offset)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700658 mTouchState = TOUCH_STATE_NEXT_PAGE;
659 }
660 }
661 }
662 break;
663 }
664
665 case MotionEvent.ACTION_CANCEL:
666 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700667 mTouchState = TOUCH_STATE_REST;
668 mAllowLongPress = false;
669 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 break;
671
672 case MotionEvent.ACTION_POINTER_UP:
673 onSecondaryPointerUp(ev);
674 break;
675 }
676
677 /*
678 * The only time we want to intercept motion events is if we are in the
679 * drag mode.
680 */
681 return mTouchState != TOUCH_STATE_REST;
682 }
683
Winson Chung80baf5a2010-08-09 16:03:15 -0700684 protected void animateClickFeedback(View v, final Runnable r) {
685 // animate the view slightly to show click feedback running some logic after it is "pressed"
686 Animation anim = AnimationUtils.loadAnimation(getContext(),
687 R.anim.paged_view_click_feedback);
688 anim.setAnimationListener(new AnimationListener() {
689 @Override
690 public void onAnimationStart(Animation animation) {}
691 @Override
692 public void onAnimationRepeat(Animation animation) {
693 r.run();
694 }
695 @Override
696 public void onAnimationEnd(Animation animation) {}
697 });
698 v.startAnimation(anim);
699 }
700
Winson Chung321e9ee2010-08-09 13:37:56 -0700701 /*
702 * Determines if we should change the touch state to start scrolling after the
703 * user moves their touch point too far.
704 */
705 private void determineScrollingStart(MotionEvent ev) {
706 /*
707 * Locally do absolute value. mLastMotionX is set to the y value
708 * of the down event.
709 */
710 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
711 final float x = ev.getX(pointerIndex);
712 final float y = ev.getY(pointerIndex);
713 final int xDiff = (int) Math.abs(x - mLastMotionX);
714 final int yDiff = (int) Math.abs(y - mLastMotionY);
715
716 final int touchSlop = mTouchSlop;
717 boolean xPaged = xDiff > mPagingTouchSlop;
718 boolean xMoved = xDiff > touchSlop;
719 boolean yMoved = yDiff > touchSlop;
720
721 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700722 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 // Scroll if the user moved far enough along the X axis
724 mTouchState = TOUCH_STATE_SCROLLING;
725 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700726 mTouchX = mScrollX;
727 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
728 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 }
730 // Either way, cancel any pending longpress
731 if (mAllowLongPress) {
732 mAllowLongPress = false;
733 // Try canceling the long press. It could also have been scheduled
734 // by a distant descendant, so use the mAllowLongPress flag to block
735 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700736 final View currentPage = getPageAt(mCurrentPage);
737 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700738 }
739 }
740 }
741
742 @Override
743 public boolean onTouchEvent(MotionEvent ev) {
744 if (mVelocityTracker == null) {
745 mVelocityTracker = VelocityTracker.obtain();
746 }
747 mVelocityTracker.addMovement(ev);
748
749 final int action = ev.getAction();
750
751 switch (action & MotionEvent.ACTION_MASK) {
752 case MotionEvent.ACTION_DOWN:
753 /*
754 * If being flinged and user touches, stop the fling. isFinished
755 * will be false if being flinged.
756 */
757 if (!mScroller.isFinished()) {
758 mScroller.abortAnimation();
759 }
760
761 // Remember where the motion event started
762 mDownMotionX = mLastMotionX = ev.getX();
763 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700764 if (mTouchState == TOUCH_STATE_SCROLLING) {
765 pageBeginMoving();
766 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 break;
768
769 case MotionEvent.ACTION_MOVE:
770 if (mTouchState == TOUCH_STATE_SCROLLING) {
771 // Scroll to follow the motion event
772 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
773 final float x = ev.getX(pointerIndex);
774 final int deltaX = (int) (mLastMotionX - x);
775 mLastMotionX = x;
776
777 int sx = getScrollX();
778 if (deltaX < 0) {
779 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700780 mTouchX += Math.max(-mTouchX, deltaX);
781 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
782 if (!mDeferScrollUpdate) {
783 scrollBy(Math.max(-sx, deltaX), 0);
784 } else {
785 // This will trigger a call to computeScroll() on next drawChild() call
786 invalidate();
787 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700788 }
789 } else if (deltaX > 0) {
790 final int lastChildIndex = getChildCount() - 1;
791 final int availableToScroll = getChildOffset(lastChildIndex) -
792 getRelativeChildOffset(lastChildIndex) - sx;
793 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700794 mTouchX += Math.min(availableToScroll, deltaX);
795 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
796 if (!mDeferScrollUpdate) {
797 scrollBy(Math.min(availableToScroll, deltaX), 0);
798 } else {
799 // This will trigger a call to computeScroll() on next drawChild() call
800 invalidate();
801 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700802 }
803 } else {
804 awakenScrollBars();
805 }
806 } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) ||
807 (mTouchState == TOUCH_STATE_NEXT_PAGE)) {
808 determineScrollingStart(ev);
809 }
810 break;
811
812 case MotionEvent.ACTION_UP:
813 if (mTouchState == TOUCH_STATE_SCROLLING) {
814 final int activePointerId = mActivePointerId;
815 final int pointerIndex = ev.findPointerIndex(activePointerId);
816 final float x = ev.getX(pointerIndex);
817 final VelocityTracker velocityTracker = mVelocityTracker;
818 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
819 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
820 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
821
Michael Jurka0142d492010-08-25 17:46:15 -0700822 final int snapVelocity = mSnapVelocity;
823 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
824 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
825 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700826 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700827 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700828 } else {
829 snapToDestination();
830 }
831
832 if (mVelocityTracker != null) {
833 mVelocityTracker.recycle();
834 mVelocityTracker = null;
835 }
836 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
837 // at this point we have not moved beyond the touch slop
838 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
839 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700840 int nextPage = Math.max(0, mCurrentPage - 1);
841 if (nextPage != mCurrentPage) {
842 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700843 } else {
844 snapToDestination();
845 }
846 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
847 // at this point we have not moved beyond the touch slop
848 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
849 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700850 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
851 if (nextPage != mCurrentPage) {
852 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700853 } else {
854 snapToDestination();
855 }
856 }
857 mTouchState = TOUCH_STATE_REST;
858 mActivePointerId = INVALID_POINTER;
859 break;
860
861 case MotionEvent.ACTION_CANCEL:
862 mTouchState = TOUCH_STATE_REST;
863 mActivePointerId = INVALID_POINTER;
864 break;
865
866 case MotionEvent.ACTION_POINTER_UP:
867 onSecondaryPointerUp(ev);
868 break;
869 }
870
871 return true;
872 }
873
874 private void onSecondaryPointerUp(MotionEvent ev) {
875 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
876 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
877 final int pointerId = ev.getPointerId(pointerIndex);
878 if (pointerId == mActivePointerId) {
879 // This was our active pointer going up. Choose a new
880 // active pointer and adjust accordingly.
881 // TODO: Make this decision more intelligent.
882 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
883 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
884 mLastMotionY = ev.getY(newPointerIndex);
885 mActivePointerId = ev.getPointerId(newPointerIndex);
886 if (mVelocityTracker != null) {
887 mVelocityTracker.clear();
888 }
889 }
890 }
891
892 @Override
893 public void requestChildFocus(View child, View focused) {
894 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700895 int page = indexOfChild(child);
896 if (page >= 0 && !isInTouchMode()) {
897 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700898 }
899 }
900
Winson Chunge3193b92010-09-10 11:44:42 -0700901 protected int getChildIndexForRelativeOffset(int relativeOffset) {
902 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700903 int left;
904 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700905 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700906 left = getRelativeChildOffset(i);
907 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700908 if (left <= relativeOffset && relativeOffset <= right) {
909 return i;
910 }
Winson Chunge3193b92010-09-10 11:44:42 -0700911 }
912 return -1;
913 }
914
Winson Chung321e9ee2010-08-09 13:37:56 -0700915 protected int getRelativeChildOffset(int index) {
916 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
917 }
918
919 protected int getChildOffset(int index) {
920 if (getChildCount() == 0)
921 return 0;
922
923 int offset = getRelativeChildOffset(0);
924 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700925 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700926 }
927 return offset;
928 }
929
Adam Cohend19d3ca2010-09-15 14:43:42 -0700930 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700931 int minDistanceFromScreenCenter = getMeasuredWidth();
932 int minDistanceFromScreenCenterIndex = -1;
933 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
934 final int childCount = getChildCount();
935 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700936 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700937 int childWidth = layout.getMeasuredWidth();
938 int halfChildWidth = (childWidth / 2);
939 int childCenter = getChildOffset(i) + halfChildWidth;
940 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
941 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
942 minDistanceFromScreenCenter = distanceFromScreenCenter;
943 minDistanceFromScreenCenterIndex = i;
944 }
945 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700946 return minDistanceFromScreenCenterIndex;
947 }
948
949 protected void snapToDestination() {
950 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700951 }
952
Michael Jurka0142d492010-08-25 17:46:15 -0700953 protected void snapToPageWithVelocity(int whichPage, int velocity) {
954 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
955 // can use it
956 snapToPage(whichPage);
957 }
958
959 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700960 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700961 }
962
Michael Jurka0142d492010-08-25 17:46:15 -0700963 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700964 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700965
Winson Chung86f77532010-08-24 11:08:22 -0700966 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700967 final int sX = getScrollX();
968 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700969 snapToPage(whichPage, delta, duration);
970 }
971
972 protected void snapToPage(int whichPage, int delta, int duration) {
973 mNextPage = whichPage;
974
975 View focusedChild = getFocusedChild();
976 if (focusedChild != null && whichPage != mCurrentPage &&
977 focusedChild == getChildAt(mCurrentPage)) {
978 focusedChild.clearFocus();
979 }
980
981 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 awakenScrollBars(duration);
983 if (duration == 0) {
984 duration = Math.abs(delta);
985 }
986
987 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -0700988 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700989
990 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -0700991 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700992 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700993 invalidate();
994 }
995
996 @Override
997 protected Parcelable onSaveInstanceState() {
998 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -0700999 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 return state;
1001 }
1002
1003 @Override
1004 protected void onRestoreInstanceState(Parcelable state) {
1005 SavedState savedState = (SavedState) state;
1006 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001007 if (savedState.currentPage != -1) {
1008 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 }
1010 }
1011
1012 public void scrollLeft() {
1013 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001014 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001015 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001016 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001017 }
1018 }
1019
1020 public void scrollRight() {
1021 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001022 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001023 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001024 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001025 }
1026 }
1027
Winson Chung86f77532010-08-24 11:08:22 -07001028 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001029 int result = -1;
1030 if (v != null) {
1031 ViewParent vp = v.getParent();
1032 int count = getChildCount();
1033 for (int i = 0; i < count; i++) {
1034 if (vp == getChildAt(i)) {
1035 return i;
1036 }
1037 }
1038 }
1039 return result;
1040 }
1041
1042 /**
1043 * @return True is long presses are still allowed for the current touch
1044 */
1045 public boolean allowLongPress() {
1046 return mAllowLongPress;
1047 }
1048
Michael Jurka0142d492010-08-25 17:46:15 -07001049 /**
1050 * Set true to allow long-press events to be triggered, usually checked by
1051 * {@link Launcher} to accept or block dpad-initiated long-presses.
1052 */
1053 public void setAllowLongPress(boolean allowLongPress) {
1054 mAllowLongPress = allowLongPress;
1055 }
1056
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001058 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001059
1060 SavedState(Parcelable superState) {
1061 super(superState);
1062 }
1063
1064 private SavedState(Parcel in) {
1065 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001066 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001067 }
1068
1069 @Override
1070 public void writeToParcel(Parcel out, int flags) {
1071 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001072 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001073 }
1074
1075 public static final Parcelable.Creator<SavedState> CREATOR =
1076 new Parcelable.Creator<SavedState>() {
1077 public SavedState createFromParcel(Parcel in) {
1078 return new SavedState(in);
1079 }
1080
1081 public SavedState[] newArray(int size) {
1082 return new SavedState[size];
1083 }
1084 };
1085 }
1086
Winson Chung86f77532010-08-24 11:08:22 -07001087 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001088 if (mContentIsRefreshable) {
1089 final int count = getChildCount();
1090 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001091 int lowerPageBound = getAssociatedLowerPageBound(page);
1092 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001093 for (int i = 0; i < count; ++i) {
1094 final ViewGroup layout = (ViewGroup) getChildAt(i);
1095 final int childCount = layout.getChildCount();
1096 if (lowerPageBound <= i && i <= upperPageBound) {
1097 if (mDirtyPageContent.get(i)) {
1098 syncPageItems(i);
1099 mDirtyPageContent.set(i, false);
1100 }
1101 } else {
1102 if (childCount > 0) {
1103 layout.removeAllViews();
1104 }
1105 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001106 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001107 }
1108 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001109 }
1110 }
1111
Winson Chunge3193b92010-09-10 11:44:42 -07001112 protected int getAssociatedLowerPageBound(int page) {
1113 return Math.max(0, page - 1);
1114 }
1115 protected int getAssociatedUpperPageBound(int page) {
1116 final int count = getChildCount();
1117 return Math.min(page + 1, count - 1);
1118 }
1119
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001120 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001121 if (isChoiceMode(CHOICE_MODE_NONE)) {
1122 mChoiceMode = mode;
1123 mActionMode = startActionMode(callback);
1124 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001125 }
1126
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001127 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001128 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001129 mChoiceMode = CHOICE_MODE_NONE;
1130 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001131 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001132 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001133 }
1134 }
1135
1136 protected boolean isChoiceMode(int mode) {
1137 return mChoiceMode == mode;
1138 }
1139
1140 protected ArrayList<Checkable> getCheckedGrandchildren() {
1141 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1142 final int childCount = getChildCount();
1143 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001144 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001145 final int grandChildCount = layout.getChildCount();
1146 for (int j = 0; j < grandChildCount; ++j) {
1147 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001148 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001149 checked.add((Checkable) v);
1150 }
1151 }
1152 }
1153 return checked;
1154 }
1155
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001156 /**
1157 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1158 * Otherwise, returns null.
1159 */
1160 protected Checkable getSingleCheckedGrandchild() {
1161 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1162 final int childCount = getChildCount();
1163 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001164 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001165 final int grandChildCount = layout.getChildCount();
1166 for (int j = 0; j < grandChildCount; ++j) {
1167 final View v = layout.getChildAt(j);
1168 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1169 return (Checkable) v;
1170 }
1171 }
1172 }
1173 }
1174 return null;
1175 }
1176
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001177 public Object getChosenItem() {
1178 View checkedView = (View) getSingleCheckedGrandchild();
1179 if (checkedView != null) {
1180 return checkedView.getTag();
1181 }
1182 return null;
1183 }
1184
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001185 protected void resetCheckedGrandchildren() {
1186 // loop through children, and set all of their children to _not_ be checked
1187 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1188 for (int i = 0; i < checked.size(); ++i) {
1189 final Checkable c = checked.get(i);
1190 c.setChecked(false);
1191 }
1192 }
1193
Winson Chung86f77532010-08-24 11:08:22 -07001194 /**
1195 * This method is called ONLY to synchronize the number of pages that the paged view has.
1196 * To actually fill the pages with information, implement syncPageItems() below. It is
1197 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1198 * and therefore, individual page items do not need to be updated in this method.
1199 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001200 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001201
1202 /**
1203 * This method is called to synchronize the items that are on a particular page. If views on
1204 * the page can be reused, then they should be updated within this method.
1205 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001206 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001207
Winson Chung321e9ee2010-08-09 13:37:56 -07001208 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001209 if (mContentIsRefreshable) {
1210 // Update all the pages
1211 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001212
Michael Jurka0142d492010-08-25 17:46:15 -07001213 // Mark each of the pages as dirty
1214 final int count = getChildCount();
1215 mDirtyPageContent.clear();
1216 for (int i = 0; i < count; ++i) {
1217 mDirtyPageContent.add(true);
1218 }
1219
1220 // Load any pages that are necessary for the current window of views
1221 loadAssociatedPages(mCurrentPage);
1222 mDirtyPageAlpha = true;
1223 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001224 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 }
1226}