blob: e9829fbfa3d477401629713442ab045d2907e514 [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 Chung7da10252010-10-28 16:07:04 -0700105 protected boolean mCenterPagesVertically;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106
Michael Jurka5f1c5092010-09-03 14:15:02 -0700107 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Michael Jurka5f1c5092010-09-03 14:15:02 -0700109 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700110
Winson Chung86f77532010-08-24 11:08:22 -0700111 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700112
Winson Chung86f77532010-08-24 11:08:22 -0700113 private ArrayList<Boolean> mDirtyPageContent;
114 private boolean mDirtyPageAlpha;
Winson Chung321e9ee2010-08-09 13:37:56 -0700115
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700116 // choice modes
117 protected static final int CHOICE_MODE_NONE = 0;
118 protected static final int CHOICE_MODE_SINGLE = 1;
119 // Multiple selection mode is not supported by all Launcher actions atm
120 protected static final int CHOICE_MODE_MULTIPLE = 2;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700121
Michael Jurkae17e19c2010-09-28 11:01:39 -0700122 protected int mChoiceMode;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700123 private ActionMode mActionMode;
124
Winson Chung241c3b42010-08-25 16:53:03 -0700125 protected PagedViewIconCache mPageViewIconCache;
126
Michael Jurka0142d492010-08-25 17:46:15 -0700127 // If true, syncPages and syncPageItems will be called to refresh pages
128 protected boolean mContentIsRefreshable = true;
129
130 // If true, modify alpha of neighboring pages as user scrolls left/right
131 protected boolean mFadeInAdjacentScreens = true;
132
133 // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding
134 // to switch to a new page
135 protected boolean mUsePagingTouchSlop = true;
136
137 // If true, the subclass should directly update mScrollX itself in its computeScroll method
138 // (SmoothPagedView does this)
139 protected boolean mDeferScrollUpdate = false;
140
Patrick Dubroy1262e362010-10-06 15:49:50 -0700141 protected boolean mIsPageMoving = false;
142
Winson Chung241c3b42010-08-25 16:53:03 -0700143 /**
144 * Simple cache mechanism for PagedViewIcon outlines.
145 */
146 class PagedViewIconCache {
147 private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>();
148
149 public void clear() {
150 iconOutlineCache.clear();
151 }
152 public void addOutline(Object key, Bitmap b) {
153 iconOutlineCache.put(key, b);
154 }
155 public void removeOutline(Object key) {
156 if (iconOutlineCache.containsKey(key)) {
157 iconOutlineCache.remove(key);
158 }
159 }
160 public Bitmap getOutline(Object key) {
161 return iconOutlineCache.get(key);
162 }
163 }
164
Winson Chung86f77532010-08-24 11:08:22 -0700165 public interface PageSwitchListener {
166 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 }
168
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 public PagedView(Context context) {
170 this(context, null);
171 }
172
173 public PagedView(Context context, AttributeSet attrs) {
174 this(context, attrs, 0);
175 }
176
177 public PagedView(Context context, AttributeSet attrs, int defStyle) {
178 super(context, attrs, defStyle);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 mChoiceMode = CHOICE_MODE_NONE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700180
Adam Cohen9c4949e2010-10-05 12:27:22 -0700181 TypedArray a = context.obtainStyledAttributes(attrs,
182 R.styleable.PagedView, defStyle, 0);
183 mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
184 mPageLayoutPaddingTop = a.getDimensionPixelSize(
185 R.styleable.PagedView_pageLayoutPaddingTop, 10);
186 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
187 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
188 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
189 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
190 mPageLayoutPaddingRight = a.getDimensionPixelSize(
191 R.styleable.PagedView_pageLayoutPaddingRight, 10);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700192 mPageLayoutWidthGap = a.getDimensionPixelSize(
193 R.styleable.PagedView_pageLayoutWidthGap, -1);
194 mPageLayoutHeightGap = a.getDimensionPixelSize(
195 R.styleable.PagedView_pageLayoutHeightGap, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700196 a.recycle();
197
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700199 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
202 /**
203 * Initializes various states for this workspace.
204 */
Michael Jurka0142d492010-08-25 17:46:15 -0700205 protected void init() {
Winson Chung86f77532010-08-24 11:08:22 -0700206 mDirtyPageContent = new ArrayList<Boolean>();
207 mDirtyPageContent.ensureCapacity(32);
Winson Chung241c3b42010-08-25 16:53:03 -0700208 mPageViewIconCache = new PagedViewIconCache();
Winson Chung321e9ee2010-08-09 13:37:56 -0700209 mScroller = new Scroller(getContext());
Winson Chung86f77532010-08-24 11:08:22 -0700210 mCurrentPage = 0;
Winson Chung7da10252010-10-28 16:07:04 -0700211 mCenterPagesVertically = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mPagingTouchSlop = configuration.getScaledPagingTouchSlop();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
217 }
218
Winson Chung86f77532010-08-24 11:08:22 -0700219 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
220 mPageSwitchListener = pageSwitchListener;
221 if (mPageSwitchListener != null) {
222 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224 }
225
226 /**
Winson Chung86f77532010-08-24 11:08:22 -0700227 * Returns the index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 *
Winson Chung86f77532010-08-24 11:08:22 -0700229 * @return The index of the currently displayed page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 */
Winson Chung86f77532010-08-24 11:08:22 -0700231 int getCurrentPage() {
232 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 }
234
Winson Chung86f77532010-08-24 11:08:22 -0700235 int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 return getChildCount();
237 }
238
Winson Chung86f77532010-08-24 11:08:22 -0700239 View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 return getChildAt(index);
241 }
242
243 int getScrollWidth() {
244 return getWidth();
245 }
246
247 /**
Winson Chung86f77532010-08-24 11:08:22 -0700248 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 */
Winson Chung86f77532010-08-24 11:08:22 -0700250 void setCurrentPage(int currentPage) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 if (!mScroller.isFinished()) mScroller.abortAnimation();
252 if (getChildCount() == 0) return;
253
Winson Chung86f77532010-08-24 11:08:22 -0700254 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700255 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
256 scrollTo(newX, 0);
257 mScroller.setFinalX(newX);
Winson Chung80baf5a2010-08-09 16:03:15 -0700258
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700260 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700261 }
262
Michael Jurka0142d492010-08-25 17:46:15 -0700263 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700264 if (mPageSwitchListener != null) {
265 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 }
267 }
268
Patrick Dubroy1262e362010-10-06 15:49:50 -0700269 private void pageBeginMoving() {
270 mIsPageMoving = true;
271 onPageBeginMoving();
272 }
273
274 private void pageEndMoving() {
275 onPageEndMoving();
276 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700277 }
278
279 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700280 protected void onPageBeginMoving() {
281 }
282
283 // a method that subclasses can override to add behavior
284 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700285 }
286
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 /**
Winson Chung86f77532010-08-24 11:08:22 -0700288 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 *
290 * @param l The listener used to respond to long clicks.
291 */
292 @Override
293 public void setOnLongClickListener(OnLongClickListener l) {
294 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700295 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700297 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 }
299 }
300
301 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700302 public void scrollTo(int x, int y) {
303 super.scrollTo(x, y);
304 mTouchX = x;
305 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
306 }
307
308 // we moved this functionality to a helper function so SmoothPagedView can reuse it
309 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700310 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700311 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700312 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700313 invalidate();
314 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700315 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700316 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700317 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700318 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700319 notifyPageSwitchListener();
320 pageEndMoving();
321 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700322 }
Michael Jurka0142d492010-08-25 17:46:15 -0700323 return false;
324 }
325
326 @Override
327 public void computeScroll() {
328 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 }
330
331 @Override
332 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
333 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
334 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
335 if (widthMode != MeasureSpec.EXACTLY) {
336 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
337 }
338
339 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
340 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
341 if (heightMode != MeasureSpec.EXACTLY) {
342 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
343 }
344
345 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700346 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 final int childCount = getChildCount();
348 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700349 // disallowing padding in paged view (just pass 0)
350 final View child = getChildAt(i);
351 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
352
353 int childWidthMode;
354 if (lp.width == LayoutParams.WRAP_CONTENT) {
355 childWidthMode = MeasureSpec.AT_MOST;
356 } else {
357 childWidthMode = MeasureSpec.EXACTLY;
358 }
359
360 int childHeightMode;
361 if (lp.height == LayoutParams.WRAP_CONTENT) {
362 childHeightMode = MeasureSpec.AT_MOST;
363 } else {
364 childHeightMode = MeasureSpec.EXACTLY;
365 }
366
367 final int childWidthMeasureSpec =
368 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
369 final int childHeightMeasureSpec =
370 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
371
372 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700373 }
374
375 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 }
377
378 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700379 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700380 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
381 setHorizontalScrollBarEnabled(false);
382 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
383 scrollTo(newX, 0);
384 mScroller.setFinalX(newX);
385 setHorizontalScrollBarEnabled(true);
386 mFirstLayout = false;
387 }
388
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 final int childCount = getChildCount();
390 int childLeft = 0;
391 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700392 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 }
394
395 for (int i = 0; i < childCount; i++) {
396 final View child = getChildAt(i);
397 if (child.getVisibility() != View.GONE) {
398 final int childWidth = child.getMeasuredWidth();
Winson Chung7da10252010-10-28 16:07:04 -0700399 final int childHeight = (mCenterPagesVertically ?
400 (getMeasuredHeight() - child.getMeasuredHeight()) / 2 : 0);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700401 child.layout(childLeft, childHeight,
402 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700403 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 }
405 }
406 }
407
Winson Chunge3193b92010-09-10 11:44:42 -0700408 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700409 if (mFadeInAdjacentScreens) {
410 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700411 int halfScreenSize = getMeasuredWidth() / 2;
412 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700413 final int childCount = getChildCount();
414 for (int i = 0; i < childCount; ++i) {
415 View layout = (View) getChildAt(i);
416 int childWidth = layout.getMeasuredWidth();
417 int halfChildWidth = (childWidth / 2);
418 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700419
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700420 // On the first layout, we may not have a width nor a proper offset, so for now
421 // we should just assume full page width (and calculate the offset according to
422 // that).
423 if (childWidth <= 0) {
424 childWidth = getMeasuredWidth();
425 childCenter = (i * childWidth) + (childWidth / 2);
426 }
427
Winson Chunge8878e32010-09-15 20:37:09 -0700428 int d = halfChildWidth;
429 int distanceFromScreenCenter = childCenter - screenCenter;
430 if (distanceFromScreenCenter > 0) {
431 if (i > 0) {
432 d += getChildAt(i - 1).getMeasuredWidth() / 2;
433 }
Michael Jurka0142d492010-08-25 17:46:15 -0700434 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700435 if (i < childCount - 1) {
436 d += getChildAt(i + 1).getMeasuredWidth() / 2;
437 }
Michael Jurka0142d492010-08-25 17:46:15 -0700438 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700439 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700440
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700441 // Preventing potential divide-by-zero
442 d = Math.max(1, d);
443
Winson Chunge8878e32010-09-15 20:37:09 -0700444 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
445 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
446 float alpha = 1.0f - dimAlpha;
447
Adam Cohenf34bab52010-09-30 14:11:56 -0700448 if (alpha < ALPHA_QUANTIZE_LEVEL) {
449 alpha = 0.0f;
450 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
451 alpha = 1.0f;
452 }
453
Michael Jurka0142d492010-08-25 17:46:15 -0700454 if (Float.compare(alpha, layout.getAlpha()) != 0) {
455 layout.setAlpha(alpha);
456 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700457 }
Michael Jurka0142d492010-08-25 17:46:15 -0700458 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 }
460 }
Winson Chunge3193b92010-09-10 11:44:42 -0700461 }
462
Adam Cohenf34bab52010-09-30 14:11:56 -0700463 protected void screenScrolled(int screenCenter) {
464 }
465
Winson Chunge3193b92010-09-10 11:44:42 -0700466 @Override
467 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700468 int halfScreenSize = getMeasuredWidth() / 2;
469 int screenCenter = mScrollX + halfScreenSize;
470
471 if (screenCenter != mLastScreenCenter) {
472 screenScrolled(screenCenter);
473 updateAdjacentPagesAlpha();
474 mLastScreenCenter = screenCenter;
475 }
Michael Jurka0142d492010-08-25 17:46:15 -0700476
477 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700478 // As an optimization, this code assumes that all pages have the same width as the 0th
479 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700480 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700481 if (pageCount > 0) {
482 final int pageWidth = getChildAt(0).getMeasuredWidth();
483 final int screenWidth = getMeasuredWidth();
484 int x = getRelativeChildOffset(0) + pageWidth;
485 int leftScreen = 0;
486 int rightScreen = 0;
487 while (x <= mScrollX) {
488 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700489 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700490 // replace above line with this if you don't assume all pages have same width as 0th
491 // page:
492 // x += getChildAt(leftScreen).getMeasuredWidth();
493 }
494 rightScreen = leftScreen;
495 while (x < mScrollX + screenWidth) {
496 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700497 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700498 // replace above line with this if you don't assume all pages have same width as 0th
499 // page:
500 //if (rightScreen < pageCount) {
501 // x += getChildAt(rightScreen).getMeasuredWidth();
502 //}
503 }
504 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700505
Michael Jurkac4fb9172010-09-02 17:19:20 -0700506 final long drawingTime = getDrawingTime();
507 for (int i = leftScreen; i <= rightScreen; i++) {
508 drawChild(canvas, getChildAt(i), drawingTime);
509 }
Michael Jurka0142d492010-08-25 17:46:15 -0700510 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700511 }
512
513 @Override
514 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700515 int page = indexOfChild(child);
516 if (page != mCurrentPage || !mScroller.isFinished()) {
517 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700518 return true;
519 }
520 return false;
521 }
522
523 @Override
524 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700525 int focusablePage;
526 if (mNextPage != INVALID_PAGE) {
527 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700528 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700529 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 }
Winson Chung86f77532010-08-24 11:08:22 -0700531 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 if (v != null) {
533 v.requestFocus(direction, previouslyFocusedRect);
534 }
535 return false;
536 }
537
538 @Override
539 public boolean dispatchUnhandledMove(View focused, int direction) {
540 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700541 if (getCurrentPage() > 0) {
542 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 return true;
544 }
545 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700546 if (getCurrentPage() < getPageCount() - 1) {
547 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700548 return true;
549 }
550 }
551 return super.dispatchUnhandledMove(focused, direction);
552 }
553
554 @Override
555 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700556 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
557 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 }
559 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700560 if (mCurrentPage > 0) {
561 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
563 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700564 if (mCurrentPage < getPageCount() - 1) {
565 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 }
567 }
568 }
569
570 /**
571 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700572 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700573 *
Winson Chung86f77532010-08-24 11:08:22 -0700574 * This happens when live folders requery, and if they're off page, they
575 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700576 */
577 @Override
578 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700579 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 View v = focused;
581 while (true) {
582 if (v == current) {
583 super.focusableViewAvailable(focused);
584 return;
585 }
586 if (v == this) {
587 return;
588 }
589 ViewParent parent = v.getParent();
590 if (parent instanceof View) {
591 v = (View)v.getParent();
592 } else {
593 return;
594 }
595 }
596 }
597
598 /**
599 * {@inheritDoc}
600 */
601 @Override
602 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
603 if (disallowIntercept) {
604 // We need to make sure to cancel our long press if
605 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700606 final View currentPage = getChildAt(mCurrentPage);
607 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700608 }
609 super.requestDisallowInterceptTouchEvent(disallowIntercept);
610 }
611
612 @Override
613 public boolean onInterceptTouchEvent(MotionEvent ev) {
614 /*
615 * This method JUST determines whether we want to intercept the motion.
616 * If we return true, onTouchEvent will be called and we do the actual
617 * scrolling there.
618 */
619
620 /*
621 * Shortcut the most recurring case: the user is in the dragging
622 * state and he is moving his finger. We want to intercept this
623 * motion.
624 */
625 final int action = ev.getAction();
626 if ((action == MotionEvent.ACTION_MOVE) &&
627 (mTouchState == TOUCH_STATE_SCROLLING)) {
628 return true;
629 }
630
Winson Chung321e9ee2010-08-09 13:37:56 -0700631 switch (action & MotionEvent.ACTION_MASK) {
632 case MotionEvent.ACTION_MOVE: {
633 /*
634 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
635 * whether the user has moved far enough from his original down touch.
636 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700637 if (mActivePointerId != INVALID_POINTER) {
638 determineScrollingStart(ev);
639 break;
640 }
641 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
642 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
643 // i.e. fall through to the next case (don't break)
644 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
645 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700646 }
647
648 case MotionEvent.ACTION_DOWN: {
649 final float x = ev.getX();
650 final float y = ev.getY();
651 // Remember location of down touch
652 mDownMotionX = x;
653 mLastMotionX = x;
654 mLastMotionY = y;
655 mActivePointerId = ev.getPointerId(0);
656 mAllowLongPress = true;
657
658 /*
659 * If being flinged and user touches the screen, initiate drag;
660 * otherwise don't. mScroller.isFinished should be false when
661 * being flinged.
662 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700663 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700664 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
665 if (finishedScrolling) {
666 mTouchState = TOUCH_STATE_REST;
667 mScroller.abortAnimation();
668 } else {
669 mTouchState = TOUCH_STATE_SCROLLING;
670 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700671
Winson Chung86f77532010-08-24 11:08:22 -0700672 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700673 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700674 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700675 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
676 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700677 int width = getMeasuredWidth();
678 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700679 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700680 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700681 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700682 mTouchState = TOUCH_STATE_NEXT_PAGE;
683 }
684 }
685 }
686 break;
687 }
688
689 case MotionEvent.ACTION_CANCEL:
690 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700691 mTouchState = TOUCH_STATE_REST;
692 mAllowLongPress = false;
693 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700694 break;
695
696 case MotionEvent.ACTION_POINTER_UP:
697 onSecondaryPointerUp(ev);
698 break;
699 }
700
701 /*
702 * The only time we want to intercept motion events is if we are in the
703 * drag mode.
704 */
705 return mTouchState != TOUCH_STATE_REST;
706 }
707
Winson Chung80baf5a2010-08-09 16:03:15 -0700708 protected void animateClickFeedback(View v, final Runnable r) {
709 // animate the view slightly to show click feedback running some logic after it is "pressed"
710 Animation anim = AnimationUtils.loadAnimation(getContext(),
711 R.anim.paged_view_click_feedback);
712 anim.setAnimationListener(new AnimationListener() {
713 @Override
714 public void onAnimationStart(Animation animation) {}
715 @Override
716 public void onAnimationRepeat(Animation animation) {
717 r.run();
718 }
719 @Override
720 public void onAnimationEnd(Animation animation) {}
721 });
722 v.startAnimation(anim);
723 }
724
Winson Chung321e9ee2010-08-09 13:37:56 -0700725 /*
726 * Determines if we should change the touch state to start scrolling after the
727 * user moves their touch point too far.
728 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700729 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700730 /*
731 * Locally do absolute value. mLastMotionX is set to the y value
732 * of the down event.
733 */
734 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
735 final float x = ev.getX(pointerIndex);
736 final float y = ev.getY(pointerIndex);
737 final int xDiff = (int) Math.abs(x - mLastMotionX);
738 final int yDiff = (int) Math.abs(y - mLastMotionY);
739
740 final int touchSlop = mTouchSlop;
741 boolean xPaged = xDiff > mPagingTouchSlop;
742 boolean xMoved = xDiff > touchSlop;
743 boolean yMoved = yDiff > touchSlop;
744
745 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700746 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700747 // Scroll if the user moved far enough along the X axis
748 mTouchState = TOUCH_STATE_SCROLLING;
749 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700750 mTouchX = mScrollX;
751 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
752 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700753 }
754 // Either way, cancel any pending longpress
755 if (mAllowLongPress) {
756 mAllowLongPress = false;
757 // Try canceling the long press. It could also have been scheduled
758 // by a distant descendant, so use the mAllowLongPress flag to block
759 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700760 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700761 if (currentPage != null) {
762 currentPage.cancelLongPress();
763 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700764 }
765 }
766 }
767
Adam Cohen21f12b52010-10-07 17:15:37 -0700768 protected boolean handlePagingClicks() {
769 return false;
770 }
771
Winson Chung321e9ee2010-08-09 13:37:56 -0700772 @Override
773 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700774 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700775
776 final int action = ev.getAction();
777
778 switch (action & MotionEvent.ACTION_MASK) {
779 case MotionEvent.ACTION_DOWN:
780 /*
781 * If being flinged and user touches, stop the fling. isFinished
782 * will be false if being flinged.
783 */
784 if (!mScroller.isFinished()) {
785 mScroller.abortAnimation();
786 }
787
788 // Remember where the motion event started
789 mDownMotionX = mLastMotionX = ev.getX();
790 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700791 if (mTouchState == TOUCH_STATE_SCROLLING) {
792 pageBeginMoving();
793 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700794 break;
795
796 case MotionEvent.ACTION_MOVE:
797 if (mTouchState == TOUCH_STATE_SCROLLING) {
798 // Scroll to follow the motion event
799 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
800 final float x = ev.getX(pointerIndex);
801 final int deltaX = (int) (mLastMotionX - x);
802 mLastMotionX = x;
803
804 int sx = getScrollX();
805 if (deltaX < 0) {
806 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700807 mTouchX += Math.max(-mTouchX, deltaX);
808 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
809 if (!mDeferScrollUpdate) {
810 scrollBy(Math.max(-sx, deltaX), 0);
811 } else {
812 // This will trigger a call to computeScroll() on next drawChild() call
813 invalidate();
814 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700815 }
816 } else if (deltaX > 0) {
817 final int lastChildIndex = getChildCount() - 1;
818 final int availableToScroll = getChildOffset(lastChildIndex) -
819 getRelativeChildOffset(lastChildIndex) - sx;
820 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700821 mTouchX += Math.min(availableToScroll, deltaX);
822 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
823 if (!mDeferScrollUpdate) {
824 scrollBy(Math.min(availableToScroll, deltaX), 0);
825 } else {
826 // This will trigger a call to computeScroll() on next drawChild() call
827 invalidate();
828 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700829 }
830 } else {
831 awakenScrollBars();
832 }
Adam Cohen564976a2010-10-13 18:52:07 -0700833 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700834 determineScrollingStart(ev);
835 }
836 break;
837
838 case MotionEvent.ACTION_UP:
839 if (mTouchState == TOUCH_STATE_SCROLLING) {
840 final int activePointerId = mActivePointerId;
841 final int pointerIndex = ev.findPointerIndex(activePointerId);
842 final float x = ev.getX(pointerIndex);
843 final VelocityTracker velocityTracker = mVelocityTracker;
844 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
845 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700846 final int deltaX = (int) (x - mDownMotionX);
847 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
848 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700849
Michael Jurka0142d492010-08-25 17:46:15 -0700850 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700851 if ((isSignificantMove && deltaX > 0 ||
852 (isfling && velocityX > snapVelocity)) &&
853 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700854 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700855 } else if ((isSignificantMove && deltaX < 0 ||
856 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700857 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700858 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700859 } else {
860 snapToDestination();
861 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700862 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 // at this point we have not moved beyond the touch slop
864 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
865 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700866 int nextPage = Math.max(0, mCurrentPage - 1);
867 if (nextPage != mCurrentPage) {
868 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700869 } else {
870 snapToDestination();
871 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700872 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 // at this point we have not moved beyond the touch slop
874 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
875 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700876 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
877 if (nextPage != mCurrentPage) {
878 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700879 } else {
880 snapToDestination();
881 }
882 }
883 mTouchState = TOUCH_STATE_REST;
884 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700885 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700886 break;
887
888 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700889 if (mTouchState == TOUCH_STATE_SCROLLING) {
890 snapToDestination();
891 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700892 mTouchState = TOUCH_STATE_REST;
893 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700894 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700895 break;
896
897 case MotionEvent.ACTION_POINTER_UP:
898 onSecondaryPointerUp(ev);
899 break;
900 }
901
902 return true;
903 }
904
Michael Jurkab8f06722010-10-10 15:58:46 -0700905 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
906 if (mVelocityTracker == null) {
907 mVelocityTracker = VelocityTracker.obtain();
908 }
909 mVelocityTracker.addMovement(ev);
910 }
911
912 private void releaseVelocityTracker() {
913 if (mVelocityTracker != null) {
914 mVelocityTracker.recycle();
915 mVelocityTracker = null;
916 }
917 }
918
Winson Chung321e9ee2010-08-09 13:37:56 -0700919 private void onSecondaryPointerUp(MotionEvent ev) {
920 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
921 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
922 final int pointerId = ev.getPointerId(pointerIndex);
923 if (pointerId == mActivePointerId) {
924 // This was our active pointer going up. Choose a new
925 // active pointer and adjust accordingly.
926 // TODO: Make this decision more intelligent.
927 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
928 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
929 mLastMotionY = ev.getY(newPointerIndex);
930 mActivePointerId = ev.getPointerId(newPointerIndex);
931 if (mVelocityTracker != null) {
932 mVelocityTracker.clear();
933 }
934 }
935 }
936
937 @Override
938 public void requestChildFocus(View child, View focused) {
939 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700940 int page = indexOfChild(child);
941 if (page >= 0 && !isInTouchMode()) {
942 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700943 }
944 }
945
Winson Chunge3193b92010-09-10 11:44:42 -0700946 protected int getChildIndexForRelativeOffset(int relativeOffset) {
947 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700948 int left;
949 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700950 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700951 left = getRelativeChildOffset(i);
952 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700953 if (left <= relativeOffset && relativeOffset <= right) {
954 return i;
955 }
Winson Chunge3193b92010-09-10 11:44:42 -0700956 }
957 return -1;
958 }
959
Winson Chung321e9ee2010-08-09 13:37:56 -0700960 protected int getRelativeChildOffset(int index) {
961 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
962 }
963
964 protected int getChildOffset(int index) {
965 if (getChildCount() == 0)
966 return 0;
967
968 int offset = getRelativeChildOffset(0);
969 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700970 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700971 }
972 return offset;
973 }
974
Adam Cohend19d3ca2010-09-15 14:43:42 -0700975 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700976 int minDistanceFromScreenCenter = getMeasuredWidth();
977 int minDistanceFromScreenCenterIndex = -1;
978 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
979 final int childCount = getChildCount();
980 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700981 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700982 int childWidth = layout.getMeasuredWidth();
983 int halfChildWidth = (childWidth / 2);
984 int childCenter = getChildOffset(i) + halfChildWidth;
985 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
986 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
987 minDistanceFromScreenCenter = distanceFromScreenCenter;
988 minDistanceFromScreenCenterIndex = i;
989 }
990 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700991 return minDistanceFromScreenCenterIndex;
992 }
993
994 protected void snapToDestination() {
995 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -0700996 }
997
Michael Jurka0142d492010-08-25 17:46:15 -0700998 protected void snapToPageWithVelocity(int whichPage, int velocity) {
999 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
1000 // can use it
1001 snapToPage(whichPage);
1002 }
1003
1004 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001005 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001006 }
1007
Michael Jurka0142d492010-08-25 17:46:15 -07001008 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001009 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001010
Winson Chung86f77532010-08-24 11:08:22 -07001011 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001012 final int sX = getScrollX();
1013 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001014 snapToPage(whichPage, delta, duration);
1015 }
1016
1017 protected void snapToPage(int whichPage, int delta, int duration) {
1018 mNextPage = whichPage;
1019
1020 View focusedChild = getFocusedChild();
1021 if (focusedChild != null && whichPage != mCurrentPage &&
1022 focusedChild == getChildAt(mCurrentPage)) {
1023 focusedChild.clearFocus();
1024 }
1025
1026 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001027 awakenScrollBars(duration);
1028 if (duration == 0) {
1029 duration = Math.abs(delta);
1030 }
1031
1032 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001033 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001034
1035 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001036 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001037 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001038 invalidate();
1039 }
1040
1041 @Override
1042 protected Parcelable onSaveInstanceState() {
1043 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001044 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001045 return state;
1046 }
1047
1048 @Override
1049 protected void onRestoreInstanceState(Parcelable state) {
1050 SavedState savedState = (SavedState) state;
1051 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001052 if (savedState.currentPage != -1) {
1053 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001054 }
1055 }
1056
1057 public void scrollLeft() {
1058 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001059 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001060 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001061 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001062 }
1063 }
1064
1065 public void scrollRight() {
1066 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001067 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001068 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001069 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001070 }
1071 }
1072
Winson Chung86f77532010-08-24 11:08:22 -07001073 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 int result = -1;
1075 if (v != null) {
1076 ViewParent vp = v.getParent();
1077 int count = getChildCount();
1078 for (int i = 0; i < count; i++) {
1079 if (vp == getChildAt(i)) {
1080 return i;
1081 }
1082 }
1083 }
1084 return result;
1085 }
1086
1087 /**
1088 * @return True is long presses are still allowed for the current touch
1089 */
1090 public boolean allowLongPress() {
1091 return mAllowLongPress;
1092 }
1093
Michael Jurka0142d492010-08-25 17:46:15 -07001094 /**
1095 * Set true to allow long-press events to be triggered, usually checked by
1096 * {@link Launcher} to accept or block dpad-initiated long-presses.
1097 */
1098 public void setAllowLongPress(boolean allowLongPress) {
1099 mAllowLongPress = allowLongPress;
1100 }
1101
Winson Chung321e9ee2010-08-09 13:37:56 -07001102 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001103 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001104
1105 SavedState(Parcelable superState) {
1106 super(superState);
1107 }
1108
1109 private SavedState(Parcel in) {
1110 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001111 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001112 }
1113
1114 @Override
1115 public void writeToParcel(Parcel out, int flags) {
1116 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001117 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001118 }
1119
1120 public static final Parcelable.Creator<SavedState> CREATOR =
1121 new Parcelable.Creator<SavedState>() {
1122 public SavedState createFromParcel(Parcel in) {
1123 return new SavedState(in);
1124 }
1125
1126 public SavedState[] newArray(int size) {
1127 return new SavedState[size];
1128 }
1129 };
1130 }
1131
Winson Chung86f77532010-08-24 11:08:22 -07001132 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001133 if (mContentIsRefreshable) {
1134 final int count = getChildCount();
1135 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001136 int lowerPageBound = getAssociatedLowerPageBound(page);
1137 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001138 for (int i = 0; i < count; ++i) {
1139 final ViewGroup layout = (ViewGroup) getChildAt(i);
1140 final int childCount = layout.getChildCount();
1141 if (lowerPageBound <= i && i <= upperPageBound) {
1142 if (mDirtyPageContent.get(i)) {
1143 syncPageItems(i);
1144 mDirtyPageContent.set(i, false);
1145 }
1146 } else {
1147 if (childCount > 0) {
1148 layout.removeAllViews();
1149 }
1150 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001151 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001152 }
1153 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001154 }
1155 }
1156
Winson Chunge3193b92010-09-10 11:44:42 -07001157 protected int getAssociatedLowerPageBound(int page) {
1158 return Math.max(0, page - 1);
1159 }
1160 protected int getAssociatedUpperPageBound(int page) {
1161 final int count = getChildCount();
1162 return Math.min(page + 1, count - 1);
1163 }
1164
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001165 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001166 if (isChoiceMode(CHOICE_MODE_NONE)) {
1167 mChoiceMode = mode;
1168 mActionMode = startActionMode(callback);
1169 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001170 }
1171
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001172 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001173 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001174 mChoiceMode = CHOICE_MODE_NONE;
1175 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001176 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001177 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001178 }
1179 }
1180
1181 protected boolean isChoiceMode(int mode) {
1182 return mChoiceMode == mode;
1183 }
1184
1185 protected ArrayList<Checkable> getCheckedGrandchildren() {
1186 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1187 final int childCount = getChildCount();
1188 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001189 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001190 final int grandChildCount = layout.getChildCount();
1191 for (int j = 0; j < grandChildCount; ++j) {
1192 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001193 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001194 checked.add((Checkable) v);
1195 }
1196 }
1197 }
1198 return checked;
1199 }
1200
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001201 /**
1202 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1203 * Otherwise, returns null.
1204 */
1205 protected Checkable getSingleCheckedGrandchild() {
1206 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1207 final int childCount = getChildCount();
1208 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001209 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001210 final int grandChildCount = layout.getChildCount();
1211 for (int j = 0; j < grandChildCount; ++j) {
1212 final View v = layout.getChildAt(j);
1213 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1214 return (Checkable) v;
1215 }
1216 }
1217 }
1218 }
1219 return null;
1220 }
1221
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001222 public Object getChosenItem() {
1223 View checkedView = (View) getSingleCheckedGrandchild();
1224 if (checkedView != null) {
1225 return checkedView.getTag();
1226 }
1227 return null;
1228 }
1229
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001230 protected void resetCheckedGrandchildren() {
1231 // loop through children, and set all of their children to _not_ be checked
1232 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1233 for (int i = 0; i < checked.size(); ++i) {
1234 final Checkable c = checked.get(i);
1235 c.setChecked(false);
1236 }
1237 }
1238
Winson Chung86f77532010-08-24 11:08:22 -07001239 /**
1240 * This method is called ONLY to synchronize the number of pages that the paged view has.
1241 * To actually fill the pages with information, implement syncPageItems() below. It is
1242 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1243 * and therefore, individual page items do not need to be updated in this method.
1244 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001246
1247 /**
1248 * This method is called to synchronize the items that are on a particular page. If views on
1249 * the page can be reused, then they should be updated within this method.
1250 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001251 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001252
Winson Chung321e9ee2010-08-09 13:37:56 -07001253 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001254 if (mContentIsRefreshable) {
1255 // Update all the pages
1256 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001257
Michael Jurka0142d492010-08-25 17:46:15 -07001258 // Mark each of the pages as dirty
1259 final int count = getChildCount();
1260 mDirtyPageContent.clear();
1261 for (int i = 0; i < count; ++i) {
1262 mDirtyPageContent.add(true);
1263 }
1264
1265 // Load any pages that are necessary for the current window of views
1266 loadAssociatedPages(mCurrentPage);
1267 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001268 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001269 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001270 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001271 }
1272}