blob: 0622aaea0121a0c00448b6a3b529b93f2718b784 [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) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800251 if (!mScroller.isFinished()) {
252 mScroller.abortAnimation();
253 }
254 if (getChildCount() == 0 || currentPage == mCurrentPage) {
255 return;
256 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700257
Winson Chung86f77532010-08-24 11:08:22 -0700258 mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700259 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
260 scrollTo(newX, 0);
261 mScroller.setFinalX(newX);
Winson Chung80baf5a2010-08-09 16:03:15 -0700262
Winson Chung321e9ee2010-08-09 13:37:56 -0700263 invalidate();
Winson Chung86f77532010-08-24 11:08:22 -0700264 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -0700265 }
266
Michael Jurka0142d492010-08-25 17:46:15 -0700267 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700268 if (mPageSwitchListener != null) {
269 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 }
271 }
272
Patrick Dubroy1262e362010-10-06 15:49:50 -0700273 private void pageBeginMoving() {
274 mIsPageMoving = true;
275 onPageBeginMoving();
276 }
277
278 private void pageEndMoving() {
279 onPageEndMoving();
280 mIsPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700281 }
282
283 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700284 protected void onPageBeginMoving() {
285 }
286
287 // a method that subclasses can override to add behavior
288 protected void onPageEndMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700289 }
290
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 /**
Winson Chung86f77532010-08-24 11:08:22 -0700292 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 *
294 * @param l The listener used to respond to long clicks.
295 */
296 @Override
297 public void setOnLongClickListener(OnLongClickListener l) {
298 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700299 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700301 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 }
303 }
304
305 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700306 public void scrollTo(int x, int y) {
307 super.scrollTo(x, y);
308 mTouchX = x;
309 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
310 }
311
312 // we moved this functionality to a helper function so SmoothPagedView can reuse it
313 protected boolean computeScrollHelper() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 if (mScroller.computeScrollOffset()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700315 mDirtyPageAlpha = true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700316 scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
Michael Jurka0142d492010-08-25 17:46:15 -0700317 invalidate();
318 return true;
Winson Chung86f77532010-08-24 11:08:22 -0700319 } else if (mNextPage != INVALID_PAGE) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700320 mDirtyPageAlpha = true;
Winson Chung86f77532010-08-24 11:08:22 -0700321 mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
Winson Chung86f77532010-08-24 11:08:22 -0700322 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700323 notifyPageSwitchListener();
324 pageEndMoving();
325 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 }
Michael Jurka0142d492010-08-25 17:46:15 -0700327 return false;
328 }
329
330 @Override
331 public void computeScroll() {
332 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 }
334
335 @Override
336 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
337 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
338 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
339 if (widthMode != MeasureSpec.EXACTLY) {
340 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
341 }
342
343 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
344 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
345 if (heightMode != MeasureSpec.EXACTLY) {
346 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
347 }
348
349 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700350 // unless they were set to WRAP_CONTENT
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 final int childCount = getChildCount();
352 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700353 // disallowing padding in paged view (just pass 0)
354 final View child = getChildAt(i);
355 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
356
357 int childWidthMode;
358 if (lp.width == LayoutParams.WRAP_CONTENT) {
359 childWidthMode = MeasureSpec.AT_MOST;
360 } else {
361 childWidthMode = MeasureSpec.EXACTLY;
362 }
363
364 int childHeightMode;
365 if (lp.height == LayoutParams.WRAP_CONTENT) {
366 childHeightMode = MeasureSpec.AT_MOST;
367 } else {
368 childHeightMode = MeasureSpec.EXACTLY;
369 }
370
371 final int childWidthMeasureSpec =
372 MeasureSpec.makeMeasureSpec(widthSize, childWidthMode);
373 final int childHeightMeasureSpec =
374 MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);
375
376 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Winson Chung321e9ee2010-08-09 13:37:56 -0700377 }
378
379 setMeasuredDimension(widthSize, heightSize);
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 }
381
382 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700383 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkacfc62942010-09-14 14:01:07 -0700384 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
385 setHorizontalScrollBarEnabled(false);
386 int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
387 scrollTo(newX, 0);
388 mScroller.setFinalX(newX);
389 setHorizontalScrollBarEnabled(true);
390 mFirstLayout = false;
391 }
392
Winson Chung321e9ee2010-08-09 13:37:56 -0700393 final int childCount = getChildCount();
394 int childLeft = 0;
395 if (childCount > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700396 childLeft = getRelativeChildOffset(0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700397 }
398
399 for (int i = 0; i < childCount; i++) {
400 final View child = getChildAt(i);
401 if (child.getVisibility() != View.GONE) {
402 final int childWidth = child.getMeasuredWidth();
Winson Chung7da10252010-10-28 16:07:04 -0700403 final int childHeight = (mCenterPagesVertically ?
404 (getMeasuredHeight() - child.getMeasuredHeight()) / 2 : 0);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700405 child.layout(childLeft, childHeight,
406 childLeft + childWidth, childHeight + child.getMeasuredHeight());
Adam Cohen9c4949e2010-10-05 12:27:22 -0700407 childLeft += childWidth + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700408 }
409 }
410 }
411
Winson Chunge3193b92010-09-10 11:44:42 -0700412 protected void updateAdjacentPagesAlpha() {
Michael Jurka0142d492010-08-25 17:46:15 -0700413 if (mFadeInAdjacentScreens) {
414 if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) {
Winson Chunge3193b92010-09-10 11:44:42 -0700415 int halfScreenSize = getMeasuredWidth() / 2;
416 int screenCenter = mScrollX + halfScreenSize;
Michael Jurka0142d492010-08-25 17:46:15 -0700417 final int childCount = getChildCount();
418 for (int i = 0; i < childCount; ++i) {
419 View layout = (View) getChildAt(i);
420 int childWidth = layout.getMeasuredWidth();
421 int halfChildWidth = (childWidth / 2);
422 int childCenter = getChildOffset(i) + halfChildWidth;
Winson Chunge8878e32010-09-15 20:37:09 -0700423
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700424 // On the first layout, we may not have a width nor a proper offset, so for now
425 // we should just assume full page width (and calculate the offset according to
426 // that).
427 if (childWidth <= 0) {
428 childWidth = getMeasuredWidth();
429 childCenter = (i * childWidth) + (childWidth / 2);
430 }
431
Winson Chunge8878e32010-09-15 20:37:09 -0700432 int d = halfChildWidth;
433 int distanceFromScreenCenter = childCenter - screenCenter;
434 if (distanceFromScreenCenter > 0) {
435 if (i > 0) {
436 d += getChildAt(i - 1).getMeasuredWidth() / 2;
437 }
Michael Jurka0142d492010-08-25 17:46:15 -0700438 } else {
Winson Chunge8878e32010-09-15 20:37:09 -0700439 if (i < childCount - 1) {
440 d += getChildAt(i + 1).getMeasuredWidth() / 2;
441 }
Michael Jurka0142d492010-08-25 17:46:15 -0700442 }
Adam Cohen9c4949e2010-10-05 12:27:22 -0700443 d += mPageSpacing;
Winson Chunge8878e32010-09-15 20:37:09 -0700444
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700445 // Preventing potential divide-by-zero
446 d = Math.max(1, d);
447
Winson Chunge8878e32010-09-15 20:37:09 -0700448 float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
449 dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
450 float alpha = 1.0f - dimAlpha;
451
Adam Cohenf34bab52010-09-30 14:11:56 -0700452 if (alpha < ALPHA_QUANTIZE_LEVEL) {
453 alpha = 0.0f;
454 } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) {
455 alpha = 1.0f;
456 }
457
Michael Jurka0142d492010-08-25 17:46:15 -0700458 if (Float.compare(alpha, layout.getAlpha()) != 0) {
459 layout.setAlpha(alpha);
460 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 }
Michael Jurka0142d492010-08-25 17:46:15 -0700462 mDirtyPageAlpha = false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 }
464 }
Winson Chunge3193b92010-09-10 11:44:42 -0700465 }
466
Adam Cohenf34bab52010-09-30 14:11:56 -0700467 protected void screenScrolled(int screenCenter) {
468 }
469
Winson Chunge3193b92010-09-10 11:44:42 -0700470 @Override
471 protected void dispatchDraw(Canvas canvas) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700472 int halfScreenSize = getMeasuredWidth() / 2;
473 int screenCenter = mScrollX + halfScreenSize;
474
475 if (screenCenter != mLastScreenCenter) {
476 screenScrolled(screenCenter);
477 updateAdjacentPagesAlpha();
478 mLastScreenCenter = screenCenter;
479 }
Michael Jurka0142d492010-08-25 17:46:15 -0700480
481 // Find out which screens are visible; as an optimization we only call draw on them
Michael Jurka0142d492010-08-25 17:46:15 -0700482 // As an optimization, this code assumes that all pages have the same width as the 0th
483 // page.
Michael Jurka0142d492010-08-25 17:46:15 -0700484 final int pageCount = getChildCount();
Michael Jurkac4fb9172010-09-02 17:19:20 -0700485 if (pageCount > 0) {
486 final int pageWidth = getChildAt(0).getMeasuredWidth();
487 final int screenWidth = getMeasuredWidth();
488 int x = getRelativeChildOffset(0) + pageWidth;
489 int leftScreen = 0;
490 int rightScreen = 0;
491 while (x <= mScrollX) {
492 leftScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700493 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700494 // replace above line with this if you don't assume all pages have same width as 0th
495 // page:
496 // x += getChildAt(leftScreen).getMeasuredWidth();
497 }
498 rightScreen = leftScreen;
499 while (x < mScrollX + screenWidth) {
500 rightScreen++;
Adam Cohen9c4949e2010-10-05 12:27:22 -0700501 x += pageWidth + mPageSpacing;
Michael Jurkac4fb9172010-09-02 17:19:20 -0700502 // replace above line with this if you don't assume all pages have same width as 0th
503 // page:
504 //if (rightScreen < pageCount) {
505 // x += getChildAt(rightScreen).getMeasuredWidth();
506 //}
507 }
508 rightScreen = Math.min(getChildCount() - 1, rightScreen);
Michael Jurka0142d492010-08-25 17:46:15 -0700509
Michael Jurkac4fb9172010-09-02 17:19:20 -0700510 final long drawingTime = getDrawingTime();
511 for (int i = leftScreen; i <= rightScreen; i++) {
512 drawChild(canvas, getChildAt(i), drawingTime);
513 }
Michael Jurka0142d492010-08-25 17:46:15 -0700514 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
516
517 @Override
518 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Winson Chung86f77532010-08-24 11:08:22 -0700519 int page = indexOfChild(child);
520 if (page != mCurrentPage || !mScroller.isFinished()) {
521 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700522 return true;
523 }
524 return false;
525 }
526
527 @Override
528 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -0700529 int focusablePage;
530 if (mNextPage != INVALID_PAGE) {
531 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 } else {
Winson Chung86f77532010-08-24 11:08:22 -0700533 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700534 }
Winson Chung86f77532010-08-24 11:08:22 -0700535 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700536 if (v != null) {
537 v.requestFocus(direction, previouslyFocusedRect);
538 }
539 return false;
540 }
541
542 @Override
543 public boolean dispatchUnhandledMove(View focused, int direction) {
544 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700545 if (getCurrentPage() > 0) {
546 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700547 return true;
548 }
549 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -0700550 if (getCurrentPage() < getPageCount() - 1) {
551 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -0700552 return true;
553 }
554 }
555 return super.dispatchUnhandledMove(focused, direction);
556 }
557
558 @Override
559 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Winson Chung86f77532010-08-24 11:08:22 -0700560 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
561 getPageAt(mCurrentPage).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
563 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -0700564 if (mCurrentPage > 0) {
565 getPageAt(mCurrentPage - 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700566 }
567 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -0700568 if (mCurrentPage < getPageCount() - 1) {
569 getPageAt(mCurrentPage + 1).addFocusables(views, direction);
Winson Chung321e9ee2010-08-09 13:37:56 -0700570 }
571 }
572 }
573
574 /**
575 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -0700576 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700577 *
Winson Chung86f77532010-08-24 11:08:22 -0700578 * This happens when live folders requery, and if they're off page, they
579 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 */
581 @Override
582 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -0700583 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700584 View v = focused;
585 while (true) {
586 if (v == current) {
587 super.focusableViewAvailable(focused);
588 return;
589 }
590 if (v == this) {
591 return;
592 }
593 ViewParent parent = v.getParent();
594 if (parent instanceof View) {
595 v = (View)v.getParent();
596 } else {
597 return;
598 }
599 }
600 }
601
602 /**
603 * {@inheritDoc}
604 */
605 @Override
606 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
607 if (disallowIntercept) {
608 // We need to make sure to cancel our long press if
609 // a scrollable widget takes over touch events
Winson Chung86f77532010-08-24 11:08:22 -0700610 final View currentPage = getChildAt(mCurrentPage);
611 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -0700612 }
613 super.requestDisallowInterceptTouchEvent(disallowIntercept);
614 }
615
616 @Override
617 public boolean onInterceptTouchEvent(MotionEvent ev) {
618 /*
619 * This method JUST determines whether we want to intercept the motion.
620 * If we return true, onTouchEvent will be called and we do the actual
621 * scrolling there.
622 */
623
624 /*
625 * Shortcut the most recurring case: the user is in the dragging
626 * state and he is moving his finger. We want to intercept this
627 * motion.
628 */
629 final int action = ev.getAction();
630 if ((action == MotionEvent.ACTION_MOVE) &&
631 (mTouchState == TOUCH_STATE_SCROLLING)) {
632 return true;
633 }
634
Winson Chung321e9ee2010-08-09 13:37:56 -0700635 switch (action & MotionEvent.ACTION_MASK) {
636 case MotionEvent.ACTION_MOVE: {
637 /*
638 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
639 * whether the user has moved far enough from his original down touch.
640 */
Michael Jurka1ff706b2010-09-14 17:35:20 -0700641 if (mActivePointerId != INVALID_POINTER) {
642 determineScrollingStart(ev);
643 break;
644 }
645 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
646 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
647 // i.e. fall through to the next case (don't break)
648 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
649 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Winson Chung321e9ee2010-08-09 13:37:56 -0700650 }
651
652 case MotionEvent.ACTION_DOWN: {
653 final float x = ev.getX();
654 final float y = ev.getY();
655 // Remember location of down touch
656 mDownMotionX = x;
657 mLastMotionX = x;
658 mLastMotionY = y;
659 mActivePointerId = ev.getPointerId(0);
660 mAllowLongPress = true;
661
662 /*
663 * If being flinged and user touches the screen, initiate drag;
664 * otherwise don't. mScroller.isFinished should be false when
665 * being flinged.
666 */
Michael Jurkafd177c12010-10-19 15:50:43 -0700667 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700668 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
669 if (finishedScrolling) {
670 mTouchState = TOUCH_STATE_REST;
671 mScroller.abortAnimation();
672 } else {
673 mTouchState = TOUCH_STATE_SCROLLING;
674 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700675
Winson Chung86f77532010-08-24 11:08:22 -0700676 // check if this can be the beginning of a tap on the side of the pages
Winson Chung321e9ee2010-08-09 13:37:56 -0700677 // to scroll the current page
Adam Cohen21f12b52010-10-07 17:15:37 -0700678 if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() &&
Winson Chung321e9ee2010-08-09 13:37:56 -0700679 (mTouchState != TOUCH_STATE_NEXT_PAGE)) {
680 if (getChildCount() > 0) {
Winson Chunge3193b92010-09-10 11:44:42 -0700681 int width = getMeasuredWidth();
682 int offset = getRelativeChildOffset(mCurrentPage);
Adam Cohen21f12b52010-10-07 17:15:37 -0700683 if (x < offset - mPageSpacing) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700684 mTouchState = TOUCH_STATE_PREV_PAGE;
Adam Cohen21f12b52010-10-07 17:15:37 -0700685 } else if (x > (width - offset + mPageSpacing)) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700686 mTouchState = TOUCH_STATE_NEXT_PAGE;
687 }
688 }
689 }
690 break;
691 }
692
693 case MotionEvent.ACTION_CANCEL:
694 case MotionEvent.ACTION_UP:
Winson Chung321e9ee2010-08-09 13:37:56 -0700695 mTouchState = TOUCH_STATE_REST;
696 mAllowLongPress = false;
697 mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700698 break;
699
700 case MotionEvent.ACTION_POINTER_UP:
701 onSecondaryPointerUp(ev);
702 break;
703 }
704
705 /*
706 * The only time we want to intercept motion events is if we are in the
707 * drag mode.
708 */
709 return mTouchState != TOUCH_STATE_REST;
710 }
711
Winson Chung80baf5a2010-08-09 16:03:15 -0700712 protected void animateClickFeedback(View v, final Runnable r) {
713 // animate the view slightly to show click feedback running some logic after it is "pressed"
714 Animation anim = AnimationUtils.loadAnimation(getContext(),
715 R.anim.paged_view_click_feedback);
716 anim.setAnimationListener(new AnimationListener() {
717 @Override
718 public void onAnimationStart(Animation animation) {}
719 @Override
720 public void onAnimationRepeat(Animation animation) {
721 r.run();
722 }
723 @Override
724 public void onAnimationEnd(Animation animation) {}
725 });
726 v.startAnimation(anim);
727 }
728
Winson Chung321e9ee2010-08-09 13:37:56 -0700729 /*
730 * Determines if we should change the touch state to start scrolling after the
731 * user moves their touch point too far.
732 */
Michael Jurka1adf5392010-10-18 18:10:22 -0700733 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700734 /*
735 * Locally do absolute value. mLastMotionX is set to the y value
736 * of the down event.
737 */
738 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
739 final float x = ev.getX(pointerIndex);
740 final float y = ev.getY(pointerIndex);
741 final int xDiff = (int) Math.abs(x - mLastMotionX);
742 final int yDiff = (int) Math.abs(y - mLastMotionY);
743
744 final int touchSlop = mTouchSlop;
745 boolean xPaged = xDiff > mPagingTouchSlop;
746 boolean xMoved = xDiff > touchSlop;
747 boolean yMoved = yDiff > touchSlop;
748
749 if (xMoved || yMoved) {
Michael Jurka0142d492010-08-25 17:46:15 -0700750 if (mUsePagingTouchSlop ? xPaged : xMoved) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700751 // Scroll if the user moved far enough along the X axis
752 mTouchState = TOUCH_STATE_SCROLLING;
753 mLastMotionX = x;
Michael Jurka0142d492010-08-25 17:46:15 -0700754 mTouchX = mScrollX;
755 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
756 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 }
758 // Either way, cancel any pending longpress
759 if (mAllowLongPress) {
760 mAllowLongPress = false;
761 // Try canceling the long press. It could also have been scheduled
762 // by a distant descendant, so use the mAllowLongPress flag to block
763 // everything
Winson Chung86f77532010-08-24 11:08:22 -0700764 final View currentPage = getPageAt(mCurrentPage);
Winson Chung10fefb12010-11-01 11:57:06 -0700765 if (currentPage != null) {
766 currentPage.cancelLongPress();
767 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700768 }
769 }
770 }
771
Adam Cohen21f12b52010-10-07 17:15:37 -0700772 protected boolean handlePagingClicks() {
773 return false;
774 }
775
Winson Chung321e9ee2010-08-09 13:37:56 -0700776 @Override
777 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurkab8f06722010-10-10 15:58:46 -0700778 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -0700779
780 final int action = ev.getAction();
781
782 switch (action & MotionEvent.ACTION_MASK) {
783 case MotionEvent.ACTION_DOWN:
784 /*
785 * If being flinged and user touches, stop the fling. isFinished
786 * will be false if being flinged.
787 */
788 if (!mScroller.isFinished()) {
789 mScroller.abortAnimation();
790 }
791
792 // Remember where the motion event started
793 mDownMotionX = mLastMotionX = ev.getX();
794 mActivePointerId = ev.getPointerId(0);
Michael Jurka0142d492010-08-25 17:46:15 -0700795 if (mTouchState == TOUCH_STATE_SCROLLING) {
796 pageBeginMoving();
797 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700798 break;
799
800 case MotionEvent.ACTION_MOVE:
801 if (mTouchState == TOUCH_STATE_SCROLLING) {
802 // Scroll to follow the motion event
803 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
804 final float x = ev.getX(pointerIndex);
805 final int deltaX = (int) (mLastMotionX - x);
806 mLastMotionX = x;
807
808 int sx = getScrollX();
809 if (deltaX < 0) {
810 if (sx > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700811 mTouchX += Math.max(-mTouchX, deltaX);
812 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
813 if (!mDeferScrollUpdate) {
814 scrollBy(Math.max(-sx, deltaX), 0);
815 } else {
816 // This will trigger a call to computeScroll() on next drawChild() call
817 invalidate();
818 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700819 }
820 } else if (deltaX > 0) {
821 final int lastChildIndex = getChildCount() - 1;
822 final int availableToScroll = getChildOffset(lastChildIndex) -
823 getRelativeChildOffset(lastChildIndex) - sx;
824 if (availableToScroll > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700825 mTouchX += Math.min(availableToScroll, deltaX);
826 mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
827 if (!mDeferScrollUpdate) {
828 scrollBy(Math.min(availableToScroll, deltaX), 0);
829 } else {
830 // This will trigger a call to computeScroll() on next drawChild() call
831 invalidate();
832 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 }
834 } else {
835 awakenScrollBars();
836 }
Adam Cohen564976a2010-10-13 18:52:07 -0700837 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -0700838 determineScrollingStart(ev);
839 }
840 break;
841
842 case MotionEvent.ACTION_UP:
843 if (mTouchState == TOUCH_STATE_SCROLLING) {
844 final int activePointerId = mActivePointerId;
845 final int pointerIndex = ev.findPointerIndex(activePointerId);
846 final float x = ev.getX(pointerIndex);
847 final VelocityTracker velocityTracker = mVelocityTracker;
848 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
849 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700850 final int deltaX = (int) (x - mDownMotionX);
851 boolean isfling = Math.abs(deltaX) > MIN_LENGTH_FOR_FLING;
852 boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
Winson Chung321e9ee2010-08-09 13:37:56 -0700853
Michael Jurka0142d492010-08-25 17:46:15 -0700854 final int snapVelocity = mSnapVelocity;
Winson Chung9cfd25f2010-10-24 16:09:28 -0700855 if ((isSignificantMove && deltaX > 0 ||
856 (isfling && velocityX > snapVelocity)) &&
857 mCurrentPage > 0) {
Michael Jurka0142d492010-08-25 17:46:15 -0700858 snapToPageWithVelocity(mCurrentPage - 1, velocityX);
Winson Chung9cfd25f2010-10-24 16:09:28 -0700859 } else if ((isSignificantMove && deltaX < 0 ||
860 (isfling && velocityX < -snapVelocity)) &&
Winson Chung86f77532010-08-24 11:08:22 -0700861 mCurrentPage < getChildCount() - 1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700862 snapToPageWithVelocity(mCurrentPage + 1, velocityX);
Winson Chung321e9ee2010-08-09 13:37:56 -0700863 } else {
864 snapToDestination();
865 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700866 } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700867 // at this point we have not moved beyond the touch slop
868 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
869 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700870 int nextPage = Math.max(0, mCurrentPage - 1);
871 if (nextPage != mCurrentPage) {
872 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700873 } else {
874 snapToDestination();
875 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700876 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700877 // at this point we have not moved beyond the touch slop
878 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
879 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -0700880 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
881 if (nextPage != mCurrentPage) {
882 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700883 } else {
884 snapToDestination();
885 }
886 }
887 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_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -0700893 if (mTouchState == TOUCH_STATE_SCROLLING) {
894 snapToDestination();
895 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700896 mTouchState = TOUCH_STATE_REST;
897 mActivePointerId = INVALID_POINTER;
Michael Jurkab8f06722010-10-10 15:58:46 -0700898 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -0700899 break;
900
901 case MotionEvent.ACTION_POINTER_UP:
902 onSecondaryPointerUp(ev);
903 break;
904 }
905
906 return true;
907 }
908
Michael Jurkab8f06722010-10-10 15:58:46 -0700909 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
910 if (mVelocityTracker == null) {
911 mVelocityTracker = VelocityTracker.obtain();
912 }
913 mVelocityTracker.addMovement(ev);
914 }
915
916 private void releaseVelocityTracker() {
917 if (mVelocityTracker != null) {
918 mVelocityTracker.recycle();
919 mVelocityTracker = null;
920 }
921 }
922
Winson Chung321e9ee2010-08-09 13:37:56 -0700923 private void onSecondaryPointerUp(MotionEvent ev) {
924 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
925 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
926 final int pointerId = ev.getPointerId(pointerIndex);
927 if (pointerId == mActivePointerId) {
928 // This was our active pointer going up. Choose a new
929 // active pointer and adjust accordingly.
930 // TODO: Make this decision more intelligent.
931 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
932 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
933 mLastMotionY = ev.getY(newPointerIndex);
934 mActivePointerId = ev.getPointerId(newPointerIndex);
935 if (mVelocityTracker != null) {
936 mVelocityTracker.clear();
937 }
938 }
939 }
940
941 @Override
942 public void requestChildFocus(View child, View focused) {
943 super.requestChildFocus(child, focused);
Winson Chung86f77532010-08-24 11:08:22 -0700944 int page = indexOfChild(child);
945 if (page >= 0 && !isInTouchMode()) {
946 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -0700947 }
948 }
949
Winson Chunge3193b92010-09-10 11:44:42 -0700950 protected int getChildIndexForRelativeOffset(int relativeOffset) {
951 final int childCount = getChildCount();
Adam Cohen9c4949e2010-10-05 12:27:22 -0700952 int left;
953 int right;
Winson Chunge3193b92010-09-10 11:44:42 -0700954 for (int i = 0; i < childCount; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700955 left = getRelativeChildOffset(i);
956 right = (left + getChildAt(i).getMeasuredWidth());
Winson Chunge3193b92010-09-10 11:44:42 -0700957 if (left <= relativeOffset && relativeOffset <= right) {
958 return i;
959 }
Winson Chunge3193b92010-09-10 11:44:42 -0700960 }
961 return -1;
962 }
963
Winson Chung321e9ee2010-08-09 13:37:56 -0700964 protected int getRelativeChildOffset(int index) {
965 return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
966 }
967
968 protected int getChildOffset(int index) {
969 if (getChildCount() == 0)
970 return 0;
971
972 int offset = getRelativeChildOffset(0);
973 for (int i = 0; i < index; ++i) {
Adam Cohen9c4949e2010-10-05 12:27:22 -0700974 offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
Winson Chung321e9ee2010-08-09 13:37:56 -0700975 }
976 return offset;
977 }
978
Adam Cohend19d3ca2010-09-15 14:43:42 -0700979 int getPageNearestToCenterOfScreen() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700980 int minDistanceFromScreenCenter = getMeasuredWidth();
981 int minDistanceFromScreenCenterIndex = -1;
982 int screenCenter = mScrollX + (getMeasuredWidth() / 2);
983 final int childCount = getChildCount();
984 for (int i = 0; i < childCount; ++i) {
Michael Jurka0142d492010-08-25 17:46:15 -0700985 View layout = (View) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700986 int childWidth = layout.getMeasuredWidth();
987 int halfChildWidth = (childWidth / 2);
988 int childCenter = getChildOffset(i) + halfChildWidth;
989 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
990 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
991 minDistanceFromScreenCenter = distanceFromScreenCenter;
992 minDistanceFromScreenCenterIndex = i;
993 }
994 }
Adam Cohend19d3ca2010-09-15 14:43:42 -0700995 return minDistanceFromScreenCenterIndex;
996 }
997
998 protected void snapToDestination() {
999 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001000 }
1001
Michael Jurka0142d492010-08-25 17:46:15 -07001002 protected void snapToPageWithVelocity(int whichPage, int velocity) {
1003 // We ignore velocity in this implementation, but children (e.g. SmoothPagedView)
1004 // can use it
1005 snapToPage(whichPage);
1006 }
1007
1008 protected void snapToPage(int whichPage) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001009 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001010 }
1011
Michael Jurka0142d492010-08-25 17:46:15 -07001012 protected void snapToPage(int whichPage, int duration) {
Winson Chung86f77532010-08-24 11:08:22 -07001013 whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
Winson Chung321e9ee2010-08-09 13:37:56 -07001014
Winson Chung86f77532010-08-24 11:08:22 -07001015 int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001016 final int sX = getScrollX();
1017 final int delta = newX - sX;
Michael Jurka0142d492010-08-25 17:46:15 -07001018 snapToPage(whichPage, delta, duration);
1019 }
1020
1021 protected void snapToPage(int whichPage, int delta, int duration) {
1022 mNextPage = whichPage;
1023
1024 View focusedChild = getFocusedChild();
1025 if (focusedChild != null && whichPage != mCurrentPage &&
1026 focusedChild == getChildAt(mCurrentPage)) {
1027 focusedChild.clearFocus();
1028 }
1029
1030 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07001031 awakenScrollBars(duration);
1032 if (duration == 0) {
1033 duration = Math.abs(delta);
1034 }
1035
1036 if (!mScroller.isFinished()) mScroller.abortAnimation();
Michael Jurka0142d492010-08-25 17:46:15 -07001037 mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07001038
1039 // only load some associated pages
Winson Chung86f77532010-08-24 11:08:22 -07001040 loadAssociatedPages(mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -07001041 notifyPageSwitchListener();
Winson Chung321e9ee2010-08-09 13:37:56 -07001042 invalidate();
1043 }
1044
1045 @Override
1046 protected Parcelable onSaveInstanceState() {
1047 final SavedState state = new SavedState(super.onSaveInstanceState());
Winson Chung86f77532010-08-24 11:08:22 -07001048 state.currentPage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001049 return state;
1050 }
1051
1052 @Override
1053 protected void onRestoreInstanceState(Parcelable state) {
1054 SavedState savedState = (SavedState) state;
1055 super.onRestoreInstanceState(savedState.getSuperState());
Winson Chung86f77532010-08-24 11:08:22 -07001056 if (savedState.currentPage != -1) {
1057 mCurrentPage = savedState.currentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001058 }
1059 }
1060
1061 public void scrollLeft() {
1062 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001063 if (mCurrentPage > 0) snapToPage(mCurrentPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001064 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001065 if (mNextPage > 0) snapToPage(mNextPage - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001066 }
1067 }
1068
1069 public void scrollRight() {
1070 if (mScroller.isFinished()) {
Winson Chung86f77532010-08-24 11:08:22 -07001071 if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001072 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001073 if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001074 }
1075 }
1076
Winson Chung86f77532010-08-24 11:08:22 -07001077 public int getPageForView(View v) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001078 int result = -1;
1079 if (v != null) {
1080 ViewParent vp = v.getParent();
1081 int count = getChildCount();
1082 for (int i = 0; i < count; i++) {
1083 if (vp == getChildAt(i)) {
1084 return i;
1085 }
1086 }
1087 }
1088 return result;
1089 }
1090
1091 /**
1092 * @return True is long presses are still allowed for the current touch
1093 */
1094 public boolean allowLongPress() {
1095 return mAllowLongPress;
1096 }
1097
Michael Jurka0142d492010-08-25 17:46:15 -07001098 /**
1099 * Set true to allow long-press events to be triggered, usually checked by
1100 * {@link Launcher} to accept or block dpad-initiated long-presses.
1101 */
1102 public void setAllowLongPress(boolean allowLongPress) {
1103 mAllowLongPress = allowLongPress;
1104 }
1105
Winson Chung321e9ee2010-08-09 13:37:56 -07001106 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07001107 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07001108
1109 SavedState(Parcelable superState) {
1110 super(superState);
1111 }
1112
1113 private SavedState(Parcel in) {
1114 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07001115 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07001116 }
1117
1118 @Override
1119 public void writeToParcel(Parcel out, int flags) {
1120 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07001121 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001122 }
1123
1124 public static final Parcelable.Creator<SavedState> CREATOR =
1125 new Parcelable.Creator<SavedState>() {
1126 public SavedState createFromParcel(Parcel in) {
1127 return new SavedState(in);
1128 }
1129
1130 public SavedState[] newArray(int size) {
1131 return new SavedState[size];
1132 }
1133 };
1134 }
1135
Winson Chung86f77532010-08-24 11:08:22 -07001136 public void loadAssociatedPages(int page) {
Michael Jurka0142d492010-08-25 17:46:15 -07001137 if (mContentIsRefreshable) {
1138 final int count = getChildCount();
1139 if (page < count) {
Winson Chunge3193b92010-09-10 11:44:42 -07001140 int lowerPageBound = getAssociatedLowerPageBound(page);
1141 int upperPageBound = getAssociatedUpperPageBound(page);
Michael Jurka0142d492010-08-25 17:46:15 -07001142 for (int i = 0; i < count; ++i) {
1143 final ViewGroup layout = (ViewGroup) getChildAt(i);
1144 final int childCount = layout.getChildCount();
1145 if (lowerPageBound <= i && i <= upperPageBound) {
1146 if (mDirtyPageContent.get(i)) {
1147 syncPageItems(i);
1148 mDirtyPageContent.set(i, false);
1149 }
1150 } else {
1151 if (childCount > 0) {
1152 layout.removeAllViews();
1153 }
1154 mDirtyPageContent.set(i, true);
Winson Chung86f77532010-08-24 11:08:22 -07001155 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001156 }
1157 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001158 }
1159 }
1160
Winson Chunge3193b92010-09-10 11:44:42 -07001161 protected int getAssociatedLowerPageBound(int page) {
1162 return Math.max(0, page - 1);
1163 }
1164 protected int getAssociatedUpperPageBound(int page) {
1165 final int count = getChildCount();
1166 return Math.min(page + 1, count - 1);
1167 }
1168
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001169 protected void startChoiceMode(int mode, ActionMode.Callback callback) {
Patrick Dubroy430c53b2010-09-08 16:01:19 -07001170 if (isChoiceMode(CHOICE_MODE_NONE)) {
1171 mChoiceMode = mode;
1172 mActionMode = startActionMode(callback);
1173 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001174 }
1175
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001176 public void endChoiceMode() {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001177 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001178 mChoiceMode = CHOICE_MODE_NONE;
1179 resetCheckedGrandchildren();
Michael Jurkae17e19c2010-09-28 11:01:39 -07001180 if (mActionMode != null) mActionMode.finish();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001181 mActionMode = null;
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001182 }
1183 }
1184
1185 protected boolean isChoiceMode(int mode) {
1186 return mChoiceMode == mode;
1187 }
1188
1189 protected ArrayList<Checkable> getCheckedGrandchildren() {
1190 ArrayList<Checkable> checked = new ArrayList<Checkable>();
1191 final int childCount = getChildCount();
1192 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001193 final ViewGroup layout = (ViewGroup) getChildAt(i);
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001194 final int grandChildCount = layout.getChildCount();
1195 for (int j = 0; j < grandChildCount; ++j) {
1196 final View v = layout.getChildAt(j);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001197 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001198 checked.add((Checkable) v);
1199 }
1200 }
1201 }
1202 return checked;
1203 }
1204
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001205 /**
1206 * If in CHOICE_MODE_SINGLE and an item is checked, returns that item.
1207 * Otherwise, returns null.
1208 */
1209 protected Checkable getSingleCheckedGrandchild() {
1210 if (mChoiceMode == CHOICE_MODE_SINGLE) {
1211 final int childCount = getChildCount();
1212 for (int i = 0; i < childCount; ++i) {
Winson Chungd0d43012010-09-26 17:26:45 -07001213 final ViewGroup layout = (ViewGroup) getChildAt(i);
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001214 final int grandChildCount = layout.getChildCount();
1215 for (int j = 0; j < grandChildCount; ++j) {
1216 final View v = layout.getChildAt(j);
1217 if (v instanceof Checkable && ((Checkable) v).isChecked()) {
1218 return (Checkable) v;
1219 }
1220 }
1221 }
1222 }
1223 return null;
1224 }
1225
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001226 public Object getChosenItem() {
1227 View checkedView = (View) getSingleCheckedGrandchild();
1228 if (checkedView != null) {
1229 return checkedView.getTag();
1230 }
1231 return null;
1232 }
1233
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001234 protected void resetCheckedGrandchildren() {
1235 // loop through children, and set all of their children to _not_ be checked
1236 final ArrayList<Checkable> checked = getCheckedGrandchildren();
1237 for (int i = 0; i < checked.size(); ++i) {
1238 final Checkable c = checked.get(i);
1239 c.setChecked(false);
1240 }
1241 }
1242
Winson Chung86f77532010-08-24 11:08:22 -07001243 /**
1244 * This method is called ONLY to synchronize the number of pages that the paged view has.
1245 * To actually fill the pages with information, implement syncPageItems() below. It is
1246 * guaranteed that syncPageItems() will be called for a particular page before it is shown,
1247 * and therefore, individual page items do not need to be updated in this method.
1248 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 public abstract void syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001250
1251 /**
1252 * This method is called to synchronize the items that are on a particular page. If views on
1253 * the page can be reused, then they should be updated within this method.
1254 */
Winson Chung321e9ee2010-08-09 13:37:56 -07001255 public abstract void syncPageItems(int page);
Winson Chung86f77532010-08-24 11:08:22 -07001256
Winson Chung321e9ee2010-08-09 13:37:56 -07001257 public void invalidatePageData() {
Michael Jurka0142d492010-08-25 17:46:15 -07001258 if (mContentIsRefreshable) {
1259 // Update all the pages
1260 syncPages();
Winson Chung86f77532010-08-24 11:08:22 -07001261
Michael Jurka0142d492010-08-25 17:46:15 -07001262 // Mark each of the pages as dirty
1263 final int count = getChildCount();
1264 mDirtyPageContent.clear();
1265 for (int i = 0; i < count; ++i) {
1266 mDirtyPageContent.add(true);
1267 }
1268
1269 // Load any pages that are necessary for the current window of views
1270 loadAssociatedPages(mCurrentPage);
1271 mDirtyPageAlpha = true;
Winson Chungb0b2e6f2010-10-12 17:49:56 -07001272 updateAdjacentPagesAlpha();
Michael Jurka0142d492010-08-25 17:46:15 -07001273 requestLayout();
Winson Chung86f77532010-08-24 11:08:22 -07001274 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001275 }
1276}