blob: 9739b156fd57636f407a3ed1dd6d8eb449eb1fc0 [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 Chung9cfd25f2010-10-24 16:09:28 -070056 private static final int MIN_LENGTH_FOR_FLING = 25;
57 // The min drag distance to trigger a page shift (regardless of velocity)
58 private static final int MIN_LENGTH_FOR_MOVE = 200;
Winson Chung321e9ee2010-08-09 13:37:56 -070059
Winson Chung5f2aa4e2010-08-20 14:49:25 -070060 private static final int PAGE_SNAP_ANIMATION_DURATION = 1000;
Michael Jurka0142d492010-08-25 17:46:15 -070061 protected static final float NANOTIME_DIV = 1000000000.0f;
Winson Chung321e9ee2010-08-09 13:37:56 -070062
Michael Jurka0142d492010-08-25 17:46:15 -070063 // the velocity at which a fling gesture will cause us to snap to the next page
64 protected int mSnapVelocity = 500;
65
66 protected float mSmoothingTime;
67 protected float mTouchX;
68
69 protected boolean mFirstLayout = true;
70
71 protected int mCurrentPage;
72 protected int mNextPage = INVALID_PAGE;
73 protected Scroller mScroller;
Winson Chung321e9ee2010-08-09 13:37:56 -070074 private VelocityTracker mVelocityTracker;
75
76 private float mDownMotionX;
77 private float mLastMotionX;
78 private float mLastMotionY;
Adam Cohenf34bab52010-09-30 14:11:56 -070079 private int mLastScreenCenter = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070080
Michael Jurka0142d492010-08-25 17:46:15 -070081 protected final static int TOUCH_STATE_REST = 0;
82 protected final static int TOUCH_STATE_SCROLLING = 1;
83 protected final static int TOUCH_STATE_PREV_PAGE = 2;
84 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohene45440e2010-10-14 18:33:38 -070085 protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f;
Winson Chung321e9ee2010-08-09 13:37:56 -070086
Michael Jurka0142d492010-08-25 17:46:15 -070087 protected int mTouchState = TOUCH_STATE_REST;
Winson Chung321e9ee2010-08-09 13:37:56 -070088
Michael Jurka0142d492010-08-25 17:46:15 -070089 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -070090
91 private boolean mAllowLongPress = true;
92
93 private int mTouchSlop;
94 private int mPagingTouchSlop;
95 private int mMaximumVelocity;
Adam Cohen9c4949e2010-10-05 12:27:22 -070096 protected int mPageSpacing;
97 protected int mPageLayoutPaddingTop;
98 protected int mPageLayoutPaddingBottom;
99 protected int mPageLayoutPaddingLeft;
100 protected int mPageLayoutPaddingRight;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700101 protected int mPageLayoutWidthGap;
102 protected int mPageLayoutHeightGap;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700103 protected int mCellCountX;
104 protected int mCellCountY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700105
Michael Jurka5f1c5092010-09-03 14:15:02 -0700106 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700107
Michael Jurka5f1c5092010-09-03 14:15:02 -0700108 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700109
Winson Chung86f77532010-08-24 11:08:22 -0700110 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111
Winson Chung86f77532010-08-24 11:08:22 -0700112 private ArrayList<Boolean> mDirtyPageContent;
113 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700114
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700115 // choice modes
116 protected static final int CHOICE_MODE_NONE = 0;
117 protected static final int CHOICE_MODE_SINGLE = 1;
118 // Multiple selection mode is not supported by all Launcher actions atm
119 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700120
Michael Jurkae17e19c2010-09-28 11:01:39 -0700121 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700122 private ActionMode mActionMode;
123
Winson Chung241c3b42010-08-25 16:53:03 -0700124 protected PagedViewIconCache mPageViewIconCache;
125
Michael Jurka0142d492010-08-25 17:46:15 -0700126 // If true, syncPages and syncPageItems will be called to refresh pages
127 protected boolean mContentIsRefreshable = true;
128
129 // If true, modify alpha of neighboring pages as user scrolls left/right
130 protected boolean mFadeInAdjacentScreens = true;
131
132 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
133 // to switch to a new page
134 protected boolean mUsePagingTouchSlop = true;
135
136 // If true, the subclass should directly update mScrollX itself in its computeScroll method
137 // (SmoothPagedView does this)
138 protected boolean mDeferScrollUpdate = false;
139
Patrick Dubroy1262e362010-10-06 15:49:50 -0700140 protected boolean mIsPageMoving = false;
141
Winson Chung241c3b42010-08-25 16:53:03 -0700142 /**
143 * Simple cache mechanism for PagedViewIcon outlines.
144 */
145 class PagedViewIconCache {
146 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
147
148 public void clear() {
149 iconOutlineCache.clear();
150 }
151 public void addOutline(Object key, Bitmap b) {
152 iconOutlineCache.put(key, b);
153 }
154 public void removeOutline(Object key) {
155 if (iconOutlineCache.containsKey(key)) {
156 iconOutlineCache.remove(key);
157 }
158 }
159 public Bitmap getOutline(Object key) {
160 return iconOutlineCache.get(key);
161 }
162 }
163
Winson Chung86f77532010-08-24 11:08:22 -0700164 public interface PageSwitchListener {
165 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 }
167
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 public PagedView(Context context) {
169 this(context, null);
170 }
171
172 public PagedView(Context context, AttributeSet attrs) {
173 this(context, attrs, 0);
174 }
175
176 public PagedView(Context context, AttributeSet attrs, int defStyle) {
177 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700178 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700179
Adam Cohen9c4949e2010-10-05 12:27:22 -0700180 TypedArray a = context.obtainStyledAttributes(attrs,
181 R.styleable.PagedView, defStyle, 0);
182 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
183 mPageLayoutPaddingTop = a.getDimensionPixelSize(
184 R.styleable.PagedView_pageLayoutPaddingTop, 10);
185 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
186 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
187 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
188 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
189 mPageLayoutPaddingRight = a.getDimensionPixelSize(
190 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700191 mPageLayoutWidthGap = a.getDimensionPixelSize(
192 R.styleable.PagedView_pageLayoutWidthGap, -1);
193 mPageLayoutHeightGap = a.getDimensionPixelSize(
194 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700195 a.recycle();
196
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700198 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700199 }
200
201 /**
202 * Initializes various states for this workspace.
203 */
Michael Jurka0142d492010-08-25 17:46:15 -0700204 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700205 mDirtyPageContent = new ArrayList<Boolean>();
206 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700207 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700209 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700210
211 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
212 mTouchSlop = configuration.getScaledTouchSlop();
213 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
214 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
215 }
216
Winson Chung86f77532010-08-24 11:08:22 -0700217 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
218 mPageSwitchListener = pageSwitchListener;
219 if (mPageSwitchListener != null) {
220 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222 }
223
224 /**
Winson Chung86f77532010-08-24 11:08:22 -0700225 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 *
Winson Chung86f77532010-08-24 11:08:22 -0700227 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 */
Winson Chung86f77532010-08-24 11:08:22 -0700229 int getCurrentPage() {
230 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700231 }
232
Winson Chung86f77532010-08-24 11:08:22 -0700233 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 return getChildCount();
235 }
236
Winson Chung86f77532010-08-24 11:08:22 -0700237 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 return getChildAt(index);
239 }
240
241 int getScrollWidth() {
242 return getWidth();
243 }
244
245 /**
Winson Chung86f77532010-08-24 11:08:22 -0700246 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 */
Winson Chung86f77532010-08-24 11:08:22 -0700248 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 if (!mScroller.isFinished()) mScroller.abortAnimation();
250 if (getChildCount() == 0) return;
251
Winson Chung86f77532010-08-24 11:08:22 -0700252 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700253 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
254 scrollTo(newX, 0);
255 mScroller.setFinalX(newX);
Winson Chung80baf5a2010-08-09 16:03:15 -0700256
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700258 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 }
260
Michael Jurka0142d492010-08-25 17:46:15 -0700261 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700262 if (mPageSwitchListener != null) {
263 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 }
265 }
266
Patrick Dubroy1262e362010-10-06 15:49:50 -0700267 private void pageBeginMoving() {
268 mIsPageMoving = true;
269 onPageBeginMoving();
270 }
271
272 private void pageEndMoving() {
273 onPageEndMoving();
274 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700275 }
276
277 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700278 protected void onPageBeginMoving() {
279 }
280
281 // a method that subclasses can override to add behavior
282 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700283 }
284
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 /**
Winson Chung86f77532010-08-24 11:08:22 -0700286 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 *
288 * @param l The listener used to respond to long clicks.
289 */
290 @Override
291 public void setOnLongClickListener(OnLongClickListener l) {
292 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700293 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700294 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700295 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 }
297 }
298
299 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700300 public void scrollTo(int x, int y) {
301 super.scrollTo(x, y);
302 mTouchX = x;
303 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
304 }
305
306 // we moved this functionality to a helper function so SmoothPagedView can reuse it
307 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700309 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700311 invalidate();
312 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700313 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700314 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700315 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700316 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700317 notifyPageSwitchListener();
318 pageEndMoving();
319 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 }
Michael Jurka0142d492010-08-25 17:46:15 -0700321 return false;
322 }
323
324 @Override
325 public void computeScroll() {
326 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700327 }
328
329 @Override
330 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
331 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
332 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
333 if (widthMode != MeasureSpec.EXACTLY) {
334 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
335 }
336
337 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
338 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
339 if (heightMode != MeasureSpec.EXACTLY) {
340 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
341 }
342
343 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700344 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 final int childCount = getChildCount();
346 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700347 // disallowing padding in paged view (just pass 0)
348 final View child = getChildAt(i);
349 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
350
351 int childWidthMode;
352 if (lp.width == LayoutParams.WRAP_CONTENT) {
353 childWidthMode = MeasureSpec.AT_MOST;
354 } else {
355 childWidthMode = MeasureSpec.EXACTLY;
356 }
357
358 int childHeightMode;
359 if (lp.height == LayoutParams.WRAP_CONTENT) {
360 childHeightMode = MeasureSpec.AT_MOST;
361 } else {
362 childHeightMode = MeasureSpec.EXACTLY;
363 }
364
365 final int childWidthMeasureSpec =
366 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
367 final int childHeightMeasureSpec =
368 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
369
370 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 }
372
373 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 }
375
376 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700377 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700378 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
379 setHorizontalScrollBarEnabled(false);
380 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
381 scrollTo(newX, 0);
382 mScroller.setFinalX(newX);
383 setHorizontalScrollBarEnabled(true);
384 mFirstLayout = false;
385 }
386
Winson Chung321e9ee2010-08-09 13:37:56 -0700387 final int childCount = getChildCount();
388 int childLeft = 0;
389 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700390 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700391 }
392
393 for (int i = 0; i < childCount; i++) {
394 final View child = getChildAt(i);
395 if (child.getVisibility() != View.GONE) {
396 final int childWidth = child.getMeasuredWidth();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700397 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
398 child.layout(childLeft, childHeight,
399 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700400 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 }
402 }
403 }
404
Winson Chunge3193b92010-09-10 11:44:42 -0700405 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700406 if (mFadeInAdjacentScreens) {
407 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700408 int halfScreenSize = getMeasuredWidth() / 2;
409 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700410 final int childCount = getChildCount();
411 for (int i = 0; i < childCount; ++i) {
412 View layout = (View) getChildAt(i);
413 int childWidth = layout.getMeasuredWidth();
414 int halfChildWidth = (childWidth / 2);
415 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700416
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700417 // On the first layout, we may not have a width nor a proper offset, so for now
418 // we should just assume full page width (and calculate the offset according to
419 // that).
420 if (childWidth <= 0) {
421 childWidth = getMeasuredWidth();
422 childCenter = (i * childWidth) + (childWidth / 2);
423 }
424
Winson Chunge8878e32010-09-15 20:37:09 -0700425 int d = halfChildWidth;
426 int distanceFromScreenCenter = childCenter - screenCenter;
427 if (distanceFromScreenCenter > 0) {
428 if (i > 0) {
429 d += getChildAt(i - 1).getMeasuredWidth() / 2;
430 }
Michael Jurka0142d492010-08-25 17:46:15 -0700431 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700432 if (i < childCount - 1) {
433 d += getChildAt(i + 1).getMeasuredWidth() / 2;
434 }
Michael Jurka0142d492010-08-25 17:46:15 -0700435 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700436 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700437
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700438 // Preventing potential divide-by-zero
439 d = Math.max(1, d);
440
Winson Chunge8878e32010-09-15 20:37:09 -0700441 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
442 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
443 float alpha = 1.0f - dimAlpha;
444
Adam Cohenf34bab52010-09-30 14:11:56 -0700445 if (alpha < ALPHA_QUANTIZE_LEVEL) {
446 alpha = 0.0f;
447 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
448 alpha = 1.0f;
449 }
450
Michael Jurka0142d492010-08-25 17:46:15 -0700451 if (Float.compare(alpha, layout.getAlpha()) != 0) {
452 layout.setAlpha(alpha);
453 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
Michael Jurka0142d492010-08-25 17:46:15 -0700455 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 }
457 }
Winson Chunge3193b92010-09-10 11:44:42 -0700458 }
459
Adam Cohenf34bab52010-09-30 14:11:56 -0700460 protected void screenScrolled(int screenCenter) {
461 }
462
Winson Chunge3193b92010-09-10 11:44:42 -0700463 @Override
464 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700465 int halfScreenSize = getMeasuredWidth() / 2;
466 int screenCenter = mScrollX + halfScreenSize;
467
468 if (screenCenter != mLastScreenCenter) {
469 screenScrolled(screenCenter);
470 updateAdjacentPagesAlpha();
471 mLastScreenCenter = screenCenter;
472 }
Michael Jurka0142d492010-08-25 17:46:15 -0700473
474 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700475 // As an optimization, this code assumes that all pages have the same width as the 0th
476 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700477 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700478 if (pageCount > 0) {
479 final int pageWidth = getChildAt(0).getMeasuredWidth();
480 final int screenWidth = getMeasuredWidth();
481 int x = getRelativeChildOffset(0) + pageWidth;
482 int leftScreen = 0;
483 int rightScreen = 0;
484 while (x <= mScrollX) {
485 leftScreen++;
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 // x += getChildAt(leftScreen).getMeasuredWidth();
490 }
491 rightScreen = leftScreen;
492 while (x < mScrollX + screenWidth) {
493 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700494 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700495 // replace above line with this if you don't assume all pages have same width as 0th
496 // page:
497 //if (rightScreen < pageCount) {
498 // x += getChildAt(rightScreen).getMeasuredWidth();
499 //}
500 }
501 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700502
Michael Jurkac4fb9172010-09-02 17:19:20 -0700503 final long drawingTime = getDrawingTime();
504 for (int i = leftScreen; i <= rightScreen; i++) {
505 drawChild(canvas, getChildAt(i), drawingTime);
506 }
Michael Jurka0142d492010-08-25 17:46:15 -0700507 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700508 }
509
510 @Override
511 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700512 int page = indexOfChild(child);
513 if (page != mCurrentPage || !mScroller.isFinished()) {
514 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 return true;
516 }
517 return false;
518 }
519
520 @Override
521 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700522 int focusablePage;
523 if (mNextPage != INVALID_PAGE) {
524 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700525 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700526 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700527 }
Winson Chung86f77532010-08-24 11:08:22 -0700528 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700529 if (v != null) {
530 v.requestFocus(direction, previouslyFocusedRect);
531 }
532 return false;
533 }
534
535 @Override
536 public boolean dispatchUnhandledMove(View focused, int direction) {
537 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700538 if (getCurrentPage() > 0) {
539 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700540 return true;
541 }
542 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700543 if (getCurrentPage() < getPageCount() - 1) {
544 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 return true;
546 }
547 }
548 return super.dispatchUnhandledMove(focused, direction);
549 }
550
551 @Override
552 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700553 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
554 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700555 }
556 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700557 if (mCurrentPage > 0) {
558 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700559 }
560 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700561 if (mCurrentPage < getPageCount() - 1) {
562 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564 }
565 }
566
567 /**
568 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700569 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 *
Winson Chung86f77532010-08-24 11:08:22 -0700571 * This happens when live folders requery, and if they're off page, they
572 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 */
574 @Override
575 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700576 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 View v = focused;
578 while (true) {
579 if (v == current) {
580 super.focusableViewAvailable(focused);
581 return;
582 }
583 if (v == this) {
584 return;
585 }
586 ViewParent parent = v.getParent();
587 if (parent instanceof View) {
588 v = (View)v.getParent();
589 } else {
590 return;
591 }
592 }
593 }
594
595 /**
596 * {@inheritDoc}
597 */
598 @Override
599 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
600 if (disallowIntercept) {
601 // We need to make sure to cancel our long press if
602 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700603 final View currentPage = getChildAt(mCurrentPage);
604 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700605 }
606 super.requestDisallowInterceptTouchEvent(disallowIntercept);
607 }
608
609 @Override
610 public boolean onInterceptTouchEvent(MotionEvent ev) {
611 /*
612 * This method JUST determines whether we want to intercept the motion.
613 * If we return true, onTouchEvent will be called and we do the actual
614 * scrolling there.
615 */
616
617 /*
618 * Shortcut the most recurring case: the user is in the dragging
619 * state and he is moving his finger. We want to intercept this
620 * motion.
621 */
622 final int action = ev.getAction();
623 if ((action == MotionEvent.ACTION_MOVE) &&
624 (mTouchState == TOUCH_STATE_SCROLLING)) {
625 return true;
626 }
627
Winson Chung321e9ee2010-08-09 13:37:56 -0700628 switch (action & MotionEvent.ACTION_MASK) {
629 case MotionEvent.ACTION_MOVE: {
630 /*
631 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
632 * whether the user has moved far enough from his original down touch.
633 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700634 if (mActivePointerId != INVALID_POINTER) {
635 determineScrollingStart(ev);
636 break;
637 }
638 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
639 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
640 // i.e. fall through to the next case (don't break)
641 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
642 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700643 }
644
645 case MotionEvent.ACTION_DOWN: {
646 final float x = ev.getX();
647 final float y = ev.getY();
648 // Remember location of down touch
649 mDownMotionX = x;
650 mLastMotionX = x;
651 mLastMotionY = y;
652 mActivePointerId = ev.getPointerId(0);
653 mAllowLongPress = true;
654
655 /*
656 * If being flinged and user touches the screen, initiate drag;
657 * otherwise don't. mScroller.isFinished should be false when
658 * being flinged.
659 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700660 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700661 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
662 if (finishedScrolling) {
663 mTouchState = TOUCH_STATE_REST;
664 mScroller.abortAnimation();
665 } else {
666 mTouchState = TOUCH_STATE_SCROLLING;
667 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700668
Winson Chung86f77532010-08-24 11:08:22 -0700669 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700670 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700671 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
673 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700674 int width = getMeasuredWidth();
675 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700676 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700678 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 mTouchState = TOUCH_STATE_NEXT_PAGE;
680 }
681 }
682 }
683 break;
684 }
685
686 case MotionEvent.ACTION_CANCEL:
687 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700688 mTouchState = TOUCH_STATE_REST;
689 mAllowLongPress = false;
690 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 break;
692
693 case MotionEvent.ACTION_POINTER_UP:
694 onSecondaryPointerUp(ev);
695 break;
696 }
697
698 /*
699 * The only time we want to intercept motion events is if we are in the
700 * drag mode.
701 */
702 return mTouchState != TOUCH_STATE_REST;
703 }
704
Winson Chung80baf5a2010-08-09 16:03:15 -0700705 protected void animateClickFeedback(View v, final Runnable r) {
706 // animate the view slightly to show click feedback running some logic after it is "pressed"
707 Animation anim = AnimationUtils.loadAnimation(getContext(),
708 R.anim.paged_view_click_feedback);
709 anim.setAnimationListener(new AnimationListener() {
710 @Override
711 public void onAnimationStart(Animation animation) {}
712 @Override
713 public void onAnimationRepeat(Animation animation) {
714 r.run();
715 }
716 @Override
717 public void onAnimationEnd(Animation animation) {}
718 });
719 v.startAnimation(anim);
720 }
721
Winson Chung321e9ee2010-08-09 13:37:56 -0700722 /*
723 * Determines if we should change the touch state to start scrolling after the
724 * user moves their touch point too far.
725 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700726 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700727 /*
728 * Locally do absolute value. mLastMotionX is set to the y value
729 * of the down event.
730 */
731 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
732 final float x = ev.getX(pointerIndex);
733 final float y = ev.getY(pointerIndex);
734 final int xDiff = (int) Math.abs(x - mLastMotionX);
735 final int yDiff = (int) Math.abs(y - mLastMotionY);
736
737 final int touchSlop = mTouchSlop;
738 boolean xPaged = xDiff > mPagingTouchSlop;
739 boolean xMoved = xDiff > touchSlop;
740 boolean yMoved = yDiff > touchSlop;
741
742 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700743 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700744 // Scroll if the user moved far enough along the X axis
745 mTouchState = TOUCH_STATE_SCROLLING;
746 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700747 mTouchX = mScrollX;
748 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
749 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700750 }
751 // Either way, cancel any pending longpress
752 if (mAllowLongPress) {
753 mAllowLongPress = false;
754 // Try canceling the long press. It could also have been scheduled
755 // by a distant descendant, so use the mAllowLongPress flag to block
756 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700757 final View currentPage = getPageAt(mCurrentPage);
758 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700759 }
760 }
761 }
762
Adam Cohen21f12b52010-10-07 17:15:37 -0700763 protected boolean handlePagingClicks() {
764 return false;
765 }
766
Winson Chung321e9ee2010-08-09 13:37:56 -0700767 @Override
768 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700769 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700770
771 final int action = ev.getAction();
772
773 switch (action & MotionEvent.ACTION_MASK) {
774 case MotionEvent.ACTION_DOWN:
775 /*
776 * If being flinged and user touches, stop the fling. isFinished
777 * will be false if being flinged.
778 */
779 if (!mScroller.isFinished()) {
780 mScroller.abortAnimation();
781 }
782
783 // Remember where the motion event started
784 mDownMotionX = mLastMotionX = ev.getX();
785 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700786 if (mTouchState == TOUCH_STATE_SCROLLING) {
787 pageBeginMoving();
788 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700789 break;
790
791 case MotionEvent.ACTION_MOVE:
792 if (mTouchState == TOUCH_STATE_SCROLLING) {
793 // Scroll to follow the motion event
794 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
795 final float x = ev.getX(pointerIndex);
796 final int deltaX = (int) (mLastMotionX - x);
797 mLastMotionX = x;
798
799 int sx = getScrollX();
800 if (deltaX < 0) {
801 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700802 mTouchX += Math.max(-mTouchX, deltaX);
803 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
804 if (!mDeferScrollUpdate) {
805 scrollBy(Math.max(-sx, deltaX), 0);
806 } else {
807 // This will trigger a call to computeScroll() on next drawChild() call
808 invalidate();
809 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700810 }
811 } else if (deltaX > 0) {
812 final int lastChildIndex = getChildCount() - 1;
813 final int availableToScroll = getChildOffset(lastChildIndex) -
814 getRelativeChildOffset(lastChildIndex) - sx;
815 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700816 mTouchX += Math.min(availableToScroll, deltaX);
817 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
818 if (!mDeferScrollUpdate) {
819 scrollBy(Math.min(availableToScroll, deltaX), 0);
820 } else {
821 // This will trigger a call to computeScroll() on next drawChild() call
822 invalidate();
823 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700824 }
825 } else {
826 awakenScrollBars();
827 }
Adam Cohen564976a2010-10-13 18:52:07 -0700828 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 determineScrollingStart(ev);
830 }
831 break;
832
833 case MotionEvent.ACTION_UP:
834 if (mTouchState == TOUCH_STATE_SCROLLING) {
835 final int activePointerId = mActivePointerId;
836 final int pointerIndex = ev.findPointerIndex(activePointerId);
837 final float x = ev.getX(pointerIndex);
838 final VelocityTracker velocityTracker = mVelocityTracker;
839 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
840 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700841 final int deltaX = (int) (x - mDownMotionX);
842 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
843 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700844
Michael Jurka0142d492010-08-25 17:46:15 -0700845 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700846 if ((isSignificantMove && deltaX > 0 ||
847 (isfling && velocityX > snapVelocity)) &&
848 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700849 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700850 } else if ((isSignificantMove && deltaX < 0 ||
851 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700852 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700853 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700854 } else {
855 snapToDestination();
856 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700857 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700858 // at this point we have not moved beyond the touch slop
859 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
860 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700861 int nextPage = Math.max(0, mCurrentPage - 1);
862 if (nextPage != mCurrentPage) {
863 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 } else {
865 snapToDestination();
866 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700867 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700868 // at this point we have not moved beyond the touch slop
869 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
870 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700871 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
872 if (nextPage != mCurrentPage) {
873 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700874 } else {
875 snapToDestination();
876 }
877 }
878 mTouchState = TOUCH_STATE_REST;
879 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700880 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700881 break;
882
883 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700884 if (mTouchState == TOUCH_STATE_SCROLLING) {
885 snapToDestination();
886 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700887 mTouchState = TOUCH_STATE_REST;
888 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700889 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700890 break;
891
892 case MotionEvent.ACTION_POINTER_UP:
893 onSecondaryPointerUp(ev);
894 break;
895 }
896
897 return true;
898 }
899
Michael Jurkab8f06722010-10-10 15:58:46 -0700900 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
901 if (mVelocityTracker == null) {
902 mVelocityTracker = VelocityTracker.obtain();
903 }
904 mVelocityTracker.addMovement(ev);
905 }
906
907 private void releaseVelocityTracker() {
908 if (mVelocityTracker != null) {
909 mVelocityTracker.recycle();
910 mVelocityTracker = null;
911 }
912 }
913
Winson Chung321e9ee2010-08-09 13:37:56 -0700914 private void onSecondaryPointerUp(MotionEvent ev) {
915 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
916 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
917 final int pointerId = ev.getPointerId(pointerIndex);
918 if (pointerId == mActivePointerId) {
919 // This was our active pointer going up. Choose a new
920 // active pointer and adjust accordingly.
921 // TODO: Make this decision more intelligent.
922 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
923 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
924 mLastMotionY = ev.getY(newPointerIndex);
925 mActivePointerId = ev.getPointerId(newPointerIndex);
926 if (mVelocityTracker != null) {
927 mVelocityTracker.clear();
928 }
929 }
930 }
931
932 @Override
933 public void requestChildFocus(View child, View focused) {
934 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700935 int page = indexOfChild(child);
936 if (page >= 0 && !isInTouchMode()) {
937 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700938 }
939 }
940
Winson Chunge3193b92010-09-10 11:44:42 -0700941 protected int getChildIndexForRelativeOffset(int relativeOffset) {
942 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700943 int left;
944 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700945 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700946 left = getRelativeChildOffset(i);
947 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700948 if (left <= relativeOffset && relativeOffset <= right) {
949 return i;
950 }
Winson Chunge3193b92010-09-10 11:44:42 -0700951 }
952 return -1;
953 }
954
Winson Chung321e9ee2010-08-09 13:37:56 -0700955 protected int getRelativeChildOffset(int index) {
956 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
957 }
958
959 protected int getChildOffset(int index) {
960 if (getChildCount() == 0)
961 return 0;
962
963 int offset = getRelativeChildOffset(0);
964 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700965 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700966 }
967 return offset;
968 }
969
Adam Cohend19d3ca2010-09-15 14:43:42 -0700970 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 int minDistanceFromScreenCenter = getMeasuredWidth();
972 int minDistanceFromScreenCenterIndex = -1;
973 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
974 final int childCount = getChildCount();
975 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700976 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700977 int childWidth = layout.getMeasuredWidth();
978 int halfChildWidth = (childWidth / 2);
979 int childCenter = getChildOffset(i) + halfChildWidth;
980 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
981 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
982 minDistanceFromScreenCenter = distanceFromScreenCenter;
983 minDistanceFromScreenCenterIndex = i;
984 }
985 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700986 return minDistanceFromScreenCenterIndex;
987 }
988
989 protected void snapToDestination() {
990 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700991 }
992
Michael Jurka0142d492010-08-25 17:46:15 -0700993 protected void snapToPageWithVelocity(int whichPage, int velocity) {
994 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
995 // can use it
996 snapToPage(whichPage);
997 }
998
999 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001000 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001001 }
1002
Michael Jurka0142d492010-08-25 17:46:15 -07001003 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001004 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001005
Winson Chung86f77532010-08-24 11:08:22 -07001006 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001007 final int sX = getScrollX();
1008 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001009 snapToPage(whichPage, delta, duration);
1010 }
1011
1012 protected void snapToPage(int whichPage, int delta, int duration) {
1013 mNextPage = whichPage;
1014
1015 View focusedChild = getFocusedChild();
1016 if (focusedChild != null && whichPage != mCurrentPage &&
1017 focusedChild == getChildAt(mCurrentPage)) {
1018 focusedChild.clearFocus();
1019 }
1020
1021 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001022 awakenScrollBars(duration);
1023 if (duration == 0) {
1024 duration = Math.abs(delta);
1025 }
1026
1027 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001028 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001029
1030 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001031 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001032 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001033 invalidate();
1034 }
1035
1036 @Override
1037 protected Parcelable onSaveInstanceState() {
1038 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001039 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001040 return state;
1041 }
1042
1043 @Override
1044 protected void onRestoreInstanceState(Parcelable state) {
1045 SavedState savedState = (SavedState) state;
1046 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001047 if (savedState.currentPage != -1) {
1048 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 }
1050 }
1051
1052 public void scrollLeft() {
1053 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001054 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001055 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001056 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001057 }
1058 }
1059
1060 public void scrollRight() {
1061 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001062 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001063 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001064 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001065 }
1066 }
1067
Winson Chung86f77532010-08-24 11:08:22 -07001068 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001069 int result = -1;
1070 if (v != null) {
1071 ViewParent vp = v.getParent();
1072 int count = getChildCount();
1073 for (int i = 0; i < count; i++) {
1074 if (vp == getChildAt(i)) {
1075 return i;
1076 }
1077 }
1078 }
1079 return result;
1080 }
1081
1082 /**
1083 * @return True is long presses are still allowed for the current touch
1084 */
1085 public boolean allowLongPress() {
1086 return mAllowLongPress;
1087 }
1088
Michael Jurka0142d492010-08-25 17:46:15 -07001089 /**
1090 * Set true to allow long-press events to be triggered, usually checked by
1091 * {@link Launcher} to accept or block dpad-initiated long-presses.
1092 */
1093 public void setAllowLongPress(boolean allowLongPress) {
1094 mAllowLongPress = allowLongPress;
1095 }
1096
Winson Chung321e9ee2010-08-09 13:37:56 -07001097 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001098 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001099
1100 SavedState(Parcelable superState) {
1101 super(superState);
1102 }
1103
1104 private SavedState(Parcel in) {
1105 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001106 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001107 }
1108
1109 @Override
1110 public void writeToParcel(Parcel out, int flags) {
1111 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001112 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001113 }
1114
1115 public static final Parcelable.Creator<SavedState> CREATOR =
1116 new Parcelable.Creator<SavedState>() {
1117 public SavedState createFromParcel(Parcel in) {
1118 return new SavedState(in);
1119 }
1120
1121 public SavedState[] newArray(int size) {
1122 return new SavedState[size];
1123 }
1124 };
1125 }
1126
Winson Chung86f77532010-08-24 11:08:22 -07001127 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001128 if (mContentIsRefreshable) {
1129 final int count = getChildCount();
1130 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001131 int lowerPageBound = getAssociatedLowerPageBound(page);
1132 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001133 for (int i = 0; i < count; ++i) {
1134 final ViewGroup layout = (ViewGroup) getChildAt(i);
1135 final int childCount = layout.getChildCount();
1136 if (lowerPageBound <= i && i <= upperPageBound) {
1137 if (mDirtyPageContent.get(i)) {
1138 syncPageItems(i);
1139 mDirtyPageContent.set(i, false);
1140 }
1141 } else {
1142 if (childCount > 0) {
1143 layout.removeAllViews();
1144 }
1145 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001146 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001147 }
1148 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001149 }
1150 }
1151
Winson Chunge3193b92010-09-10 11:44:42 -07001152 protected int getAssociatedLowerPageBound(int page) {
1153 return Math.max(0, page - 1);
1154 }
1155 protected int getAssociatedUpperPageBound(int page) {
1156 final int count = getChildCount();
1157 return Math.min(page + 1, count - 1);
1158 }
1159
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001160 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001161 if (isChoiceMode(CHOICE_MODE_NONE)) {
1162 mChoiceMode = mode;
1163 mActionMode = startActionMode(callback);
1164 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001165 }
1166
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001167 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001168 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001169 mChoiceMode = CHOICE_MODE_NONE;
1170 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001171 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001172 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001173 }
1174 }
1175
1176 protected boolean isChoiceMode(int mode) {
1177 return mChoiceMode == mode;
1178 }
1179
1180 protected ArrayList<Checkable> getCheckedGrandchildren() {
1181 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1182 final int childCount = getChildCount();
1183 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001184 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001185 final int grandChildCount = layout.getChildCount();
1186 for (int j = 0; j < grandChildCount; ++j) {
1187 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001188 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001189 checked.add((Checkable) v);
1190 }
1191 }
1192 }
1193 return checked;
1194 }
1195
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001196 /**
1197 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1198 * Otherwise, returns null.
1199 */
1200 protected Checkable getSingleCheckedGrandchild() {
1201 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1202 final int childCount = getChildCount();
1203 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001204 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001205 final int grandChildCount = layout.getChildCount();
1206 for (int j = 0; j < grandChildCount; ++j) {
1207 final View v = layout.getChildAt(j);
1208 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1209 return (Checkable) v;
1210 }
1211 }
1212 }
1213 }
1214 return null;
1215 }
1216
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001217 public Object getChosenItem() {
1218 View checkedView = (View) getSingleCheckedGrandchild();
1219 if (checkedView != null) {
1220 return checkedView.getTag();
1221 }
1222 return null;
1223 }
1224
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001225 protected void resetCheckedGrandchildren() {
1226 // loop through children, and set all of their children to _not_ be checked
1227 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1228 for (int i = 0; i < checked.size(); ++i) {
1229 final Checkable c = checked.get(i);
1230 c.setChecked(false);
1231 }
1232 }
1233
Winson Chung86f77532010-08-24 11:08:22 -07001234 /**
1235 * This method is called ONLY to synchronize the number of pages that the paged view has.
1236 * To actually fill the pages with information, implement syncPageItems() below. It is
1237 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1238 * and therefore, individual page items do not need to be updated in this method.
1239 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001240 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001241
1242 /**
1243 * This method is called to synchronize the items that are on a particular page. If views on
1244 * the page can be reused, then they should be updated within this method.
1245 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001246 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001247
Winson Chung321e9ee2010-08-09 13:37:56 -07001248 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001249 if (mContentIsRefreshable) {
1250 // Update all the pages
1251 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001252
Michael Jurka0142d492010-08-25 17:46:15 -07001253 // Mark each of the pages as dirty
1254 final int count = getChildCount();
1255 mDirtyPageContent.clear();
1256 for (int i = 0; i < count; ++i) {
1257 mDirtyPageContent.add(true);
1258 }
1259
1260 // Load any pages that are necessary for the current window of views
1261 loadAssociatedPages(mCurrentPage);
1262 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001263 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001264 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001265 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001266 }
1267}