blob: 11261a57572be77ce6a8f3ac213e6ba33ca080c5 [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 Cohene45440e2010-10-14 18:33:38 -070083 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
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
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700409 // On the first layout, we may not have a width nor a proper offset, so for now
410 // we should just assume full page width (and calculate the offset according to
411 // that).
412 if (childWidth <= 0) {
413 childWidth = getMeasuredWidth();
414 childCenter = (i * childWidth) + (childWidth / 2);
415 }
416
Winson Chunge8878e32010-09-15 20:37:09 -0700417 int d = halfChildWidth;
418 int distanceFromScreenCenter = childCenter - screenCenter;
419 if (distanceFromScreenCenter > 0) {
420 if (i > 0) {
421 d += getChildAt(i - 1).getMeasuredWidth() / 2;
422 }
Michael Jurka0142d492010-08-25 17:46:15 -0700423 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700424 if (i < childCount - 1) {
425 d += getChildAt(i + 1).getMeasuredWidth() / 2;
426 }
Michael Jurka0142d492010-08-25 17:46:15 -0700427 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700428 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700429
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700430 // Preventing potential divide-by-zero
431 d = Math.max(1, d);
432
Winson Chunge8878e32010-09-15 20:37:09 -0700433 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
434 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
435 float alpha = 1.0f - dimAlpha;
436
Adam Cohenf34bab52010-09-30 14:11:56 -0700437 if (alpha < ALPHA_QUANTIZE_LEVEL) {
438 alpha = 0.0f;
439 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
440 alpha = 1.0f;
441 }
442
Michael Jurka0142d492010-08-25 17:46:15 -0700443 if (Float.compare(alpha, layout.getAlpha()) != 0) {
444 layout.setAlpha(alpha);
445 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 }
Michael Jurka0142d492010-08-25 17:46:15 -0700447 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 }
449 }
Winson Chunge3193b92010-09-10 11:44:42 -0700450 }
451
Adam Cohenf34bab52010-09-30 14:11:56 -0700452 protected void screenScrolled(int screenCenter) {
453 }
454
Winson Chunge3193b92010-09-10 11:44:42 -0700455 @Override
456 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700457 int halfScreenSize = getMeasuredWidth() / 2;
458 int screenCenter = mScrollX + halfScreenSize;
459
460 if (screenCenter != mLastScreenCenter) {
461 screenScrolled(screenCenter);
462 updateAdjacentPagesAlpha();
463 mLastScreenCenter = screenCenter;
464 }
Michael Jurka0142d492010-08-25 17:46:15 -0700465
466 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700467 // As an optimization, this code assumes that all pages have the same width as the 0th
468 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700469 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700470 if (pageCount > 0) {
471 final int pageWidth = getChildAt(0).getMeasuredWidth();
472 final int screenWidth = getMeasuredWidth();
473 int x = getRelativeChildOffset(0) + pageWidth;
474 int leftScreen = 0;
475 int rightScreen = 0;
476 while (x <= mScrollX) {
477 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700478 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700479 // replace above line with this if you don't assume all pages have same width as 0th
480 // page:
481 // x += getChildAt(leftScreen).getMeasuredWidth();
482 }
483 rightScreen = leftScreen;
484 while (x < mScrollX + screenWidth) {
485 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700486 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700487 // replace above line with this if you don't assume all pages have same width as 0th
488 // page:
489 //if (rightScreen < pageCount) {
490 // x += getChildAt(rightScreen).getMeasuredWidth();
491 //}
492 }
493 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700494
Michael Jurkac4fb9172010-09-02 17:19:20 -0700495 final long drawingTime = getDrawingTime();
496 for (int i = leftScreen; i <= rightScreen; i++) {
497 drawChild(canvas, getChildAt(i), drawingTime);
498 }
Michael Jurka0142d492010-08-25 17:46:15 -0700499 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700500 }
501
502 @Override
503 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700504 int page = indexOfChild(child);
505 if (page != mCurrentPage || !mScroller.isFinished()) {
506 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 return true;
508 }
509 return false;
510 }
511
512 @Override
513 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700514 int focusablePage;
515 if (mNextPage != INVALID_PAGE) {
516 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700518 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
Winson Chung86f77532010-08-24 11:08:22 -0700520 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 if (v != null) {
522 v.requestFocus(direction, previouslyFocusedRect);
523 }
524 return false;
525 }
526
527 @Override
528 public boolean dispatchUnhandledMove(View focused, int direction) {
529 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700530 if (getCurrentPage() > 0) {
531 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 return true;
533 }
534 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700535 if (getCurrentPage() < getPageCount() - 1) {
536 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 return true;
538 }
539 }
540 return super.dispatchUnhandledMove(focused, direction);
541 }
542
543 @Override
544 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700545 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
546 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 }
548 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700549 if (mCurrentPage > 0) {
550 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700551 }
552 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700553 if (mCurrentPage < getPageCount() - 1) {
554 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 }
556 }
557 }
558
559 /**
560 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700561 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 *
Winson Chung86f77532010-08-24 11:08:22 -0700563 * This happens when live folders requery, and if they're off page, they
564 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700565 */
566 @Override
567 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700568 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700569 View v = focused;
570 while (true) {
571 if (v == current) {
572 super.focusableViewAvailable(focused);
573 return;
574 }
575 if (v == this) {
576 return;
577 }
578 ViewParent parent = v.getParent();
579 if (parent instanceof View) {
580 v = (View)v.getParent();
581 } else {
582 return;
583 }
584 }
585 }
586
587 /**
588 * {@inheritDoc}
589 */
590 @Override
591 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
592 if (disallowIntercept) {
593 // We need to make sure to cancel our long press if
594 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700595 final View currentPage = getChildAt(mCurrentPage);
596 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700597 }
598 super.requestDisallowInterceptTouchEvent(disallowIntercept);
599 }
600
601 @Override
602 public boolean onInterceptTouchEvent(MotionEvent ev) {
603 /*
604 * This method JUST determines whether we want to intercept the motion.
605 * If we return true, onTouchEvent will be called and we do the actual
606 * scrolling there.
607 */
608
609 /*
610 * Shortcut the most recurring case: the user is in the dragging
611 * state and he is moving his finger. We want to intercept this
612 * motion.
613 */
614 final int action = ev.getAction();
615 if ((action == MotionEvent.ACTION_MOVE) &&
616 (mTouchState == TOUCH_STATE_SCROLLING)) {
617 return true;
618 }
619
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 switch (action & MotionEvent.ACTION_MASK) {
621 case MotionEvent.ACTION_MOVE: {
622 /*
623 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
624 * whether the user has moved far enough from his original down touch.
625 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700626 if (mActivePointerId != INVALID_POINTER) {
627 determineScrollingStart(ev);
628 break;
629 }
630 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
631 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
632 // i.e. fall through to the next case (don't break)
633 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
634 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 }
636
637 case MotionEvent.ACTION_DOWN: {
638 final float x = ev.getX();
639 final float y = ev.getY();
640 // Remember location of down touch
641 mDownMotionX = x;
642 mLastMotionX = x;
643 mLastMotionY = y;
644 mActivePointerId = ev.getPointerId(0);
645 mAllowLongPress = true;
646
647 /*
648 * If being flinged and user touches the screen, initiate drag;
649 * otherwise don't. mScroller.isFinished should be false when
650 * being flinged.
651 */
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700652 final int xDist = (mScroller.getFinalX() - mScroller.getCurrX());
653 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
654 if (finishedScrolling) {
655 mTouchState = TOUCH_STATE_REST;
656 mScroller.abortAnimation();
657 } else {
658 mTouchState = TOUCH_STATE_SCROLLING;
659 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700660
Winson Chung86f77532010-08-24 11:08:22 -0700661 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700662 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700663 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700664 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
665 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700666 int width = getMeasuredWidth();
667 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700668 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700669 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700670 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700671 mTouchState = TOUCH_STATE_NEXT_PAGE;
672 }
673 }
674 }
675 break;
676 }
677
678 case MotionEvent.ACTION_CANCEL:
679 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 mTouchState = TOUCH_STATE_REST;
681 mAllowLongPress = false;
682 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700683 break;
684
685 case MotionEvent.ACTION_POINTER_UP:
686 onSecondaryPointerUp(ev);
687 break;
688 }
689
690 /*
691 * The only time we want to intercept motion events is if we are in the
692 * drag mode.
693 */
694 return mTouchState != TOUCH_STATE_REST;
695 }
696
Winson Chung80baf5a2010-08-09 16:03:15 -0700697 protected void animateClickFeedback(View v, final Runnable r) {
698 // animate the view slightly to show click feedback running some logic after it is "pressed"
699 Animation anim = AnimationUtils.loadAnimation(getContext(),
700 R.anim.paged_view_click_feedback);
701 anim.setAnimationListener(new AnimationListener() {
702 @Override
703 public void onAnimationStart(Animation animation) {}
704 @Override
705 public void onAnimationRepeat(Animation animation) {
706 r.run();
707 }
708 @Override
709 public void onAnimationEnd(Animation animation) {}
710 });
711 v.startAnimation(anim);
712 }
713
Winson Chung321e9ee2010-08-09 13:37:56 -0700714 /*
715 * Determines if we should change the touch state to start scrolling after the
716 * user moves their touch point too far.
717 */
718 private void determineScrollingStart(MotionEvent ev) {
719 /*
720 * Locally do absolute value. mLastMotionX is set to the y value
721 * of the down event.
722 */
723 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
724 final float x = ev.getX(pointerIndex);
725 final float y = ev.getY(pointerIndex);
726 final int xDiff = (int) Math.abs(x - mLastMotionX);
727 final int yDiff = (int) Math.abs(y - mLastMotionY);
728
729 final int touchSlop = mTouchSlop;
730 boolean xPaged = xDiff > mPagingTouchSlop;
731 boolean xMoved = xDiff > touchSlop;
732 boolean yMoved = yDiff > touchSlop;
733
734 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700735 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700736 // Scroll if the user moved far enough along the X axis
737 mTouchState = TOUCH_STATE_SCROLLING;
738 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700739 mTouchX = mScrollX;
740 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
741 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700742 }
743 // Either way, cancel any pending longpress
744 if (mAllowLongPress) {
745 mAllowLongPress = false;
746 // Try canceling the long press. It could also have been scheduled
747 // by a distant descendant, so use the mAllowLongPress flag to block
748 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700749 final View currentPage = getPageAt(mCurrentPage);
750 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 }
752 }
753 }
754
Adam Cohen21f12b52010-10-07 17:15:37 -0700755 protected boolean handlePagingClicks() {
756 return false;
757 }
758
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 @Override
760 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700761 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700762
763 final int action = ev.getAction();
764
765 switch (action & MotionEvent.ACTION_MASK) {
766 case MotionEvent.ACTION_DOWN:
767 /*
768 * If being flinged and user touches, stop the fling. isFinished
769 * will be false if being flinged.
770 */
771 if (!mScroller.isFinished()) {
772 mScroller.abortAnimation();
773 }
774
775 // Remember where the motion event started
776 mDownMotionX = mLastMotionX = ev.getX();
777 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700778 if (mTouchState == TOUCH_STATE_SCROLLING) {
779 pageBeginMoving();
780 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 break;
782
783 case MotionEvent.ACTION_MOVE:
784 if (mTouchState == TOUCH_STATE_SCROLLING) {
785 // Scroll to follow the motion event
786 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
787 final float x = ev.getX(pointerIndex);
788 final int deltaX = (int) (mLastMotionX - x);
789 mLastMotionX = x;
790
791 int sx = getScrollX();
792 if (deltaX < 0) {
793 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700794 mTouchX += Math.max(-mTouchX, deltaX);
795 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
796 if (!mDeferScrollUpdate) {
797 scrollBy(Math.max(-sx, 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 if (deltaX > 0) {
804 final int lastChildIndex = getChildCount() - 1;
805 final int availableToScroll = getChildOffset(lastChildIndex) -
806 getRelativeChildOffset(lastChildIndex) - sx;
807 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700808 mTouchX += Math.min(availableToScroll, deltaX);
809 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
810 if (!mDeferScrollUpdate) {
811 scrollBy(Math.min(availableToScroll, deltaX), 0);
812 } else {
813 // This will trigger a call to computeScroll() on next drawChild() call
814 invalidate();
815 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700816 }
817 } else {
818 awakenScrollBars();
819 }
Adam Cohen564976a2010-10-13 18:52:07 -0700820 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700821 determineScrollingStart(ev);
822 }
823 break;
824
825 case MotionEvent.ACTION_UP:
826 if (mTouchState == TOUCH_STATE_SCROLLING) {
827 final int activePointerId = mActivePointerId;
828 final int pointerIndex = ev.findPointerIndex(activePointerId);
829 final float x = ev.getX(pointerIndex);
830 final VelocityTracker velocityTracker = mVelocityTracker;
831 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
832 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
833 boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING;
834
Michael Jurka0142d492010-08-25 17:46:15 -0700835 final int snapVelocity = mSnapVelocity;
836 if (isfling && velocityX > snapVelocity && mCurrentPage > 0) {
837 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
838 } else if (isfling && velocityX < -snapVelocity &&
Winson Chung86f77532010-08-24 11:08:22 -0700839 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700840 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700841 } else {
842 snapToDestination();
843 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700844 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700845 // at this point we have not moved beyond the touch slop
846 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
847 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700848 int nextPage = Math.max(0, mCurrentPage - 1);
849 if (nextPage != mCurrentPage) {
850 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700851 } else {
852 snapToDestination();
853 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700854 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700855 // at this point we have not moved beyond the touch slop
856 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
857 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700858 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
859 if (nextPage != mCurrentPage) {
860 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700861 } else {
862 snapToDestination();
863 }
864 }
865 mTouchState = TOUCH_STATE_REST;
866 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700867 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 break;
869
870 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700871 if (mTouchState == TOUCH_STATE_SCROLLING) {
872 snapToDestination();
873 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 mTouchState = TOUCH_STATE_REST;
875 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700876 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 break;
878
879 case MotionEvent.ACTION_POINTER_UP:
880 onSecondaryPointerUp(ev);
881 break;
882 }
883
884 return true;
885 }
886
Michael Jurkab8f06722010-10-10 15:58:46 -0700887 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
888 if (mVelocityTracker == null) {
889 mVelocityTracker = VelocityTracker.obtain();
890 }
891 mVelocityTracker.addMovement(ev);
892 }
893
894 private void releaseVelocityTracker() {
895 if (mVelocityTracker != null) {
896 mVelocityTracker.recycle();
897 mVelocityTracker = null;
898 }
899 }
900
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 private void onSecondaryPointerUp(MotionEvent ev) {
902 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
903 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
904 final int pointerId = ev.getPointerId(pointerIndex);
905 if (pointerId == mActivePointerId) {
906 // This was our active pointer going up. Choose a new
907 // active pointer and adjust accordingly.
908 // TODO: Make this decision more intelligent.
909 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
910 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
911 mLastMotionY = ev.getY(newPointerIndex);
912 mActivePointerId = ev.getPointerId(newPointerIndex);
913 if (mVelocityTracker != null) {
914 mVelocityTracker.clear();
915 }
916 }
917 }
918
919 @Override
920 public void requestChildFocus(View child, View focused) {
921 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700922 int page = indexOfChild(child);
923 if (page >= 0 && !isInTouchMode()) {
924 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700925 }
926 }
927
Winson Chunge3193b92010-09-10 11:44:42 -0700928 protected int getChildIndexForRelativeOffset(int relativeOffset) {
929 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700930 int left;
931 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700932 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700933 left = getRelativeChildOffset(i);
934 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700935 if (left <= relativeOffset && relativeOffset <= right) {
936 return i;
937 }
Winson Chunge3193b92010-09-10 11:44:42 -0700938 }
939 return -1;
940 }
941
Winson Chung321e9ee2010-08-09 13:37:56 -0700942 protected int getRelativeChildOffset(int index) {
943 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
944 }
945
946 protected int getChildOffset(int index) {
947 if (getChildCount() == 0)
948 return 0;
949
950 int offset = getRelativeChildOffset(0);
951 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700952 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700953 }
954 return offset;
955 }
956
Adam Cohend19d3ca2010-09-15 14:43:42 -0700957 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700958 int minDistanceFromScreenCenter = getMeasuredWidth();
959 int minDistanceFromScreenCenterIndex = -1;
960 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
961 final int childCount = getChildCount();
962 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700963 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 int childWidth = layout.getMeasuredWidth();
965 int halfChildWidth = (childWidth / 2);
966 int childCenter = getChildOffset(i) + halfChildWidth;
967 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
968 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
969 minDistanceFromScreenCenter = distanceFromScreenCenter;
970 minDistanceFromScreenCenterIndex = i;
971 }
972 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700973 return minDistanceFromScreenCenterIndex;
974 }
975
976 protected void snapToDestination() {
977 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700978 }
979
Michael Jurka0142d492010-08-25 17:46:15 -0700980 protected void snapToPageWithVelocity(int whichPage, int velocity) {
981 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
982 // can use it
983 snapToPage(whichPage);
984 }
985
986 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700987 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700988 }
989
Michael Jurka0142d492010-08-25 17:46:15 -0700990 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -0700991 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700992
Winson Chung86f77532010-08-24 11:08:22 -0700993 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700994 final int sX = getScrollX();
995 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -0700996 snapToPage(whichPage, delta, duration);
997 }
998
999 protected void snapToPage(int whichPage, int delta, int duration) {
1000 mNextPage = whichPage;
1001
1002 View focusedChild = getFocusedChild();
1003 if (focusedChild != null && whichPage != mCurrentPage &&
1004 focusedChild == getChildAt(mCurrentPage)) {
1005 focusedChild.clearFocus();
1006 }
1007
1008 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001009 awakenScrollBars(duration);
1010 if (duration == 0) {
1011 duration = Math.abs(delta);
1012 }
1013
1014 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001015 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001016
1017 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001018 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001019 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001020 invalidate();
1021 }
1022
1023 @Override
1024 protected Parcelable onSaveInstanceState() {
1025 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001026 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 return state;
1028 }
1029
1030 @Override
1031 protected void onRestoreInstanceState(Parcelable state) {
1032 SavedState savedState = (SavedState) state;
1033 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001034 if (savedState.currentPage != -1) {
1035 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001036 }
1037 }
1038
1039 public void scrollLeft() {
1040 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001041 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001043 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001044 }
1045 }
1046
1047 public void scrollRight() {
1048 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001049 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001050 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001051 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001052 }
1053 }
1054
Winson Chung86f77532010-08-24 11:08:22 -07001055 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001056 int result = -1;
1057 if (v != null) {
1058 ViewParent vp = v.getParent();
1059 int count = getChildCount();
1060 for (int i = 0; i < count; i++) {
1061 if (vp == getChildAt(i)) {
1062 return i;
1063 }
1064 }
1065 }
1066 return result;
1067 }
1068
1069 /**
1070 * @return True is long presses are still allowed for the current touch
1071 */
1072 public boolean allowLongPress() {
1073 return mAllowLongPress;
1074 }
1075
Michael Jurka0142d492010-08-25 17:46:15 -07001076 /**
1077 * Set true to allow long-press events to be triggered, usually checked by
1078 * {@link Launcher} to accept or block dpad-initiated long-presses.
1079 */
1080 public void setAllowLongPress(boolean allowLongPress) {
1081 mAllowLongPress = allowLongPress;
1082 }
1083
Winson Chung321e9ee2010-08-09 13:37:56 -07001084 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001085 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001086
1087 SavedState(Parcelable superState) {
1088 super(superState);
1089 }
1090
1091 private SavedState(Parcel in) {
1092 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001093 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001094 }
1095
1096 @Override
1097 public void writeToParcel(Parcel out, int flags) {
1098 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001099 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001100 }
1101
1102 public static final Parcelable.Creator<SavedState> CREATOR =
1103 new Parcelable.Creator<SavedState>() {
1104 public SavedState createFromParcel(Parcel in) {
1105 return new SavedState(in);
1106 }
1107
1108 public SavedState[] newArray(int size) {
1109 return new SavedState[size];
1110 }
1111 };
1112 }
1113
Winson Chung86f77532010-08-24 11:08:22 -07001114 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001115 if (mContentIsRefreshable) {
1116 final int count = getChildCount();
1117 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001118 int lowerPageBound = getAssociatedLowerPageBound(page);
1119 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001120 for (int i = 0; i < count; ++i) {
1121 final ViewGroup layout = (ViewGroup) getChildAt(i);
1122 final int childCount = layout.getChildCount();
1123 if (lowerPageBound <= i && i <= upperPageBound) {
1124 if (mDirtyPageContent.get(i)) {
1125 syncPageItems(i);
1126 mDirtyPageContent.set(i, false);
1127 }
1128 } else {
1129 if (childCount > 0) {
1130 layout.removeAllViews();
1131 }
1132 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001133 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001134 }
1135 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001136 }
1137 }
1138
Winson Chunge3193b92010-09-10 11:44:42 -07001139 protected int getAssociatedLowerPageBound(int page) {
1140 return Math.max(0, page - 1);
1141 }
1142 protected int getAssociatedUpperPageBound(int page) {
1143 final int count = getChildCount();
1144 return Math.min(page + 1, count - 1);
1145 }
1146
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001147 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001148 if (isChoiceMode(CHOICE_MODE_NONE)) {
1149 mChoiceMode = mode;
1150 mActionMode = startActionMode(callback);
1151 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001152 }
1153
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001154 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001155 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001156 mChoiceMode = CHOICE_MODE_NONE;
1157 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001158 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001159 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001160 }
1161 }
1162
1163 protected boolean isChoiceMode(int mode) {
1164 return mChoiceMode == mode;
1165 }
1166
1167 protected ArrayList<Checkable> getCheckedGrandchildren() {
1168 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1169 final int childCount = getChildCount();
1170 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001171 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001172 final int grandChildCount = layout.getChildCount();
1173 for (int j = 0; j < grandChildCount; ++j) {
1174 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001175 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001176 checked.add((Checkable) v);
1177 }
1178 }
1179 }
1180 return checked;
1181 }
1182
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001183 /**
1184 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1185 * Otherwise, returns null.
1186 */
1187 protected Checkable getSingleCheckedGrandchild() {
1188 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1189 final int childCount = getChildCount();
1190 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001191 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001192 final int grandChildCount = layout.getChildCount();
1193 for (int j = 0; j < grandChildCount; ++j) {
1194 final View v = layout.getChildAt(j);
1195 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1196 return (Checkable) v;
1197 }
1198 }
1199 }
1200 }
1201 return null;
1202 }
1203
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001204 public Object getChosenItem() {
1205 View checkedView = (View) getSingleCheckedGrandchild();
1206 if (checkedView != null) {
1207 return checkedView.getTag();
1208 }
1209 return null;
1210 }
1211
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001212 protected void resetCheckedGrandchildren() {
1213 // loop through children, and set all of their children to _not_ be checked
1214 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1215 for (int i = 0; i < checked.size(); ++i) {
1216 final Checkable c = checked.get(i);
1217 c.setChecked(false);
1218 }
1219 }
1220
Winson Chung86f77532010-08-24 11:08:22 -07001221 /**
1222 * This method is called ONLY to synchronize the number of pages that the paged view has.
1223 * To actually fill the pages with information, implement syncPageItems() below. It is
1224 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1225 * and therefore, individual page items do not need to be updated in this method.
1226 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001227 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001228
1229 /**
1230 * This method is called to synchronize the items that are on a particular page. If views on
1231 * the page can be reused, then they should be updated within this method.
1232 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001233 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001234
Winson Chung321e9ee2010-08-09 13:37:56 -07001235 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001236 if (mContentIsRefreshable) {
1237 // Update all the pages
1238 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001239
Michael Jurka0142d492010-08-25 17:46:15 -07001240 // Mark each of the pages as dirty
1241 final int count = getChildCount();
1242 mDirtyPageContent.clear();
1243 for (int i = 0; i < count; ++i) {
1244 mDirtyPageContent.add(true);
1245 }
1246
1247 // Load any pages that are necessary for the current window of views
1248 loadAssociatedPages(mCurrentPage);
1249 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001250 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001251 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001252 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 }
1254}